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

Commit

Permalink
Fix for test suits and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Vlad Barosan committed Jan 28, 2019
1 parent eb702e7 commit d5dfc5c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 73 deletions.
71 changes: 30 additions & 41 deletions src/goDebugTest.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

import vscode = require('vscode');
import { getTestFunctions } from './testUtils';
import { getTestFunctions, extractInstanceTestName, findAllTestSuiteRuns, getTestFunctionDebugArgs } from './testUtils';

/**
* Debugs the unit test at the primary cursor. Output
Expand All @@ -10,7 +10,7 @@ import { getTestFunctions } from './testUtils';
* @param goConfig Configuration for the Go extension.
*/
export function debugTestAtCursor(goConfig: vscode.WorkspaceConfiguration, args: any) {
let editor = vscode.window.activeTextEditor;
const editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showInformationMessage('No editor is active.');
return;
Expand All @@ -20,46 +20,35 @@ export function debugTestAtCursor(goConfig: vscode.WorkspaceConfiguration, args:
return;
}

getTestFunctions(editor.document, null).then(testFunctions => {
let testFunctionName: string;

// We use functionName if it was provided as argument
// Otherwise find any test function containing the cursor.
if (args && args.functionName) {
testFunctionName = args.functionName;
} else {
for (let func of testFunctions) {
let selection = editor.selection;
if (selection && func.location.range.contains(selection.start)) {
testFunctionName = func.name;
break;
}
editor.document.save().then(() => {
return getTestFunctions(editor.document, null).then(testFunctions => {
// We use functionName if it was provided as argument
// Otherwise find any test function containing the cursor.
const testFunctionName = args && args.functionName
? args.functionName
: testFunctions.find(func => func.location.range.contains(editor.selection.start)).name;

if (!testFunctionName) {
vscode.window.showInformationMessage('No test function found at cursor.');
return;
}
}

if (!testFunctionName) {
vscode.window.showInformationMessage('No test function found at cursor.');
return;
}

const configArgs = ['-test.run', testFunctionName];
const env = goConfig['testEnvVars'] || {};
const envFile = goConfig['testEnvFile'];

let workspaceFolder = vscode.workspace.getWorkspaceFolder(editor.document.uri);
let debugConfig: vscode.DebugConfiguration = {
'name': 'Debug Test',
'type': 'go',
'request': 'launch',
'mode': 'auto',
'program': editor.document.fileName,
'env': env,
'envFile': envFile,
'args': configArgs
};

return vscode.debug.startDebugging(workspaceFolder, debugConfig);
}).then(null, err => {
console.log(err);
const dbgArgs = getTestFunctionDebugArgs(editor.document, testFunctionName, testFunctions);
const workspaceFolder = vscode.workspace.getWorkspaceFolder(editor.document.uri);
const debugConfig: vscode.DebugConfiguration = {
name: 'Debug Test',
type: 'go',
request: 'launch',
mode: 'auto',
program: editor.document.fileName,
env: goConfig.get('testEnvVars', {}),
envFile: goConfig.get('testEnvFile'),
args: dbgArgs
};

return vscode.debug.startDebugging(workspaceFolder, debugConfig);
}).then(null, err => {
console.log(err);
});
});
}
10 changes: 5 additions & 5 deletions src/goMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,15 +332,15 @@ export function activate(ctx: vscode.ExtensionContext): void {
testAtCursor(goConfig, isBenchmark, args);
}));

ctx.subscriptions.push(vscode.commands.registerCommand('go.benchmark.cursor', (args) => {
ctx.subscriptions.push(vscode.commands.registerCommand('go.test.debug', (args) => {
let goConfig = vscode.workspace.getConfiguration('go', vscode.window.activeTextEditor ? vscode.window.activeTextEditor.document.uri : null);
let isBenchmark = true;
testAtCursor(goConfig, isBenchmark, args);
debugTestAtCursor(goConfig, args);
}));

ctx.subscriptions.push(vscode.commands.registerCommand('go.test.debug', (args) => {
ctx.subscriptions.push(vscode.commands.registerCommand('go.benchmark.cursor', (args) => {
let goConfig = vscode.workspace.getConfiguration('go', vscode.window.activeTextEditor ? vscode.window.activeTextEditor.document.uri : null);
debugTestAtCursor(goConfig, args);
let isBenchmark = true;
testAtCursor(goConfig, isBenchmark, args);
}));

ctx.subscriptions.push(vscode.commands.registerCommand('go.test.package', (args) => {
Expand Down
31 changes: 9 additions & 22 deletions src/goRunTestCodelens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

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

export class GoRunTestCodeLensProvider extends GoBaseCodeLensProvider {
private readonly benchmarkRegex = /^Benchmark.+/;
Expand Down Expand Up @@ -101,47 +101,34 @@ export class GoRunTestCodeLensProvider extends GoBaseCodeLensProvider {

const testPromise = getTestFunctions(document, token).then(testFunctions => {
testFunctions.forEach(func => {
let runTestCmd: Command = {
const runTestCmd: Command = {
title: 'run test',
command: 'go.test.cursor',
arguments: [{ functionName: func.name }]
};

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

let args: string[] = [];
let instanceMethod = extractInstanceTestName(func.name);
if (instanceMethod) {
const testFns = findAllTestSuiteRuns(document, testFunctions);
if (testFns && testFns.length > 0) {
args = args.concat('-test.run', `^${testFns.map(t => t.name).join('|')}$`);
}
args = args.concat('-testify.m', `^${instanceMethod}$`);
} else {
args = args.concat('-test.run', `^${func.name}$`);
}

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

codelens.push(new CodeLens(func.location.range, debugTestCmd));
});
});

const benchmarkPromise = getBenchmarkFunctions(document, token).then(benchmarkFunctions => {
benchmarkFunctions.forEach(func => {
let runBenchmarkCmd: Command = {
const runBenchmarkCmd: Command = {
title: 'run benchmark',
command: 'go.benchmark.cursor',
arguments: [{ functionName: func.name }]
};

codelens.push(new CodeLens(func.location.range, runBenchmarkCmd));

let debugTestCmd: Command = {
const debugTestCmd: Command = {
title: 'debug benchmark',
command: 'go.debug.startSession',
arguments: [Object.assign({}, currentDebugConfig, { args: ['-test.bench', '^' + func.name + '$', '-test.run', 'a^'] })]
Expand Down
28 changes: 23 additions & 5 deletions src/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
* Copyright (C) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------*/

import cp = require('child_process');
import path = require('path');
import vscode = require('vscode');
import util = require('util');
import { parseEnvFile, getCurrentGoWorkspaceFromGOPATH } from './goPath';
import { getToolsEnvVars, getGoVersion, LineBuffer, SemVersion, resolvePath, getCurrentGoPath, getBinPath } from './util';
import vscode = require('vscode');

import { getCurrentPackage } from './goModules';
import { GoDocumentSymbolProvider } from './goOutline';
import { getNonVendorPackages } from './goPackages';
import { getCurrentPackage } from './goModules';
import { getCurrentGoWorkspaceFromGOPATH, parseEnvFile } from './goPath';
import { getBinPath, getCurrentGoPath, getGoVersion, getToolsEnvVars, LineBuffer, resolvePath } from './util';


const sendSignal = 'SIGKILL';
const outputChannel = vscode.window.createOutputChannel('Go Tests');
Expand Down Expand Up @@ -126,6 +127,23 @@ export function extractInstanceTestName(symbolName: string): string {
return match[2];
}

/**
* Gets the appropriate debug arguments for a debug session on a test function.
* @param document The document containing the tests
* @param testFunctionName The test function to get the debug args
* @param testFunctions The test functions found in the document
*/
export function getTestFunctionDebugArgs(document: vscode.TextDocument, testFunctionName: string, testFunctions: vscode.SymbolInformation[]): string[] {
const instanceMethod = extractInstanceTestName(testFunctionName);
if (instanceMethod) {
const testFns = findAllTestSuiteRuns(document, testFunctions);
const testSuiteRuns = ['-test.run', `^${testFns.map(t => t.name).join('|')}$`];
const testSuiteTests = ['-testify.m', `^${instanceMethod}$`];
return [...testSuiteRuns, ...testSuiteTests];
} else {
return ['-test.run', `^${testFunctionName}$`];
}
}
/**
* Finds test methods containing "suite.Run()" call.
*
Expand Down

0 comments on commit d5dfc5c

Please sign in to comment.