forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.ts
More file actions
78 lines (68 loc) · 2.6 KB
/
helper.ts
File metadata and controls
78 lines (68 loc) · 2.6 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import * as assert from 'assert';
import { sep } from 'path';
import { Uri } from 'vscode';
import { IS_WINDOWS } from '../../client/common/platform/constants';
import { Tests } from '../../client/testing/common/types';
export const RESOURCE = Uri.file(__filename);
export function lookForTestFile(tests: Tests, testFile: string) {
let found: boolean;
// Perform case insensitive search on windows.
if (IS_WINDOWS) {
// In the mock output, we'd have paths separated using '/' (but on windows, path separators are '\')
const testFileToSearch = testFile.split(sep).join('/');
found = tests.testFiles.some(
t =>
(t.name.toUpperCase() === testFile.toUpperCase() ||
t.name.toUpperCase() === testFileToSearch.toUpperCase()) &&
t.nameToRun.toUpperCase() === t.name.toUpperCase()
);
} else {
found = tests.testFiles.some(t => t.name === testFile && t.nameToRun === t.name);
}
assert.equal(found, true, `Test File not found '${testFile}'`);
}
// Return a filename that uses the OS-specific path separator.
//
// Only "/" (forward slash) in the given filename is affected.
//
// This helps with readability in test code. It allows us to use
// literals for filenames and dirnames instead of oath.join().
export function fixPath(filename: string): string {
return filename.replace(/\//, sep);
}
// Return the indentation part of the given line.
export function getIndent(line: string): string {
const found = line.match(/^ */);
return found![0];
}
// Return the dedented lines in the given text.
//
// This is used to represent text concisely and readably, which is
// particularly useful for declarative definitions (e.g. in tests).
//
// (inspired by Python's textwrap.dedent())
export function getDedentedLines(text: string): string[] {
const linesep = text.includes('\r') ? '\r\n' : '\n';
const lines = text.split(linesep);
if (!lines) {
return [text];
}
if (lines[0] !== '') {
throw Error('expected actual first line to be blank');
}
lines.shift();
if (lines[0] === '') {
throw Error('expected "first" line to not be blank');
}
const leading = getIndent(lines[0]).length;
for (let i = 0; i < lines.length; i += 1) {
const line = lines[i];
if (getIndent(line).length < leading) {
throw Error(`line ${i} has less indent than the "first" line`);
}
lines[i] = line.substring(leading);
}
return lines;
}