Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Commit

Permalink
Pass build flags & args from testFlags separately to dlv #2115
Browse files Browse the repository at this point in the history
  • Loading branch information
ramya-rao-a committed Dec 27, 2019
1 parent d6b6668 commit 9ab7b8b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
22 changes: 5 additions & 17 deletions src/goRunTestCodelens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
'use strict';

import vscode = require('vscode');
import path = require('path');
import { CancellationToken, CodeLens, Command, TextDocument } from 'vscode';
import { GoBaseCodeLensProvider } from './goBaseCodelens';
import { GoDocumentSymbolProvider } from './goOutline';
import { getBenchmarkFunctions, getTestFlags, getTestFunctionDebugArgs, getTestFunctions, getTestTags } from './testUtils';
import { getBenchmarkFunctions, getTestFunctions } from './testUtils';
import { getCurrentGoPath, getGoConfig } from './util';

export class GoRunTestCodeLensProvider extends GoBaseCodeLensProvider {
Expand Down Expand Up @@ -84,16 +83,6 @@ export class GoRunTestCodeLensProvider extends GoBaseCodeLensProvider {
private async getCodeLensForFunctions(vsConfig: vscode.WorkspaceConfiguration, document: TextDocument, token: CancellationToken): Promise<CodeLens[]> {
const codelens: CodeLens[] = [];

const program = path.dirname(document.fileName);
const env = Object.assign({}, this.debugConfig.env, vsConfig['testEnvVars']);
const envFile = vsConfig['testEnvFile'];
const buildFlags = getTestFlags(vsConfig);
const tags = getTestTags(vsConfig);
if (tags && buildFlags.indexOf('-tags') === -1) {
buildFlags.push('-tags', tags);
}
const currentDebugConfig = Object.assign({}, this.debugConfig, { program, env, envFile, buildFlags: buildFlags.map(x => `'${x}'`).join(' ') });

const testPromise = getTestFunctions(document, token).then(testFunctions => {
testFunctions.forEach(func => {
const runTestCmd: Command = {
Expand All @@ -104,11 +93,10 @@ export class GoRunTestCodeLensProvider extends GoBaseCodeLensProvider {

codelens.push(new CodeLens(func.range, runTestCmd));

const args = getTestFunctionDebugArgs(document, func.name, testFunctions);
const debugTestCmd: Command = {
title: 'debug test',
command: 'go.debug.startSession',
arguments: [Object.assign({}, currentDebugConfig, { args })]
command: 'go.debug.cursor',
arguments: [{ functionName: func.name }]
};

codelens.push(new CodeLens(func.range, debugTestCmd));
Expand All @@ -127,8 +115,8 @@ export class GoRunTestCodeLensProvider extends GoBaseCodeLensProvider {

const debugTestCmd: Command = {
title: 'debug benchmark',
command: 'go.debug.startSession',
arguments: [Object.assign({}, currentDebugConfig, { args: ['-test.bench', '^' + func.name + '$', '-test.run', 'a^'] })]
command: 'go.debug.cursor',
arguments: [{ functionName: func.name }]
};

codelens.push(new CodeLens(func.range, debugTestCmd));
Expand Down
17 changes: 16 additions & 1 deletion src/goTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,21 @@ async function runTestAtCursor(editor: vscode.TextEditor, testFunctionName: stri
async function debugTestAtCursor(editor: vscode.TextEditor, testFunctionName: string, testFunctions: vscode.DocumentSymbol[], goConfig: vscode.WorkspaceConfiguration) {

const args = getTestFunctionDebugArgs(editor.document, testFunctionName, testFunctions);
const tags = getTestTags(goConfig);
const buildFlags = tags ? ['-tags', tags] : [];
const flagsFromConfig = getTestFlags(goConfig);
let foundArgsFlag = false;
flagsFromConfig.forEach(x => {
if (foundArgsFlag) {
args.push(x);
return;
}
if (x === '-args') {
foundArgsFlag = true;
return;
}
buildFlags.push(x);
});
const workspaceFolder = vscode.workspace.getWorkspaceFolder(editor.document.uri);
const debugConfig: vscode.DebugConfiguration = {
name: 'Debug Test',
Expand All @@ -107,7 +122,7 @@ async function debugTestAtCursor(editor: vscode.TextEditor, testFunctionName: st
env: goConfig.get('testEnvVars', {}),
envFile: goConfig.get('testEnvFile'),
args,
buildFlags: ['-tags', getTestTags(goConfig)].join(' ')
buildFlags: buildFlags.join(' ')
};
return await vscode.debug.startDebugging(workspaceFolder, debugConfig);
}
Expand Down
3 changes: 3 additions & 0 deletions src/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ export function extractInstanceTestName(symbolName: string): string {
* @param testFunctions The test functions found in the document
*/
export function getTestFunctionDebugArgs(document: vscode.TextDocument, testFunctionName: string, testFunctions: vscode.DocumentSymbol[]): string[] {
if (benchmarkRegex.test(testFunctionName)) {
return ['-test.bench', '^' + testFunctionName + '$', '-test.run', 'a^'];
}
const instanceMethod = extractInstanceTestName(testFunctionName);
if (instanceMethod) {
const testFns = findAllTestSuiteRuns(document, testFunctions);
Expand Down

0 comments on commit 9ab7b8b

Please sign in to comment.