-
Notifications
You must be signed in to change notification settings - Fork 643
Set environment variables for go test runs #498
Changes from 5 commits
f8ce303
1f87cb5
ceaf061
545a178
81e5aaf
b16cf38
e51fbe7
cab04b5
beadb1a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,7 +34,7 @@ interface TestConfig { | |
| /** | ||
| * Executes the unit test at the primary cursor using `go test`. Output | ||
| * is sent to the 'Go' channel. | ||
| * | ||
| * | ||
| * @param timeout a ParseDuration formatted timeout string for the tests. | ||
| * | ||
| * TODO: go test returns filenames with no path information for failures, | ||
|
|
@@ -94,19 +94,19 @@ export function testCurrentPackage(timeout: string) { | |
| * | ||
| * @param timeout a ParseDuration formatted timeout string for the tests. | ||
| */ | ||
| export function testCurrentFile(timeout: string) { | ||
| export function testCurrentFile(timeout: string, goConfig?: vscode.WorkspaceConfiguration): Thenable<boolean> { | ||
| let editor = vscode.window.activeTextEditor; | ||
| if (!editor) { | ||
| vscode.window.showInformationMessage('No editor is active.'); | ||
| return; | ||
| } | ||
| getTestFunctions(editor.document).then(testFunctions => { | ||
| return getTestFunctions(editor.document).then(testFunctions => { | ||
| return goTest({ | ||
| timeout: timeout, | ||
| dir: path.dirname(editor.document.fileName), | ||
| functions: testFunctions.map(func => { return func.name; }) | ||
| }); | ||
| }).then(null, err => { | ||
| }, goConfig); | ||
| }, err => { | ||
| console.error(err); | ||
| }); | ||
| } | ||
|
|
@@ -116,19 +116,23 @@ export function testCurrentFile(timeout: string) { | |
| * | ||
| * @param config the test execution configuration. | ||
| */ | ||
| function goTest(config: TestConfig): Thenable<boolean> { | ||
| function goTest(config: TestConfig, goConfig?: vscode.WorkspaceConfiguration): Thenable<boolean> { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason not to make this required and fix up callers?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. None at all. Made the update to pass goConfig from goMain.ts |
||
| return new Promise<boolean>((resolve, reject) => { | ||
| outputChannel.clear(); | ||
| outputChannel.show(2); | ||
| let buildFlags: string[] = vscode.workspace.getConfiguration('go')['buildFlags']; | ||
| let buildTags: string = vscode.workspace.getConfiguration('go')['buildTags']; | ||
| if (!goConfig) { | ||
| goConfig = vscode.workspace.getConfiguration('go'); | ||
| } | ||
| let buildFlags: string[] = goConfig['buildFlags']; | ||
| let buildTags: string = goConfig['buildTags']; | ||
| let args = ['test', '-v', '-timeout', config.timeout, '-tags', buildTags, ...buildFlags]; | ||
| let testEnvVars = Object.assign({}, goConfig['testEnvVars'], process.env); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't the order need to be changed to ensure that the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True, nice catch. Corrected |
||
|
|
||
| if (config.functions) { | ||
| args.push('-run'); | ||
| args.push(util.format('^%s$', config.functions.join('|'))); | ||
| } | ||
| let proc = cp.spawn(getGoRuntimePath(), args, { env: process.env, cwd: config.dir }); | ||
| let proc = cp.spawn(getGoRuntimePath(), args, { env: testEnvVars, cwd: config.dir }); | ||
| proc.stdout.on('data', chunk => outputChannel.append(chunk.toString())); | ||
| proc.stderr.on('data', chunk => outputChannel.append(chunk.toString())); | ||
| proc.on('close', code => { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "testing" | ||
| ) | ||
|
|
||
| func hello() { | ||
| fmt.Println("Hello") | ||
| } | ||
|
|
||
| // TestMe | ||
| func TestMe(t *testing.T) { | ||
| if os.Getenv("dummyEnvVar") != "dummyEnvValue" { | ||
| t.Errorf("Oops! Value for the variable is %q", os.Getenv("dummyEnvVar")) | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possibly minor - but this change will mean that any exceptions thrown during execution of
goTestare not handled and reported toconsole.error. A weirdness of.thenon promises. Possibly just leave the additional.thencall.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put the
console.error(err)to back where it was and returned promise from there