forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.ts
More file actions
584 lines (535 loc) · 21.5 KB
/
common.ts
File metadata and controls
584 lines (535 loc) · 21.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
// tslint:disable:no-console no-require-imports no-var-requires
import * as assert from 'assert';
import * as fs from 'fs-extra';
import * as glob from 'glob';
import * as path from 'path';
import { coerce, SemVer } from 'semver';
import { ConfigurationTarget, Event, TextDocument, Uri } from 'vscode';
import { IExtensionApi } from '../client/api';
import { IProcessService } from '../client/common/process/types';
import { IPythonSettings, Resource } from '../client/common/types';
import { PythonInterpreter } from '../client/interpreter/contracts';
import { IServiceContainer, IServiceManager } from '../client/ioc/types';
import { EXTENSION_ROOT_DIR_FOR_TESTS, IS_MULTI_ROOT_TEST, IS_PERF_TEST, IS_SMOKE_TEST } from './constants';
import { noop } from './core';
const StreamZip = require('node-stream-zip');
export { sleep } from './core';
// tslint:disable:no-invalid-this no-any
const fileInNonRootWorkspace = path.join(EXTENSION_ROOT_DIR_FOR_TESTS, 'src', 'test', 'pythonFiles', 'dummy.py');
export const rootWorkspaceUri = getWorkspaceRoot();
export const PYTHON_PATH = getPythonPath();
const arch = require('arch');
export const IS_64_BIT = arch() === 'x64';
export enum OSType {
Unknown = 'Unknown',
Windows = 'Windows',
OSX = 'OSX',
Linux = 'Linux'
}
export type PythonSettingKeys =
| 'workspaceSymbols.enabled'
| 'pythonPath'
| 'linting.lintOnSave'
| 'linting.enabled'
| 'linting.pylintEnabled'
| 'linting.flake8Enabled'
| 'linting.pycodestyleEnabled'
| 'linting.pylamaEnabled'
| 'linting.prospectorEnabled'
| 'linting.pydocstyleEnabled'
| 'linting.mypyEnabled'
| 'linting.banditEnabled'
| 'testing.nosetestArgs'
| 'testing.pytestArgs'
| 'testing.unittestArgs'
| 'formatting.provider'
| 'sortImports.args'
| 'testing.nosetestsEnabled'
| 'testing.pytestEnabled'
| 'testing.unittestEnabled'
| 'envFile'
| 'jediEnabled'
| 'linting.ignorePatterns'
| 'terminal.activateEnvironment';
async function disposePythonSettings() {
if (!IS_SMOKE_TEST) {
const configSettings = await import('../client/common/configSettings');
configSettings.PythonSettings.dispose();
}
}
export async function updateSetting(
setting: PythonSettingKeys,
value: {} | undefined,
resource: Uri | undefined,
configTarget: ConfigurationTarget
) {
const vscode = require('vscode') as typeof import('vscode');
const settings = vscode.workspace.getConfiguration('python', resource || null);
const currentValue = settings.inspect(setting);
if (
currentValue !== undefined &&
((configTarget === vscode.ConfigurationTarget.Global && currentValue.globalValue === value) ||
(configTarget === vscode.ConfigurationTarget.Workspace && currentValue.workspaceValue === value) ||
(configTarget === vscode.ConfigurationTarget.WorkspaceFolder &&
currentValue.workspaceFolderValue === value))
) {
await disposePythonSettings();
return;
}
await settings.update(setting, value, configTarget);
// We've experienced trouble with .update in the past, where VSC returns stale data even
// after invoking the update method. This issue has regressed a few times as well. This
// delay is merely a backup to ensure it extension doesn't break the tests due to similar
// regressions in VSC:
// await sleep(2000);
// ... please see issue #2356 and PR #2332 for a discussion on the matter
await disposePythonSettings();
}
export async function clearPythonPathInWorkspaceFolder(resource: string | Uri) {
const vscode = require('vscode') as typeof import('vscode');
return retryAsync(setPythonPathInWorkspace)(resource, vscode.ConfigurationTarget.WorkspaceFolder);
}
export async function setPythonPathInWorkspaceRoot(pythonPath: string) {
const vscode = require('vscode') as typeof import('vscode');
return retryAsync(setPythonPathInWorkspace)(undefined, vscode.ConfigurationTarget.Workspace, pythonPath);
}
export async function restorePythonPathInWorkspaceRoot() {
const vscode = require('vscode') as typeof import('vscode');
return retryAsync(setPythonPathInWorkspace)(undefined, vscode.ConfigurationTarget.Workspace, PYTHON_PATH);
}
export const resetGlobalPythonPathSetting = async () => retryAsync(restoreGlobalPythonPathSetting)();
export async function setAutoSaveDelayInWorkspaceRoot(delayinMS: number) {
const vscode = require('vscode') as typeof import('vscode');
return retryAsync(setAutoSaveDelay)(undefined, vscode.ConfigurationTarget.Workspace, delayinMS);
}
function getWorkspaceRoot() {
if (IS_SMOKE_TEST || IS_PERF_TEST) {
return;
}
const vscode = require('vscode') as typeof import('vscode');
if (!Array.isArray(vscode.workspace.workspaceFolders) || vscode.workspace.workspaceFolders.length === 0) {
return vscode.Uri.file(path.join(EXTENSION_ROOT_DIR_FOR_TESTS, 'src', 'test'));
}
if (vscode.workspace.workspaceFolders.length === 1) {
return vscode.workspace.workspaceFolders[0].uri;
}
const workspaceFolder = vscode.workspace.getWorkspaceFolder(vscode.Uri.file(fileInNonRootWorkspace));
return workspaceFolder ? workspaceFolder.uri : vscode.workspace.workspaceFolders[0].uri;
}
export function getExtensionSettings(resource: Uri | undefined): IPythonSettings {
const vscode = require('vscode') as typeof import('vscode');
class AutoSelectionService {
get onDidChangeAutoSelectedInterpreter(): Event<void> {
return new vscode.EventEmitter<void>().event;
}
public autoSelectInterpreter(_resource: Resource): Promise<void> {
return Promise.resolve();
}
public getAutoSelectedInterpreter(_resource: Resource): PythonInterpreter | undefined {
return;
}
public async setWorkspaceInterpreter(
_resource: Uri,
_interpreter: PythonInterpreter | undefined
): Promise<void> {
return;
}
}
const pythonSettings = require('../client/common/configSettings') as typeof import('../client/common/configSettings');
return pythonSettings.PythonSettings.getInstance(resource, new AutoSelectionService());
}
export function retryAsync(this: any, wrapped: Function, retryCount: number = 2) {
return async (...args: any[]) => {
return new Promise((resolve, reject) => {
const reasons: any[] = [];
const makeCall = () => {
wrapped.call(this as Function, ...args).then(resolve, (reason: any) => {
reasons.push(reason);
if (reasons.length >= retryCount) {
reject(reasons);
} else {
// If failed once, lets wait for some time before trying again.
setTimeout(makeCall, 500);
}
});
};
makeCall();
});
};
}
async function setAutoSaveDelay(resource: string | Uri | undefined, config: ConfigurationTarget, delayinMS: number) {
const vscode = require('vscode') as typeof import('vscode');
if (config === vscode.ConfigurationTarget.WorkspaceFolder && !IS_MULTI_ROOT_TEST) {
return;
}
const resourceUri = typeof resource === 'string' ? vscode.Uri.file(resource) : resource;
const settings = vscode.workspace.getConfiguration('files', resourceUri || null);
const value = settings.inspect<number>('autoSaveDelay');
const prop: 'workspaceFolderValue' | 'workspaceValue' =
config === vscode.ConfigurationTarget.Workspace ? 'workspaceValue' : 'workspaceFolderValue';
if (value && value[prop] !== delayinMS) {
await settings.update('autoSaveDelay', delayinMS, config);
await settings.update('autoSave', 'afterDelay');
}
}
async function setPythonPathInWorkspace(
resource: string | Uri | undefined,
config: ConfigurationTarget,
pythonPath?: string
) {
const vscode = require('vscode') as typeof import('vscode');
if (config === vscode.ConfigurationTarget.WorkspaceFolder && !IS_MULTI_ROOT_TEST) {
return;
}
const resourceUri = typeof resource === 'string' ? vscode.Uri.file(resource) : resource;
const settings = vscode.workspace.getConfiguration('python', resourceUri || null);
const value = settings.inspect<string>('pythonPath');
const prop: 'workspaceFolderValue' | 'workspaceValue' =
config === vscode.ConfigurationTarget.Workspace ? 'workspaceValue' : 'workspaceFolderValue';
if (value && value[prop] !== pythonPath) {
await settings.update('pythonPath', pythonPath, config);
await disposePythonSettings();
}
}
async function restoreGlobalPythonPathSetting(): Promise<void> {
const vscode = require('vscode') as typeof import('vscode');
const pythonConfig = vscode.workspace.getConfiguration('python', (null as any) as Uri);
await pythonConfig.update('pythonPath', undefined, true);
await disposePythonSettings();
}
export async function deleteDirectory(dir: string) {
const exists = await fs.pathExists(dir);
if (exists) {
await fs.remove(dir);
}
}
export async function deleteFile(file: string) {
const exists = await fs.pathExists(file);
if (exists) {
await fs.remove(file);
}
}
export async function deleteFiles(globPattern: string) {
const items = await new Promise<string[]>((resolve, reject) => {
glob(globPattern, (ex, files) => (ex ? reject(ex) : resolve(files)));
});
return Promise.all(items.map(item => fs.remove(item).catch(noop)));
}
function getPythonPath(): string {
if (process.env.CI_PYTHON_PATH && fs.existsSync(process.env.CI_PYTHON_PATH)) {
return process.env.CI_PYTHON_PATH;
}
return 'python';
}
/**
* Determine if the current platform is included in a list of platforms.
*
* @param {OSes} OSType[] List of operating system Ids to check within.
* @return true if the current OS matches one from the list, false otherwise.
*/
export function isOs(...OSes: OSType[]): boolean {
// get current OS
const currentOS: OSType = getOSType();
// compare and return
if (OSes.indexOf(currentOS) === -1) {
return false;
}
return true;
}
export function getOSType(platform: string = process.platform): OSType {
if (/^win/.test(platform)) {
return OSType.Windows;
} else if (/^darwin/.test(platform)) {
return OSType.OSX;
} else if (/^linux/.test(platform)) {
return OSType.Linux;
} else {
return OSType.Unknown;
}
}
/**
* Update a string that represents a path in any OS to the string representation of
* that same path in a different OS. Note: Does not handle drive letter if the path
* is intended for a root.
*
* @param pathToCorrect The string representation of a path from a specific OS.
* @param os The OS representation to switch to - if left undefined the current OS is used.
*/
export function correctPathForOsType(pathToCorrect: string, os?: OSType): string {
if (os === undefined) {
os = getOSType();
}
const pathSep: string = os === OSType.Windows ? '\\' : '/';
const replacePathSepRegex: RegExp = os === OSType.Windows ? /\//g : /\\/g;
return pathToCorrect.replace(replacePathSepRegex, pathSep);
}
/**
* Get the current Python interpreter version.
*
* @param {procService} IProcessService Optionally specify the IProcessService implementation to use to execute with.
* @return `SemVer` version of the Python interpreter, or `undefined` if an error occurs.
*/
export async function getPythonSemVer(procService?: IProcessService): Promise<SemVer | undefined> {
const decoder = await import('../client/common/process/decoder');
const proc = await import('../client/common/process/proc');
const pythonProcRunner = procService ? procService : new proc.ProcessService(new decoder.BufferDecoder());
const pyVerArgs = ['-c', 'import sys;print("{0}.{1}.{2}".format(*sys.version_info[:3]))'];
return pythonProcRunner
.exec(PYTHON_PATH, pyVerArgs)
.then(strVersion => new SemVer(strVersion.stdout.trim()))
.catch(err => {
// if the call fails this should make it loudly apparent.
console.error('Failed to get Python Version in getPythonSemVer', err);
return undefined;
});
}
/**
* Match a given semver version specification with a list of loosely defined
* version strings.
*
* Specify versions by their major version at minimum - the minor and patch
* version numbers are optional.
*
* '3', '3.6', '3.6.6', are all vald and only the portions specified will be matched
* against the current running Python interpreter version.
*
* Example scenarios:
* '3' will match version 3.5.6, 3.6.4, 3.6.6, and 3.7.0.
* '3.6' will match version 3.6.4 and 3.6.6.
* '3.6.4' will match version 3.6.4 only.
*
* @param {version} SemVer the version to look for.
* @param {searchVersions} string[] List of loosely-specified versions to match against.
*/
export function isVersionInList(version: SemVer, ...searchVersions: string[]): boolean {
// see if the major/minor version matches any member of the skip-list.
const isPresent = searchVersions.findIndex(ver => {
const semverChecker = coerce(ver);
if (semverChecker) {
if (semverChecker.compare(version) === 0) {
return true;
} else {
// compare all the parts of the version that we have, we know we have
// at minimum the major version or semverChecker would be 'null'...
const versionParts = ver.split('.');
let matches = parseInt(versionParts[0], 10) === version.major;
if (matches && versionParts.length >= 2) {
matches = parseInt(versionParts[1], 10) === version.minor;
}
if (matches && versionParts.length >= 3) {
matches = parseInt(versionParts[2], 10) === version.patch;
}
return matches;
}
}
return false;
});
if (isPresent >= 0) {
return true;
}
return false;
}
/**
* Determine if the Python interpreter version running in a given `IProcessService`
* is in a selection of versions.
*
* You can specify versions by specifying the major version at minimum - the minor and
* patch version numbers are optional.
*
* '3', '3.6', '3.6.6', are all vald and only the portions specified will be matched
* against the current running Python interpreter version.
*
* Example scenarios:
* '3' will match version 3.5.6, 3.6.4, 3.6.6, and 3.7.0.
* '3.6' will match version 3.6.4 and 3.6.6.
* '3.6.4' will match version 3.6.4 only.
*
* If you don't need to specify the environment (ie. the workspace) that the Python
* interpreter is running under, use the simpler `isPythonVersion` instead.
*
* @param {procService} IProcessService Optionally, use this process service to call out to python with.
* @param {versions} string[] Python versions to test for, specified as described above.
* @return true if the current Python version matches a version in the skip list, false otherwise.
*/
export async function isPythonVersionInProcess(procService?: IProcessService, ...versions: string[]): Promise<boolean> {
// get the current python version major/minor
const currentPyVersion = await getPythonSemVer(procService);
if (currentPyVersion) {
return isVersionInList(currentPyVersion, ...versions);
} else {
console.error(
`Failed to determine the current Python version when comparing against list [${versions.join(', ')}].`
);
return false;
}
}
/**
* Determine if the current interpreter version is in a given selection of versions.
*
* You can specify versions by using up to the first three semver parts of a python
* version.
*
* '3', '3.6', '3.6.6', are all vald and only the portions specified will be matched
* against the current running Python interpreter version.
*
* Example scenarios:
* '3' will match version 3.5.6, 3.6.4, 3.6.6, and 3.7.0.
* '3.6' will match version 3.6.4 and 3.6.6.
* '3.6.4' will match version 3.6.4 only.
*
* If you need to specify the environment (ie. the workspace) that the Python
* interpreter is running under, use `isPythonVersionInProcess` instead.
*
* @param {versions} string[] List of versions of python that are to be skipped.
* @param {resource} vscode.Uri Current workspace resource Uri or undefined.
* @return true if the current Python version matches a version in the skip list, false otherwise.
*/
export async function isPythonVersion(...versions: string[]): Promise<boolean> {
const currentPyVersion = await getPythonSemVer();
if (currentPyVersion) {
return isVersionInList(currentPyVersion, ...versions);
} else {
console.error(
`Failed to determine the current Python version when comparing against list [${versions.join(', ')}].`
);
return false;
}
}
export interface IExtensionTestApi extends IExtensionApi {
serviceContainer: IServiceContainer;
serviceManager: IServiceManager;
}
export async function unzip(zipFile: string, targetFolder: string): Promise<void> {
await fs.ensureDir(targetFolder);
return new Promise<void>((resolve, reject) => {
const zip = new StreamZip({
file: zipFile,
storeEntries: true
});
zip.on('ready', async () => {
zip.extract('extension', targetFolder, (err: any) => {
if (err) {
reject(err);
} else {
resolve();
}
zip.close();
});
});
});
}
/**
* Wait for a condition to be fulfilled within a timeout.
*
* @export
* @param {() => Promise<boolean>} condition
* @param {number} timeoutMs
* @param {string} errorMessage
* @returns {Promise<void>}
*/
export async function waitForCondition(
condition: () => Promise<boolean>,
timeoutMs: number,
errorMessage: string
): Promise<void> {
return new Promise<void>(async (resolve, reject) => {
const timeout = setTimeout(() => {
clearTimeout(timeout);
// tslint:disable-next-line: no-use-before-declare
clearTimeout(timer);
reject(new Error(errorMessage));
}, timeoutMs);
const timer = setInterval(async () => {
if (!(await condition().catch(() => false))) {
return;
}
clearTimeout(timeout);
clearTimeout(timer);
resolve();
}, 10);
});
}
export async function openFile(file: string): Promise<TextDocument> {
const vscode = require('vscode') as typeof import('vscode');
const textDocument = await vscode.workspace.openTextDocument(file);
await vscode.window.showTextDocument(textDocument);
assert(vscode.window.activeTextEditor, 'No active editor');
return textDocument;
}
/**
* Fakes for timers in nodejs when testing, using `lolex`.
* An alternative to `sinon.useFakeTimers` (which in turn uses `lolex`, but doesn't expose the `async` methods).
* Use this class when you have tests with `setTimeout` and which to avoid them for faster tests.
*
* For further information please refer:
* - https://www.npmjs.com/package/lolex
* - https://sinonjs.org/releases/v1.17.6/fake-timers/
*
* @class FakeClock
*/
export class FakeClock {
// tslint:disable-next-line:no-any
private clock?: any;
/**
* Creates an instance of FakeClock.
* @param {number} [advacenTimeMs=10_000] Default `timeout` value. Defaults to 10s. Assuming we do not have anything bigger.
* @memberof FakeClock
*/
constructor(private readonly advacenTimeMs: number = 10_000) {}
public install() {
// tslint:disable-next-line:no-require-imports
const lolex = require('lolex');
this.clock = lolex.install();
}
public uninstall() {
this.clock?.uninstall();
}
/**
* Wait for timers to kick in, and then wait for all of them to complete.
*
* @returns {Promise<void>}
* @memberof FakeClock
*/
public async wait(): Promise<void> {
await this.waitForTimersToStart();
await this.waitForTimersToFinish();
}
/**
* Wait for timers to start.
*
* @returns {Promise<void>}
* @memberof FakeClock
*/
private async waitForTimersToStart(): Promise<void> {
if (!this.clock) {
throw new Error('Fake clock not installed');
}
while (this.clock.countTimers() === 0) {
// Relinquish control to event loop, so other timer code will run.
// We want to wait for `setTimeout` to kick in.
await new Promise(resolve => process.nextTick(resolve));
}
}
/**
* Wait for timers to finish.
*
* @returns {Promise<void>}
* @memberof FakeClock
*/
private async waitForTimersToFinish(): Promise<void> {
if (!this.clock) {
throw new Error('Fake clock not installed');
}
while (this.clock.countTimers()) {
// Advance clock by 10s (can be anything to ensure the next scheduled block of code executes).
// Assuming we do not have timers > 10s
// This will ensure any such such as `setTimeout(..., 10)` will get executed.
this.clock.tick(this.advacenTimeMs);
// Wait for the timer code to run to completion (incase they are promises).
await this.clock.runAllAsync();
}
}
}