1
1
import * as assert from 'assert' ;
2
2
import * as fs from 'fs' ;
3
3
import * as path from 'path' ;
4
- import { IMock , Mock } from 'typemoq ' ;
4
+ import * as sinon from 'sinon ' ;
5
5
import { Delve , escapeGoModPath , GoDebugSession ,
6
6
PackageBuildInfo , RemoteSourcesAndPackages } from '../../src/debugAdapter/goDebug' ;
7
7
@@ -11,30 +11,39 @@ suite('Path Manipulation Tests', () => {
11
11
} ) ;
12
12
} ) ;
13
13
14
- suite ( 'GoDebugSession Tests' , ( ) => {
14
+ suite . only ( 'GoDebugSession Tests' , ( ) => {
15
15
const workspaceFolder = '/usr/workspacefolder' ;
16
+ const delve : Delve = { } as Delve ;
17
+ const previousGoPath = process . env . GOPATH ;
18
+ const previousGoRoot = process . env . GOROOT ;
16
19
17
20
let goDebugSession : GoDebugSession ;
18
- let remoteSourcesAndPackagesMock : IMock < RemoteSourcesAndPackages > ;
19
- let fileSystemMock : IMock < typeof fs > ;
20
- let delve : IMock < Delve > ;
21
+ let remoteSourcesAndPackages : RemoteSourcesAndPackages ;
22
+ let fileSystem : typeof fs ;
21
23
setup ( ( ) => {
22
- remoteSourcesAndPackagesMock = Mock . ofType < RemoteSourcesAndPackages > ( ) ;
23
- fileSystemMock = Mock . ofType < typeof fs > ( ) ;
24
- delve = Mock . ofType < Delve > ( ) ;
25
- delve . setup ( ( mock ) => mock . program ) . returns ( ( ) => workspaceFolder ) ;
26
- goDebugSession = new GoDebugSession ( true , false , fileSystemMock . object ) ;
27
- goDebugSession [ 'delve' ] = delve . object ;
28
- goDebugSession [ 'remoteSourcesAndPackages' ] = remoteSourcesAndPackagesMock . object ;
24
+ process . env . GOPATH = '/usr/gopath' ;
25
+ process . env . GOROOT = '/usr/goroot' ;
26
+ remoteSourcesAndPackages = new RemoteSourcesAndPackages ( ) ;
27
+ fileSystem = { existsSync : ( ) => false } as unknown as typeof fs ;
28
+ delve . program = workspaceFolder ;
29
+ delve . isApiV1 = false ;
30
+ goDebugSession = new GoDebugSession ( true , false , fileSystem ) ;
31
+ goDebugSession [ 'delve' ] = delve ;
32
+ goDebugSession [ 'remoteSourcesAndPackages' ] = remoteSourcesAndPackages ;
33
+ } ) ;
34
+
35
+ teardown ( ( ) => {
36
+ process . env . GOPATH = previousGoPath ;
37
+ process . env . GOROOT = previousGoRoot ;
38
+ sinon . restore ( ) ;
29
39
} ) ;
30
40
31
41
test ( 'inferRemotePathFromLocalPath works' , ( ) => {
32
42
const sourceFileMapping = new Map < string , string [ ] > ( ) ;
33
43
sourceFileMapping . set ( 'main.go' , [ '/app/hello-world/main.go' , '/app/main.go' ] ) ;
34
44
sourceFileMapping . set ( 'blah.go' , [ '/app/blah.go' ] ) ;
35
45
36
- remoteSourcesAndPackagesMock . setup ( ( mock ) => mock . remoteSourceFilesNameGrouping )
37
- . returns ( ( ) => ( sourceFileMapping ) ) ;
46
+ remoteSourcesAndPackages . remoteSourceFilesNameGrouping = sourceFileMapping ;
38
47
39
48
const inferredPath = goDebugSession [ 'inferRemotePathFromLocalPath' ] (
40
49
'C:\\Users\\Documents\\src\\hello-world\\main.go' ) ;
@@ -55,13 +64,11 @@ suite('GoDebugSession Tests', () => {
55
64
Files :
[ 'remote/pkg/mod/!foo!bar/[email protected] /test.go' ]
56
65
} ;
57
66
58
- process . env . GOPATH = '/usr/go' ;
59
67
const localPath = path . join ( workspaceFolder , 'hello-world/morestrings/morestrings.go' ) ;
60
- fileSystemMock . setup ( ( mock ) => mock . existsSync ( localPath ) )
61
- . returns ( ( ) => true ) ;
68
+ const existsSyncStub = sinon . stub ( fileSystem , 'existsSync' ) ;
69
+ existsSyncStub . withArgs ( localPath ) . returns ( true ) ;
62
70
63
- remoteSourcesAndPackagesMock . setup ( ( mock ) => mock . remotePackagesBuildInfo )
64
- . returns ( ( ) => [ helloPackage , testPackage ] ) ;
71
+ remoteSourcesAndPackages . remotePackagesBuildInfo = [ helloPackage , testPackage ] ;
65
72
66
73
goDebugSession [ 'localPathSeparator' ] = '/' ;
67
74
const inferredLocalPath = goDebugSession [ 'inferLocalPathFromRemoteGoPackage' ] ( remotePath ) ;
@@ -82,13 +89,11 @@ suite('GoDebugSession Tests', () => {
82
89
Files :
[ 'remote/pkg/mod/!foo!bar/[email protected] /test.go' ]
83
90
} ;
84
91
85
- process . env . GOPATH = '/usr/go' ;
86
92
const localPath = path . join ( process . env . GOPATH , 'pkg/mod/!foo!bar/[email protected] /test.go' ) ;
87
- fileSystemMock . setup ( ( mock ) => mock . existsSync ( localPath ) )
88
- . returns ( ( ) => true ) ;
93
+ const existsSyncStub = sinon . stub ( fileSystem , 'existsSync' ) ;
94
+ existsSyncStub . withArgs ( localPath ) . returns ( true ) ;
89
95
90
- remoteSourcesAndPackagesMock . setup ( ( mock ) => mock . remotePackagesBuildInfo )
91
- . returns ( ( ) => [ helloPackage , testPackage ] ) ;
96
+ remoteSourcesAndPackages . remotePackagesBuildInfo = [ helloPackage , testPackage ] ;
92
97
93
98
goDebugSession [ 'localPathSeparator' ] = '/' ;
94
99
const inferredLocalPath = goDebugSession [ 'inferLocalPathFromRemoteGoPackage' ] ( remotePath ) ;
@@ -109,21 +114,19 @@ suite('GoDebugSession Tests', () => {
109
114
Files :
[ '!foo!bar/[email protected] /test.go' ]
110
115
} ;
111
116
112
- process . env . GOPATH = '/usr/go' ;
113
117
const localPath = path . join ( process . env . GOPATH , 'pkg/mod/!foo!bar/[email protected] /test.go' ) ;
114
- fileSystemMock . setup ( ( mock ) => mock . existsSync ( localPath ) )
115
- . returns ( ( ) => true ) ;
118
+ const existsSyncStub = sinon . stub ( fileSystem , 'existsSync' ) ;
119
+ existsSyncStub . withArgs ( localPath ) . returns ( true ) ;
116
120
117
- remoteSourcesAndPackagesMock . setup ( ( mock ) => mock . remotePackagesBuildInfo )
118
- . returns ( ( ) => [ helloPackage , testPackage ] ) ;
121
+ remoteSourcesAndPackages . remotePackagesBuildInfo = [ helloPackage , testPackage ] ;
119
122
120
123
goDebugSession [ 'localPathSeparator' ] = '/' ;
121
124
const inferredLocalPath = goDebugSession [ 'inferLocalPathFromRemoteGoPackage' ] ( remotePath ) ;
122
125
assert . strictEqual ( inferredLocalPath , localPath ) ;
123
126
} ) ;
124
127
125
128
test ( 'inferLocalPathFromRemoteGoPackage works for package in GOPATH/src' , ( ) => {
126
- const remotePath = 'remote/gopath/src/foobar/[email protected] /test.go' ;
129
+ const remotePath = 'remote/gopath/src/foobar/[email protected] -abcde-34 /test.go' ;
127
130
const helloPackage : PackageBuildInfo = {
128
131
ImportPath : 'hello-world' ,
129
132
DirectoryPath : '/src/hello-world' ,
@@ -132,17 +135,15 @@ suite('GoDebugSession Tests', () => {
132
135
133
136
const testPackage : PackageBuildInfo = {
134
137
ImportPath : 'foobar/test' ,
135
- DirectoryPath :
'remote/gopath/src/foobar/[email protected] ' ,
136
- Files :
[ 'remote/gopath/src/foobar/[email protected] /test.go' ]
138
+ DirectoryPath :
'remote/gopath/src/foobar/[email protected] -abcde-34 ' ,
139
+ Files :
[ 'remote/gopath/src/foobar/[email protected] -abcde-34 /test.go' ]
137
140
} ;
138
141
139
- process . env . GOPATH = '/usr/go' ;
140
- const localPath = path . join ( process . env . GOPATH , 'src' , 'foobar/[email protected] /test.go' ) ;
141
- fileSystemMock . setup ( ( mock ) => mock . existsSync ( localPath ) )
142
- . returns ( ( ) => true ) ;
142
+ const localPath = path . join ( process . env . GOPATH , 'src' , 'foobar/[email protected] /test.go' ) ;
143
+ const existsSyncStub = sinon . stub ( fileSystem , 'existsSync' ) ;
144
+ existsSyncStub . withArgs ( localPath ) . returns ( true ) ;
143
145
144
- remoteSourcesAndPackagesMock . setup ( ( mock ) => mock . remotePackagesBuildInfo )
145
- . returns ( ( ) => [ helloPackage , testPackage ] ) ;
146
+ remoteSourcesAndPackages . remotePackagesBuildInfo = [ helloPackage , testPackage ] ;
146
147
147
148
goDebugSession [ 'localPathSeparator' ] = '/' ;
148
149
const inferredLocalPath = goDebugSession [ 'inferLocalPathFromRemoteGoPackage' ] ( remotePath ) ;
@@ -163,13 +164,11 @@ suite('GoDebugSession Tests', () => {
163
164
Files :
[ 'foobar/[email protected] /test.go' ]
164
165
} ;
165
166
166
- process . env . GOPATH = '/usr/go' ;
167
167
const localPath = path . join ( process . env . GOPATH , 'src' , 'foobar/[email protected] /test.go' ) ;
168
- fileSystemMock . setup ( ( mock ) => mock . existsSync ( localPath ) )
169
- . returns ( ( ) => true ) ;
168
+ const existsSyncStub = sinon . stub ( fileSystem , 'existsSync' ) ;
169
+ existsSyncStub . withArgs ( localPath ) . returns ( true ) ;
170
170
171
- remoteSourcesAndPackagesMock . setup ( ( mock ) => mock . remotePackagesBuildInfo )
172
- . returns ( ( ) => [ helloPackage , testPackage ] ) ;
171
+ remoteSourcesAndPackages . remotePackagesBuildInfo = [ helloPackage , testPackage ] ;
173
172
174
173
goDebugSession [ 'localPathSeparator' ] = '/' ;
175
174
const inferredLocalPath = goDebugSession [ 'inferLocalPathFromRemoteGoPackage' ] ( remotePath ) ;
@@ -190,13 +189,11 @@ suite('GoDebugSession Tests', () => {
190
189
Files :
[ 'remote/goroot/src/foobar/[email protected] /test.go' ]
191
190
} ;
192
191
193
- process . env . GOROOT = '/usr/go' ;
194
192
const localPath = path . join ( process . env . GOROOT , 'src' , 'foobar/[email protected] /test.go' ) ;
195
- fileSystemMock . setup ( ( mock ) => mock . existsSync ( localPath ) )
196
- . returns ( ( ) => true ) ;
193
+ const existsSyncStub = sinon . stub ( fileSystem , 'existsSync' ) ;
194
+ existsSyncStub . withArgs ( localPath ) . returns ( true ) ;
197
195
198
- remoteSourcesAndPackagesMock . setup ( ( mock ) => mock . remotePackagesBuildInfo )
199
- . returns ( ( ) => [ helloPackage , testPackage ] ) ;
196
+ remoteSourcesAndPackages . remotePackagesBuildInfo = [ helloPackage , testPackage ] ;
200
197
201
198
goDebugSession [ 'localPathSeparator' ] = '/' ;
202
199
const inferredLocalPath = goDebugSession [ 'inferLocalPathFromRemoteGoPackage' ] ( remotePath ) ;
@@ -217,13 +214,11 @@ suite('GoDebugSession Tests', () => {
217
214
Files :
[ 'foobar/[email protected] /test.go' ]
218
215
} ;
219
216
220
- process . env . GOROOT = '/usr/go' ;
221
217
const localPath = path . join ( process . env . GOROOT , 'src' , 'foobar/[email protected] /test.go' ) ;
222
- fileSystemMock . setup ( ( mock ) => mock . existsSync ( localPath ) )
223
- . returns ( ( ) => true ) ;
218
+ const existsSyncStub = sinon . stub ( fileSystem , 'existsSync' ) ;
219
+ existsSyncStub . withArgs ( localPath ) . returns ( true ) ;
224
220
225
- remoteSourcesAndPackagesMock . setup ( ( mock ) => mock . remotePackagesBuildInfo )
226
- . returns ( ( ) => [ helloPackage , testPackage ] ) ;
221
+ remoteSourcesAndPackages . remotePackagesBuildInfo = [ helloPackage , testPackage ] ;
227
222
228
223
goDebugSession [ 'localPathSeparator' ] = '/' ;
229
224
const inferredLocalPath = goDebugSession [ 'inferLocalPathFromRemoteGoPackage' ] ( remotePath ) ;
@@ -244,20 +239,24 @@ suite('RemoteSourcesAndPackages Tests', () => {
244
239
} ;
245
240
const sources = [ 'src/hello-world/hello.go' , 'src/hello-world/world.go' , 'src/test/test.go' ] ;
246
241
let remoteSourcesAndPackages : RemoteSourcesAndPackages ;
247
- let delve : IMock < Delve > ;
242
+ let delve : Delve ;
248
243
setup ( ( ) => {
249
- delve = Mock . ofType < Delve > ( ) ;
250
- delve . setup ( ( mock ) => mock . isApiV1 ) . returns ( ( ) => false ) ;
244
+ delve = { callPromise : ( ) => ( { } ) , isApiV1 : false } as unknown as Delve ;
251
245
remoteSourcesAndPackages = new RemoteSourcesAndPackages ( ) ;
252
246
} ) ;
253
247
248
+ teardown ( ( ) => {
249
+ sinon . restore ( ) ;
250
+ } ) ;
251
+
254
252
test ( 'initializeRemotePackagesAndSources retrieves remote packages and sources' , async ( ) => {
255
- delve . setup ( ( mock ) => mock . callPromise ( 'ListPackagesBuildInfo' , [ { IncludeFiles : true } ] ) )
256
- . returns ( ( ) => Promise . resolve ( { List : [ helloPackage , testPackage ] } ) ) ;
257
- delve . setup ( ( mock ) => mock . callPromise ( 'ListSources' , [ { } ] ) )
258
- . returns ( ( ) => Promise . resolve ( { Sources : sources } ) ) ;
253
+ const stub = sinon . stub ( delve , 'callPromise' ) ;
254
+ stub . withArgs ( 'ListPackagesBuildInfo' , [ { IncludeFiles : true } ] )
255
+ . returns ( Promise . resolve ( { List : [ helloPackage , testPackage ] } ) ) ;
256
+ stub . withArgs ( 'ListSources' , [ { } ] )
257
+ . returns ( Promise . resolve ( { Sources : sources } ) ) ;
259
258
260
- await remoteSourcesAndPackages . initializeRemotePackagesAndSources ( delve . object ) ;
259
+ await remoteSourcesAndPackages . initializeRemotePackagesAndSources ( delve ) ;
261
260
assert . deepEqual ( remoteSourcesAndPackages . remoteSourceFiles , sources ) ;
262
261
assert . deepEqual ( remoteSourcesAndPackages . remotePackagesBuildInfo , [ helloPackage , testPackage ] ) ;
263
262
} ) ;
0 commit comments