Skip to content

Commit 614a301

Browse files
committed
Fix tests
1 parent b06821f commit 614a301

File tree

3 files changed

+64
-66
lines changed

3 files changed

+64
-66
lines changed

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
"moment": "^2.24.0",
4747
"semver": "^7.3.2",
4848
"tree-kill": "^1.2.2",
49-
"typemoq": "^2.1.0",
5049
"vscode-debugadapter": "^1.40.0",
5150
"vscode-debugprotocol": "^1.40.0",
5251
"vscode-extension-telemetry": "^0.1.2",

src/debugAdapter/goDebug.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1020,7 +1020,7 @@ export class GoDebugSession extends LoggingDebugSession {
10201020
return localGoPathImportPath;
10211021
}
10221022

1023-
// Scenario 4: The package is inside GOROOT.
1023+
// Scenario 3: The package is inside GOROOT.
10241024
return this.inferLocalPathInGoRootFromRemoteGoPackage(pathToConvertWithLocalSeparator, relativeRemotePath);
10251025
}
10261026

@@ -1032,7 +1032,7 @@ export class GoDebugSession extends LoggingDebugSession {
10321032
protected inferLocalPathInGoRootFromRemoteGoPackage(
10331033
remotePathWithLocalSeparator: string, relativeRemotePath: string): string|undefined {
10341034
const srcIndex = remotePathWithLocalSeparator.indexOf(`${this.localPathSeparator}src${this.localPathSeparator}`);
1035-
const goroot = process.env['GOROOT'];
1035+
const goroot = process.env['GOROOT'] || '';
10361036
const localGoRootImportPath = path.join(
10371037
goroot,
10381038
srcIndex >= 0
@@ -1053,7 +1053,7 @@ export class GoDebugSession extends LoggingDebugSession {
10531053
*/
10541054
protected inferLocalPathInGoPathFromRemoteGoPackage(
10551055
remotePathWithLocalSeparator: string, relativeRemotePath: string): string|undefined {
1056-
// Scenario 1: The package is inside GOPATH.
1056+
// Scenario 1: The package is inside $GOPATH/pkg/mod.
10571057
const gopath = (process.env['GOPATH'] || '').split(path.delimiter)[0];
10581058

10591059
const indexGoModCache = remotePathWithLocalSeparator.indexOf(
@@ -1068,7 +1068,7 @@ export class GoDebugSession extends LoggingDebugSession {
10681068
return localGoPathImportPath;
10691069
}
10701070

1071-
// Scenario 2: The file is in a package in GOPATH/src.
1071+
// Scenario 2: The file is in a package in $GOPATH/src.
10721072
const localGoPathSrcPath = path.join(
10731073
gopath, 'src',
10741074
relativeRemotePath.split(this.remotePathSeparator).join(this.localPathSeparator));

test/integration/goDebug.test.ts

+60-61
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as assert from 'assert';
22
import * as fs from 'fs';
33
import * as path from 'path';
4-
import {IMock, Mock} from 'typemoq';
4+
import * as sinon from 'sinon';
55
import { Delve, escapeGoModPath, GoDebugSession,
66
PackageBuildInfo, RemoteSourcesAndPackages } from '../../src/debugAdapter/goDebug';
77

@@ -11,30 +11,39 @@ suite('Path Manipulation Tests', () => {
1111
});
1212
});
1313

14-
suite('GoDebugSession Tests', () => {
14+
suite.only('GoDebugSession Tests', () => {
1515
const workspaceFolder = '/usr/workspacefolder';
16+
const delve: Delve = {} as Delve;
17+
const previousGoPath = process.env.GOPATH;
18+
const previousGoRoot = process.env.GOROOT;
1619

1720
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;
2123
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();
2939
});
3040

3141
test('inferRemotePathFromLocalPath works', () => {
3242
const sourceFileMapping = new Map<string, string[]>();
3343
sourceFileMapping.set('main.go', ['/app/hello-world/main.go', '/app/main.go']);
3444
sourceFileMapping.set('blah.go', ['/app/blah.go']);
3545

36-
remoteSourcesAndPackagesMock.setup((mock) => mock.remoteSourceFilesNameGrouping)
37-
.returns(() => (sourceFileMapping));
46+
remoteSourcesAndPackages.remoteSourceFilesNameGrouping = sourceFileMapping;
3847

3948
const inferredPath = goDebugSession['inferRemotePathFromLocalPath'](
4049
'C:\\Users\\Documents\\src\\hello-world\\main.go');
@@ -55,13 +64,11 @@ suite('GoDebugSession Tests', () => {
5564
Files: ['remote/pkg/mod/!foo!bar/[email protected]/test.go']
5665
};
5766

58-
process.env.GOPATH = '/usr/go';
5967
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);
6270

63-
remoteSourcesAndPackagesMock.setup((mock) => mock.remotePackagesBuildInfo)
64-
.returns(() => [helloPackage, testPackage]);
71+
remoteSourcesAndPackages.remotePackagesBuildInfo = [helloPackage, testPackage];
6572

6673
goDebugSession['localPathSeparator'] = '/';
6774
const inferredLocalPath = goDebugSession['inferLocalPathFromRemoteGoPackage'](remotePath);
@@ -82,13 +89,11 @@ suite('GoDebugSession Tests', () => {
8289
Files: ['remote/pkg/mod/!foo!bar/[email protected]/test.go']
8390
};
8491

85-
process.env.GOPATH = '/usr/go';
8692
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);
8995

90-
remoteSourcesAndPackagesMock.setup((mock) => mock.remotePackagesBuildInfo)
91-
.returns(() => [helloPackage, testPackage]);
96+
remoteSourcesAndPackages.remotePackagesBuildInfo = [helloPackage, testPackage];
9297

9398
goDebugSession['localPathSeparator'] = '/';
9499
const inferredLocalPath = goDebugSession['inferLocalPathFromRemoteGoPackage'](remotePath);
@@ -109,21 +114,19 @@ suite('GoDebugSession Tests', () => {
109114
Files: ['!foo!bar/[email protected]/test.go']
110115
};
111116

112-
process.env.GOPATH = '/usr/go';
113117
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);
116120

117-
remoteSourcesAndPackagesMock.setup((mock) => mock.remotePackagesBuildInfo)
118-
.returns(() => [helloPackage, testPackage]);
121+
remoteSourcesAndPackages.remotePackagesBuildInfo = [helloPackage, testPackage];
119122

120123
goDebugSession['localPathSeparator'] = '/';
121124
const inferredLocalPath = goDebugSession['inferLocalPathFromRemoteGoPackage'](remotePath);
122125
assert.strictEqual(inferredLocalPath, localPath);
123126
});
124127

125128
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';
127130
const helloPackage: PackageBuildInfo = {
128131
ImportPath: 'hello-world',
129132
DirectoryPath: '/src/hello-world',
@@ -132,17 +135,15 @@ suite('GoDebugSession Tests', () => {
132135

133136
const testPackage: PackageBuildInfo = {
134137
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']
137140
};
138141

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);
143145

144-
remoteSourcesAndPackagesMock.setup((mock) => mock.remotePackagesBuildInfo)
145-
.returns(() => [helloPackage, testPackage]);
146+
remoteSourcesAndPackages.remotePackagesBuildInfo = [helloPackage, testPackage];
146147

147148
goDebugSession['localPathSeparator'] = '/';
148149
const inferredLocalPath = goDebugSession['inferLocalPathFromRemoteGoPackage'](remotePath);
@@ -163,13 +164,11 @@ suite('GoDebugSession Tests', () => {
163164
Files: ['foobar/[email protected]/test.go']
164165
};
165166

166-
process.env.GOPATH = '/usr/go';
167167
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);
170170

171-
remoteSourcesAndPackagesMock.setup((mock) => mock.remotePackagesBuildInfo)
172-
.returns(() => [helloPackage, testPackage]);
171+
remoteSourcesAndPackages.remotePackagesBuildInfo = [helloPackage, testPackage];
173172

174173
goDebugSession['localPathSeparator'] = '/';
175174
const inferredLocalPath = goDebugSession['inferLocalPathFromRemoteGoPackage'](remotePath);
@@ -190,13 +189,11 @@ suite('GoDebugSession Tests', () => {
190189
Files: ['remote/goroot/src/foobar/[email protected]/test.go']
191190
};
192191

193-
process.env.GOROOT = '/usr/go';
194192
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);
197195

198-
remoteSourcesAndPackagesMock.setup((mock) => mock.remotePackagesBuildInfo)
199-
.returns(() => [helloPackage, testPackage]);
196+
remoteSourcesAndPackages.remotePackagesBuildInfo = [helloPackage, testPackage];
200197

201198
goDebugSession['localPathSeparator'] = '/';
202199
const inferredLocalPath = goDebugSession['inferLocalPathFromRemoteGoPackage'](remotePath);
@@ -217,13 +214,11 @@ suite('GoDebugSession Tests', () => {
217214
Files: ['foobar/[email protected]/test.go']
218215
};
219216

220-
process.env.GOROOT = '/usr/go';
221217
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);
224220

225-
remoteSourcesAndPackagesMock.setup((mock) => mock.remotePackagesBuildInfo)
226-
.returns(() => [helloPackage, testPackage]);
221+
remoteSourcesAndPackages.remotePackagesBuildInfo = [helloPackage, testPackage];
227222

228223
goDebugSession['localPathSeparator'] = '/';
229224
const inferredLocalPath = goDebugSession['inferLocalPathFromRemoteGoPackage'](remotePath);
@@ -244,20 +239,24 @@ suite('RemoteSourcesAndPackages Tests', () => {
244239
};
245240
const sources = ['src/hello-world/hello.go', 'src/hello-world/world.go', 'src/test/test.go'];
246241
let remoteSourcesAndPackages: RemoteSourcesAndPackages;
247-
let delve: IMock<Delve>;
242+
let delve: Delve;
248243
setup(() => {
249-
delve = Mock.ofType<Delve>();
250-
delve.setup((mock) => mock.isApiV1).returns(() => false);
244+
delve = {callPromise: () => ({}), isApiV1: false} as unknown as Delve;
251245
remoteSourcesAndPackages = new RemoteSourcesAndPackages();
252246
});
253247

248+
teardown(() => {
249+
sinon.restore();
250+
});
251+
254252
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}));
259258

260-
await remoteSourcesAndPackages.initializeRemotePackagesAndSources(delve.object);
259+
await remoteSourcesAndPackages.initializeRemotePackagesAndSources(delve);
261260
assert.deepEqual(remoteSourcesAndPackages.remoteSourceFiles, sources);
262261
assert.deepEqual(remoteSourcesAndPackages.remotePackagesBuildInfo, [helloPackage, testPackage]);
263262
});

0 commit comments

Comments
 (0)