-
-
Notifications
You must be signed in to change notification settings - Fork 435
/
Copy pathsketches-service-impl.slow-test.ts
436 lines (401 loc) · 16.7 KB
/
sketches-service-impl.slow-test.ts
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
import {
Disposable,
DisposableCollection,
} from '@theia/core/lib/common/disposable';
import { isWindows } from '@theia/core/lib/common/os';
import { FileUri } from '@theia/core/lib/node/file-uri';
import { Container } from '@theia/core/shared/inversify';
import { expect } from 'chai';
import { rejects } from 'node:assert/strict';
import { promises as fs } from 'node:fs';
import { basename, join } from 'node:path';
import { sync as rimrafSync } from 'rimraf';
import temp from 'temp';
import { Sketch, SketchesService } from '../../common/protocol';
import {
isAccessibleSketchPath,
SketchesServiceImpl,
} from '../../node/sketches-service-impl';
import { ErrnoException } from '../../node/utils/errors';
import { createBaseContainer, startDaemon } from './node-test-bindings';
const testTimeout = 10_000;
describe('isAccessibleSketchPath', () => {
let tracked: typeof temp;
let testDirPath: string;
before(() => (tracked = temp.track()));
beforeEach(() => (testDirPath = tracked.mkdirSync()));
after(() => tracked.cleanupSync());
it('should be accessible by the main sketch file', async () => {
const sketchFolderPath = join(testDirPath, 'my_sketch');
const mainSketchFilePath = join(sketchFolderPath, 'my_sketch.ino');
await fs.mkdir(sketchFolderPath, { recursive: true });
await fs.writeFile(mainSketchFilePath, '', { encoding: 'utf8' });
const actual = await isAccessibleSketchPath(mainSketchFilePath);
expect(actual).to.be.equal(mainSketchFilePath);
});
it('should be accessible by the sketch folder', async () => {
const sketchFolderPath = join(testDirPath, 'my_sketch');
const mainSketchFilePath = join(sketchFolderPath, 'my_sketch.ino');
await fs.mkdir(sketchFolderPath, { recursive: true });
await fs.writeFile(mainSketchFilePath, '', { encoding: 'utf8' });
const actual = await isAccessibleSketchPath(sketchFolderPath);
expect(actual).to.be.equal(mainSketchFilePath);
});
it('should be accessible when the sketch folder and main sketch file basenames are different', async () => {
const sketchFolderPath = join(testDirPath, 'my_sketch');
const mainSketchFilePath = join(sketchFolderPath, 'other_name_sketch.ino');
await fs.mkdir(sketchFolderPath, { recursive: true });
await fs.writeFile(mainSketchFilePath, '', { encoding: 'utf8' });
const actual = await isAccessibleSketchPath(sketchFolderPath);
expect(actual).to.be.equal(mainSketchFilePath);
});
it('should be deterministic (and sort by basename) when multiple sketch files exist', async () => {
const sketchFolderPath = join(testDirPath, 'my_sketch');
const aSketchFilePath = join(sketchFolderPath, 'a.ino');
const bSketchFilePath = join(sketchFolderPath, 'b.ino');
await fs.mkdir(sketchFolderPath, { recursive: true });
await fs.writeFile(aSketchFilePath, '', { encoding: 'utf8' });
await fs.writeFile(bSketchFilePath, '', { encoding: 'utf8' });
const actual = await isAccessibleSketchPath(sketchFolderPath);
expect(actual).to.be.equal(aSketchFilePath);
});
it('should ignore EACCESS (non-Windows)', async function () {
if (isWindows) {
// `stat` syscall does not result in an EACCESS on Windows after stripping the file permissions.
// an `open` syscall would, but IDE2 on purpose does not check the files.
// the sketch files are provided by the CLI after loading the sketch.
return this.skip();
}
const sketchFolderPath = join(testDirPath, 'my_sketch');
const mainSketchFilePath = join(sketchFolderPath, 'my_sketch.ino');
await fs.mkdir(sketchFolderPath, { recursive: true });
await fs.writeFile(mainSketchFilePath, '', { encoding: 'utf8' });
await fs.chmod(mainSketchFilePath, 0o000); // remove all permissions
await rejects(fs.readFile(mainSketchFilePath), ErrnoException.isEACCES);
const actual = await isAccessibleSketchPath(sketchFolderPath);
expect(actual).to.be.equal(mainSketchFilePath);
});
it("should not be accessible when there are no '.ino' files in the folder", async () => {
const sketchFolderPath = join(testDirPath, 'my_sketch');
await fs.mkdir(sketchFolderPath, { recursive: true });
const actual = await isAccessibleSketchPath(sketchFolderPath);
expect(actual).to.be.undefined;
});
it("should not be accessible when the main sketch file extension is not '.ino'", async () => {
const sketchFolderPath = join(testDirPath, 'my_sketch');
const mainSketchFilePath = join(sketchFolderPath, 'my_sketch.cpp');
await fs.mkdir(sketchFolderPath, { recursive: true });
await fs.writeFile(mainSketchFilePath, '', { encoding: 'utf8' });
const actual = await isAccessibleSketchPath(sketchFolderPath);
expect(actual).to.be.undefined;
});
it('should handle ENOENT', async () => {
const sketchFolderPath = join(testDirPath, 'my_sketch');
const actual = await isAccessibleSketchPath(sketchFolderPath);
expect(actual).to.be.undefined;
});
it('should handle UNKNOWN (Windows)', async function () {
if (!isWindows) {
return this.skip();
}
this.timeout(60_000);
const actual = await isAccessibleSketchPath('\\\\10.0.0.200\\path');
expect(actual).to.be.undefined;
});
});
describe('sketches-service-impl', () => {
let container: Container;
let toDispose: DisposableCollection;
before(async () => {
toDispose = new DisposableCollection();
container = await createContainer();
await start(container, toDispose);
});
after(() => toDispose.dispose());
describe('copy', () => {
it('should copy a sketch when the destination does not exist', async function () {
this.timeout(testTimeout);
const sketchesService =
container.get<SketchesServiceImpl>(SketchesService);
const destinationPath = await sketchesService['createTempFolder']();
let sketch = await sketchesService.createNewSketch();
toDispose.push(disposeSketch(sketch));
const sourcePath = FileUri.fsPath(sketch.uri);
const libBasename = 'lib.cpp';
const libContent = 'lib content';
const libPath = join(sourcePath, libBasename);
await fs.writeFile(libPath, libContent, { encoding: 'utf8' });
const headerBasename = 'header.h';
const headerContent = 'header content';
const headerPath = join(sourcePath, headerBasename);
await fs.writeFile(headerPath, headerContent, { encoding: 'utf8' });
sketch = await sketchesService.loadSketch(sketch.uri);
expect(Sketch.isInSketch(FileUri.create(libPath), sketch)).to.be.true;
expect(Sketch.isInSketch(FileUri.create(headerPath), sketch)).to.be.true;
const copied = await sketchesService.copy(sketch, {
destinationUri: FileUri.create(destinationPath).toString(),
});
toDispose.push(disposeSketch(copied));
expect(copied.name).to.be.equal(basename(destinationPath));
expect(
Sketch.isInSketch(
FileUri.create(
join(destinationPath, `${basename(destinationPath)}.ino`)
),
copied
)
).to.be.true;
expect(
Sketch.isInSketch(
FileUri.create(join(destinationPath, libBasename)),
copied
)
).to.be.true;
expect(
Sketch.isInSketch(
FileUri.create(join(destinationPath, headerBasename)),
copied
)
).to.be.true;
});
it("should copy only sketch files if 'onlySketchFiles' is true", async function () {
this.timeout(testTimeout);
const sketchesService =
container.get<SketchesServiceImpl>(SketchesService);
const destinationPath = await sketchesService['createTempFolder']();
let sketch = await sketchesService.createNewSketch();
toDispose.push(disposeSketch(sketch));
const sourcePath = FileUri.fsPath(sketch.uri);
const libBasename = 'lib.cpp';
const libContent = 'lib content';
const libPath = join(sourcePath, libBasename);
await fs.writeFile(libPath, libContent, { encoding: 'utf8' });
const headerBasename = 'header.h';
const headerContent = 'header content';
const headerPath = join(sourcePath, headerBasename);
await fs.writeFile(headerPath, headerContent, { encoding: 'utf8' });
const logBasename = 'inols-clangd-err.log';
const logContent = 'log file content';
const logPath = join(sourcePath, logBasename);
await fs.writeFile(logPath, logContent, { encoding: 'utf8' });
sketch = await sketchesService.loadSketch(sketch.uri);
expect(Sketch.isInSketch(FileUri.create(libPath), sketch)).to.be.true;
expect(Sketch.isInSketch(FileUri.create(headerPath), sketch)).to.be.true;
expect(Sketch.isInSketch(FileUri.create(logPath), sketch)).to.be.false;
const reloadedLogContent = await fs.readFile(logPath, {
encoding: 'utf8',
});
expect(reloadedLogContent).to.be.equal(logContent);
const copied = await sketchesService.copy(sketch, {
destinationUri: FileUri.create(destinationPath).toString(),
onlySketchFiles: true,
});
toDispose.push(disposeSketch(copied));
expect(copied.name).to.be.equal(basename(destinationPath));
expect(
Sketch.isInSketch(
FileUri.create(
join(destinationPath, `${basename(destinationPath)}.ino`)
),
copied
)
).to.be.true;
expect(
Sketch.isInSketch(
FileUri.create(join(destinationPath, libBasename)),
copied
)
).to.be.true;
expect(
Sketch.isInSketch(
FileUri.create(join(destinationPath, headerBasename)),
copied
)
).to.be.true;
expect(
Sketch.isInSketch(
FileUri.create(join(destinationPath, logBasename)),
copied
)
).to.be.false;
try {
await fs.readFile(join(destinationPath, logBasename), {
encoding: 'utf8',
});
expect.fail(
'Log file must not exist in the destination. Expected ENOENT when loading the log file.'
);
} catch (err) {
expect(ErrnoException.isENOENT(err)).to.be.true;
}
});
it('should copy sketch inside the sketch folder', async function () {
this.timeout(testTimeout);
const sketchesService =
container.get<SketchesServiceImpl>(SketchesService);
let sketch = await sketchesService.createNewSketch();
const destinationPath = join(FileUri.fsPath(sketch.uri), 'nested_copy');
toDispose.push(disposeSketch(sketch));
const sourcePath = FileUri.fsPath(sketch.uri);
const libBasename = 'lib.cpp';
const libContent = 'lib content';
const libPath = join(sourcePath, libBasename);
await fs.writeFile(libPath, libContent, { encoding: 'utf8' });
const headerBasename = 'header.h';
const headerContent = 'header content';
const headerPath = join(sourcePath, headerBasename);
await fs.writeFile(headerPath, headerContent, { encoding: 'utf8' });
sketch = await sketchesService.loadSketch(sketch.uri);
expect(Sketch.isInSketch(FileUri.create(libPath), sketch)).to.be.true;
expect(Sketch.isInSketch(FileUri.create(headerPath), sketch)).to.be.true;
const copied = await sketchesService.copy(sketch, {
destinationUri: FileUri.create(destinationPath).toString(),
});
toDispose.push(disposeSketch(copied));
expect(copied.name).to.be.equal(basename(destinationPath));
expect(
Sketch.isInSketch(
FileUri.create(
join(destinationPath, `${basename(destinationPath)}.ino`)
),
copied
)
).to.be.true;
expect(
Sketch.isInSketch(
FileUri.create(join(destinationPath, libBasename)),
copied
)
).to.be.true;
expect(
Sketch.isInSketch(
FileUri.create(join(destinationPath, headerBasename)),
copied
)
).to.be.true;
});
it('should copy sketch with overwrite when source and destination sketch folder names are the same', async function () {
this.timeout(testTimeout);
const sketchesService =
container.get<SketchesServiceImpl>(SketchesService);
const sketchFolderName = 'alma';
const contentOne = 'korte';
const contentTwo = 'szilva';
const [sketchOne, sketchTwo] = await Promise.all([
sketchesService.createNewSketch(sketchFolderName, contentOne),
sketchesService.createNewSketch(sketchFolderName, contentTwo),
]);
toDispose.push(disposeSketch(sketchOne, sketchTwo));
const [mainFileContentOne, mainFileContentTwo] = await Promise.all([
mainFileContentOf(sketchOne),
mainFileContentOf(sketchTwo),
]);
expect(mainFileContentOne).to.be.equal(contentOne);
expect(mainFileContentTwo).to.be.equal(contentTwo);
await sketchesService.copy(sketchOne, { destinationUri: sketchTwo.uri });
const [mainFileContentOneAfterCopy, mainFileContentTwoAfterCopy] =
await Promise.all([
mainFileContentOf(sketchOne),
mainFileContentOf(sketchTwo),
]);
expect(mainFileContentOneAfterCopy).to.be.equal(contentOne);
expect(mainFileContentTwoAfterCopy).to.be.equal(contentOne);
});
(
[
['(', ')', 'parentheses'],
['[', ']', 'brackets'],
['{', '}', 'braces'],
[
'<',
'>',
'chevrons',
{
predicate: () => isWindows,
why: '< (less than) and > (greater than) are reserved characters on Windows (https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions)',
},
],
] as [
open: string,
close: string,
name: string,
skip?: { predicate: () => boolean; why: string }
][]
).map(([open, close, name, skip]) =>
it(`should copy a sketch when the path contains ${name} in the sketch folder path: '${open},${close}'`, async function () {
if (skip) {
const { predicate, why } = skip;
if (predicate()) {
console.info(why);
return this.skip();
}
}
this.timeout(testTimeout);
const sketchesService =
container.get<SketchesServiceImpl>(SketchesService);
const content = `// special content when ${name} are in the path`;
const tempRoot = await sketchesService['createTempFolder']();
toDispose.push(disposeFolder(tempRoot));
const sketch = await sketchesService.createNewSketch(
'punctuation_marks',
content
);
toDispose.push(disposeSketch(sketch));
// the destination path contains punctuation marks
const tempRootUri = FileUri.create(tempRoot);
const testSegment = `path segment with ${open}${name}${close}`;
const firstDestinationUri = tempRootUri
.resolve(testSegment)
.resolve('first')
.resolve(sketch.name);
const firstSketchCopy = await sketchesService.copy(sketch, {
destinationUri: firstDestinationUri.toString(),
});
expect(firstSketchCopy).to.be.not.undefined;
expect(firstSketchCopy.mainFileUri).to.be.equal(
firstDestinationUri.resolve(`${sketch.name}.ino`).toString()
);
const firstCopyContent = await mainFileContentOf(firstSketchCopy);
expect(firstCopyContent).to.be.equal(content);
// the source path contains punctuation marks. yes, the target too, but it does not matter
const secondDestinationUri = tempRootUri
.resolve(testSegment)
.resolve('second')
.resolve(sketch.name);
const secondSketchCopy = await sketchesService.copy(firstSketchCopy, {
destinationUri: secondDestinationUri.toString(),
});
expect(secondSketchCopy).to.be.not.undefined;
expect(secondSketchCopy.mainFileUri).to.be.equal(
secondDestinationUri.resolve(`${sketch.name}.ino`).toString()
);
const secondCopyContent = await mainFileContentOf(secondSketchCopy);
expect(secondCopyContent).to.be.equal(content);
})
);
});
});
function disposeSketch(...sketch: Sketch[]): Disposable {
return disposeFolder(...sketch.map(({ uri }) => FileUri.fsPath(uri)));
}
function disposeFolder(...paths: string[]): Disposable {
return new DisposableCollection(
...paths.map((path) =>
Disposable.create(() => rimrafSync(path, { maxBusyTries: 5 }))
)
);
}
async function mainFileContentOf(sketch: Sketch): Promise<string> {
return fs.readFile(FileUri.fsPath(sketch.mainFileUri), {
encoding: 'utf8',
});
}
async function start(
container: Container,
toDispose: DisposableCollection
): Promise<void> {
await startDaemon(container, toDispose);
}
async function createContainer(): Promise<Container> {
return createBaseContainer();
}