Skip to content

Commit 1cd4a9a

Browse files
h9jianggopherbot
authored andcommitted
extension/test/gopls: add integration test for gopls.vulncheck
gopls.vulncheck runs the exact same test as gopls.run_vulncheck. In production, the gopls.vulncheck streams the log to the terminal if terminal remain active in ActiveProgressTerminals. For simplicity, we assumed the terminal will never be closed and added to the map once created. For #3572 Change-Id: Ie7c94677ee76a10f340d5334e902b4946b2b34dd Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/650646 LUCI-TryBot-Result: Go LUCI <[email protected]> kokoro-CI: kokoro <[email protected]> Reviewed-by: Robert Findley <[email protected]> Auto-Submit: Hongxiang Jiang <[email protected]> Reviewed-by: Madeline Kalil <[email protected]>
1 parent 3d2f1cf commit 1cd4a9a

File tree

1 file changed

+41
-17
lines changed

1 file changed

+41
-17
lines changed

extension/test/gopls/vulncheck.test.ts

+41-17
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import assert from 'assert';
66
import path = require('path');
77
import sinon = require('sinon');
88
import vscode = require('vscode');
9-
import { ProgressTerminal } from '../../src/progressTerminal';
9+
import { ActiveProgressTerminals, ProgressTerminal } from '../../src/progressTerminal';
1010
import { ExecuteCommandParams, ExecuteCommandRequest } from 'vscode-languageserver-protocol';
1111
import { Env, FakeOutputChannel } from './goplsTestEnv.utils';
1212
import { URI } from 'vscode-uri';
1313
import { getGoConfig } from '../../src/config';
14+
import { ProgressToken } from 'vscode-languageclient';
1415

1516
suite('writeVulns', function () {
1617
this.timeout(30000);
@@ -22,8 +23,8 @@ suite('writeVulns', function () {
2223
// By reusing the editor session, we reduce the test time by 1.5~2 seconds for each test.
2324
const fixtureDir = path.join(__dirname, '..', '..', '..', 'test', 'testdata', 'vuln');
2425

25-
// Tests will run govulncheck and see if expected output is accumulated in fakeTerminal.
26-
const fakeTerminal = new FakeOutputChannel();
26+
// Tests will run govulncheck and see if expected output is accumulated in fakeChannel.
27+
const fakeChannel = new FakeOutputChannel();
2728
suiteSetup(async () => {
2829
const config = require('../../src/config');
2930
const goConfig = Object.create(getGoConfig(), {
@@ -34,10 +35,19 @@ suite('writeVulns', function () {
3435
sandbox.stub(config, 'getGoConfig').returns(goConfig);
3536
await env.startGopls(undefined, goConfig, fixtureDir);
3637

37-
sandbox.stub(ProgressTerminal, 'Open').returns({
38-
appendLine: fakeTerminal.appendLine,
39-
show: () => {},
40-
exit: () => {}
38+
sandbox.stub(ProgressTerminal, 'Open').callsFake((_name?: string, token?: ProgressToken) => {
39+
const fakeTerminal = {
40+
appendLine: fakeChannel.appendLine,
41+
show: () => {},
42+
exit: () => {}
43+
} as ProgressTerminal;
44+
if (token) {
45+
// Add the fake terminal to ActiveProgressTerminals to stream
46+
// logs from executeCommand and workDoneProgress. Test assumes
47+
// terminal remains open, unlike production scenarios.
48+
ActiveProgressTerminals.set(token, fakeTerminal);
49+
}
50+
return fakeTerminal;
4151
});
4252
});
4353

@@ -46,37 +56,51 @@ suite('writeVulns', function () {
4656
console.log('=== Gopls Trace ===');
4757
env.flushTrace(true);
4858
console.log('=== Vulncheck Terminal Output ===');
49-
console.log(fakeTerminal.toString());
59+
console.log(fakeChannel.toString());
5060
}
5161
env.flushTrace(false);
52-
fakeTerminal.clear();
62+
fakeChannel.clear();
5363
});
5464

5565
suiteTeardown(async () => {
5666
await env.teardown();
5767
sandbox.restore();
5868
});
5969

60-
test('govulncheck finds vulnerabilities', async () => {
70+
test('gopls.run_govulncheck finds vulnerabilities', async () => {
6171
const workspaceDir = path.join(fixtureDir, 'mod1');
62-
const output = await testRunGovulncheck(workspaceDir);
72+
const output = await testRunGovulncheck(workspaceDir, 'gopls.run_govulncheck');
6373
const result = output.toString();
6474
assert(result.includes('GO-1970-TEXT'));
6575
assert(result.includes('vulnerabilities found'));
6676
});
6777

68-
test('govulncheck finds no vulnerabilities', async () => {
78+
test('gopls.run_govulncheck finds no vulnerabilities', async () => {
6979
const workspaceDir = path.join(fixtureDir, 'mod2');
70-
const output = await testRunGovulncheck(workspaceDir);
80+
const output = await testRunGovulncheck(workspaceDir, 'gopls.run_govulncheck');
7181
assert(output.toString().includes('No vulnerabilities found'));
7282
});
7383

74-
async function testRunGovulncheck(workspaceDir: string) {
84+
test('gopls.vulncheck finds vulnerabilities', async () => {
85+
const workspaceDir = path.join(fixtureDir, 'mod1');
86+
const output = await testRunGovulncheck(workspaceDir, 'gopls.vulncheck');
87+
const result = output.toString();
88+
assert(result.includes('GO-1970-TEXT'));
89+
assert(result.includes('vulnerabilities found'));
90+
});
91+
92+
test('gopls.vulncheck finds no vulnerabilities', async () => {
93+
const workspaceDir = path.join(fixtureDir, 'mod2');
94+
const output = await testRunGovulncheck(workspaceDir, 'gopls.vulncheck');
95+
assert(output.toString().includes('No vulnerabilities found'));
96+
});
97+
98+
async function testRunGovulncheck(workspaceDir: string, command: string) {
7599
const languageClient = env.languageClient!;
76100
const document = await vscode.workspace.openTextDocument(vscode.Uri.file(path.join(workspaceDir, 'go.mod')));
77101
const uri = languageClient.code2ProtocolConverter.asTextDocumentIdentifier(document).uri;
78102

79-
languageClient.middleware!.executeCommand!('gopls.run_govulncheck', [{ URI: uri }], async (cmd, args) => {
103+
languageClient.middleware!.executeCommand!(command, [{ URI: uri }], async (cmd, args) => {
80104
const params: ExecuteCommandParams = {
81105
command: cmd,
82106
arguments: args
@@ -89,11 +113,11 @@ suite('writeVulns', function () {
89113
const timeout = setTimeout(() => {
90114
reject(`Timed out while waiting for '${msg}'`);
91115
}, timeoutMS);
92-
fakeTerminal.onPattern(msg, () => {
116+
fakeChannel.onPattern(msg, () => {
93117
clearTimeout(timeout);
94118
resolve();
95119
});
96120
});
97-
return fakeTerminal;
121+
return fakeChannel;
98122
}
99123
});

0 commit comments

Comments
 (0)