forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
252 lines (222 loc) · 7.6 KB
/
utils.ts
File metadata and controls
252 lines (222 loc) · 7.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
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
// 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;
})();
export const SUPPORTS_SOCKETS = (() => {
if (WINDOWS) {
// Windows requires named pipes to have a specific path under
// the local domain ("\\.\pipe\*"). This makes them relatively
// useless in our functional tests, where we want to use them
// to exercise FileType.Unknown.
return false;
}
const tmp = tmpMod.dirSync({
prefix: 'pyvsc-test-',
unsafeCleanup: true // for non-empty dir
});
const filename = path.join(tmp.name, 'test.sock');
try {
const srv = net.createServer();
try {
srv.listen(filename);
} finally {
srv.close();
}
} catch {
return false;
} finally {
tmp.removeCallback();
}
return true;
})();
export const DOES_NOT_EXIST = 'this file does not exist';
export async function assertDoesNotExist(filename: string) {
const promise = fsextra.stat(filename);
await expect(promise).to.eventually.be.rejected;
}
export async function assertExists(filename: string) {
const promise = fsextra.stat(filename);
await expect(promise).to.not.eventually.be.rejected;
}
export async function assertFileText(filename: string, expected: string): Promise<string> {
const data = await fsextra.readFile(filename);
const text = data.toString();
expect(text).to.equal(expected);
return text;
}
export function fixPath(filename: string): string {
return path.normalize(filename);
}
export class SystemError extends Error {
public code: string;
public errno: number;
public syscall: string;
public info?: string;
public path?: string;
public address?: string;
public dest?: string;
public port?: string;
constructor(code: string, syscall: string, message: string) {
super(`${code}: ${message} ${syscall} '...'`);
this.code = code;
this.errno = 0; // Don't bother until we actually need it.
this.syscall = syscall;
}
}
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) {
const dirname = path.dirname(filename);
await fsextra.mkdirp(dirname);
}
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;
}
}