@@ -6,11 +6,12 @@ import assert from 'assert';
6
6
import path = require( 'path' ) ;
7
7
import sinon = require( 'sinon' ) ;
8
8
import vscode = require( 'vscode' ) ;
9
- import { ProgressTerminal } from '../../src/progressTerminal' ;
9
+ import { ActiveProgressTerminals , ProgressTerminal } from '../../src/progressTerminal' ;
10
10
import { ExecuteCommandParams , ExecuteCommandRequest } from 'vscode-languageserver-protocol' ;
11
11
import { Env , FakeOutputChannel } from './goplsTestEnv.utils' ;
12
12
import { URI } from 'vscode-uri' ;
13
13
import { getGoConfig } from '../../src/config' ;
14
+ import { ProgressToken } from 'vscode-languageclient' ;
14
15
15
16
suite ( 'writeVulns' , function ( ) {
16
17
this . timeout ( 30000 ) ;
@@ -22,8 +23,8 @@ suite('writeVulns', function () {
22
23
// By reusing the editor session, we reduce the test time by 1.5~2 seconds for each test.
23
24
const fixtureDir = path . join ( __dirname , '..' , '..' , '..' , 'test' , 'testdata' , 'vuln' ) ;
24
25
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 ( ) ;
27
28
suiteSetup ( async ( ) => {
28
29
const config = require ( '../../src/config' ) ;
29
30
const goConfig = Object . create ( getGoConfig ( ) , {
@@ -34,10 +35,19 @@ suite('writeVulns', function () {
34
35
sandbox . stub ( config , 'getGoConfig' ) . returns ( goConfig ) ;
35
36
await env . startGopls ( undefined , goConfig , fixtureDir ) ;
36
37
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 ;
41
51
} ) ;
42
52
} ) ;
43
53
@@ -46,37 +56,51 @@ suite('writeVulns', function () {
46
56
console . log ( '=== Gopls Trace ===' ) ;
47
57
env . flushTrace ( true ) ;
48
58
console . log ( '=== Vulncheck Terminal Output ===' ) ;
49
- console . log ( fakeTerminal . toString ( ) ) ;
59
+ console . log ( fakeChannel . toString ( ) ) ;
50
60
}
51
61
env . flushTrace ( false ) ;
52
- fakeTerminal . clear ( ) ;
62
+ fakeChannel . clear ( ) ;
53
63
} ) ;
54
64
55
65
suiteTeardown ( async ( ) => {
56
66
await env . teardown ( ) ;
57
67
sandbox . restore ( ) ;
58
68
} ) ;
59
69
60
- test ( 'govulncheck finds vulnerabilities' , async ( ) => {
70
+ test ( 'gopls.run_govulncheck finds vulnerabilities' , async ( ) => {
61
71
const workspaceDir = path . join ( fixtureDir , 'mod1' ) ;
62
- const output = await testRunGovulncheck ( workspaceDir ) ;
72
+ const output = await testRunGovulncheck ( workspaceDir , 'gopls.run_govulncheck' ) ;
63
73
const result = output . toString ( ) ;
64
74
assert ( result . includes ( 'GO-1970-TEXT' ) ) ;
65
75
assert ( result . includes ( 'vulnerabilities found' ) ) ;
66
76
} ) ;
67
77
68
- test ( 'govulncheck finds no vulnerabilities' , async ( ) => {
78
+ test ( 'gopls.run_govulncheck finds no vulnerabilities' , async ( ) => {
69
79
const workspaceDir = path . join ( fixtureDir , 'mod2' ) ;
70
- const output = await testRunGovulncheck ( workspaceDir ) ;
80
+ const output = await testRunGovulncheck ( workspaceDir , 'gopls.run_govulncheck' ) ;
71
81
assert ( output . toString ( ) . includes ( 'No vulnerabilities found' ) ) ;
72
82
} ) ;
73
83
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 ) {
75
99
const languageClient = env . languageClient ! ;
76
100
const document = await vscode . workspace . openTextDocument ( vscode . Uri . file ( path . join ( workspaceDir , 'go.mod' ) ) ) ;
77
101
const uri = languageClient . code2ProtocolConverter . asTextDocumentIdentifier ( document ) . uri ;
78
102
79
- languageClient . middleware ! . executeCommand ! ( 'gopls.run_govulncheck' , [ { URI : uri } ] , async ( cmd , args ) => {
103
+ languageClient . middleware ! . executeCommand ! ( command , [ { URI : uri } ] , async ( cmd , args ) => {
80
104
const params : ExecuteCommandParams = {
81
105
command : cmd ,
82
106
arguments : args
@@ -89,11 +113,11 @@ suite('writeVulns', function () {
89
113
const timeout = setTimeout ( ( ) => {
90
114
reject ( `Timed out while waiting for '${ msg } '` ) ;
91
115
} , timeoutMS ) ;
92
- fakeTerminal . onPattern ( msg , ( ) => {
116
+ fakeChannel . onPattern ( msg , ( ) => {
93
117
clearTimeout ( timeout ) ;
94
118
resolve ( ) ;
95
119
} ) ;
96
120
} ) ;
97
- return fakeTerminal ;
121
+ return fakeChannel ;
98
122
}
99
123
} ) ;
0 commit comments