Skip to content

Commit

Permalink
debug: respect go test flags usage
Browse files Browse the repository at this point in the history
  • Loading branch information
luoxiaohei committed Sep 15, 2022
1 parent 4298b52 commit dd931e4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
24 changes: 12 additions & 12 deletions src/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,11 +427,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 @@ -469,32 +472,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
32 changes: 16 additions & 16 deletions test/integration/test.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,57 +42,57 @@ 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']
});
Expand Down

0 comments on commit dd931e4

Please sign in to comment.