Skip to content

Commit 4f880a1

Browse files
committed
clean up
1 parent 7d4f0f7 commit 4f880a1

File tree

2 files changed

+28
-57
lines changed

2 files changed

+28
-57
lines changed

src/test/common.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,19 @@ export function retryAsync(wrapped: Function, retryCount: number = 2) {
6969
};
7070
}
7171

72-
async function clearPythonPathInWorkspaceFolderImpl(resource: string | Uri) {
73-
if (!IS_MULTI_ROOT_TEST) {
72+
async function setPythonPathInWorkspace(resource: string | Uri | undefined, config: ConfigurationTarget, pythonPath?: string) {
73+
if (config === ConfigurationTarget.WorkspaceFolder && !IS_MULTI_ROOT_TEST) {
7474
return;
7575
}
7676
const resourceUri = typeof resource === 'string' ? Uri.file(resource) : resource;
7777
const settings = workspace.getConfiguration('python', resourceUri);
7878
const value = settings.inspect<string>('pythonPath');
79-
if (value && typeof value.workspaceFolderValue === 'string') {
80-
await settings.update('pythonPath', undefined, ConfigurationTarget.WorkspaceFolder);
79+
const prop: 'workspaceFolderValue' | 'workspaceValue' = config === ConfigurationTarget.Workspace ? 'workspaceValue' : 'workspaceFolderValue';
80+
if (value && value[prop] !== pythonPath) {
81+
await settings.update('pythonPath', pythonPath, config);
8182
PythonSettings.dispose();
8283
}
83-
const settings2 = workspace.getConfiguration('python', resourceUri);
84-
const value2 = settings.inspect<string>('pythonPath');
85-
const y = '';
8684
}
8785

88-
export const clearPythonPathInWorkspaceFolder = async (resource: string | Uri) => retryAsync(clearPythonPathInWorkspaceFolderImpl)(resource);
86+
export const clearPythonPathInWorkspaceFolder = async (resource: string | Uri) => retryAsync(setPythonPathInWorkspace)(resource, ConfigurationTarget.WorkspaceFolder);
87+
export const setPythonPathInWorkspaceRoot = async (pythonPath: string) => retryAsync(setPythonPathInWorkspace)(undefined, ConfigurationTarget.Workspace, pythonPath);

src/test/initialize.ts

Lines changed: 21 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,43 @@ import * as assert from 'assert';
22
import * as fs from 'fs';
33
import * as path from 'path';
44
import * as vscode from 'vscode';
5-
import { clearPythonPathInWorkspaceFolder } from './common';
5+
import { PythonSettings } from '../client/common/configSettings';
6+
import { activated } from '../client/extension';
7+
import { clearPythonPathInWorkspaceFolder, setPythonPathInWorkspaceRoot } from './common';
8+
69
const dummyPythonFile = path.join(__dirname, '..', '..', 'src', 'test', 'pythonFiles', 'dummy.py');
710

8-
//First thing to be executed
11+
//First thing to be executed.
912
// tslint:disable-next-line:no-string-literal
1013
process.env['VSC_PYTHON_CI_TEST'] = '1';
1114

12-
// tslint:disable-next-line:no-any
13-
let configSettings: any;
14-
let extensionActivated: boolean = false;
15+
const PYTHON_PATH = getPythonPath();
16+
// tslint:disable-next-line:no-string-literal prefer-template
17+
export const IS_TRAVIS = (process.env['TRAVIS'] + '') === 'true';
18+
export const TEST_TIMEOUT = 25000;
19+
export const IS_MULTI_ROOT_TEST = isMultitrootTest();
20+
21+
// Ability to use custom python environments for testing
22+
export async function initializePython() {
23+
await clearPythonPathInWorkspaceFolder(dummyPythonFile);
24+
await setPythonPathInWorkspaceRoot(PYTHON_PATH);
25+
}
26+
1527
// tslint:disable-next-line:no-any
1628
export async function initialize(): Promise<any> {
1729
await initializePython();
1830
// Opening a python file activates the extension.
1931
await vscode.workspace.openTextDocument(dummyPythonFile);
20-
if (!extensionActivated) {
21-
// tslint:disable-next-line:no-require-imports
22-
const ext = require('../client/extension');
23-
// tslint:disable-next-line:no-unsafe-any
24-
await ext.activated;
25-
extensionActivated = true;
26-
}
27-
if (!configSettings) {
28-
// tslint:disable-next-line:no-require-imports
29-
configSettings = await require('../client/common/configSettings');
30-
}
32+
await activated;
3133
// Dispose any cached python settings (used only in test env).
32-
// tslint:disable-next-line:no-unsafe-any)
33-
configSettings.PythonSettings.dispose();
34+
PythonSettings.dispose();
3435
}
3536
// tslint:disable-next-line:no-any
3637
export async function initializeTest(): Promise<any> {
3738
await initializePython();
3839
await closeActiveWindows();
39-
if (!configSettings) {
40-
// tslint:disable-next-line:no-require-imports no-unsafe-any
41-
configSettings = await require('../client/common/configSettings');
42-
}
43-
// Dispose any cached python settings (used only in test env)
44-
// tslint:disable-next-line:no-unsafe-any
45-
configSettings.PythonSettings.dispose();
40+
// Dispose any cached python settings (used only in test env).
41+
PythonSettings.dispose();
4642
}
4743

4844
export async function wait(timeoutMilliseconds: number) {
@@ -101,27 +97,3 @@ function getPythonPath(): string {
10197
function isMultitrootTest() {
10298
return Array.isArray(vscode.workspace.workspaceFolders) && vscode.workspace.workspaceFolders.length > 1;
10399
}
104-
105-
const PYTHON_PATH = getPythonPath();
106-
// tslint:disable-next-line:no-string-literal prefer-template
107-
export const IS_TRAVIS = (process.env['TRAVIS'] + '') === 'true';
108-
export const TEST_TIMEOUT = 25000;
109-
export const IS_MULTI_ROOT_TEST = isMultitrootTest();
110-
111-
// Ability to use custom python environments for testing
112-
export async function initializePython() {
113-
await clearPythonPathInWorkspaceFolder(dummyPythonFile);
114-
115-
const pythonConfig = vscode.workspace.getConfiguration('python');
116-
const value = pythonConfig.inspect('pythonPath');
117-
if (value && value.workspaceValue !== PYTHON_PATH) {
118-
await pythonConfig.update('pythonPath', PYTHON_PATH, vscode.ConfigurationTarget.Workspace);
119-
if (!configSettings) {
120-
// tslint:disable-next-line:no-require-imports no-unsafe-any
121-
configSettings = await require('../client/common/configSettings');
122-
}
123-
// Dispose any cached python settings (used only in test env)
124-
// tslint:disable-next-line:no-unsafe-any
125-
configSettings.PythonSettings.dispose();
126-
}
127-
}

0 commit comments

Comments
 (0)