@@ -11,9 +11,10 @@ import sinon = require('sinon');
1111import vscode = require( 'vscode' ) ;
1212import { updateGoVarsFromConfig } from '../../src/goInstallTools' ;
1313import { GoRunTestCodeLensProvider } from '../../src/goRunTestCodelens' ;
14- import { subTestAtCursor } from '../../src/goTest' ;
14+ import { subTestAtCursor , testAtCursor } from '../../src/goTest' ;
1515import { MockExtensionContext } from '../mocks/MockContext' ;
1616import { Env } from './goplsTestEnv.utils' ;
17+ import * as testUtils from '../../src/testUtils' ;
1718
1819suite ( 'Code lenses for testing and benchmarking' , function ( ) {
1920 this . timeout ( 20000 ) ;
@@ -200,4 +201,141 @@ suite('Code lenses for testing and benchmarking', function () {
200201 // Results should match `go test -list`.
201202 assert . deepStrictEqual ( found , [ 'TestNotMain' ] ) ;
202203 } ) ;
204+
205+ test ( 'Debug - debugs a test with cursor on t.Run line' , async ( ) => {
206+ const startDebuggingStub = sinon . stub ( vscode . debug , 'startDebugging' ) . returns ( Promise . resolve ( true ) ) ;
207+
208+ const editor = await vscode . window . showTextDocument ( document ) ;
209+ editor . selection = new vscode . Selection ( 7 , 4 , 7 , 4 ) ;
210+ const result = await subTestAtCursor ( 'debug' ) ( ctx , env . goCtx ) ( [ ] ) ;
211+ assert . strictEqual ( result , true ) ;
212+
213+ assert . strictEqual ( startDebuggingStub . callCount , 1 , 'expected one call to startDebugging' ) ;
214+ const gotConfig = startDebuggingStub . getCall ( 0 ) . args [ 1 ] as vscode . DebugConfiguration ;
215+ gotConfig . program = '' ;
216+ assert . deepStrictEqual < vscode . DebugConfiguration > ( gotConfig , {
217+ name : 'Debug Test' ,
218+ type : 'go' ,
219+ request : 'launch' ,
220+ args : [ '-test.run' , '^TestSample$/^sample_test_passing$' ] ,
221+ buildFlags : '' ,
222+ env : { } ,
223+ sessionID : undefined ,
224+ mode : 'test' ,
225+ envFile : null ,
226+ program : ''
227+ } ) ;
228+ } ) ;
229+ } ) ;
230+
231+ suite ( 'Code lenses with stretchr/testify/suite' , function ( ) {
232+ const ctx = MockExtensionContext . new ( ) ;
233+
234+ const testdataDir = path . join ( __dirname , '..' , '..' , '..' , 'test' , 'testdata' , 'stretchrTestSuite' ) ;
235+ const env = new Env ( ) ;
236+
237+ this . afterEach ( async function ( ) {
238+ // Note: this shouldn't use () => {...}. Arrow functions do not have 'this'.
239+ // I don't know why but this.currentTest.state does not have the expected value when
240+ // used with teardown.
241+ env . flushTrace ( this . currentTest ?. state === 'failed' ) ;
242+ ctx . teardown ( ) ;
243+ sinon . restore ( ) ;
244+ } ) ;
245+
246+ suiteSetup ( async ( ) => {
247+ await updateGoVarsFromConfig ( { } ) ;
248+ await env . startGopls ( undefined , undefined , testdataDir ) ;
249+ } ) ;
250+
251+ suiteTeardown ( async ( ) => {
252+ await env . teardown ( ) ;
253+ } ) ;
254+
255+ test ( 'Run test at cursor' , async ( ) => {
256+ const goTestStub = sinon . stub ( testUtils , 'goTest' ) . returns ( Promise . resolve ( true ) ) ;
257+
258+ const editor = await vscode . window . showTextDocument ( vscode . Uri . file ( path . join ( testdataDir , 'suite_test.go' ) ) ) ;
259+ editor . selection = new vscode . Selection ( 25 , 4 , 25 , 4 ) ;
260+
261+ const result = await testAtCursor ( 'test' ) ( ctx , env . goCtx ) ( [ ] ) ;
262+ assert . strictEqual ( result , true ) ;
263+
264+ assert . strictEqual ( goTestStub . callCount , 1 , 'expected one call to goTest' ) ;
265+ const gotConfig = goTestStub . getCall ( 0 ) . args [ 0 ] ;
266+ assert . deepStrictEqual ( gotConfig . functions , [ '(*ExampleTestSuite).TestExample' , 'TestExampleTestSuite' ] ) ;
267+ } ) ;
268+
269+ test ( 'Run test at cursor in different file than test suite definition' , async ( ) => {
270+ const goTestStub = sinon . stub ( testUtils , 'goTest' ) . returns ( Promise . resolve ( true ) ) ;
271+
272+ const editor = await vscode . window . showTextDocument (
273+ vscode . Uri . file ( path . join ( testdataDir , 'another_suite_test.go' ) )
274+ ) ;
275+ editor . selection = new vscode . Selection ( 3 , 4 , 3 , 4 ) ;
276+
277+ const result = await testAtCursor ( 'test' ) ( ctx , env . goCtx ) ( [ ] ) ;
278+ assert . strictEqual ( result , true ) ;
279+
280+ assert . strictEqual ( goTestStub . callCount , 1 , 'expected one call to goTest' ) ;
281+ const gotConfig = goTestStub . getCall ( 0 ) . args [ 0 ] ;
282+ assert . deepStrictEqual ( gotConfig . functions , [
283+ '(*ExampleTestSuite).TestExampleInAnotherFile' ,
284+ 'TestExampleTestSuite'
285+ ] ) ;
286+ } ) ;
287+
288+ test ( 'Debug test at cursor' , async ( ) => {
289+ const startDebuggingStub = sinon . stub ( vscode . debug , 'startDebugging' ) . returns ( Promise . resolve ( true ) ) ;
290+
291+ const editor = await vscode . window . showTextDocument ( vscode . Uri . file ( path . join ( testdataDir , 'suite_test.go' ) ) ) ;
292+ editor . selection = new vscode . Selection ( 25 , 4 , 25 , 4 ) ;
293+
294+ const result = await testAtCursor ( 'debug' ) ( ctx , env . goCtx ) ( [ ] ) ;
295+ assert . strictEqual ( result , true ) ;
296+
297+ assert . strictEqual ( startDebuggingStub . callCount , 1 , 'expected one call to startDebugging' ) ;
298+ const gotConfig = startDebuggingStub . getCall ( 0 ) . args [ 1 ] as vscode . DebugConfiguration ;
299+ gotConfig . program = '' ;
300+ assert . deepStrictEqual < vscode . DebugConfiguration > ( gotConfig , {
301+ name : 'Debug Test' ,
302+ type : 'go' ,
303+ request : 'launch' ,
304+ args : [ '-test.run' , '^TestExampleTestSuite$' , '-testify.m' , '^TestExample$' ] ,
305+ buildFlags : '' ,
306+ env : { } ,
307+ sessionID : undefined ,
308+ mode : 'test' ,
309+ envFile : null ,
310+ program : ''
311+ } ) ;
312+ } ) ;
313+
314+ test ( 'Debug test at cursor in different file than test suite definition' , async ( ) => {
315+ const startDebuggingStub = sinon . stub ( vscode . debug , 'startDebugging' ) . returns ( Promise . resolve ( true ) ) ;
316+
317+ const editor = await vscode . window . showTextDocument (
318+ vscode . Uri . file ( path . join ( testdataDir , 'another_suite_test.go' ) )
319+ ) ;
320+ editor . selection = new vscode . Selection ( 3 , 4 , 3 , 4 ) ;
321+
322+ const result = await testAtCursor ( 'debug' ) ( ctx , env . goCtx ) ( [ ] ) ;
323+ assert . strictEqual ( result , true ) ;
324+
325+ assert . strictEqual ( startDebuggingStub . callCount , 1 , 'expected one call to startDebugging' ) ;
326+ const gotConfig = startDebuggingStub . getCall ( 0 ) . args [ 1 ] as vscode . DebugConfiguration ;
327+ gotConfig . program = '' ;
328+ assert . deepStrictEqual < vscode . DebugConfiguration > ( gotConfig , {
329+ name : 'Debug Test' ,
330+ type : 'go' ,
331+ request : 'launch' ,
332+ args : [ '-test.run' , '^TestExampleTestSuite$' , '-testify.m' , '^TestExampleInAnotherFile$' ] ,
333+ buildFlags : '' ,
334+ env : { } ,
335+ sessionID : undefined ,
336+ mode : 'test' ,
337+ envFile : null ,
338+ program : ''
339+ } ) ;
340+ } ) ;
203341} ) ;
0 commit comments