Skip to content

Commit

Permalink
fix: respect go test flags usage
Browse files Browse the repository at this point in the history
  • Loading branch information
luoxiaohei committed Oct 10, 2024
1 parent c107653 commit f9633df
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
24 changes: 12 additions & 12 deletions extension/src/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,11 +545,14 @@ export function computeTestCommand(
addJSONFlag: boolean | undefined; // true if we add extra -json flag for stream processing.
} {
const args: Array<string> = ['test'];
const outArgs: Array<string> = ['test']; // command to show
// user-specified flags
const argsFlagIdx = testconfig.flags?.indexOf('-args') ?? -1;
const userFlags = argsFlagIdx < 0 ? testconfig.flags : testconfig.flags.slice(0, argsFlagIdx);
const userArgsFlags = argsFlagIdx < 0 ? [] : testconfig.flags.slice(argsFlagIdx);

args.push(...targets);

// flags to limit test time
if (testconfig.isBenchmark) {
args.push('-benchmem', '-run=^$');
Expand Down Expand Up @@ -587,32 +590,29 @@ export function computeTestCommand(
// all other test run/benchmark flags
args.push(...targetArgs(testconfig));

const outArgs = args.slice(0); // command to show

// if user set -v, set -json to emulate streaming test output
const addJSONFlag = (userFlags.includes('-v') || testconfig.goTestOutputConsumer) && !userFlags.includes('-json');
if (addJSONFlag) {
args.push('-json'); // this is not shown to the user.
}

if (targets.length > 4) {
outArgs.push('<long arguments omitted>');
} else {
outArgs.push(...targets);
}
args.push(...targets);

// ensure that user provided flags are appended last (allow use of -args ...)
// ignore user provided -run flag if we are already using it
if (args.indexOf('-run') > -1) {
removeRunFlag(userFlags);
}

args.push(...userFlags);
outArgs.push(...userFlags);

args.push(...userArgsFlags);
outArgs.push(...userArgsFlags);

// build outArgs
if (targets.length > 4) {
outArgs.push('<long arguments omitted>');
} else {
outArgs.push(...targets);
}

outArgs.push(...args.slice(targets.length + 1));

return {
args,
Expand Down
36 changes: 18 additions & 18 deletions extension/test/integration/test.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,67 +42,67 @@ suite('Test Go Test Args', () => {

test('default config', () => {
runTest({
expectedArgs: 'test -timeout 30s ./...',
expectedOutArgs: 'test -timeout 30s ./...'
expectedArgs: 'test ./... -timeout 30s',
expectedOutArgs: 'test ./... -timeout 30s'
});
});
test('user flag [-v] enables -json flag', () => {
runTest({
expectedArgs: 'test -timeout 30s -json ./... -v',
expectedOutArgs: 'test -timeout 30s ./... -v',
expectedArgs: 'test ./... -timeout 30s -json -v',
expectedOutArgs: 'test ./... -timeout 30s -v',
flags: ['-v']
});
});
test('user flag [-json -v] prevents -json flag addition', () => {
runTest({
expectedArgs: 'test -timeout 30s ./... -json -v',
expectedOutArgs: 'test -timeout 30s ./... -json -v',
expectedArgs: 'test ./... -timeout 30s -json -v',
expectedOutArgs: 'test ./... -timeout 30s -json -v',
flags: ['-json', '-v']
});
});
test('user flag [-args] does not crash', () => {
runTest({
expectedArgs: 'test -timeout 30s ./... -args',
expectedOutArgs: 'test -timeout 30s ./... -args',
expectedArgs: 'test ./... -timeout 30s -args',
expectedOutArgs: 'test ./... -timeout 30s -args',
flags: ['-args']
});
});
test('user flag [-args -v] does not enable -json flag', () => {
runTest({
expectedArgs: 'test -timeout 30s ./... -args -v',
expectedOutArgs: 'test -timeout 30s ./... -args -v',
expectedArgs: 'test ./... -timeout 30s -args -v',
expectedOutArgs: 'test ./... -timeout 30s -args -v',
flags: ['-args', '-v']
});
});
test('specifying functions adds -run flags', () => {
runTest({
expectedArgs: 'test -timeout 30s -run ^(TestA|TestB)$ ./...',
expectedOutArgs: 'test -timeout 30s -run ^(TestA|TestB)$ ./...',
expectedArgs: 'test ./... -timeout 30s -run ^(TestA|TestB)$',
expectedOutArgs: 'test ./... -timeout 30s -run ^(TestA|TestB)$',
functions: ['TestA', 'TestB']
});
});
test('functions & benchmark adds -bench flags and skips timeout', () => {
runTest({
expectedArgs: 'test -benchmem -run=^$ -bench ^(TestA|TestB)$ ./...',
expectedOutArgs: 'test -benchmem -run=^$ -bench ^(TestA|TestB)$ ./...',
expectedArgs: 'test ./... -benchmem -run=^$ -bench ^(TestA|TestB)$',
expectedOutArgs: 'test ./... -benchmem -run=^$ -bench ^(TestA|TestB)$',
functions: ['TestA', 'TestB'],
isBenchmark: true
});
});
test('user -run flag is ignored when functions are provided', () => {
runTest({
expectedArgs: 'test -timeout 30s -run ^(TestA|TestB)$ ./...',
expectedOutArgs: 'test -timeout 30s -run ^(TestA|TestB)$ ./...',
expectedArgs: 'test ./... -timeout 30s -run ^(TestA|TestB)$',
expectedOutArgs: 'test ./... -timeout 30s -run ^(TestA|TestB)$',
functions: ['TestA', 'TestB'],
flags: ['-run', 'TestC']
});
});
test('use -testify.m for methods', () => {
runTest({
expectedArgs:
'test -timeout 30s -run ^TestExampleTestSuite$ -testify.m ^(TestExample|TestAnotherExample)$ ./...',
'test ./... -timeout 30s -run ^TestExampleTestSuite$ -testify.m ^(TestExample|TestAnotherExample)$',
expectedOutArgs:
'test -timeout 30s -run ^TestExampleTestSuite$ -testify.m ^(TestExample|TestAnotherExample)$ ./...',
'test ./... -timeout 30s -run ^TestExampleTestSuite$ -testify.m ^(TestExample|TestAnotherExample)$',
functions: [
'(*ExampleTestSuite).TestExample',
'(*ExampleTestSuite).TestAnotherExample',
Expand Down

0 comments on commit f9633df

Please sign in to comment.