@@ -6,11 +6,12 @@ import assert from 'assert';
66import path = require( 'path' ) ;
77import sinon = require( 'sinon' ) ;
88import vscode = require( 'vscode' ) ;
9- import { ProgressTerminal } from '../../src/progressTerminal' ;
9+ import { ActiveProgressTerminals , ProgressTerminal } from '../../src/progressTerminal' ;
1010import { ExecuteCommandParams , ExecuteCommandRequest } from 'vscode-languageserver-protocol' ;
1111import { Env , FakeOutputChannel } from './goplsTestEnv.utils' ;
1212import { URI } from 'vscode-uri' ;
1313import { getGoConfig } from '../../src/config' ;
14+ import { ProgressToken } from 'vscode-languageclient' ;
1415
1516suite ( '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