Skip to content

Commit 1084907

Browse files
Nikola HristovNikola Hristov
authored andcommitted
2 parents dc8c9de + 5bb5c48 commit 1084907

File tree

2 files changed

+115
-1
lines changed

2 files changed

+115
-1
lines changed

Source/extension/debugger/configuration/launch.json/launchJsonReader.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ export function getConfigurationsFromSettings(workspace: WorkspaceFolder): Debug
5151
!Array.isArray(codeWorkspaceConfig.configurations) ||
5252
codeWorkspaceConfig.configurations.length === 0
5353
) {
54-
throw Error('No configurations found in launch.json or settings.json');
54+
traceLog('No configurations found in settings.json or launch.json.');
55+
return [];
5556
}
5657
traceLog('Using configuration in workspace settings.json.');
5758
return codeWorkspaceConfig.configurations;
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
'use strict';
2+
3+
import * as assert from 'assert';
4+
import * as sinon from 'sinon';
5+
import * as typemoq from 'typemoq';
6+
import * as fs from 'fs-extra';
7+
import { WorkspaceFolder, Uri, WorkspaceConfiguration } from 'vscode';
8+
import {
9+
getConfigurationsForWorkspace,
10+
getConfigurationsFromSettings,
11+
} from '../../../../extension/debugger/configuration/launch.json/launchJsonReader';
12+
import * as vscodeapi from '../../../../extension/common/vscodeapi';
13+
14+
suite('Debugging - launchJsonReader', () => {
15+
let sandbox: sinon.SinonSandbox;
16+
17+
setup(() => {
18+
sandbox = sinon.createSandbox();
19+
});
20+
21+
teardown(() => {
22+
sandbox.restore();
23+
});
24+
25+
suite('getConfigurationsForWorkspace', () => {
26+
test('Should return configurations from launch.json if it exists', async () => {
27+
const workspace = typemoq.Mock.ofType<WorkspaceFolder>();
28+
workspace.setup((w) => w.uri).returns(() => Uri.file('/path/to/workspace'));
29+
30+
const launchJsonContent = `{
31+
"version": "0.2.0",
32+
"configurations": [
33+
{
34+
"name": "Launch Program",
35+
"type": "python",
36+
"request": "launch",
37+
"program": "${workspace.object.uri}/app.py"
38+
}
39+
]
40+
}`;
41+
42+
sandbox.stub(fs, 'pathExists').resolves(true);
43+
sandbox.stub(fs, 'readFile').resolves(launchJsonContent);
44+
45+
const configurations = await getConfigurationsForWorkspace(workspace.object);
46+
assert.strictEqual(configurations.length, 1);
47+
assert.strictEqual(configurations[0].name, 'Launch Program');
48+
});
49+
50+
test('Should return configurations from settings.json if launch.json does not exist', async () => {
51+
const workspace = typemoq.Mock.ofType<WorkspaceFolder>();
52+
workspace.setup((w) => w.uri).returns(() => Uri.file('/path/to/workspace'));
53+
54+
const mockConfig = typemoq.Mock.ofType<WorkspaceConfiguration>();
55+
mockConfig
56+
.setup((c) => c.configurations)
57+
.returns(() => [
58+
{
59+
name: 'Launch Program 2',
60+
type: 'python',
61+
request: 'launch',
62+
program: '${workspaceFolder}/app.py',
63+
},
64+
]);
65+
66+
sandbox.stub(fs, 'pathExists').resolves(false);
67+
sandbox.stub(vscodeapi, 'getConfiguration').returns(mockConfig.object);
68+
69+
const configurations = await getConfigurationsForWorkspace(workspace.object);
70+
assert.strictEqual(configurations.length, 1);
71+
assert.strictEqual(configurations[0].name, 'Launch Program 2');
72+
});
73+
});
74+
75+
suite('getConfigurationsFromSettings', () => {
76+
test('Should return configurations from settings.json', () => {
77+
const workspace = typemoq.Mock.ofType<WorkspaceFolder>();
78+
workspace.setup((w) => w.uri).returns(() => Uri.file('/path/to/workspace'));
79+
80+
const mockConfig = typemoq.Mock.ofType<WorkspaceConfiguration>();
81+
mockConfig
82+
.setup((c) => c.configurations)
83+
.returns(() => [
84+
{
85+
name: 'Launch Program 3',
86+
type: 'python',
87+
request: 'launch',
88+
program: '${workspaceFolder}/app.py',
89+
},
90+
]);
91+
92+
sandbox.stub(vscodeapi, 'getConfiguration').returns(mockConfig.object);
93+
94+
const configurations = getConfigurationsFromSettings(workspace.object);
95+
assert.strictEqual(configurations.length, 1);
96+
assert.strictEqual(configurations[0].name, 'Launch Program 3');
97+
});
98+
99+
test('Should return empty array if no configurations in settings.json', () => {
100+
const workspace = typemoq.Mock.ofType<WorkspaceFolder>();
101+
workspace.setup((w) => w.uri).returns(() => Uri.file('/path/to/workspace'));
102+
103+
const mockConfig = typemoq.Mock.ofType<WorkspaceConfiguration>();
104+
mockConfig.setup((c) => c.get('configurations')).returns(() => []);
105+
mockConfig.setup((c) => c.configurations).returns(() => []);
106+
107+
sandbox.stub(vscodeapi, 'getConfiguration').returns(mockConfig.object);
108+
109+
const configurations = getConfigurationsFromSettings(workspace.object);
110+
assert.strictEqual(configurations.length, 0);
111+
});
112+
});
113+
});

0 commit comments

Comments
 (0)