This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 647
run benchmark at cursor (#972) #1303
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,10 @@ export interface TestConfig { | |
* Run all tests from all sub directories under `dir` | ||
*/ | ||
includeSubDirectories?: boolean; | ||
/** | ||
* Whether this is a benchmark. | ||
*/ | ||
isBenchmark?: boolean; | ||
} | ||
|
||
export function getTestEnvVars(config: vscode.WorkspaceConfiguration): any { | ||
|
@@ -78,28 +82,48 @@ export function getTestFlags(goConfig: vscode.WorkspaceConfiguration, args: any) | |
* @param the URI of a Go source file. | ||
* @return test function symbols for the source file. | ||
*/ | ||
export function getTestFunctions(doc: vscode.TextDocument): Thenable<vscode.SymbolInformation[]> { | ||
export function getTestFunctions(doc: vscode.TextDocument, checker: prefixChecker): Thenable<vscode.SymbolInformation[]> { | ||
let documentSymbolProvider = new GoDocumentSymbolProvider(); | ||
return documentSymbolProvider | ||
.provideDocumentSymbols(doc, null) | ||
.then(symbols => | ||
symbols.filter(sym => | ||
sym.kind === vscode.SymbolKind.Function | ||
&& hasTestFunctionPrefix(sym.name)) | ||
&& checker(sym.name)) | ||
); | ||
} | ||
|
||
/** | ||
* Function type for function that given a function name has | ||
* returns whether it is of a certain type of prefix. | ||
* | ||
* @param the function name. | ||
* @return whether the name has a function prefix. | ||
*/ | ||
type prefixChecker = (name: string) => boolean; | ||
|
||
/** | ||
* Returns whether a given function name has a test prefix. | ||
* Test functions have "Test" or "Example" as a prefix. | ||
* | ||
* @param the function name. | ||
* @return whether the name has a test function prefix. | ||
*/ | ||
function hasTestFunctionPrefix(name: string): boolean { | ||
export function hasTestFunctionPrefix(name: string): boolean { | ||
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. There was no real point of making this a separate function in the first place (my apologies). Instead of exporting this prefix function and adding another for benchmark, we can simplify things as below
It will definitely get simpler when importing these functions in other files |
||
return name.startsWith('Test') || name.startsWith('Example'); | ||
} | ||
|
||
/** | ||
* Returns whether a given function name has a benchmark prefix. | ||
* Benchmark functions have "Benchmark" as a prefix. | ||
* | ||
* @param the function name. | ||
* @return whether the name has a benchmark function prefix. | ||
*/ | ||
export function hasBenchmarkFunctionPrefix(name: string): boolean { | ||
return name.startsWith('Benchmark'); | ||
} | ||
|
||
/** | ||
* Runs go test and presents the output in the 'Go' channel. | ||
* | ||
|
@@ -114,7 +138,20 @@ export function goTest(testconfig: TestConfig): Thenable<boolean> { | |
} | ||
|
||
let buildTags: string = testconfig.goConfig['buildTags']; | ||
let args = ['test', ...testconfig.flags, '-timeout', testconfig.goConfig['testTimeout']]; | ||
|
||
let args: Array<string>; | ||
let handleFunc: argsHandleFunc; | ||
let testType: string; | ||
|
||
if (testconfig.isBenchmark) { | ||
args = ['test', ...testconfig.flags, '-benchmem', '-run=^$']; | ||
handleFunc = benchmarkTargetArgs; | ||
testType = 'Benchmarks'; | ||
} else { | ||
args = ['test', ...testconfig.flags, '-timeout', testconfig.goConfig['testTimeout']]; | ||
handleFunc = testTargetArgs; | ||
testType = 'Tests'; | ||
} | ||
if (buildTags && testconfig.flags.indexOf('-tags') === -1) { | ||
args.push('-tags'); | ||
args.push(buildTags); | ||
|
@@ -133,7 +170,7 @@ export function goTest(testconfig: TestConfig): Thenable<boolean> { | |
args.push(testconfig.dir.substr(currentGoWorkspace.length + 1)); | ||
} | ||
|
||
targetArgs(testconfig).then(targets => { | ||
handleFunc(testconfig).then(targets => { | ||
let outTargets = args.slice(0); | ||
if (targets.length > 2) { | ||
outTargets.push('<long arguments omitted>'); | ||
|
@@ -163,14 +200,14 @@ export function goTest(testconfig: TestConfig): Thenable<boolean> { | |
errBuf.done(); | ||
|
||
if (code) { | ||
outputChannel.appendLine('Error: Tests failed.'); | ||
outputChannel.appendLine(`Error: ${testType} failed.`); | ||
} else { | ||
outputChannel.appendLine('Success: Tests passed.'); | ||
outputChannel.appendLine(`Success: ${testType} passed.`); | ||
} | ||
resolve(code === 0); | ||
}); | ||
}, err => { | ||
outputChannel.appendLine('Error: Tests failed.'); | ||
outputChannel.appendLine(`Error: ${testType} failed.`); | ||
outputChannel.appendLine(err); | ||
resolve(false); | ||
}); | ||
|
@@ -195,12 +232,19 @@ function expandFilePathInOutput(output: string, cwd: string): string { | |
return lines.join('\n'); | ||
} | ||
|
||
/** | ||
* Function type for getting the target arguments. | ||
* | ||
* @param testconfig Configuration for the Go extension. | ||
*/ | ||
type argsHandleFunc = (testconfig: TestConfig) => Thenable<Array<string>>; | ||
|
||
/** | ||
* Get the test target arguments. | ||
* | ||
* @param testconfig Configuration for the Go extension. | ||
*/ | ||
function targetArgs(testconfig: TestConfig): Thenable<Array<string>> { | ||
function testTargetArgs(testconfig: TestConfig): Thenable<Array<string>> { | ||
if (testconfig.functions) { | ||
return Promise.resolve(['-run', util.format('^%s$', testconfig.functions.join('|'))]); | ||
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. Instead of having a separate |
||
} else if (testconfig.includeSubDirectories) { | ||
|
@@ -213,3 +257,12 @@ function targetArgs(testconfig: TestConfig): Thenable<Array<string>> { | |
} | ||
return Promise.resolve([]); | ||
} | ||
|
||
/** | ||
* Get the benchmark target arguments. | ||
* | ||
* @param testconfig Configuration for the Go extension. | ||
*/ | ||
function benchmarkTargetArgs(testconfig: TestConfig): Thenable<Array<string>> { | ||
return Promise.resolve(['-bench', util.format('^%s$', testconfig.functions.join('|'))]); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This Promise should resolve only after both tests and benchmark functions are found