Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

debug: respect go test flags usage #2428

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 -json -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