forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
203 lines (174 loc) · 6.2 KB
/
utils.ts
File metadata and controls
203 lines (174 loc) · 6.2 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// tslint:disable:no-console
import { expect } from 'chai';
import * as fsextra from 'fs-extra';
import * as net from 'net';
import * as path from 'path';
import * as tmpMod from 'tmp';
// Note: all functional tests that trigger the VS Code "fs" API are
// found in filesystem.test.ts.
export const WINDOWS = /^win/.test(process.platform);
export const OSX = /^darwin/.test(process.platform);
export const SUPPORTS_SYMLINKS = (() => {
const source = fsextra.readdirSync('.')[0];
const symlink = `${source}.symlink`;
try {
fsextra.symlinkSync(source, symlink);
} catch {
return false;
}
fsextra.unlinkSync(symlink);
return true;
})();
// tslint:disable-next-line:no-suspicious-comment
// TODO(GH-8995) For the moment we simply say we cannot test with
// sockets on Windows.
export const SUPPORTS_SOCKETS = !WINDOWS;
export const DOES_NOT_EXIST = 'this file does not exist';
export async function assertDoesNotExist(filename: string) {
await expect(fsextra.stat(filename)).to.eventually.be.rejected;
}
export async function assertExists(filename: string) {
await expect(fsextra.stat(filename)).to.not.eventually.be.rejected;
}
export function fixPath(filename: string): string {
return path.normalize(filename);
}
export class CleanupFixture {
private cleanups: (() => void | Promise<void>)[];
constructor() {
this.cleanups = [];
}
public addCleanup(cleanup: () => void | Promise<void>) {
this.cleanups.push(cleanup);
}
public async cleanUp() {
const cleanups = this.cleanups;
this.cleanups = [];
return Promise.all(
cleanups.map(async (cleanup, i) => {
try {
const res = cleanup();
if (res) {
await res;
}
} catch (err) {
console.log(`cleanup ${i + 1} failed: ${err}`);
console.log('moving on...');
}
})
);
}
}
export class FSFixture extends CleanupFixture {
private tempDir: string | undefined;
private sockServer: net.Server | undefined;
public addFSCleanup(filename: string, dispose?: () => void) {
this.addCleanup(() => this.ensureDeleted(filename, dispose));
}
public async resolve(relname: string, mkdirs = true): Promise<string> {
const tempDir = this.ensureTempDir();
relname = path.normalize(relname);
const filename = path.join(tempDir, relname);
if (mkdirs) {
await fsextra.mkdirp(path.dirname(filename));
}
return filename;
}
public async createFile(relname: string, text = ''): Promise<string> {
const filename = await this.resolve(relname);
await fsextra.writeFile(filename, text);
return filename;
}
public async createDirectory(relname: string): Promise<string> {
const dirname = await this.resolve(relname);
await fsextra.mkdir(dirname);
return dirname;
}
public async createSymlink(relname: string, source: string): Promise<string> {
if (!SUPPORTS_SYMLINKS) {
throw Error('this platform does not support symlinks');
}
const symlink = await this.resolve(relname);
// We cannot use fsextra.ensureSymlink() because it requires
// that "source" exist.
await fsextra.symlink(source, symlink);
return symlink;
}
public async createSocket(relname: string): Promise<string> {
const srv = this.ensureSocketServer();
const filename = await this.resolve(relname);
await new Promise(resolve => srv!.listen(filename, 0, resolve));
return filename;
}
public async ensureDeleted(filename: string, dispose?: () => void) {
if (dispose) {
try {
dispose();
return; // Trust that dispose() did what it's supposed to.
} catch (err) {
// For temp directories, the "unsafeCleanup: true"
// option of the "tmp" module is supposed to support
// a non-empty directory, but apparently that isn't
// always the case.
// (see #8804)
if (!(await fsextra.pathExists(filename))) {
return;
}
console.log(`failure during dispose() for ${filename}: ${err}`);
console.log('...manually deleting');
// Fall back to fsextra.
}
}
try {
await fsextra.remove(filename);
} catch (err) {
if (!(await fsextra.pathExists(filename))) {
return;
}
console.log(`failure while deleting ${filename}: ${err}`);
}
}
private ensureTempDir(): string {
if (this.tempDir) {
return this.tempDir;
}
const tempDir = tmpMod.dirSync({
prefix: 'pyvsc-fs-tests-',
unsafeCleanup: true
});
this.tempDir = tempDir.name;
this.addFSCleanup(tempDir.name, async () => {
if (!this.tempDir) {
return;
}
this.tempDir = undefined;
await this.ensureDeleted(tempDir.name, tempDir.removeCallback);
//try {
// tempDir.removeCallback();
//} catch {
// // The "unsafeCleanup: true" option is supposed
// // to support a non-empty directory, but apparently
// // that isn't always the case. (see #8804)
// await fsextra.remove(tempDir.name);
//}
});
return tempDir.name;
}
private ensureSocketServer(): net.Server {
if (this.sockServer) {
return this.sockServer;
}
const srv = net.createServer();
this.sockServer = srv;
this.addCleanup(async () => {
try {
await new Promise(resolve => srv.close(resolve));
} catch (err) {
console.log(`failure while closing socket server: ${err}`);
}
});
return srv;
}
}