Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

21249 read launch config in workspace file #21426

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@ import * as path from 'path';
import * as fs from 'fs-extra';
import { parse } from 'jsonc-parser';
import { DebugConfiguration, Uri, WorkspaceFolder } from 'vscode';
import { getWorkspaceFolder } from '../../../../common/vscodeApis/workspaceApis';
import { getConfiguration, getWorkspaceFolder } from '../../../../common/vscodeApis/workspaceApis';
import { traceLog } from '../../../../logging';

export async function getConfigurationsForWorkspace(workspace: WorkspaceFolder): Promise<DebugConfiguration[]> {
const filename = path.join(workspace.uri.fsPath, '.vscode', 'launch.json');

if (!(await fs.pathExists(filename))) {
return [];
// Check launch config in the workspace file
const codeWorkspaceConfig = getConfiguration('launch');
if (!codeWorkspaceConfig.configurations || !Array.isArray(codeWorkspaceConfig.configurations)) {
return [];
}
traceLog(`Using launch configuration in workspace folder.`);
return codeWorkspaceConfig.configurations;
}

const text = await fs.readFile(filename, 'utf-8');
Expand All @@ -23,6 +29,7 @@ export async function getConfigurationsForWorkspace(workspace: WorkspaceFolder):
throw Error('Missing field in launch.json: version');
}
// We do not bother ensuring each item is a DebugConfiguration...
traceLog(`Using launch configuration in launch.json file.`);
return parsed.configurations;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

'use strict';

import * as sinon from 'sinon';
import * as fs from 'fs-extra';
import * as path from 'path';
import { Uri } from 'vscode';
import { assert } from 'chai';
import { getConfigurationsForWorkspace } from '../../../../../client/debugger/extension/configuration/launch.json/launchJsonReader';
import * as vscodeApis from '../../../../../client/common/vscodeApis/workspaceApis';

suite('Launch Json Reader', () => {
let pathExistsStub: sinon.SinonStub;
let readFileStub: sinon.SinonStub;
let getConfigurationStub: sinon.SinonStub;
const workspacePath = 'path/to/workspace';
const workspaceFolder = {
name: 'workspace',
uri: Uri.file(workspacePath),
index: 0,
};

setup(() => {
pathExistsStub = sinon.stub(fs, 'pathExists');
readFileStub = sinon.stub(fs, 'readFile');
getConfigurationStub = sinon.stub(vscodeApis, 'getConfiguration');
});

teardown(() => {
sinon.restore();
});

test('Return the config in the launch.json file', async () => {
const launchPath = path.join(workspaceFolder.uri.fsPath, '.vscode', 'launch.json');
pathExistsStub.withArgs(launchPath).resolves(true);
const launchJson = `{
"version": "0.1.0",
"configurations": [
{
"name": "Python: Launch.json",
"type": "python",
"request": "launch",
"purpose": ["debug-test"],
},
]
}`;
readFileStub.withArgs(launchPath, 'utf-8').returns(launchJson);

const config = await getConfigurationsForWorkspace(workspaceFolder);

assert.deepStrictEqual(config, [
{
name: 'Python: Launch.json',
type: 'python',
request: 'launch',
purpose: ['debug-test'],
},
]);
});

test('If there is no launch.json return the config in the workspace file', async () => {
getConfigurationStub.withArgs('launch').returns({
configurations: [
{
name: 'Python: Workspace File',
type: 'python',
request: 'launch',
purpose: ['debug-test'],
},
],
});

const config = await getConfigurationsForWorkspace(workspaceFolder);

assert.deepStrictEqual(config, [
{
name: 'Python: Workspace File',
type: 'python',
request: 'launch',
purpose: ['debug-test'],
},
]);
});
});