Skip to content

Commit

Permalink
Use workspaceFolder token instead of workspaceRoot (#267)
Browse files Browse the repository at this point in the history
Fixes #258
* use workspaceFolder token instead of workspaceRoot
* fix typo
* updated to properly describe intent of test
  • Loading branch information
DonJayamanne authored Nov 22, 2017
1 parent f37fe13 commit a975862
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 92 deletions.
26 changes: 13 additions & 13 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,44 @@
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"--extensionDevelopmentPath=${workspaceRoot}"
"--extensionDevelopmentPath=${workspaceFolder}"
],
"stopOnEntry": false,
"sourceMaps": true,
"outFiles": [
"${workspaceRoot}/out/**/*.js"
"${workspaceFolder}/out/**/*.js"
],
"preLaunchTask": "compile"
},
{
"name": "Launch Extension as debugServer", // https://code.visualstudio.com/docs/extensions/example-debuggers
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/out/client/debugger/Main.js",
"program": "${workspaceFolder}/out/client/debugger/Main.js",
"stopOnEntry": false,
"args": [
"--server=4711"
],
"sourceMaps": true,
"outFiles": [
"${workspaceRoot}/out/client/**/*.js"
"${workspaceFolder}/out/client/**/*.js"
],
"cwd": "${workspaceRoot}"
"cwd": "${workspaceFolder}"
},
{
"name": "Launch Tests",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"${workspaceRoot}/src/test",
"--extensionDevelopmentPath=${workspaceRoot}",
"--extensionTestsPath=${workspaceRoot}/out/test"
"${workspaceFolder}/src/test",
"--extensionDevelopmentPath=${workspaceFolder}",
"--extensionTestsPath=${workspaceFolder}/out/test"
],
"stopOnEntry": false,
"sourceMaps": true,
"outFiles": [
"${workspaceRoot}/out/**/*.js"
"${workspaceFolder}/out/**/*.js"
],
"preLaunchTask": "compile"
},
Expand All @@ -55,14 +55,14 @@
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"${workspaceRoot}/src/testMultiRootWkspc/multi.code-workspace",
"--extensionDevelopmentPath=${workspaceRoot}",
"--extensionTestsPath=${workspaceRoot}/out/test"
"${workspaceFolder}/src/testMultiRootWkspc/multi.code-workspace",
"--extensionDevelopmentPath=${workspaceFolder}",
"--extensionTestsPath=${workspaceFolder}/out/test"
],
"stopOnEntry": false,
"sourceMaps": true,
"outFiles": [
"${workspaceRoot}/out/**/*.js"
"${workspaceFolder}/out/**/*.js"
],
"preLaunchTask": "compile"
}
Expand Down
110 changes: 55 additions & 55 deletions package.json

Large diffs are not rendered by default.

24 changes: 16 additions & 8 deletions src/client/common/systemVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,27 +131,35 @@ export abstract class AbstractSystemVariables implements ISystemVariables {


export class SystemVariables extends AbstractSystemVariables {
private _workspaceRoot: string;
private _workspaceRootFolderName: string;
private _workspaceFolder: string;
private _workspaceFolderName: string;

constructor(workspaceRoot?: string) {
constructor(workspaceFolder?: string) {
super();
this._workspaceRoot = typeof workspaceRoot === 'string' ? workspaceRoot : __dirname;
this._workspaceRootFolderName = Path.basename(this._workspaceRoot);
this._workspaceFolder = typeof workspaceFolder === 'string' ? workspaceFolder : __dirname;
this._workspaceFolderName = Path.basename(this._workspaceFolder);
Object.keys(process.env).forEach(key => {
this[`env:${key}`] = this[`env.${key}`] = process.env[key];
});
}

public get cwd(): string {
return this.workspaceRoot;
return this.workspaceFolder;
}

public get workspaceRoot(): string {
return this._workspaceRoot;
return this._workspaceFolder;
}

public get workspaceFolder(): string {
return this._workspaceFolder;
}

public get workspaceRootFolderName(): string {
return this._workspaceRootFolderName;
return this._workspaceFolderName;
}

public get workspaceFolderBasename(): string {
return this._workspaceFolderName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class WorkspaceFolderPythonPathUpdaterService implements IPythonPathUpdat
}
if (pythonPath.startsWith(this.workspaceFolder.fsPath)) {
// tslint:disable-next-line:no-invalid-template-strings
pythonPath = path.join('${workspaceRoot}', path.relative(this.workspaceFolder.fsPath, pythonPath));
pythonPath = path.join('${workspaceFolder}', path.relative(this.workspaceFolder.fsPath, pythonPath));
}
await pythonConfig.update('pythonPath', pythonPath, ConfigurationTarget.WorkspaceFolder);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class WorkspacePythonPathUpdaterService implements IPythonPathUpdaterServ
}
if (pythonPath.startsWith(this.wkspace.fsPath)) {
// tslint:disable-next-line:no-invalid-template-strings
pythonPath = path.join('${workspaceRoot}', path.relative(this.wkspace.fsPath, pythonPath));
pythonPath = path.join('${workspaceFolder}', path.relative(this.wkspace.fsPath, pythonPath));
}
await pythonConfig.update('pythonPath', pythonPath, false);
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/common/configSettings.multiroot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ suite('Multiroot Config Settings', () => {
});

// tslint:disable-next-line:no-invalid-template-strings
test('${workspaceRoot} variable in settings should be replaced with the right value', async () => {
test('${workspaceFolder} variable in settings should be replaced with the right value', async () => {
const workspace2Uri = Uri.file(path.join(multirootPath, 'workspace2'));
let fileToOpen = path.join(workspace2Uri.fsPath, 'file.py');

Expand Down
63 changes: 53 additions & 10 deletions src/test/interpreters/pythonPathUpdater.multiroot.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as assert from 'assert';
import * as path from 'path';
import { ConfigurationTarget, Uri, workspace } from 'vscode';
import { PythonSettings } from '../../client/common/configSettings';
import { PythonPathUpdaterService } from '../../client/interpreter/configuration/pythonPathUpdaterService';
import { PythonPathUpdaterServiceFactory } from '../../client/interpreter/configuration/pythonPathUpdaterServiceFactory';
import { WorkspaceFolderPythonPathUpdaterService } from '../../client/interpreter/configuration/services/workspaceFolderUpdaterService';
Expand Down Expand Up @@ -32,40 +33,82 @@ suite('Multiroot Python Path Settings Updater', () => {

test('Updating Workspace Folder Python Path should work', async () => {
const workspaceUri = workspace3Uri;
const workspaceUpdater = new WorkspaceFolderPythonPathUpdaterService(workspace.getWorkspaceFolder(workspaceUri).uri);
const workspaceUpdater = new WorkspaceFolderPythonPathUpdaterService(workspace.getWorkspaceFolder(workspaceUri)!.uri);
const pythonPath = `xWorkspacePythonPath${new Date().getMilliseconds()}`;
await workspaceUpdater.updatePythonPath(pythonPath);
const folderValue = workspace.getConfiguration('python', workspace3Uri).inspect('pythonPath').workspaceFolderValue;
const folderValue = workspace.getConfiguration('python', workspace3Uri).inspect('pythonPath')!.workspaceFolderValue!;
assert.equal(folderValue, pythonPath, 'Workspace Python Path not updated');
});

test('Updating Workspace Folder Python Path when Python Path is in a sub directory of Workspace Folder', async () => {
const workspaceUri = workspace3Uri;
const workspaceFolder = workspace.getWorkspaceFolder(workspaceUri)!.uri;
const workspaceUpdater = new WorkspaceFolderPythonPathUpdaterService(workspaceFolder);

// Python path within a sub directory of the workspace folder.
const pythonExec = path.join('virtual Envs', 'env1', `xWorkspacePythonPath${new Date().getMilliseconds()}`);

// Expected path to python executable where ${workspaceFolder} token represents the fully qualified path to executable.
// tslint:disable-next-line:no-invalid-template-strings
const rawPythonPath = path.join('${workspaceFolder}', pythonExec);

// Fully qualified path to the python executable.
const pythonPath = path.join(workspaceFolder.fsPath, pythonExec);
await workspaceUpdater.updatePythonPath(pythonPath);
PythonSettings.dispose();

const folderValue = workspace.getConfiguration('python', workspace3Uri).inspect('pythonPath')!.workspaceFolderValue!;
assert.equal(folderValue, rawPythonPath, 'Raw Workspace Python Path not updated with a path relative to workspace folder');
const resolvedPythonPath = PythonSettings.getInstance(workspaceUri).pythonPath;
assert.equal(resolvedPythonPath, pythonPath, 'Resolved Workspace Python Path is incorrect');
});

test('Updating Workspace Folder Python Path using the factor service should work', async () => {
const workspaceUri = workspace3Uri;
const factory = new PythonPathUpdaterServiceFactory();
const workspaceUpdater = factory.getWorkspaceFolderPythonPathConfigurationService(workspace.getWorkspaceFolder(workspaceUri).uri);
const workspaceUpdater = factory.getWorkspaceFolderPythonPathConfigurationService(workspace.getWorkspaceFolder(workspaceUri)!.uri);
const pythonPath = `xWorkspacePythonPathFromFactory${new Date().getMilliseconds()}`;
await workspaceUpdater.updatePythonPath(pythonPath);
const folderValue = workspace.getConfiguration('python', workspace3Uri).inspect('pythonPath').workspaceFolderValue;
const folderValue = workspace.getConfiguration('python', workspace3Uri).inspect('pythonPath')!.workspaceFolderValue!;
assert.equal(folderValue, pythonPath, 'Workspace Python Path not updated');
});

test('Updating Workspace Python Path using the PythonPathUpdaterService should work', async () => {
const workspaceUri = workspace3Uri;
const updaterService = new PythonPathUpdaterService(new PythonPathUpdaterServiceFactory(), new InterpreterVersionService());
const pythonPath = `xWorkspacePythonPathFromUpdater${new Date().getMilliseconds()}`;
await updaterService.updatePythonPath(pythonPath, ConfigurationTarget.WorkspaceFolder, 'ui', workspace.getWorkspaceFolder(workspaceUri).uri);
const folderValue = workspace.getConfiguration('python', workspace3Uri).inspect('pythonPath').workspaceFolderValue;
await updaterService.updatePythonPath(pythonPath, ConfigurationTarget.WorkspaceFolder, 'ui', workspace.getWorkspaceFolder(workspaceUri)!.uri);
const folderValue = workspace.getConfiguration('python', workspace3Uri).inspect('pythonPath')!.workspaceFolderValue!;
assert.equal(folderValue, pythonPath, 'Workspace Python Path not updated');
});

test('Python Path should be relative to workspace', async () => {
const workspaceUri = workspace.getWorkspaceFolder(workspace3Uri).uri;
// tslint:disable-next-line:no-invalid-template-strings
test('Python Paths containing ${workspaceRoot} should be resolved as ${workspaceFolder}', async () => {
const workspaceUri = workspace.getWorkspaceFolder(workspace3Uri)!.uri;
const pythonInterpreter = `xWorkspacePythonPath${new Date().getMilliseconds()}`;
// tslint:disable-next-line:no-invalid-template-strings
const pythonPath = path.join('${workspaceRoot}', 'x', 'y', 'z', pythonInterpreter);
const workspaceUpdater = new WorkspacePythonPathUpdaterService(workspaceUri);
await workspaceUpdater.updatePythonPath(pythonPath);
const workspaceValue = workspace.getConfiguration('python').inspect('pythonPath')!.workspaceValue!;
const resolvedPythonPath = path.join(workspaceUri.fsPath, 'x', 'y', 'z', pythonInterpreter);
// tslint:disable-next-line:no-invalid-template-strings
assert.equal(workspaceValue, pythonPath, 'Workspace Python Path not updated');
PythonSettings.dispose();
assert.equal(PythonSettings.getInstance(workspace3Uri).pythonPath, resolvedPythonPath, 'Resolved Workspace Python Path is incorrect');
});

// tslint:disable-next-line:no-invalid-template-strings
test('Python Path should be relative to workspace when using ${workspaceFolder}', async () => {
const workspaceUri = workspace.getWorkspaceFolder(workspace3Uri)!.uri;
const pythonInterpreter = `xWorkspacePythonPath${new Date().getMilliseconds()}`;
const pythonPath = path.join(workspaceUri.fsPath, 'x', 'y', 'z', pythonInterpreter);
const workspaceUpdater = new WorkspacePythonPathUpdaterService(workspaceUri);
await workspaceUpdater.updatePythonPath(pythonPath);
const workspaceValue = workspace.getConfiguration('python').inspect('pythonPath').workspaceValue;
const workspaceValue = workspace.getConfiguration('python').inspect('pythonPath')!.workspaceValue!;
// tslint:disable-next-line:no-invalid-template-strings
assert.equal(workspaceValue, path.join('${workspaceRoot}', 'x', 'y', 'z', pythonInterpreter), 'Workspace Python Path not updated');
assert.equal(workspaceValue, path.join('${workspaceFolder}', 'x', 'y', 'z', pythonInterpreter), 'Workspace Python Path not updated');
PythonSettings.dispose();
assert.equal(PythonSettings.getInstance(workspace3Uri).pythonPath, pythonPath, 'Resolved Workspace Python Path is incorrect');
});
});
9 changes: 7 additions & 2 deletions src/test/interpreters/pythonPathUpdater.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import * as assert from 'assert';
import * as path from 'path';
import { ConfigurationTarget, Uri, workspace } from 'vscode';
import { PythonSettings } from '../../client/common/configSettings';
import { PythonPathUpdaterService } from '../../client/interpreter/configuration/pythonPathUpdaterService';
import { PythonPathUpdaterServiceFactory } from '../../client/interpreter/configuration/pythonPathUpdaterServiceFactory';
import { GlobalPythonPathUpdaterService } from '../../client/interpreter/configuration/services/globalUpdaterService';
import { WorkspacePythonPathUpdaterService } from '../../client/interpreter/configuration/services/workspaceUpdaterService';
import { InterpreterVersionService } from '../../client/interpreter/interpreterVersion';
import { closeActiveWindows, initialize, initializeTest } from '../initialize';
Expand Down Expand Up @@ -76,14 +78,17 @@ suite('Python Path Settings Updater', () => {
assert.equal(workspaceValue, pythonPath, 'Workspace Python Path not updated');
});

test('Python Path should be relative to workspace', async () => {
test('Python Path should be relative to workspaceFolder', async () => {
const workspaceUri = workspace.getWorkspaceFolder(Uri.file(workspaceRoot)).uri;
const pythonInterpreter = `xWorkspacePythonPath${new Date().getMilliseconds()}`;
const pythonPath = path.join(workspaceUri.fsPath, 'x', 'y', 'z', pythonInterpreter);
const workspaceUpdater = new WorkspacePythonPathUpdaterService(workspaceUri);
await workspaceUpdater.updatePythonPath(pythonPath);
const workspaceValue = workspace.getConfiguration('python').inspect('pythonPath').workspaceValue;
// tslint:disable-next-line:no-invalid-template-strings
assert.equal(workspaceValue, path.join('${workspaceRoot}', 'x', 'y', 'z', pythonInterpreter), 'Workspace Python Path not updated');
assert.equal(workspaceValue, path.join('${workspaceFolder}', 'x', 'y', 'z', pythonInterpreter), 'Workspace Python Path not updated');
const resolvedPath = PythonSettings.getInstance(Uri.file(workspaceRoot)).pythonPath;
assert.equal(resolvedPath, pythonPath, 'Resolved Workspace Python Path not updated');
});

});
2 changes: 1 addition & 1 deletion src/testMultiRootWkspc/workspace2/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"python.workspaceSymbols.tagFilePath": "${workspaceRoot}/workspace2.tags.file",
"python.workspaceSymbols.tagFilePath": "${workspaceFolder}/workspace2.tags.file",
"python.workspaceSymbols.enabled": false
}

0 comments on commit a975862

Please sign in to comment.