Skip to content

Commit 13adcaf

Browse files
committed
add test
1 parent 4cbd211 commit 13adcaf

File tree

4 files changed

+100
-11
lines changed

4 files changed

+100
-11
lines changed

.vscode/launch.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,15 @@
3131
"args": [
3232
"./out/test/**/*.unit.test.js",
3333
"--extensionDevelopmentPath=${workspaceFolder}",
34-
"--extensionTestsPath=${workspaceFolder}/out/test/unittest/index"
34+
"--extensionTestsPath=${workspaceFolder}/out/test/unittest/index",
35+
//"--grep",
36+
//"this test suite"
3537
],
3638
"outFiles": [
3739
"${workspaceFolder}/out/**/*.js",
3840
],
39-
"preLaunchTask": "tasks: watch-tests"
41+
"preLaunchTask": "tasks: watch-tests",
42+
"timeout": 600000 // Increase timeout to 10 minutes (600000 ms)
4043
},
4144
]
4245
}

src/extension/noConfigDebugInit.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as fs from 'fs';
22
import * as path from 'path';
33
import { IExtensionContext } from './common/types';
4-
import { DebugSessionOptions, debug, RelativePattern, workspace } from 'vscode';
4+
import { DebugSessionOptions, debug, RelativePattern } from 'vscode';
5+
import { createFileSystemWatcher } from './utils';
56

67
/**
78
* Registers the configuration-less debugging setup for the extension.
@@ -32,19 +33,19 @@ export async function registerConfiglessDebug(context: IExtensionContext): Promi
3233

3334
// create file system watcher for the debuggerAdapterEndpointFolder for when the communication port is written
3435
context.subscriptions.push(
35-
workspace.createFileSystemWatcher(new RelativePattern(debugAdapterEndpointDir, '**/*')).onDidCreate((uri) => {
36-
console.log(`File created: ${uri.fsPath}`);
36+
createFileSystemWatcher(new RelativePattern(debugAdapterEndpointDir, '**/*')).onDidCreate((uri) => {
37+
// console.log(`File created: ${uri.fsPath}`);
3738
const filePath = uri.fsPath;
3839
fs.readFile(filePath, 'utf8', (err, data) => {
3940
if (err) {
40-
console.error(`Error reading file: ${err}`);
41+
// console.error(`Error reading file: ${err}`);
4142
return;
4243
}
4344
try {
4445
// parse the client port
4546
const jsonData = JSON.parse(data);
4647
const clientPort = jsonData.client?.port;
47-
console.log(`Client port: ${clientPort}`);
48+
//console.log(`Client port: ${clientPort}`);
4849

4950
const options: DebugSessionOptions = {
5051
noDebug: false,
@@ -66,17 +67,18 @@ export async function registerConfiglessDebug(context: IExtensionContext): Promi
6667
.then(
6768
(started) => {
6869
if (started) {
69-
console.log('Debug session started successfully');
70+
//console.log('Debug session started successfully');
7071
} else {
71-
console.error('Failed to start debug session');
72+
//console.error('Failed to start debug session');
7273
}
7374
},
7475
(error) => {
75-
console.error(`Error starting debug session: ${error}`);
76+
//console.error(`Error starting debug session: ${error}`);
77+
error;
7678
},
7779
);
7880
} catch (parseErr) {
79-
console.error(`Error parsing JSON: ${parseErr}`);
81+
//console.error(`Error parsing JSON: ${parseErr}`);
8082
}
8183
});
8284
JSON.parse;

src/extension/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { workspace } from 'vscode';
2+
3+
export function createFileSystemWatcher(args: any) {
4+
return workspace.createFileSystemWatcher(args);
5+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import * as path from 'path';
2+
import { IExtensionContext } from '../../extension/common/types';
3+
import { registerConfiglessDebug } from '../../extension/noConfigDebugInit';
4+
import * as TypeMoq from 'typemoq';
5+
import * as sinon from 'sinon';
6+
import { Uri } from 'vscode';
7+
import * as utils from '../../extension/utils';
8+
import { assert } from 'console';
9+
import * as fs from 'fs';
10+
11+
suite('registerConfiglessDebug', function () {
12+
this.timeout(100000); // Increase timeout to 10 seconds
13+
let replaceStub: sinon.SinonStub;
14+
let appendStub: sinon.SinonStub;
15+
let context: TypeMoq.IMock<IExtensionContext>;
16+
let createFileSystemWatcher: sinon.SinonStub;
17+
18+
setup(() => {
19+
context = TypeMoq.Mock.ofType<IExtensionContext>();
20+
21+
context.setup((c) => c.storageUri).returns(() => Uri.parse('a'));
22+
context.setup((c) => (c as any).extensionPath).returns(() => 'b');
23+
context.setup((c) => c.subscriptions).returns(() => []);
24+
25+
createFileSystemWatcher = sinon.stub(utils, 'createFileSystemWatcher');
26+
createFileSystemWatcher.callsFake(() => {
27+
return {
28+
onDidCreate: (cb: (arg0: Uri) => void) => {
29+
cb(Uri.parse('a'));
30+
},
31+
};
32+
});
33+
sinon.stub(fs, 'readFile').callsFake(
34+
(TypeMoq.It.isAny(),
35+
TypeMoq.It.isAny(),
36+
(err, data) => {
37+
console.log(err, data);
38+
}),
39+
);
40+
});
41+
teardown(() => {
42+
sinon.restore();
43+
});
44+
45+
test('should add environment variables for DEBUGPY_ADAPTER_ENDPOINTS, BUNDLED_DEBUGPY_PATH, and PATH', async () => {
46+
const debugAdapterEndpointDir = path.join(context.object.extensionPath, 'noConfigDebugAdapterEndpoints');
47+
const debuggerAdapterEndpointPath = path.join(debugAdapterEndpointDir, 'debuggerAdapterEndpoint.txt');
48+
const noConfigScriptsDir = path.join(context.object.extensionPath, 'bundled/scripts/noConfigScripts');
49+
const bundledDebugPath = path.join(context.object.extensionPath, 'bundled/libs/debugpy');
50+
51+
const environmentVariableCollectionMock = TypeMoq.Mock.ofType<any>();
52+
replaceStub = sinon.stub();
53+
appendStub = sinon.stub();
54+
environmentVariableCollectionMock
55+
.setup((x) => x.replace(TypeMoq.It.isAny(), TypeMoq.It.isAny()))
56+
.callback((key, value) => {
57+
if (key === 'DEBUGPY_ADAPTER_ENDPOINTS') {
58+
assert(value === debuggerAdapterEndpointPath);
59+
} else if (key === 'BUNDLED_DEBUGPY_PATH') {
60+
assert(value === bundledDebugPath);
61+
}
62+
})
63+
.returns(replaceStub);
64+
environmentVariableCollectionMock
65+
.setup((x) => x.append(TypeMoq.It.isAny(), TypeMoq.It.isAny()))
66+
.callback((key, value) => {
67+
if (key === 'PATH') {
68+
assert(value === `:${noConfigScriptsDir}`);
69+
}
70+
})
71+
.returns(appendStub);
72+
73+
context.setup((c) => c.environmentVariableCollection).returns(() => environmentVariableCollectionMock.object);
74+
75+
await registerConfiglessDebug(context.object);
76+
console.log('All done!');
77+
sinon.assert.calledTwice(replaceStub);
78+
});
79+
});

0 commit comments

Comments
 (0)