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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { createHash } from 'crypto';
import * as fs from 'fs';
import * as os from 'os';
import { Schemas } from 'vs/base/common/network';
import * as path from 'vs/base/common/path';
import * as platform from 'vs/base/common/platform';
import { isEqual } from 'vs/base/common/resources';
import { URI } from 'vs/base/common/uri';
import * as pfs from 'vs/base/node/pfs';
import { flakySuite, getRandomTestPath } from 'vs/base/test/node/testUtils';
import { BackupMainService } from 'vs/platform/backup/electron-main/backupMainService';
import { IBackupWorkspacesFormat, ISerializedWorkspace } from 'vs/platform/backup/node/backup';
import { TestConfigurationService } from 'vs/platform/configuration/test/common/testConfigurationService';
import { EnvironmentMainService } from 'vs/platform/environment/electron-main/environmentMainService';
import { OPTIONS, parseArgs } from 'vs/platform/environment/node/argv';
import { HotExitConfiguration } from 'vs/platform/files/common/files';
import { ConsoleMainLogger, LogService } from 'vs/platform/log/common/log';
import product from 'vs/platform/product/common/product';
import { IFolderBackupInfo, isFolderBackupInfo, IWorkspaceBackupInfo } from 'vs/platform/backup/common/backup';
import { IWorkspaceIdentifier } from 'vs/platform/workspace/common/workspace';
flakySuite('BackupMainService', () => {
function assertEqualFolderInfos(actual: IFolderBackupInfo[], expected: IFolderBackupInfo[]) {
const withUriAsString = (f: IFolderBackupInfo) => ({ folderUri: f.folderUri.toString(), remoteAuthority: f.remoteAuthority });
assert.deepStrictEqual(actual.map(withUriAsString), expected.map(withUriAsString));
}
function toWorkspace(path: string): IWorkspaceIdentifier {
return {
id: createHash('md5').update(sanitizePath(path)).digest('hex'),
configPath: URI.file(path)
};
}
function toWorkspaceBackupInfo(path: string, remoteAuthority?: string): IWorkspaceBackupInfo {
return {
workspace: {
id: createHash('md5').update(sanitizePath(path)).digest('hex'),
configPath: URI.file(path)
},
remoteAuthority
};
}
function toFolderBackupInfo(uri: URI, remoteAuthority?: string): IFolderBackupInfo {
return { folderUri: uri, remoteAuthority };
}
function toSerializedWorkspace(ws: IWorkspaceIdentifier): ISerializedWorkspace {
return {
id: ws.id,
configURIPath: ws.configPath.toString()
};
}
function ensureFolderExists(uri: URI): Promise<void> {
if (!fs.existsSync(uri.fsPath)) {
fs.mkdirSync(uri.fsPath);
}
const backupFolder = service.toBackupPath(uri);
return createBackupFolder(backupFolder);
}
async function ensureWorkspaceExists(workspace: IWorkspaceIdentifier): Promise<IWorkspaceIdentifier> {
if (!fs.existsSync(workspace.configPath.fsPath)) {
await pfs.Promises.writeFile(workspace.configPath.fsPath, 'Hello');
}
const backupFolder = service.toBackupPath(workspace.id);
await createBackupFolder(backupFolder);
return workspace;
}
async function createBackupFolder(backupFolder: string): Promise<void> {
if (!fs.existsSync(backupFolder)) {
fs.mkdirSync(backupFolder);
fs.mkdirSync(path.join(backupFolder, Schemas.file));
await pfs.Promises.writeFile(path.join(backupFolder, Schemas.file, 'foo.txt'), 'Hello');
}
}
function sanitizePath(p: string): string {
return platform.isLinux ? p : p.toLowerCase();
}
const fooFile = URI.file(platform.isWindows ? 'C:\\foo' : '/foo');
const barFile = URI.file(platform.isWindows ? 'C:\\bar' : '/bar');
let service: BackupMainService & { toBackupPath(arg: URI | string): string; getFolderHash(folder: IFolderBackupInfo): string };
let configService: TestConfigurationService;
let environmentService: EnvironmentMainService;
let testDir: string;
let backupHome: string;
let backupWorkspacesPath: string;
let existingTestFolder1: URI;
setup(async () => {
testDir = getRandomTestPath(os.tmpdir(), 'vsctests', 'backupmainservice');
backupHome = path.join(testDir, 'Backups');
backupWorkspacesPath = path.join(backupHome, 'workspaces.json');
existingTestFolder1 = URI.file(path.join(testDir, 'folder1'));
environmentService = new EnvironmentMainService(parseArgs(process.argv, OPTIONS), { _serviceBrand: undefined, ...product });
await pfs.Promises.mkdir(backupHome, { recursive: true });
configService = new TestConfigurationService();
service = new class TestBackupMainService extends BackupMainService {
constructor() {
super(environmentService, configService, new LogService(new ConsoleMainLogger()));
this.backupHome = backupHome;
this.workspacesJsonPath = backupWorkspacesPath;
}
toBackupPath(arg: URI | string): string {
const id = arg instanceof URI ? super.getFolderHash({ folderUri: arg }) : arg;
return path.join(this.backupHome, id);
}
override getFolderHash(folder: IFolderBackupInfo): string {
return super.getFolderHash(folder);
}
};
return service.initialize();
});
teardown(() => {
return pfs.Promises.rm(testDir);
});
test('service validates backup workspaces on startup and cleans up (folder workspaces)', async function () {
// 1) backup workspace path does not exist
service.registerFolderBackupSync(toFolderBackupInfo(fooFile));
service.registerFolderBackupSync(toFolderBackupInfo(barFile));
await service.initialize();
assertEqualFolderInfos(service.getFolderBackupPaths(), []);
// 2) backup workspace path exists with empty contents within
fs.mkdirSync(service.toBackupPath(fooFile));
fs.mkdirSync(service.toBackupPath(barFile));
service.registerFolderBackupSync(toFolderBackupInfo(fooFile));
service.registerFolderBackupSync(toFolderBackupInfo(barFile));
await service.initialize();
assertEqualFolderInfos(service.getFolderBackupPaths(), []);
assert.ok(!fs.existsSync(service.toBackupPath(fooFile)));
assert.ok(!fs.existsSync(service.toBackupPath(barFile)));
// 3) backup workspace path exists with empty folders within
fs.mkdirSync(service.toBackupPath(fooFile));
fs.mkdirSync(service.toBackupPath(barFile));
fs.mkdirSync(path.join(service.toBackupPath(fooFile), Schemas.file));
fs.mkdirSync(path.join(service.toBackupPath(barFile), Schemas.untitled));
service.registerFolderBackupSync(toFolderBackupInfo(fooFile));
service.registerFolderBackupSync(toFolderBackupInfo(barFile));
await service.initialize();
assertEqualFolderInfos(service.getFolderBackupPaths(), []);
assert.ok(!fs.existsSync(service.toBackupPath(fooFile)));
assert.ok(!fs.existsSync(service.toBackupPath(barFile)));
// 4) backup workspace path points to a workspace that no longer exists
// so it should convert the backup worspace to an empty workspace backup
const fileBackups = path.join(service.toBackupPath(fooFile), Schemas.file);
fs.mkdirSync(service.toBackupPath(fooFile));
fs.mkdirSync(service.toBackupPath(barFile));
fs.mkdirSync(fileBackups);
service.registerFolderBackupSync(toFolderBackupInfo(fooFile));
assert.strictEqual(service.getFolderBackupPaths().length, 1);
assert.strictEqual(service.getEmptyWindowBackupPaths().length, 0);
fs.writeFileSync(path.join(fileBackups, 'backup.txt'), '');
await service.initialize();
assert.strictEqual(service.getFolderBackupPaths().length, 0);
assert.strictEqual(service.getEmptyWindowBackupPaths().length, 1);
});
test('service validates backup workspaces on startup and cleans up (root workspaces)', async function () {
// 1) backup workspace path does not exist
service.registerWorkspaceBackupSync(toWorkspaceBackupInfo(fooFile.fsPath));
service.registerWorkspaceBackupSync(toWorkspaceBackupInfo(barFile.fsPath));
await service.initialize();
assert.deepStrictEqual(service.getWorkspaceBackups(), []);
// 2) backup workspace path exists with empty contents within
fs.mkdirSync(service.toBackupPath(fooFile));
fs.mkdirSync(service.toBackupPath(barFile));
service.registerWorkspaceBackupSync(toWorkspaceBackupInfo(fooFile.fsPath));
service.registerWorkspaceBackupSync(toWorkspaceBackupInfo(barFile.fsPath));
await service.initialize();
assert.deepStrictEqual(service.getWorkspaceBackups(), []);
assert.ok(!fs.existsSync(service.toBackupPath(fooFile)));
assert.ok(!fs.existsSync(service.toBackupPath(barFile)));
// 3) backup workspace path exists with empty folders within
fs.mkdirSync(service.toBackupPath(fooFile));
fs.mkdirSync(service.toBackupPath(barFile));
fs.mkdirSync(path.join(service.toBackupPath(fooFile), Schemas.file));
fs.mkdirSync(path.join(service.toBackupPath(barFile), Schemas.untitled));
service.registerWorkspaceBackupSync(toWorkspaceBackupInfo(fooFile.fsPath));
service.registerWorkspaceBackupSync(toWorkspaceBackupInfo(barFile.fsPath));
await service.initialize();
assert.deepStrictEqual(service.getWorkspaceBackups(), []);
assert.ok(!fs.existsSync(service.toBackupPath(fooFile)));
assert.ok(!fs.existsSync(service.toBackupPath(barFile)));
// 4) backup workspace path points to a workspace that no longer exists
// so it should convert the backup worspace to an empty workspace backup
const fileBackups = path.join(service.toBackupPath(fooFile), Schemas.file);
fs.mkdirSync(service.toBackupPath(fooFile));
fs.mkdirSync(service.toBackupPath(barFile));
fs.mkdirSync(fileBackups);
service.registerWorkspaceBackupSync(toWorkspaceBackupInfo(fooFile.fsPath));
assert.strictEqual(service.getWorkspaceBackups().length, 1);
assert.strictEqual(service.getEmptyWindowBackupPaths().length, 0);
fs.writeFileSync(path.join(fileBackups, 'backup.txt'), '');
await service.initialize();
assert.strictEqual(service.getWorkspaceBackups().length, 0);
assert.strictEqual(service.getEmptyWindowBackupPaths().length, 1);
});
test('service supports to migrate backup data from another location', () => {
const backupPathToMigrate = service.toBackupPath(fooFile);
fs.mkdirSync(backupPathToMigrate);
fs.writeFileSync(path.join(backupPathToMigrate, 'backup.txt'), 'Some Data');
service.registerFolderBackupSync(toFolderBackupInfo(URI.file(backupPathToMigrate)));
const workspaceBackupPath = service.registerWorkspaceBackupSync(toWorkspaceBackupInfo(barFile.fsPath), backupPathToMigrate);
assert.ok(fs.existsSync(workspaceBackupPath));
assert.ok(fs.existsSync(path.join(workspaceBackupPath, 'backup.txt')));
assert.ok(!fs.existsSync(backupPathToMigrate));
const emptyBackups = service.getEmptyWindowBackupPaths();
assert.strictEqual(0, emptyBackups.length);
});
test('service backup migration makes sure to preserve existing backups', () => {
const backupPathToMigrate = service.toBackupPath(fooFile);
fs.mkdirSync(backupPathToMigrate);
fs.writeFileSync(path.join(backupPathToMigrate, 'backup.txt'), 'Some Data');
service.registerFolderBackupSync(toFolderBackupInfo(URI.file(backupPathToMigrate)));
const backupPathToPreserve = service.toBackupPath(barFile);
fs.mkdirSync(backupPathToPreserve);
fs.writeFileSync(path.join(backupPathToPreserve, 'backup.txt'), 'Some Data');
service.registerFolderBackupSync(toFolderBackupInfo(URI.file(backupPathToPreserve)));
const workspaceBackupPath = service.registerWorkspaceBackupSync(toWorkspaceBackupInfo(barFile.fsPath), backupPathToMigrate);
assert.ok(fs.existsSync(workspaceBackupPath));
assert.ok(fs.existsSync(path.join(workspaceBackupPath, 'backup.txt')));
assert.ok(!fs.existsSync(backupPathToMigrate));
const emptyBackups = service.getEmptyWindowBackupPaths();
assert.strictEqual(1, emptyBackups.length);
assert.strictEqual(1, fs.readdirSync(path.join(backupHome, emptyBackups[0].backupFolder!)).length);
});
suite('loadSync', () => {
test('getFolderBackupPaths() should return [] when workspaces.json doesn\'t exist', () => {
assertEqualFolderInfos(service.getFolderBackupPaths(), []);
});
test('getFolderBackupPaths() should return [] when workspaces.json is not properly formed JSON', async () => {
fs.writeFileSync(backupWorkspacesPath, '');
await service.initialize();
assertEqualFolderInfos(service.getFolderBackupPaths(), []);
fs.writeFileSync(backupWorkspacesPath, '{]');
await service.initialize();
assertEqualFolderInfos(service.getFolderBackupPaths(), []);
fs.writeFileSync(backupWorkspacesPath, 'foo');
await service.initialize();
assertEqualFolderInfos(service.getFolderBackupPaths(), []);
});
test('getFolderBackupPaths() should return [] when folderWorkspaceInfos in workspaces.json is absent', async () => {
fs.writeFileSync(backupWorkspacesPath, '{}');
await service.initialize();
assertEqualFolderInfos(service.getFolderBackupPaths(), []);
});
test('getFolderBackupPaths() should return [] when folderWorkspaceInfos in workspaces.json is not a string array', async () => {
fs.writeFileSync(backupWorkspacesPath, '{"folderWorkspaceInfos":{}}');
await service.initialize();
assertEqualFolderInfos(service.getFolderBackupPaths(), []);
fs.writeFileSync(backupWorkspacesPath, '{"folderWorkspaceInfos":{"foo": ["bar"]}}');
await service.initialize();
assertEqualFolderInfos(service.getFolderBackupPaths(), []);
fs.writeFileSync(backupWorkspacesPath, '{"folderWorkspaceInfos":{"foo": []}}');
await service.initialize();
assertEqualFolderInfos(service.getFolderBackupPaths(), []);
fs.writeFileSync(backupWorkspacesPath, '{"folderWorkspaceInfos":{"foo": "bar"}}');
await service.initialize();
assertEqualFolderInfos(service.getFolderBackupPaths(), []);
fs.writeFileSync(backupWorkspacesPath, '{"folderWorkspaceInfos":"foo"}');
await service.initialize();
assertEqualFolderInfos(service.getFolderBackupPaths(), []);
fs.writeFileSync(backupWorkspacesPath, '{"folderWorkspaceInfos":1}');
await service.initialize();
assertEqualFolderInfos(service.getFolderBackupPaths(), []);
});
test('getFolderBackupPaths() should migrate folderURIWorkspaces', async () => {
await ensureFolderExists(existingTestFolder1);
fs.writeFileSync(backupWorkspacesPath, JSON.stringify({ folderURIWorkspaces: [existingTestFolder1.toString()] }));
await service.initialize();
assertEqualFolderInfos(service.getFolderBackupPaths(), [toFolderBackupInfo(existingTestFolder1)]);
});
test('getFolderBackupPaths() should return [] when files.hotExit = "onExitAndWindowClose"', async () => {
const fi = toFolderBackupInfo(URI.file(fooFile.fsPath.toUpperCase()));
service.registerFolderBackupSync(fi);
assertEqualFolderInfos(service.getFolderBackupPaths(), [fi]);
configService.setUserConfiguration('files.hotExit', HotExitConfiguration.ON_EXIT_AND_WINDOW_CLOSE);
await service.initialize();
assertEqualFolderInfos(service.getFolderBackupPaths(), []);
});
test('getWorkspaceBackups() should return [] when workspaces.json doesn\'t exist', () => {
assert.deepStrictEqual(service.getWorkspaceBackups(), []);
});
test('getWorkspaceBackups() should return [] when workspaces.json is not properly formed JSON', async () => {
fs.writeFileSync(backupWorkspacesPath, '');
await service.initialize();
assert.deepStrictEqual(service.getWorkspaceBackups(), []);
fs.writeFileSync(backupWorkspacesPath, '{]');
await service.initialize();
assert.deepStrictEqual(service.getWorkspaceBackups(), []);
fs.writeFileSync(backupWorkspacesPath, 'foo');
await service.initialize();
assert.deepStrictEqual(service.getWorkspaceBackups(), []);
});
test('getWorkspaceBackups() should return [] when folderWorkspaces in workspaces.json is absent', async () => {
fs.writeFileSync(backupWorkspacesPath, '{}');
await service.initialize();
assert.deepStrictEqual(service.getWorkspaceBackups(), []);
});
test('getWorkspaceBackups() should return [] when rootWorkspaces in workspaces.json is not a object array', async () => {
fs.writeFileSync(backupWorkspacesPath, '{"rootWorkspaces":{}}');
await service.initialize();
assert.deepStrictEqual(service.getWorkspaceBackups(), []);
fs.writeFileSync(backupWorkspacesPath, '{"rootWorkspaces":{"foo": ["bar"]}}');
await service.initialize();
assert.deepStrictEqual(service.getWorkspaceBackups(), []);
fs.writeFileSync(backupWorkspacesPath, '{"rootWorkspaces":{"foo": []}}');
await service.initialize();
assert.deepStrictEqual(service.getWorkspaceBackups(), []);
fs.writeFileSync(backupWorkspacesPath, '{"rootWorkspaces":{"foo": "bar"}}');
await service.initialize();
assert.deepStrictEqual(service.getWorkspaceBackups(), []);
fs.writeFileSync(backupWorkspacesPath, '{"rootWorkspaces":"foo"}');
await service.initialize();
assert.deepStrictEqual(service.getWorkspaceBackups(), []);
fs.writeFileSync(backupWorkspacesPath, '{"rootWorkspaces":1}');
await service.initialize();
assert.deepStrictEqual(service.getWorkspaceBackups(), []);
});
test('getWorkspaceBackups() should return [] when rootURIWorkspaces in workspaces.json is not a object array', async () => {
fs.writeFileSync(backupWorkspacesPath, '{"rootURIWorkspaces":{}}');
await service.initialize();
assert.deepStrictEqual(service.getWorkspaceBackups(), []);
fs.writeFileSync(backupWorkspacesPath, '{"rootURIWorkspaces":{"foo": ["bar"]}}');
await service.initialize();
assert.deepStrictEqual(service.getWorkspaceBackups(), []);
fs.writeFileSync(backupWorkspacesPath, '{"rootURIWorkspaces":{"foo": []}}');
await service.initialize();
assert.deepStrictEqual(service.getWorkspaceBackups(), []);
fs.writeFileSync(backupWorkspacesPath, '{"rootURIWorkspaces":{"foo": "bar"}}');
await service.initialize();
assert.deepStrictEqual(service.getWorkspaceBackups(), []);
fs.writeFileSync(backupWorkspacesPath, '{"rootURIWorkspaces":"foo"}');
await service.initialize();
assert.deepStrictEqual(service.getWorkspaceBackups(), []);
fs.writeFileSync(backupWorkspacesPath, '{"rootURIWorkspaces":1}');
await service.initialize();
assert.deepStrictEqual(service.getWorkspaceBackups(), []);
});
test('getWorkspaceBackups() should return [] when files.hotExit = "onExitAndWindowClose"', async () => {
const upperFooPath = fooFile.fsPath.toUpperCase();
service.registerWorkspaceBackupSync(toWorkspaceBackupInfo(upperFooPath));
assert.strictEqual(service.getWorkspaceBackups().length, 1);
assert.deepStrictEqual(service.getWorkspaceBackups().map(r => r.workspace.configPath.toString()), [URI.file(upperFooPath).toString()]);
configService.setUserConfiguration('files.hotExit', HotExitConfiguration.ON_EXIT_AND_WINDOW_CLOSE);
await service.initialize();
assert.deepStrictEqual(service.getWorkspaceBackups(), []);
});
test('getEmptyWorkspaceBackupPaths() should return [] when workspaces.json doesn\'t exist', () => {
assert.deepStrictEqual(service.getEmptyWindowBackupPaths(), []);
});
test('getEmptyWorkspaceBackupPaths() should return [] when workspaces.json is not properly formed JSON', async () => {
fs.writeFileSync(backupWorkspacesPath, '');
await service.initialize();
assert.deepStrictEqual(service.getEmptyWindowBackupPaths(), []);
fs.writeFileSync(backupWorkspacesPath, '{]');
await service.initialize();
assert.deepStrictEqual(service.getEmptyWindowBackupPaths(), []);
fs.writeFileSync(backupWorkspacesPath, 'foo');
await service.initialize();
assert.deepStrictEqual(service.getEmptyWindowBackupPaths(), []);
});
test('getEmptyWorkspaceBackupPaths() should return [] when folderWorkspaces in workspaces.json is absent', async () => {
fs.writeFileSync(backupWorkspacesPath, '{}');
await service.initialize();
assert.deepStrictEqual(service.getEmptyWindowBackupPaths(), []);
});
test('getEmptyWorkspaceBackupPaths() should return [] when folderWorkspaces in workspaces.json is not a string array', async function () {
fs.writeFileSync(backupWorkspacesPath, '{"emptyWorkspaces":{}}');
await service.initialize();
assert.deepStrictEqual(service.getEmptyWindowBackupPaths(), []);
fs.writeFileSync(backupWorkspacesPath, '{"emptyWorkspaces":{"foo": ["bar"]}}');
await service.initialize();
assert.deepStrictEqual(service.getEmptyWindowBackupPaths(), []);
fs.writeFileSync(backupWorkspacesPath, '{"emptyWorkspaces":{"foo": []}}');
await service.initialize();
assert.deepStrictEqual(service.getEmptyWindowBackupPaths(), []);
fs.writeFileSync(backupWorkspacesPath, '{"emptyWorkspaces":{"foo": "bar"}}');
await service.initialize();
assert.deepStrictEqual(service.getEmptyWindowBackupPaths(), []);
fs.writeFileSync(backupWorkspacesPath, '{"emptyWorkspaces":"foo"}');
await service.initialize();
assert.deepStrictEqual(service.getEmptyWindowBackupPaths(), []);
fs.writeFileSync(backupWorkspacesPath, '{"emptyWorkspaces":1}');
await service.initialize();
assert.deepStrictEqual(service.getEmptyWindowBackupPaths(), []);
});
});
suite('dedupeFolderWorkspaces', () => {
test('should ignore duplicates (folder workspace)', async () => {
await ensureFolderExists(existingTestFolder1);
const workspacesJson: IBackupWorkspacesFormat = {
rootURIWorkspaces: [],
folderWorkspaceInfos: [{ folderUri: existingTestFolder1.toString() }, { folderUri: existingTestFolder1.toString() }],
emptyWorkspaceInfos: []
};
await pfs.Promises.writeFile(backupWorkspacesPath, JSON.stringify(workspacesJson));
await service.initialize();
const buffer = await pfs.Promises.readFile(backupWorkspacesPath, 'utf-8');
const json = <IBackupWorkspacesFormat>JSON.parse(buffer);
assert.deepStrictEqual(json.folderWorkspaceInfos, [{ folderUri: existingTestFolder1.toString() }]);
});
test('should ignore duplicates on Windows and Mac (folder workspace)', async () => {
await ensureFolderExists(existingTestFolder1);
const workspacesJson: IBackupWorkspacesFormat = {
rootURIWorkspaces: [],
folderWorkspaceInfos: [{ folderUri: existingTestFolder1.toString() }, { folderUri: existingTestFolder1.toString().toLowerCase() }],
emptyWorkspaceInfos: []
};
await pfs.Promises.writeFile(backupWorkspacesPath, JSON.stringify(workspacesJson));
await service.initialize();
const buffer = await pfs.Promises.readFile(backupWorkspacesPath, 'utf-8');
const json = <IBackupWorkspacesFormat>JSON.parse(buffer);
assert.deepStrictEqual(json.folderWorkspaceInfos, [{ folderUri: existingTestFolder1.toString() }]);
});
test('should ignore duplicates on Windows and Mac (root workspace)', async () => {
const workspacePath = path.join(testDir, 'Foo.code-workspace');
const workspacePath1 = path.join(testDir, 'FOO.code-workspace');
const workspacePath2 = path.join(testDir, 'foo.code-workspace');
const workspace1 = await ensureWorkspaceExists(toWorkspace(workspacePath));
const workspace2 = await ensureWorkspaceExists(toWorkspace(workspacePath1));
const workspace3 = await ensureWorkspaceExists(toWorkspace(workspacePath2));
const workspacesJson: IBackupWorkspacesFormat = {
rootURIWorkspaces: [workspace1, workspace2, workspace3].map(toSerializedWorkspace),
folderWorkspaceInfos: [],
emptyWorkspaceInfos: []
};
await pfs.Promises.writeFile(backupWorkspacesPath, JSON.stringify(workspacesJson));
await service.initialize();
const buffer = await pfs.Promises.readFile(backupWorkspacesPath, 'utf-8');
const json = <IBackupWorkspacesFormat>JSON.parse(buffer);
assert.strictEqual(json.rootURIWorkspaces.length, platform.isLinux ? 3 : 1);
if (platform.isLinux) {
assert.deepStrictEqual(json.rootURIWorkspaces.map(r => r.configURIPath), [URI.file(workspacePath).toString(), URI.file(workspacePath1).toString(), URI.file(workspacePath2).toString()]);
} else {
assert.deepStrictEqual(json.rootURIWorkspaces.map(r => r.configURIPath), [URI.file(workspacePath).toString()], 'should return the first duplicated entry');
}
});
});
suite('registerWindowForBackups', () => {
test('should persist paths to workspaces.json (folder workspace)', async () => {
service.registerFolderBackupSync(toFolderBackupInfo(fooFile));
service.registerFolderBackupSync(toFolderBackupInfo(barFile));
assertEqualFolderInfos(service.getFolderBackupPaths(), [toFolderBackupInfo(fooFile), toFolderBackupInfo(barFile)]);
const buffer = await pfs.Promises.readFile(backupWorkspacesPath, 'utf-8');
const json = <IBackupWorkspacesFormat>JSON.parse(buffer);
assert.deepStrictEqual(json.folderWorkspaceInfos, [{ folderUri: fooFile.toString() }, { folderUri: barFile.toString() }]);
});
test('should persist paths to workspaces.json (root workspace)', async () => {
const ws1 = toWorkspaceBackupInfo(fooFile.fsPath);
service.registerWorkspaceBackupSync(ws1);
const ws2 = toWorkspaceBackupInfo(barFile.fsPath);
service.registerWorkspaceBackupSync(ws2);
assert.deepStrictEqual(service.getWorkspaceBackups().map(b => b.workspace.configPath.toString()), [fooFile.toString(), barFile.toString()]);
assert.strictEqual(ws1.workspace.id, service.getWorkspaceBackups()[0].workspace.id);
assert.strictEqual(ws2.workspace.id, service.getWorkspaceBackups()[1].workspace.id);
const buffer = await pfs.Promises.readFile(backupWorkspacesPath, 'utf-8');
const json = <IBackupWorkspacesFormat>JSON.parse(buffer);
assert.deepStrictEqual(json.rootURIWorkspaces.map(b => b.configURIPath), [fooFile.toString(), barFile.toString()]);
assert.strictEqual(ws1.workspace.id, json.rootURIWorkspaces[0].id);
assert.strictEqual(ws2.workspace.id, json.rootURIWorkspaces[1].id);
});
});
test('should always store the workspace path in workspaces.json using the case given, regardless of whether the file system is case-sensitive (folder workspace)', async () => {
service.registerFolderBackupSync(toFolderBackupInfo(URI.file(fooFile.fsPath.toUpperCase())));
assertEqualFolderInfos(service.getFolderBackupPaths(), [toFolderBackupInfo(URI.file(fooFile.fsPath.toUpperCase()))]);
const buffer = await pfs.Promises.readFile(backupWorkspacesPath, 'utf-8');
const json = <IBackupWorkspacesFormat>JSON.parse(buffer);
assert.deepStrictEqual(json.folderWorkspaceInfos, [{ folderUri: URI.file(fooFile.fsPath.toUpperCase()).toString() }]);
});
test('should always store the workspace path in workspaces.json using the case given, regardless of whether the file system is case-sensitive (root workspace)', async () => {
const upperFooPath = fooFile.fsPath.toUpperCase();
service.registerWorkspaceBackupSync(toWorkspaceBackupInfo(upperFooPath));
assert.deepStrictEqual(service.getWorkspaceBackups().map(b => b.workspace.configPath.toString()), [URI.file(upperFooPath).toString()]);
const buffer = await pfs.Promises.readFile(backupWorkspacesPath, 'utf-8');
const json = (<IBackupWorkspacesFormat>JSON.parse(buffer));
assert.deepStrictEqual(json.rootURIWorkspaces.map(b => b.configURIPath), [URI.file(upperFooPath).toString()]);
});
suite('removeBackupPathSync', () => {
test('should remove folder workspaces from workspaces.json (folder workspace)', async () => {
service.registerFolderBackupSync(toFolderBackupInfo(fooFile));
service.registerFolderBackupSync(toFolderBackupInfo(barFile));
service.unregisterFolderBackupSync(fooFile);
const buffer = await pfs.Promises.readFile(backupWorkspacesPath, 'utf-8');
const json = (<IBackupWorkspacesFormat>JSON.parse(buffer));
assert.deepStrictEqual(json.folderWorkspaceInfos, [{ folderUri: barFile.toString() }]);
service.unregisterFolderBackupSync(barFile);
const content = await pfs.Promises.readFile(backupWorkspacesPath, 'utf-8');
const json2 = (<IBackupWorkspacesFormat>JSON.parse(content));
assert.deepStrictEqual(json2.folderWorkspaceInfos, []);
});
test('should remove folder workspaces from workspaces.json (root workspace)', async () => {
const ws1 = toWorkspaceBackupInfo(fooFile.fsPath);
service.registerWorkspaceBackupSync(ws1);
const ws2 = toWorkspaceBackupInfo(barFile.fsPath);
service.registerWorkspaceBackupSync(ws2);
service.unregisterWorkspaceBackupSync(ws1.workspace);
const buffer = await pfs.Promises.readFile(backupWorkspacesPath, 'utf-8');
const json = (<IBackupWorkspacesFormat>JSON.parse(buffer));
assert.deepStrictEqual(json.rootURIWorkspaces.map(r => r.configURIPath), [barFile.toString()]);
service.unregisterWorkspaceBackupSync(ws2.workspace);
const content = await pfs.Promises.readFile(backupWorkspacesPath, 'utf-8');
const json2 = (<IBackupWorkspacesFormat>JSON.parse(content));
assert.deepStrictEqual(json2.rootURIWorkspaces, []);
});
test('should remove empty workspaces from workspaces.json', async () => {
service.registerEmptyWindowBackupSync('foo');
service.registerEmptyWindowBackupSync('bar');
service.unregisterEmptyWindowBackupSync('foo');
const buffer = await pfs.Promises.readFile(backupWorkspacesPath, 'utf-8');
const json = (<IBackupWorkspacesFormat>JSON.parse(buffer));
assert.deepStrictEqual(json.emptyWorkspaceInfos, [{ backupFolder: 'bar' }]);
service.unregisterEmptyWindowBackupSync('bar');
const content = await pfs.Promises.readFile(backupWorkspacesPath, 'utf-8');
const json2 = (<IBackupWorkspacesFormat>JSON.parse(content));
assert.deepStrictEqual(json2.emptyWorkspaceInfos, []);
});
test('should fail gracefully when removing a path that doesn\'t exist', async () => {
await ensureFolderExists(existingTestFolder1); // make sure backup folder exists, so the folder is not removed on loadSync
const workspacesJson: IBackupWorkspacesFormat = { rootURIWorkspaces: [], folderWorkspaceInfos: [{ folderUri: existingTestFolder1.toString() }], emptyWorkspaceInfos: [] };
await pfs.Promises.writeFile(backupWorkspacesPath, JSON.stringify(workspacesJson));
await service.initialize();
service.unregisterFolderBackupSync(barFile);
service.unregisterEmptyWindowBackupSync('test');
const content = await pfs.Promises.readFile(backupWorkspacesPath, 'utf-8');
const json = (<IBackupWorkspacesFormat>JSON.parse(content));
assert.deepStrictEqual(json.folderWorkspaceInfos, [{ folderUri: existingTestFolder1.toString() }]);
});
});
suite('getWorkspaceHash', () => {
(platform.isLinux ? test.skip : test)('should ignore case on Windows and Mac', () => {
const assertFolderHash = (uri1: URI, uri2: URI) => {
assert.strictEqual(service.getFolderHash(toFolderBackupInfo(uri1)), service.getFolderHash(toFolderBackupInfo(uri2)));
};
if (platform.isMacintosh) {
assertFolderHash(URI.file('/foo'), URI.file('/FOO'));
}
if (platform.isWindows) {
assertFolderHash(URI.file('c:\\foo'), URI.file('C:\\FOO'));
}
});
});
suite('mixed path casing', () => {
test('should handle case insensitive paths properly (registerWindowForBackupsSync) (folder workspace)', () => {
service.registerFolderBackupSync(toFolderBackupInfo(fooFile));
service.registerFolderBackupSync(toFolderBackupInfo(URI.file(fooFile.fsPath.toUpperCase())));
if (platform.isLinux) {
assert.strictEqual(service.getFolderBackupPaths().length, 2);
} else {
assert.strictEqual(service.getFolderBackupPaths().length, 1);
}
});
test('should handle case insensitive paths properly (registerWindowForBackupsSync) (root workspace)', () => {
service.registerWorkspaceBackupSync(toWorkspaceBackupInfo(fooFile.fsPath));
service.registerWorkspaceBackupSync(toWorkspaceBackupInfo(fooFile.fsPath.toUpperCase()));
if (platform.isLinux) {
assert.strictEqual(service.getWorkspaceBackups().length, 2);
} else {
assert.strictEqual(service.getWorkspaceBackups().length, 1);
}
});
test('should handle case insensitive paths properly (removeBackupPathSync) (folder workspace)', () => {
// same case
service.registerFolderBackupSync(toFolderBackupInfo(fooFile));
service.unregisterFolderBackupSync(fooFile);
assert.strictEqual(service.getFolderBackupPaths().length, 0);
// mixed case
service.registerFolderBackupSync(toFolderBackupInfo(fooFile));
service.unregisterFolderBackupSync(URI.file(fooFile.fsPath.toUpperCase()));
if (platform.isLinux) {
assert.strictEqual(service.getFolderBackupPaths().length, 1);
} else {
assert.strictEqual(service.getFolderBackupPaths().length, 0);
}
});
});
suite('getDirtyWorkspaces', () => {
test('should report if a workspace or folder has backups', async () => {
const folderBackupPath = service.registerFolderBackupSync(toFolderBackupInfo(fooFile));
const backupWorkspaceInfo = toWorkspaceBackupInfo(fooFile.fsPath);
const workspaceBackupPath = service.registerWorkspaceBackupSync(backupWorkspaceInfo);
assert.strictEqual(((await service.getDirtyWorkspaces()).length), 0);
try {
await pfs.Promises.mkdir(path.join(folderBackupPath, Schemas.file), { recursive: true });
await pfs.Promises.mkdir(path.join(workspaceBackupPath, Schemas.untitled), { recursive: true });
} catch (error) {
// ignore - folder might exist already
}
assert.strictEqual(((await service.getDirtyWorkspaces()).length), 0);
fs.writeFileSync(path.join(folderBackupPath, Schemas.file, '594a4a9d82a277a899d4713a5b08f504'), '');
fs.writeFileSync(path.join(workspaceBackupPath, Schemas.untitled, '594a4a9d82a277a899d4713a5b08f504'), '');
const dirtyWorkspaces = await service.getDirtyWorkspaces();
assert.strictEqual(dirtyWorkspaces.length, 2);
let found = 0;
for (const dirtyWorkpspace of dirtyWorkspaces) {
if (isFolderBackupInfo(dirtyWorkpspace)) {
if (isEqual(fooFile, dirtyWorkpspace.folderUri)) {
found++;
}
} else {
if (isEqual(backupWorkspaceInfo.workspace.configPath, dirtyWorkpspace.workspace.configPath)) {
found++;
}
}
}
assert.strictEqual(found, 2);
});
});
});
|