Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/emmet/src/test/testUtils.ts
4774 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import * as vscode from 'vscode';
7
import * as fs from 'fs';
8
import * as os from 'os';
9
import { join } from 'path';
10
11
export function rndName() {
12
let name = '';
13
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
14
for (let i = 0; i < 10; i++) {
15
name += possible.charAt(Math.floor(Math.random() * possible.length));
16
}
17
return name;
18
}
19
20
export function createRandomFile(contents = '', fileExtension = 'txt'): Thenable<vscode.Uri> {
21
return new Promise((resolve, reject) => {
22
const tmpFile = join(os.tmpdir(), rndName() + '.' + fileExtension);
23
fs.writeFile(tmpFile, contents, (error) => {
24
if (error) {
25
return reject(error);
26
}
27
28
resolve(vscode.Uri.file(tmpFile));
29
});
30
});
31
}
32
33
export function pathEquals(path1: string, path2: string): boolean {
34
if (process.platform !== 'linux') {
35
path1 = path1.toLowerCase();
36
path2 = path2.toLowerCase();
37
}
38
39
return path1 === path2;
40
}
41
42
export function deleteFile(file: vscode.Uri): Thenable<boolean> {
43
return new Promise((resolve, reject) => {
44
fs.unlink(file.fsPath, (err) => {
45
if (err) {
46
reject(err);
47
} else {
48
resolve(true);
49
}
50
});
51
});
52
}
53
54
export function closeAllEditors(): Thenable<any> {
55
return vscode.commands.executeCommand('workbench.action.closeAllEditors');
56
}
57
58
export function withRandomFileEditor(initialContents: string, fileExtension: string = 'txt', run: (editor: vscode.TextEditor, doc: vscode.TextDocument) => Thenable<void>): Thenable<boolean> {
59
return createRandomFile(initialContents, fileExtension).then(file => {
60
return vscode.workspace.openTextDocument(file).then(doc => {
61
return vscode.window.showTextDocument(doc).then((editor) => {
62
return run(editor, doc).then(_ => {
63
if (doc.isDirty) {
64
return doc.save().then(() => {
65
return deleteFile(file);
66
});
67
} else {
68
return deleteFile(file);
69
}
70
});
71
});
72
});
73
});
74
}
75
76