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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
|
/*---------------------------------------------------------------------------------------------
* 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 { NativeWorkbenchEnvironmentService } from 'vs/workbench/services/environment/electron-sandbox/environmentService';
import { TestNativePathService, TestNativeWindowConfiguration } from 'vs/workbench/test/electron-browser/workbenchTestServices';
import { TestContextService, TestProductService, TestStorageService, TestWorkingCopy } from 'vs/workbench/test/common/workbenchTestServices';
import { NullLogService } from 'vs/platform/log/common/log';
import { FileService } from 'vs/platform/files/common/fileService';
import { DiskFileSystemProvider } from 'vs/platform/files/node/diskFileSystemProvider';
import { Schemas } from 'vs/base/common/network';
import { flakySuite, getRandomTestPath } from 'vs/base/test/node/testUtils';
import { tmpdir } from 'os';
import { dirname, join } from 'vs/base/common/path';
import { Promises } from 'vs/base/node/pfs';
import { URI } from 'vs/base/common/uri';
import { CancellationToken, CancellationTokenSource } from 'vs/base/common/cancellation';
import { existsSync, readFileSync, unlinkSync } from 'fs';
import { IWorkingCopyHistoryEntry, IWorkingCopyHistoryEntryDescriptor, IWorkingCopyHistoryEvent } from 'vs/workbench/services/workingCopy/common/workingCopyHistory';
import { IFileService } from 'vs/platform/files/common/files';
import { UriIdentityService } from 'vs/platform/uriIdentity/common/uriIdentityService';
import { LabelService } from 'vs/workbench/services/label/common/labelService';
import { TestLifecycleService, TestRemoteAgentService, TestWillShutdownEvent } from 'vs/workbench/test/browser/workbenchTestServices';
import { TestConfigurationService } from 'vs/platform/configuration/test/common/testConfigurationService';
import { NativeWorkingCopyHistoryService } from 'vs/workbench/services/workingCopy/electron-sandbox/workingCopyHistoryService';
import { joinPath, dirname as resourcesDirname, basename } from 'vs/base/common/resources';
import { firstOrDefault } from 'vs/base/common/arrays';
class TestWorkbenchEnvironmentService extends NativeWorkbenchEnvironmentService {
constructor(private readonly testDir: URI | string) {
super({ ...TestNativeWindowConfiguration, 'user-data-dir': URI.isUri(testDir) ? testDir.fsPath : testDir }, TestProductService);
}
override get localHistoryHome() {
return joinPath(URI.isUri(this.testDir) ? this.testDir : URI.file(this.testDir), 'History');
}
}
export class TestWorkingCopyHistoryService extends NativeWorkingCopyHistoryService {
readonly _fileService: IFileService;
readonly _configurationService: TestConfigurationService;
readonly _lifecycleService: TestLifecycleService;
constructor(testDir: URI | string) {
const environmentService = new TestWorkbenchEnvironmentService(testDir);
const logService = new NullLogService();
const fileService = new FileService(logService);
const diskFileSystemProvider = new DiskFileSystemProvider(logService);
fileService.registerProvider(Schemas.file, diskFileSystemProvider);
const remoteAgentService = new TestRemoteAgentService();
const uriIdentityService = new UriIdentityService(fileService);
const labelService = new LabelService(environmentService, new TestContextService(), new TestNativePathService(), new TestRemoteAgentService(), new TestStorageService(), new TestLifecycleService());
const lifecycleService = new TestLifecycleService();
const configurationService = new TestConfigurationService();
super(fileService, remoteAgentService, environmentService, uriIdentityService, labelService, lifecycleService, logService, configurationService);
this._fileService = fileService;
this._configurationService = configurationService;
this._lifecycleService = lifecycleService;
}
}
flakySuite('WorkingCopyHistoryService', () => {
let testDir: string;
let historyHome: string;
let workHome: string;
let service: TestWorkingCopyHistoryService;
let testFile1Path: string;
let testFile2Path: string;
let testFile3Path: string;
const testFile1PathContents = 'Hello Foo';
const testFile2PathContents = [
'Lorem ipsum ',
'dolor öäü sit amet ',
'adipiscing ßß elit',
'consectetur '
].join('');
const testFile3PathContents = 'Hello Bar';
setup(async () => {
testDir = getRandomTestPath(tmpdir(), 'vsctests', 'workingcopyhistoryservice');
historyHome = join(testDir, 'User', 'History');
workHome = join(testDir, 'work');
service = new TestWorkingCopyHistoryService(testDir);
await Promises.mkdir(historyHome, { recursive: true });
await Promises.mkdir(workHome, { recursive: true });
testFile1Path = join(workHome, 'foo.txt');
testFile2Path = join(workHome, 'bar.txt');
testFile3Path = join(workHome, 'foo-bar.txt');
await Promises.writeFile(testFile1Path, testFile1PathContents);
await Promises.writeFile(testFile2Path, testFile2PathContents);
await Promises.writeFile(testFile3Path, testFile3PathContents);
});
let increasingTimestampCounter = 1;
async function addEntry(descriptor: IWorkingCopyHistoryEntryDescriptor, token: CancellationToken, expectEntryAdded?: boolean): Promise<IWorkingCopyHistoryEntry>;
async function addEntry(descriptor: IWorkingCopyHistoryEntryDescriptor, token: CancellationToken, expectEntryAdded: false): Promise<IWorkingCopyHistoryEntry | undefined>;
async function addEntry(descriptor: IWorkingCopyHistoryEntryDescriptor, token: CancellationToken, expectEntryAdded = true): Promise<IWorkingCopyHistoryEntry | undefined> {
const entry = await service.addEntry({
...descriptor,
timestamp: increasingTimestampCounter++ // very important to get tests to not be flaky with stable sort order
}, token);
if (expectEntryAdded) {
assert.ok(entry, 'Unexpected undefined local history entry');
assert.strictEqual(existsSync(entry.location.fsPath), true, 'Unexpected local history not stored on disk');
}
return entry;
}
teardown(() => {
service.dispose();
return Promises.rm(testDir);
});
test('addEntry', async () => {
const addEvents: IWorkingCopyHistoryEvent[] = [];
service.onDidAddEntry(e => addEvents.push(e));
const workingCopy1 = new TestWorkingCopy(URI.file(testFile1Path));
const workingCopy2 = new TestWorkingCopy(URI.file(testFile2Path));
// Add Entry works
const entry1A = await addEntry({ resource: workingCopy1.resource }, CancellationToken.None);
const entry2A = await addEntry({ resource: workingCopy2.resource, source: 'My Source' }, CancellationToken.None);
assert.strictEqual(readFileSync(entry1A.location.fsPath).toString(), testFile1PathContents);
assert.strictEqual(readFileSync(entry2A.location.fsPath).toString(), testFile2PathContents);
assert.strictEqual(addEvents.length, 2);
assert.strictEqual(addEvents[0].entry.workingCopy.resource.toString(), workingCopy1.resource.toString());
assert.strictEqual(addEvents[1].entry.workingCopy.resource.toString(), workingCopy2.resource.toString());
assert.strictEqual(addEvents[1].entry.source, 'My Source');
const entry1B = await addEntry({ resource: workingCopy1.resource }, CancellationToken.None);
const entry2B = await addEntry({ resource: workingCopy2.resource }, CancellationToken.None);
assert.strictEqual(readFileSync(entry1B.location.fsPath).toString(), testFile1PathContents);
assert.strictEqual(readFileSync(entry2B.location.fsPath).toString(), testFile2PathContents);
assert.strictEqual(addEvents.length, 4);
assert.strictEqual(addEvents[2].entry.workingCopy.resource.toString(), workingCopy1.resource.toString());
assert.strictEqual(addEvents[3].entry.workingCopy.resource.toString(), workingCopy2.resource.toString());
// Cancellation works
const cts = new CancellationTokenSource();
const entry1CPromise = addEntry({ resource: workingCopy1.resource }, cts.token, false);
cts.dispose(true);
const entry1C = await entry1CPromise;
assert.ok(!entry1C);
assert.strictEqual(addEvents.length, 4);
// Invalid working copies are ignored
const workingCopy3 = new TestWorkingCopy(URI.file(testFile2Path).with({ scheme: 'unsupported' }));
const entry3A = await addEntry({ resource: workingCopy3.resource }, CancellationToken.None, false);
assert.ok(!entry3A);
assert.strictEqual(addEvents.length, 4);
});
test('renameEntry', async () => {
const changeEvents: IWorkingCopyHistoryEvent[] = [];
service.onDidChangeEntry(e => changeEvents.push(e));
const workingCopy1 = new TestWorkingCopy(URI.file(testFile1Path));
const entry = await addEntry({ resource: workingCopy1.resource }, CancellationToken.None);
await addEntry({ resource: workingCopy1.resource }, CancellationToken.None);
await addEntry({ resource: workingCopy1.resource, source: 'My Source' }, CancellationToken.None);
let entries = await service.getEntries(workingCopy1.resource, CancellationToken.None);
assert.strictEqual(entries.length, 3);
await service.updateEntry(entry, { source: 'Hello Rename' }, CancellationToken.None);
assert.strictEqual(changeEvents.length, 1);
assert.strictEqual(changeEvents[0].entry, entry);
entries = await service.getEntries(workingCopy1.resource, CancellationToken.None);
assert.strictEqual(entries[0].source, 'Hello Rename');
// Simulate shutdown
const event = new TestWillShutdownEvent();
service._lifecycleService.fireWillShutdown(event);
await Promise.allSettled(event.value);
// Resolve from disk fresh and verify again
service.dispose();
service = new TestWorkingCopyHistoryService(testDir);
entries = await service.getEntries(workingCopy1.resource, CancellationToken.None);
assert.strictEqual(entries.length, 3);
assert.strictEqual(entries[0].source, 'Hello Rename');
});
test('removeEntry', async () => {
const removeEvents: IWorkingCopyHistoryEvent[] = [];
service.onDidRemoveEntry(e => removeEvents.push(e));
const workingCopy1 = new TestWorkingCopy(URI.file(testFile1Path));
await addEntry({ resource: workingCopy1.resource }, CancellationToken.None);
const entry2 = await addEntry({ resource: workingCopy1.resource }, CancellationToken.None);
await addEntry({ resource: workingCopy1.resource }, CancellationToken.None);
await addEntry({ resource: workingCopy1.resource, source: 'My Source' }, CancellationToken.None);
let entries = await service.getEntries(workingCopy1.resource, CancellationToken.None);
assert.strictEqual(entries.length, 4);
let removed = await service.removeEntry(entry2, CancellationToken.None);
assert.strictEqual(removed, true);
assert.strictEqual(removeEvents.length, 1);
assert.strictEqual(removeEvents[0].entry, entry2);
// Cannot remove same entry again
removed = await service.removeEntry(entry2, CancellationToken.None);
assert.strictEqual(removed, false);
entries = await service.getEntries(workingCopy1.resource, CancellationToken.None);
assert.strictEqual(entries.length, 3);
// Simulate shutdown
const event = new TestWillShutdownEvent();
service._lifecycleService.fireWillShutdown(event);
await Promise.allSettled(event.value);
// Resolve from disk fresh and verify again
service.dispose();
service = new TestWorkingCopyHistoryService(testDir);
entries = await service.getEntries(workingCopy1.resource, CancellationToken.None);
assert.strictEqual(entries.length, 3);
});
test('removeEntry - deletes history entries folder when last entry removed', async () => {
const workingCopy1 = new TestWorkingCopy(URI.file(testFile1Path));
let entry = await addEntry({ resource: workingCopy1.resource }, CancellationToken.None);
// Simulate shutdown
let event = new TestWillShutdownEvent();
service._lifecycleService.fireWillShutdown(event);
await Promise.allSettled(event.value);
// Resolve from disk fresh and verify again
service.dispose();
service = new TestWorkingCopyHistoryService(testDir);
assert.strictEqual(existsSync(dirname(entry.location.fsPath)), true);
entry = firstOrDefault(await service.getEntries(workingCopy1.resource, CancellationToken.None))!;
assert.ok(entry);
await service.removeEntry(entry, CancellationToken.None);
// Simulate shutdown
event = new TestWillShutdownEvent();
service._lifecycleService.fireWillShutdown(event);
await Promise.allSettled(event.value);
// Resolve from disk fresh and verify again
service.dispose();
service = new TestWorkingCopyHistoryService(testDir);
assert.strictEqual(existsSync(dirname(entry.location.fsPath)), false);
});
test('removeAll', async () => {
let removed = false;
service.onDidRemoveEntries(() => removed = true);
const workingCopy1 = new TestWorkingCopy(URI.file(testFile1Path));
const workingCopy2 = new TestWorkingCopy(URI.file(testFile2Path));
await addEntry({ resource: workingCopy1.resource }, CancellationToken.None);
await addEntry({ resource: workingCopy1.resource }, CancellationToken.None);
await addEntry({ resource: workingCopy2.resource }, CancellationToken.None);
await addEntry({ resource: workingCopy2.resource, source: 'My Source' }, CancellationToken.None);
let entries = await service.getEntries(workingCopy1.resource, CancellationToken.None);
assert.strictEqual(entries.length, 2);
entries = await service.getEntries(workingCopy2.resource, CancellationToken.None);
assert.strictEqual(entries.length, 2);
await service.removeAll(CancellationToken.None);
assert.strictEqual(removed, true);
entries = await service.getEntries(workingCopy1.resource, CancellationToken.None);
assert.strictEqual(entries.length, 0);
entries = await service.getEntries(workingCopy2.resource, CancellationToken.None);
assert.strictEqual(entries.length, 0);
// Simulate shutdown
const event = new TestWillShutdownEvent();
service._lifecycleService.fireWillShutdown(event);
await Promise.allSettled(event.value);
// Resolve from disk fresh and verify again
service.dispose();
service = new TestWorkingCopyHistoryService(testDir);
entries = await service.getEntries(workingCopy1.resource, CancellationToken.None);
assert.strictEqual(entries.length, 0);
entries = await service.getEntries(workingCopy2.resource, CancellationToken.None);
assert.strictEqual(entries.length, 0);
});
test('getEntries - simple', async () => {
const workingCopy1 = new TestWorkingCopy(URI.file(testFile1Path));
const workingCopy2 = new TestWorkingCopy(URI.file(testFile2Path));
let entries = await service.getEntries(workingCopy1.resource, CancellationToken.None);
assert.strictEqual(entries.length, 0);
const entry1 = await addEntry({ resource: workingCopy1.resource, source: 'test-source' }, CancellationToken.None);
entries = await service.getEntries(workingCopy1.resource, CancellationToken.None);
assert.strictEqual(entries.length, 1);
assertEntryEqual(entries[0], entry1);
const entry2 = await addEntry({ resource: workingCopy1.resource, source: 'test-source' }, CancellationToken.None);
entries = await service.getEntries(workingCopy1.resource, CancellationToken.None);
assert.strictEqual(entries.length, 2);
assertEntryEqual(entries[1], entry2);
entries = await service.getEntries(workingCopy2.resource, CancellationToken.None);
assert.strictEqual(entries.length, 0);
const entry3 = await addEntry({ resource: workingCopy2.resource, source: 'other-test-source' }, CancellationToken.None);
entries = await service.getEntries(workingCopy2.resource, CancellationToken.None);
assert.strictEqual(entries.length, 1);
assertEntryEqual(entries[0], entry3);
});
test('getEntries - metadata preserved when stored', async () => {
const workingCopy1 = new TestWorkingCopy(URI.file(testFile1Path));
const workingCopy2 = new TestWorkingCopy(URI.file(testFile2Path));
const entry1 = await addEntry({ resource: workingCopy1.resource, source: 'test-source' }, CancellationToken.None);
const entry2 = await addEntry({ resource: workingCopy2.resource }, CancellationToken.None);
const entry3 = await addEntry({ resource: workingCopy2.resource, source: 'other-source' }, CancellationToken.None);
// Simulate shutdown
const event = new TestWillShutdownEvent();
service._lifecycleService.fireWillShutdown(event);
await Promise.allSettled(event.value);
// Resolve from disk fresh and verify again
service.dispose();
service = new TestWorkingCopyHistoryService(testDir);
let entries = await service.getEntries(workingCopy1.resource, CancellationToken.None);
assert.strictEqual(entries.length, 1);
assertEntryEqual(entries[0], entry1);
entries = await service.getEntries(workingCopy2.resource, CancellationToken.None);
assert.strictEqual(entries.length, 2);
assertEntryEqual(entries[0], entry2);
assertEntryEqual(entries[1], entry3);
});
test('getEntries - corrupt meta.json is no problem', async () => {
const workingCopy1 = new TestWorkingCopy(URI.file(testFile1Path));
const entry1 = await addEntry({ resource: workingCopy1.resource }, CancellationToken.None);
// Simulate shutdown
const event = new TestWillShutdownEvent();
service._lifecycleService.fireWillShutdown(event);
await Promise.allSettled(event.value);
// Resolve from disk fresh and verify again
service.dispose();
service = new TestWorkingCopyHistoryService(testDir);
const metaFile = join(dirname(entry1.location.fsPath), 'entries.json');
assert.ok(existsSync(metaFile));
unlinkSync(metaFile);
const entries = await service.getEntries(workingCopy1.resource, CancellationToken.None);
assert.strictEqual(entries.length, 1);
assertEntryEqual(entries[0], entry1, false /* skip timestamp that is unreliable when entries.json is gone */);
});
test('getEntries - missing entries from meta.json is no problem', async () => {
const workingCopy1 = new TestWorkingCopy(URI.file(testFile1Path));
const entry1 = await addEntry({ resource: workingCopy1.resource }, CancellationToken.None);
const entry2 = await addEntry({ resource: workingCopy1.resource }, CancellationToken.None);
// Simulate shutdown
const event = new TestWillShutdownEvent();
service._lifecycleService.fireWillShutdown(event);
await Promise.allSettled(event.value);
// Resolve from disk fresh and verify again
service.dispose();
service = new TestWorkingCopyHistoryService(testDir);
unlinkSync(entry1.location.fsPath);
const entries = await service.getEntries(workingCopy1.resource, CancellationToken.None);
assert.strictEqual(entries.length, 1);
assertEntryEqual(entries[0], entry2);
});
test('getEntries - in-memory and on-disk entries are merged', async () => {
const workingCopy1 = new TestWorkingCopy(URI.file(testFile1Path));
const entry1 = await addEntry({ resource: workingCopy1.resource, source: 'test-source' }, CancellationToken.None);
const entry2 = await addEntry({ resource: workingCopy1.resource, source: 'other-source' }, CancellationToken.None);
// Simulate shutdown
const event = new TestWillShutdownEvent();
service._lifecycleService.fireWillShutdown(event);
await Promise.allSettled(event.value);
// Resolve from disk fresh and verify again
service.dispose();
service = new TestWorkingCopyHistoryService(testDir);
const entry3 = await addEntry({ resource: workingCopy1.resource, source: 'test-source' }, CancellationToken.None);
const entry4 = await addEntry({ resource: workingCopy1.resource, source: 'other-source' }, CancellationToken.None);
const entries = await service.getEntries(workingCopy1.resource, CancellationToken.None);
assert.strictEqual(entries.length, 4);
assertEntryEqual(entries[0], entry1);
assertEntryEqual(entries[1], entry2);
assertEntryEqual(entries[2], entry3);
assertEntryEqual(entries[3], entry4);
});
test('getEntries - configured max entries respected', async () => {
const workingCopy1 = new TestWorkingCopy(URI.file(testFile1Path));
await addEntry({ resource: workingCopy1.resource }, CancellationToken.None);
await addEntry({ resource: workingCopy1.resource }, CancellationToken.None);
const entry3 = await addEntry({ resource: workingCopy1.resource, source: 'Test source' }, CancellationToken.None);
const entry4 = await addEntry({ resource: workingCopy1.resource }, CancellationToken.None);
service._configurationService.setUserConfiguration('workbench.localHistory.maxFileEntries', 2);
let entries = await service.getEntries(workingCopy1.resource, CancellationToken.None);
assert.strictEqual(entries.length, 2);
assertEntryEqual(entries[0], entry3);
assertEntryEqual(entries[1], entry4);
service._configurationService.setUserConfiguration('workbench.localHistory.maxFileEntries', 4);
entries = await service.getEntries(workingCopy1.resource, CancellationToken.None);
assert.strictEqual(entries.length, 4);
service._configurationService.setUserConfiguration('workbench.localHistory.maxFileEntries', 5);
entries = await service.getEntries(workingCopy1.resource, CancellationToken.None);
assert.strictEqual(entries.length, 4);
});
test('getAll', async () => {
const workingCopy1 = new TestWorkingCopy(URI.file(testFile1Path));
const workingCopy2 = new TestWorkingCopy(URI.file(testFile2Path));
let resources = await service.getAll(CancellationToken.None);
assert.strictEqual(resources.length, 0);
await addEntry({ resource: workingCopy1.resource, source: 'test-source' }, CancellationToken.None);
await addEntry({ resource: workingCopy1.resource, source: 'test-source' }, CancellationToken.None);
await addEntry({ resource: workingCopy2.resource, source: 'test-source' }, CancellationToken.None);
await addEntry({ resource: workingCopy2.resource, source: 'test-source' }, CancellationToken.None);
resources = await service.getAll(CancellationToken.None);
assert.strictEqual(resources.length, 2);
for (const resource of resources) {
if (resource.toString() !== workingCopy1.resource.toString() && resource.toString() !== workingCopy2.resource.toString()) {
assert.fail(`Unexpected history resource: ${resource.toString()}`);
}
}
// Simulate shutdown
const event = new TestWillShutdownEvent();
service._lifecycleService.fireWillShutdown(event);
await Promise.allSettled(event.value);
// Resolve from disk fresh and verify again
service.dispose();
service = new TestWorkingCopyHistoryService(testDir);
const workingCopy3 = new TestWorkingCopy(URI.file(testFile3Path));
await addEntry({ resource: workingCopy3.resource, source: 'test-source' }, CancellationToken.None);
resources = await service.getAll(CancellationToken.None);
assert.strictEqual(resources.length, 3);
for (const resource of resources) {
if (resource.toString() !== workingCopy1.resource.toString() && resource.toString() !== workingCopy2.resource.toString() && resource.toString() !== workingCopy3.resource.toString()) {
assert.fail(`Unexpected history resource: ${resource.toString()}`);
}
}
});
test('getAll - ignores resource when no entries exist', async () => {
const workingCopy1 = new TestWorkingCopy(URI.file(testFile1Path));
const entry = await addEntry({ resource: workingCopy1.resource, source: 'test-source' }, CancellationToken.None);
let resources = await service.getAll(CancellationToken.None);
assert.strictEqual(resources.length, 1);
await service.removeEntry(entry, CancellationToken.None);
resources = await service.getAll(CancellationToken.None);
assert.strictEqual(resources.length, 0);
// Simulate shutdown
const event = new TestWillShutdownEvent();
service._lifecycleService.fireWillShutdown(event);
await Promise.allSettled(event.value);
// Resolve from disk fresh and verify again
service.dispose();
service = new TestWorkingCopyHistoryService(testDir);
resources = await service.getAll(CancellationToken.None);
assert.strictEqual(resources.length, 0);
});
function assertEntryEqual(entryA: IWorkingCopyHistoryEntry, entryB: IWorkingCopyHistoryEntry, assertTimestamp = true): void {
assert.strictEqual(entryA.id, entryB.id);
assert.strictEqual(entryA.location.toString(), entryB.location.toString());
if (assertTimestamp) {
assert.strictEqual(entryA.timestamp, entryB.timestamp);
}
assert.strictEqual(entryA.source, entryB.source);
assert.strictEqual(entryA.workingCopy.name, entryB.workingCopy.name);
assert.strictEqual(entryA.workingCopy.resource.toString(), entryB.workingCopy.resource.toString());
}
test('entries cleaned up on shutdown', async () => {
const workingCopy1 = new TestWorkingCopy(URI.file(testFile1Path));
const entry1 = await addEntry({ resource: workingCopy1.resource, source: 'test-source' }, CancellationToken.None);
const entry2 = await addEntry({ resource: workingCopy1.resource, source: 'other-source' }, CancellationToken.None);
const entry3 = await addEntry({ resource: workingCopy1.resource, source: 'other-source' }, CancellationToken.None);
const entry4 = await addEntry({ resource: workingCopy1.resource, source: 'other-source' }, CancellationToken.None);
service._configurationService.setUserConfiguration('workbench.localHistory.maxFileEntries', 2);
// Simulate shutdown
let event = new TestWillShutdownEvent();
service._lifecycleService.fireWillShutdown(event);
await Promise.allSettled(event.value);
assert.ok(!existsSync(entry1.location.fsPath));
assert.ok(!existsSync(entry2.location.fsPath));
assert.ok(existsSync(entry3.location.fsPath));
assert.ok(existsSync(entry4.location.fsPath));
// Resolve from disk fresh and verify again
service.dispose();
service = new TestWorkingCopyHistoryService(testDir);
let entries = await service.getEntries(workingCopy1.resource, CancellationToken.None);
assert.strictEqual(entries.length, 2);
assertEntryEqual(entries[0], entry3);
assertEntryEqual(entries[1], entry4);
service._configurationService.setUserConfiguration('workbench.localHistory.maxFileEntries', 3);
const entry5 = await addEntry({ resource: workingCopy1.resource, source: 'other-source' }, CancellationToken.None);
// Simulate shutdown
event = new TestWillShutdownEvent();
service._lifecycleService.fireWillShutdown(event);
await Promise.allSettled(event.value);
assert.ok(existsSync(entry3.location.fsPath));
assert.ok(existsSync(entry4.location.fsPath));
assert.ok(existsSync(entry5.location.fsPath));
// Resolve from disk fresh and verify again
service.dispose();
service = new TestWorkingCopyHistoryService(testDir);
entries = await service.getEntries(workingCopy1.resource, CancellationToken.None);
assert.strictEqual(entries.length, 3);
assertEntryEqual(entries[0], entry3);
assertEntryEqual(entries[1], entry4);
assertEntryEqual(entries[2], entry5);
});
test('entries are merged when source is same', async () => {
let replaced: IWorkingCopyHistoryEntry | undefined = undefined;
service.onDidReplaceEntry(e => replaced = e.entry);
const workingCopy1 = new TestWorkingCopy(URI.file(testFile1Path));
service._configurationService.setUserConfiguration('workbench.localHistory.mergeWindow', 1);
const entry1 = await addEntry({ resource: workingCopy1.resource, source: 'test-source' }, CancellationToken.None);
assert.strictEqual(replaced, undefined);
const entry2 = await addEntry({ resource: workingCopy1.resource, source: 'test-source' }, CancellationToken.None);
assert.strictEqual(replaced, entry1);
const entry3 = await addEntry({ resource: workingCopy1.resource, source: 'test-source' }, CancellationToken.None);
assert.strictEqual(replaced, entry2);
let entries = await service.getEntries(workingCopy1.resource, CancellationToken.None);
assert.strictEqual(entries.length, 1);
assertEntryEqual(entries[0], entry3);
service._configurationService.setUserConfiguration('workbench.localHistory.mergeWindow', undefined);
await addEntry({ resource: workingCopy1.resource, source: 'test-source' }, CancellationToken.None);
await addEntry({ resource: workingCopy1.resource, source: 'test-source' }, CancellationToken.None);
entries = await service.getEntries(workingCopy1.resource, CancellationToken.None);
assert.strictEqual(entries.length, 3);
});
test('move entries (file rename)', async () => {
const workingCopy = new TestWorkingCopy(URI.file(testFile1Path));
const entry1 = await addEntry({ resource: workingCopy.resource, source: 'test-source' }, CancellationToken.None);
const entry2 = await addEntry({ resource: workingCopy.resource, source: 'test-source' }, CancellationToken.None);
const entry3 = await addEntry({ resource: workingCopy.resource, source: 'test-source' }, CancellationToken.None);
let entries = await service.getEntries(workingCopy.resource, CancellationToken.None);
assert.strictEqual(entries.length, 3);
const renamedWorkingCopyResource = joinPath(resourcesDirname(workingCopy.resource), 'renamed.txt');
await service._fileService.move(workingCopy.resource, renamedWorkingCopyResource);
const result = await service.moveEntries(workingCopy.resource, renamedWorkingCopyResource);
assert.strictEqual(result.length, 1);
assert.strictEqual(result[0].toString(), renamedWorkingCopyResource.toString());
entries = await service.getEntries(workingCopy.resource, CancellationToken.None);
assert.strictEqual(entries.length, 0);
entries = await service.getEntries(renamedWorkingCopyResource, CancellationToken.None);
assert.strictEqual(entries.length, 4);
assert.strictEqual(entries[0].id, entry1.id);
assert.strictEqual(entries[0].timestamp, entry1.timestamp);
assert.strictEqual(entries[0].source, entry1.source);
assert.notStrictEqual(entries[0].location, entry1.location);
assert.strictEqual(entries[0].workingCopy.resource.toString(), renamedWorkingCopyResource.toString());
assert.strictEqual(entries[1].id, entry2.id);
assert.strictEqual(entries[1].timestamp, entry2.timestamp);
assert.strictEqual(entries[1].source, entry2.source);
assert.notStrictEqual(entries[1].location, entry2.location);
assert.strictEqual(entries[1].workingCopy.resource.toString(), renamedWorkingCopyResource.toString());
assert.strictEqual(entries[2].id, entry3.id);
assert.strictEqual(entries[2].timestamp, entry3.timestamp);
assert.strictEqual(entries[2].source, entry3.source);
assert.notStrictEqual(entries[2].location, entry3.location);
assert.strictEqual(entries[2].workingCopy.resource.toString(), renamedWorkingCopyResource.toString());
const all = await service.getAll(CancellationToken.None);
assert.strictEqual(all.length, 1);
assert.strictEqual(all[0].toString(), renamedWorkingCopyResource.toString());
});
test('entries moved (folder rename)', async () => {
const workingCopy1 = new TestWorkingCopy(URI.file(testFile1Path));
const workingCopy2 = new TestWorkingCopy(URI.file(testFile2Path));
const entry1A = await addEntry({ resource: workingCopy1.resource, source: 'test-source' }, CancellationToken.None);
const entry2A = await addEntry({ resource: workingCopy1.resource, source: 'test-source' }, CancellationToken.None);
const entry3A = await addEntry({ resource: workingCopy1.resource, source: 'test-source' }, CancellationToken.None);
const entry1B = await addEntry({ resource: workingCopy2.resource, source: 'test-source' }, CancellationToken.None);
const entry2B = await addEntry({ resource: workingCopy2.resource, source: 'test-source' }, CancellationToken.None);
const entry3B = await addEntry({ resource: workingCopy2.resource, source: 'test-source' }, CancellationToken.None);
let entries = await service.getEntries(workingCopy1.resource, CancellationToken.None);
assert.strictEqual(entries.length, 3);
entries = await service.getEntries(workingCopy2.resource, CancellationToken.None);
assert.strictEqual(entries.length, 3);
const renamedWorkHome = joinPath(resourcesDirname(URI.file(workHome)), 'renamed');
await service._fileService.move(URI.file(workHome), renamedWorkHome);
const resources = await service.moveEntries(URI.file(workHome), renamedWorkHome);
const renamedWorkingCopy1Resource = joinPath(renamedWorkHome, basename(workingCopy1.resource));
const renamedWorkingCopy2Resource = joinPath(renamedWorkHome, basename(workingCopy2.resource));
assert.strictEqual(resources.length, 2);
for (const resource of resources) {
if (resource.toString() !== renamedWorkingCopy1Resource.toString() && resource.toString() !== renamedWorkingCopy2Resource.toString()) {
assert.fail(`Unexpected history resource: ${resource.toString()}`);
}
}
entries = await service.getEntries(workingCopy1.resource, CancellationToken.None);
assert.strictEqual(entries.length, 0);
entries = await service.getEntries(workingCopy2.resource, CancellationToken.None);
assert.strictEqual(entries.length, 0);
entries = await service.getEntries(renamedWorkingCopy1Resource, CancellationToken.None);
assert.strictEqual(entries.length, 4);
assert.strictEqual(entries[0].id, entry1A.id);
assert.strictEqual(entries[0].timestamp, entry1A.timestamp);
assert.strictEqual(entries[0].source, entry1A.source);
assert.notStrictEqual(entries[0].location, entry1A.location);
assert.strictEqual(entries[0].workingCopy.resource.toString(), renamedWorkingCopy1Resource.toString());
assert.strictEqual(entries[1].id, entry2A.id);
assert.strictEqual(entries[1].timestamp, entry2A.timestamp);
assert.strictEqual(entries[1].source, entry2A.source);
assert.notStrictEqual(entries[1].location, entry2A.location);
assert.strictEqual(entries[1].workingCopy.resource.toString(), renamedWorkingCopy1Resource.toString());
assert.strictEqual(entries[2].id, entry3A.id);
assert.strictEqual(entries[2].timestamp, entry3A.timestamp);
assert.strictEqual(entries[2].source, entry3A.source);
assert.notStrictEqual(entries[2].location, entry3A.location);
assert.strictEqual(entries[2].workingCopy.resource.toString(), renamedWorkingCopy1Resource.toString());
entries = await service.getEntries(renamedWorkingCopy2Resource, CancellationToken.None);
assert.strictEqual(entries.length, 4);
assert.strictEqual(entries[0].id, entry1B.id);
assert.strictEqual(entries[0].timestamp, entry1B.timestamp);
assert.strictEqual(entries[0].source, entry1B.source);
assert.notStrictEqual(entries[0].location, entry1B.location);
assert.strictEqual(entries[0].workingCopy.resource.toString(), renamedWorkingCopy2Resource.toString());
assert.strictEqual(entries[1].id, entry2B.id);
assert.strictEqual(entries[1].timestamp, entry2B.timestamp);
assert.strictEqual(entries[1].source, entry2B.source);
assert.notStrictEqual(entries[1].location, entry2B.location);
assert.strictEqual(entries[1].workingCopy.resource.toString(), renamedWorkingCopy2Resource.toString());
assert.strictEqual(entries[2].id, entry3B.id);
assert.strictEqual(entries[2].timestamp, entry3B.timestamp);
assert.strictEqual(entries[2].source, entry3B.source);
assert.notStrictEqual(entries[2].location, entry3B.location);
assert.strictEqual(entries[2].workingCopy.resource.toString(), renamedWorkingCopy2Resource.toString());
const all = await service.getAll(CancellationToken.None);
assert.strictEqual(all.length, 2);
for (const resource of all) {
if (resource.toString() !== renamedWorkingCopy1Resource.toString() && resource.toString() !== renamedWorkingCopy2Resource.toString()) {
assert.fail(`Unexpected history resource: ${resource.toString()}`);
}
}
});
});
|