-
-
Notifications
You must be signed in to change notification settings - Fork 431
/
Copy pathsketches-service-impl.slow-test.ts
336 lines (312 loc) · 12.2 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
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 { promises as fs } from 'fs';
import { basename, join } from 'path';
import { sync as rimrafSync } from 'rimraf';
import { Sketch, SketchesService } from '../../common/protocol';
import { SketchesServiceImpl } from '../../node/sketches-service-impl';
import { ErrnoException } from '../../node/utils/errors';
import { createBaseContainer, startDaemon } from './test-bindings';
const testTimeout = 10_000;
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();
}