This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 646
/
Copy pathgoRunTestCodelens.ts
88 lines (77 loc) · 2.87 KB
/
goRunTestCodelens.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------*/
'use strict';
import vscode = require('vscode');
import path = require('path');
import { CodeLensProvider, TextDocument, CancellationToken, CodeLens, Command } from 'vscode';
import { getTestFunctions, getTestEnvVars } from './goTest';
import { GoDocumentSymbolProvider } from './goOutline';
export class GoRunTestCodeLensProvider implements CodeLensProvider {
private readonly debugConfig: any = {
'name': 'Launch',
'type': 'go',
'request': 'launch',
'mode': 'test'
};
public provideCodeLenses(document: TextDocument, token: CancellationToken): CodeLens[] | Thenable<CodeLens[]> {
let config = vscode.workspace.getConfiguration('go');
let codeLensConfig = config.get('enableCodeLens');
let codelensEnabled = codeLensConfig ? codeLensConfig['runtest'] : false;
if (!codelensEnabled || !document.fileName.endsWith('_test.go')) {
return;
}
return Promise.all([
this.getCodeLensForPackage(document),
this.getCodeLensForFunctions(config, document)
]).then(res => {
return res[0].concat(res[1]);
});
}
private getCodeLensForPackage(document: TextDocument): Thenable<CodeLens[]> {
let documentSymbolProvider = new GoDocumentSymbolProvider();
return documentSymbolProvider.provideDocumentSymbols(document, null)
.then(symbols => symbols.find(sym => sym.kind === vscode.SymbolKind.Package && !!sym.name))
.then(pkg => {
if (pkg) {
const range = pkg.location.range;
return [
new CodeLens(range, {
title: 'run package tests',
command: 'go.test.package'
}),
new CodeLens(range, {
title: 'run file tests',
command: 'go.test.file'
})
];
}
});
}
private getCodeLensForFunctions(vsConfig: vscode.WorkspaceConfiguration, document: TextDocument): Thenable<CodeLens[]> {
return getTestFunctions(document).then(testFunctions => {
let codelens = [];
testFunctions.forEach(func => {
let runTestCmd: Command = {
title: 'run test',
command: 'go.test.cursor',
arguments: [ { functionName: func.name} ]
};
const args = ['-test.run', func.name];
const program = path.dirname(document.fileName);
const env = vsConfig['testEnvVars'] || {};
const envFile = vsConfig['testEnvFile'];
let config = Object.assign({}, this.debugConfig, { args, program, env, envFile });
let debugTestCmd: Command = {
title: 'debug test',
command: 'vscode.startDebug',
arguments: [ config ]
};
codelens.push(new CodeLens(func.location.range, runTestCmd));
codelens.push(new CodeLens(func.location.range, debugTestCmd));
});
return codelens;
});
}
}