Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Add support for gotests "https://github.com/cweill/gotests" #489

Merged
merged 1 commit into from
Oct 6, 2016

Conversation

clamoriniere
Copy link
Contributor

Add support for gotests "https://github.com/cweill/gotests"

Implements three new commands that use gotests:

  • go.test.generate.package: generates all unit-test squeletons for
    the current package.
  • go.test.generate.file: generates all unit-test squeletons for
    the current file.
  • go.test.generate.function: generates unit-test squeleton for the
    selected function in the current file.

@msftclas
Copy link

Hi @cedriclam, I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution!

In order for us to evaluate and accept your PR, we ask that you sign a contribution license agreement. It's all electronic and will take just minutes. I promise there's no faxing. https://cla.microsoft.com.

TTYL, MSBOT;

@msftclas
Copy link

@cedriclam, Thanks for signing the contribution license agreement so quickly! Actual humans will now validate the agreement and then evaluate the PR.

Thanks, MSBOT;

});
}

function testEditor() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add return type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

symbols.filter(sym =>
sym.kind === vscode.SymbolKind.Function)
);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Newline at EOF

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

let editor = testEditor();
let file = editor.document.uri.fsPath;
generateTests({dir: file});
vscode.window.showInformationMessage('gotests: unit-tests generated properly for file:' + path.basename(file));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps "gotests: Unit tests generated for file: "?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right.

@@ -102,6 +102,21 @@
"description": "Displays test coverage in the current package."
},
{
"command": "go.test.generate.package",
"title": "Go: Generates unit-tests (package)",
"description": "Generates unit-tests for the current package"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no hyphen

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change done in all files

@@ -19,6 +19,7 @@ This extension adds rich language support for the Go language to VS Code, includ
- Build-on-save (using `go build` and `go test`)
- Lint-on-save (using `golint` or `gometalinter`)
- Format (using `goreturns` or `goimports` or `gofmt`)
- Generate unit-test squeleton (using `gotests`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Generate unit tests"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change done.

@clamoriniere clamoriniere force-pushed the feature/gotests branch 2 times, most recently from 342c98d to 2eed87c Compare September 27, 2016 19:23
@clamoriniere
Copy link
Contributor Author

@lukehoban thanks for your code review.
Let me know if you think that I need to change something else.

Copy link
Contributor

@ramya-rao-a ramya-rao-a left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice feature. I have added a few comments to get a clean end-to-end experience.

}

export function generateTestCurrentFile() {
let editor = testEditor();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When no active editor is found, then return. Don't call generateTests(). Else you will end up with 1 info box for "no editor" and 1 error box for "running the command ... failed". Same applies to the other 2 generate.. methods as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right. I will add a check on the editor

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

@@ -0,0 +1,113 @@
/*---------------------------------------------------------
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name this file "goGenerateTests" maybe? Because we already have a file goTest.ts and another go.test.ts. We can it keep the intent of the file clear this way

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

absolutly

let editor = testEditor();
let file = editor.document.uri.fsPath;
generateTests({dir: file});
vscode.window.showInformationMessage('gotests: Unit tests generated for file:' + path.basename(file));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Show this message only after the promise returned from generateTests() is completed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same applies to the other 2 methods as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -24,7 +24,8 @@ let tools: { [key: string]: string } = {
'go-symbols': 'github.com/newhook/go-symbols',
'guru': 'golang.org/x/tools/cmd/guru',
'gorename': 'golang.org/x/tools/cmd/gorename',
'goimports': 'golang.org/x/tools/cmd/goimports'
'goimports': 'golang.org/x/tools/cmd/goimports',
'gotests': 'github.com/cweill/gotests'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This didn't work for me. The source code gets downloaded but the tool doesn't get dropped in the $GOPATH/bin. After looking at the source code structure, looks like there is another "gotests" folder.
This needs to be updated to 'github.com/cweill/gotests/gotests'

Make the change in the README as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will modify by: github.com/cweill/gotests/gotests/... since it is what they are saying in the doc.

return resolve(null);
}
if (err) {
return reject('Cannot generate test due to errors: ' + stderr);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error doesn't bubble up to the user

@@ -102,6 +102,21 @@
"description": "Displays test coverage in the current package."
},
{
"command": "go.test.generate.package",
"title": "Go: Generates unit tests (package)",
Copy link
Contributor

@ramya-rao-a ramya-rao-a Sep 28, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having parenthesis/brackets breaks autocomplete while typing commands in the command pallete.
#495 fixed this for the test commands
Can you update the title for your commands as well?
example: Go: Generate unit tests for package/file/function

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

@@ -31,6 +31,7 @@ install:
- go get -u -v github.com/newhook/go-symbols
- go get -u -v golang.org/x/tools/cmd/guru
- go get -u -v github.com/alecthomas/gometalinter
- go get -u -v github.com/cweill/gotests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need to add this to travis unless you have some unit tests. Could you add some unit tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added unit tests

func?: string;
}

function generateTests(conf: Config): Thenable<vscode.WorkspaceEdit> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why return WorkspaceEdits?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know much about the vscode internals... I done a copy/past on the gotest plugin. I will check what it is better in my case.

@ramya-rao-a
Copy link
Contributor

Also, this worked for me in Mac but not Windows. In Windows, gotests returned with this error
image

@clamoriniere
Copy link
Contributor Author

@ramya-rao-a, thanks for you code review. I done your proposed changes.

about gotests failing on windows environment, did you try to run directlygotests with the command line? Maybe it is because you installed with the command: go get -u github.com/cweill/gotests/gotests instead of go get -u github.com/cweill/gotests/...

I haven't a windows environment yet but I will try next week.

Thanks again for your help.

@clamoriniere
Copy link
Contributor Author

Hello @ramya-rao-a, I was able to test on a windows environment my last pull request version and it works fine.
env: Windows 7, vscode v1.5.3

@clamoriniere clamoriniere force-pushed the feature/gotests branch 6 times, most recently from ff397c2 to 8ce7301 Compare October 3, 2016 22:50
@clamoriniere
Copy link
Contributor Author

clamoriniere commented Oct 3, 2016

travis jobs failed due to an issue with the command "go get -u -v github.com/golang/lint/golint" and the environment go1.5

error: "../../golang/lint/lint.go:250: undefined: types.ImportMode"
link: https://travis-ci.org/Microsoft/vscode-go/jobs/164787607#L287

PR #510 should fix this issue

Copy link
Contributor

@ramya-rao-a ramya-rao-a left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just 2 more things left, otherwise things look great!

export function generateTestCurrentPackage(): Thenable<boolean> {
let editor = testEditor();
if (!editor) {
vscode.window.showInformationMessage('gotests: No editor selected');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You already have a message being shown in line 116. You don't need this message again. I'd actually suggest to remove the function testEditor altogether and replace
let editor = testEditor()
with
let editor = vscode.window.activeTextEditor

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I will do this change.

return resolve(false);
}
if (err) {
return reject('Cannot generate test due to errors: ' + stderr);
Copy link
Contributor

@ramya-rao-a ramya-rao-a Oct 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out, this was failing for me in Windows because I had Go 1.5 on my Windows machine. The error message in such cases is not helpful. We should probably check for the Go version first and not run gotests at all if it is not supported and show a meaningful message like Generating tests using gotests is not supported in Go 1.5.

#509 adds a function for checking the Go version. You can either wait for that PR to get merged or copy over the getGoVersion function from there

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I will wait your PR integration.

Copy link
Contributor

@ramya-rao-a ramya-rao-a Oct 6, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cedriclam my PR has been merged. Get latest from master, and you should have my changes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ramya-rao-a: rebase done; travis builds look fine now.

@clamoriniere clamoriniere force-pushed the feature/gotests branch 2 times, most recently from 86d9d2c to e4b0460 Compare October 6, 2016 18:36
Implements three new commands that use gotests:
- go.test.generate.package: generates all unit-test squeletons for
  the current package.
- go.test.generate.file: generates all unit-test squeletons for
  the current file.
- go.test.generate.function: generates unit-test squeleton for the
  selected function in the current file.
@ramya-rao-a ramya-rao-a merged commit 1636cbe into microsoft:master Oct 6, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants