forked from halninekilo/SnowFS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
10.cli.ts
774 lines (594 loc) · 30.7 KB
/
10.cli.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
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
import test from 'ava';
import * as fse from 'fs-extra';
import { join, dirname, basename } from '../src/path';
import {
exec, generateUniqueTmpDirName, EXEC_OPTIONS, getSnowexec,
} from './helper';
import { COMMIT_ORDER, REFERENCE_TYPE, Repository } from '../src/repository';
import { Reference } from '../src/reference';
import { DirItem, OSWALK, osWalk } from '../src/io';
// test doesn't work on the GitHub runners
// https://github.com/seb-mtl/SnowFS/runs/1923599289?check_suite_focus=true#step:7:245
test('snow add/commit/log', async (t) => {
t.timeout(180000);
const snow: string = getSnowexec();
const snowWorkdir = generateUniqueTmpDirName();
await exec(t, snow, ['init', basename(snowWorkdir)], { cwd: dirname(snowWorkdir) });
for (let i = 0; i < 2; ++i) {
t.log(`Write abc${i}.txt`);
fse.writeFileSync(join(snowWorkdir, `abc${i}.txt`), 'Hello World');
// eslint-disable-next-line no-await-in-loop
await exec(t, snow, ['add', '.'], { cwd: snowWorkdir });
// eslint-disable-next-line no-await-in-loop
await exec(t, snow, ['commit', '-m', `add hello-world ${i}`], { cwd: snowWorkdir });
// eslint-disable-next-line no-await-in-loop
await exec(t, snow, ['log'], { cwd: snowWorkdir });
}
t.is(true, true);
});
test('snow switch', async (t) => {
t.timeout(180000);
let out: string | void;
const snow: string = getSnowexec();
const snowWorkdir = generateUniqueTmpDirName();
// Create branch succesfully
await exec(t, snow, ['init', basename(snowWorkdir)], { cwd: dirname(snowWorkdir) });
for (let i = 0; i < 3; ++i) {
t.log(`Write abc${i}.txt`);
fse.writeFileSync(join(snowWorkdir, `abc${i}.txt`), `Hello World ${i}`);
// eslint-disable-next-line no-await-in-loop
await exec(t, snow, ['add', '.'], { cwd: snowWorkdir });
// eslint-disable-next-line no-await-in-loop
await exec(t, snow, ['commit', '-m', `add hello-world ${i}`], { cwd: snowWorkdir });
// eslint-disable-next-line no-await-in-loop
out = await exec(t, snow, ['branch', `branch-${i}`], { cwd: snowWorkdir }, EXEC_OPTIONS.RETURN_STDOUT);
t.true((out as string).includes(`A branch 'branch-${i}' got created.`));
}
await exec(t, snow, ['log', '--verbose'], { cwd: snowWorkdir });
let dirItems: DirItem[];
let dirPaths: string[];
// switch to all branches while no modifications are present in the working dir
t.log('Switch to branch-0');
await exec(t, snow, ['switch', 'branch-0'], { cwd: snowWorkdir });
dirItems = await osWalk(snowWorkdir, OSWALK.FILES);
dirPaths = dirItems.map((d) => basename(d.relPath));
t.is(dirItems.length, 1);
t.true(dirPaths.includes('abc0.txt'));
t.log('Switch to branch-1');
await exec(t, snow, ['switch', 'branch-1'], { cwd: snowWorkdir });
dirItems = await osWalk(snowWorkdir, OSWALK.FILES);
dirPaths = dirItems.map((d) => basename(d.relPath));
t.is(dirItems.length, 2);
t.true(dirPaths.includes('abc0.txt'));
t.true(dirPaths.includes('abc1.txt'));
t.log('Switch to branch-2');
await exec(t, snow, ['switch', 'branch-2'], { cwd: snowWorkdir });
dirItems = await osWalk(snowWorkdir, OSWALK.FILES);
dirPaths = dirItems.map((d) => basename(d.relPath));
t.is(dirItems.length, 3);
t.true(dirPaths.includes('abc0.txt'));
t.true(dirPaths.includes('abc1.txt'));
t.true(dirPaths.includes('abc2.txt'));
t.log('Make some changes to the working directory');
t.log(' Update abc0.txt');
fse.writeFileSync(join(snowWorkdir, 'abc0.txt'), 'Hello World Fooooo');
t.log(' Write abc3.txt');
fse.writeFileSync(join(snowWorkdir, 'abc3.txt'), 'Hello World 3');
fse.removeSync(join(snowWorkdir, 'abc1.txt'));
// switch to branches while...
// ... one is modified (abc0.txt)
// ... one deleted (abc1.txt)
// ... one file is untouched (abc2.txt)
// ... and one added (abc.txt)
const error = await t.throwsAsync(async () => exec(t, snow, ['switch', 'branch-0'], { cwd: snowWorkdir }, EXEC_OPTIONS.RETURN_STDOUT));
const lines = error.message.split('\n');
t.true(lines.includes('A abc3.txt')); // abc3.txt got added in the working dir
t.true(lines.includes('M abc0.txt')); // abc0.txt got added in the working dir
// abc1.txt did not get reported as deleted because switch/checkout don't mind if a file got deleted by the user since it can be restored
t.true(lines.includes("fatal: You have local changes to 'branch-0'; not switching branches."));
t.log('Switch and discard the local changes');
await exec(t, snow, ['switch', 'branch-0', '--discard-changes'], { cwd: snowWorkdir });
dirItems = await osWalk(snowWorkdir, OSWALK.FILES);
dirPaths = dirItems.map((d) => basename(d.relPath));
t.is(dirItems.length, 1);
t.true(dirPaths.includes('abc0.txt'));
t.log('Switch back to branch-2 and go from there');
await exec(t, snow, ['switch', 'branch-2'], { cwd: snowWorkdir });
t.log('Make some changes again to the working directory');
t.log(' Update abc0.txt');
fse.writeFileSync(join(snowWorkdir, 'abc0.txt'), 'Hello World Fooooo');
t.log(' Write abc3.txt');
fse.writeFileSync(join(snowWorkdir, 'abc3.txt'), 'Hello World 3');
fse.removeSync(join(snowWorkdir, 'abc1.txt'));
// switch back to branch-0 and keep all changes
await exec(t, snow, ['switch', 'branch-0', '--keep-changes'], { cwd: snowWorkdir });
// carried over the changes
t.is(fse.readFileSync(join(snowWorkdir, 'abc0.txt')).toString(), 'Hello World Fooooo');
t.is(fse.readFileSync(join(snowWorkdir, 'abc2.txt')).toString(), 'Hello World 2');
t.is(fse.readFileSync(join(snowWorkdir, 'abc3.txt')).toString(), 'Hello World 3');
t.false(fse.pathExistsSync(join(snowWorkdir, 'abc1.txt')));
});
test('snow checkout', async (t) => {
let out: string | void;
const snow: string = getSnowexec();
const snowWorkdir = generateUniqueTmpDirName();
// Create branch succesfully
await exec(t, snow, ['init', basename(snowWorkdir)], { cwd: dirname(snowWorkdir) });
for (let i = 0; i < 3; ++i) {
t.log(`Write abc${i}.txt`);
fse.writeFileSync(join(snowWorkdir, `abc${i}.txt`), `Hello World ${i}`);
// eslint-disable-next-line no-await-in-loop
await exec(t, snow, ['add', '.'], { cwd: snowWorkdir });
// eslint-disable-next-line no-await-in-loop
await exec(t, snow, ['commit', '-m', `add hello-world ${i}`], { cwd: snowWorkdir });
// eslint-disable-next-line no-await-in-loop
out = await exec(t, snow, ['branch', `branch-${i}`], { cwd: snowWorkdir }, EXEC_OPTIONS.RETURN_STDOUT);
t.true((out as string).includes(`A branch 'branch-${i}' got created.`));
}
await exec(t, snow, ['log', '--verbose'], { cwd: snowWorkdir });
let dirItems: DirItem[];
let dirPaths: string[];
const repo = await Repository.open(snowWorkdir);
const allCommits = repo.getAllCommits(COMMIT_ORDER.OLDEST_FIRST);
// switch to all branches while no modifications are present in the working dir
t.log(`Switch to ${allCommits[1]}`);
await exec(t, snow, ['checkout', allCommits[1].hash], { cwd: snowWorkdir });
dirItems = await osWalk(snowWorkdir, OSWALK.FILES);
dirPaths = dirItems.map((d) => basename(d.relPath));
t.is(dirItems.length, 1);
t.true(dirPaths.includes('abc0.txt'));
// We can now delete the Main branch
// For this unit-test, delete Main so we don't have two references for the same commit
// otherwise checking out the latest commit is ambigious
await exec(t, snow, ['branch', '--delete', 'Main'], { cwd: snowWorkdir });
t.log(`Switch to ${allCommits[2]}`);
await exec(t, snow, ['checkout', allCommits[2].hash], { cwd: snowWorkdir });
dirItems = await osWalk(snowWorkdir, OSWALK.FILES);
dirPaths = dirItems.map((d) => basename(d.relPath));
t.is(dirItems.length, 2);
t.true(dirPaths.includes('abc0.txt'));
t.true(dirPaths.includes('abc1.txt'));
t.log(`Switch to ${allCommits[3]}`);
await exec(t, snow, ['checkout', allCommits[3].hash], { cwd: snowWorkdir });
dirItems = await osWalk(snowWorkdir, OSWALK.FILES);
dirPaths = dirItems.map((d) => basename(d.relPath));
t.is(dirItems.length, 3);
t.true(dirPaths.includes('abc0.txt'));
t.true(dirPaths.includes('abc1.txt'));
t.true(dirPaths.includes('abc2.txt'));
t.log('Make some changes to the working directory');
t.log(' Update abc0.txt');
fse.writeFileSync(join(snowWorkdir, 'abc0.txt'), 'Hello World Fooooo');
t.log(' Write abc3.txt');
fse.writeFileSync(join(snowWorkdir, 'abc3.txt'), 'Hello World 3');
fse.removeSync(join(snowWorkdir, 'abc1.txt'));
// switch to branches while...
// ... one is modified (abc0.txt)
// ... one deleted (abc1.txt)
// ... one file is untouched (abc2.txt)
// ... and one added (abc.txt)
const error = await t.throwsAsync(async () => exec(t, snow, ['checkout', allCommits[1].hash], { cwd: snowWorkdir }, EXEC_OPTIONS.RETURN_STDOUT));
const lines = error.message.split('\n');
t.true(lines.includes('A abc3.txt')); // abc3.txt got added in the working dir
t.true(lines.includes('M abc0.txt')); // abc0.txt got added in the working dir
// abc1.txt did not get reported as deleted because switch/checkout don't mind if a file got deleted by the user since it can be restored
t.true(lines.includes(`fatal: You have local changes to '${allCommits[1].hash}'; not switching branches.`));
t.log('Switch and discard the local changes');
await exec(t, snow, ['checkout', allCommits[1].hash, '--discard-changes'], { cwd: snowWorkdir });
dirItems = await osWalk(snowWorkdir, OSWALK.FILES);
dirPaths = dirItems.map((d) => basename(d.relPath));
t.is(dirItems.length, 1);
t.true(dirPaths.includes('abc0.txt'));
t.log(`Switch back to ${allCommits[3].hash} and go from there`);
await exec(t, snow, ['checkout', allCommits[3].hash], { cwd: snowWorkdir });
t.log('Make some changes again to the working directory');
t.log(' Update abc0.txt');
fse.writeFileSync(join(snowWorkdir, 'abc0.txt'), 'Hello World Fooooo');
t.log(' Write abc3.txt');
fse.writeFileSync(join(snowWorkdir, 'abc3.txt'), 'Hello World 3');
fse.removeSync(join(snowWorkdir, 'abc1.txt'));
// switch back to branch-0 and keep all changes
await exec(t, snow, ['checkout', allCommits[1].hash, '--keep-changes'], { cwd: snowWorkdir });
// carried over the changes
t.is(fse.readFileSync(join(snowWorkdir, 'abc0.txt')).toString(), 'Hello World Fooooo');
t.is(fse.readFileSync(join(snowWorkdir, 'abc2.txt')).toString(), 'Hello World 2');
t.is(fse.readFileSync(join(snowWorkdir, 'abc3.txt')).toString(), 'Hello World 3');
t.false(fse.pathExistsSync(join(snowWorkdir, 'abc1.txt')));
});
test('snow branch foo-branch', async (t) => {
t.timeout(180000);
let out: string | void;
let error: any;
const snow: string = getSnowexec();
const snowWorkdir = generateUniqueTmpDirName();
// Create branch succesfully
await exec(t, snow, ['init', basename(snowWorkdir)], { cwd: dirname(snowWorkdir) });
t.log('Write foo.txt');
fse.writeFileSync(join(snowWorkdir, 'foo.txt'), 'foo');
await exec(t, snow, ['add', '.'], { cwd: snowWorkdir });
await exec(t, snow, ['commit', '-m', 'First user-commit'], { cwd: snowWorkdir });
const repoBefore = await Repository.open(snowWorkdir);
const headHash = repoBefore.getHead().hash;
const allCommits = repoBefore.getAllCommits(COMMIT_ORDER.OLDEST_FIRST);
const firstCommit = allCommits[0];
const secondCommit = allCommits[1];
t.log(`HEAD now at ${headHash}`);
// snow branch foo-branch
out = await exec(t, snow, ['branch', 'foo-branch'], { cwd: snowWorkdir }, EXEC_OPTIONS.RETURN_STDOUT);
t.true((out as string).includes("A branch 'foo-branch' got created."));
// the hash of 'foo-branch' must match the hash
const repoAfter = await Repository.open(snowWorkdir);
t.is(headHash, repoAfter.findReferenceByName(REFERENCE_TYPE.BRANCH, 'foo-branch').target());
// Don't create a branch twice
// snow branch foo-branch
error = await t.throwsAsync(async () => exec(t, snow, ['branch', 'foo-branch'], { cwd: snowWorkdir }, EXEC_OPTIONS.RETURN_STDOUT));
t.true(error.message.includes("A branch named 'foo-branch' already exists."));
// Create a branch with a different starting point
// snow branch bar-branch 768FF3AA8273DFEB81E7A111572C823EA0850499
out = await exec(t, snow, ['branch', 'bar-branch', firstCommit.hash], { cwd: snowWorkdir }, EXEC_OPTIONS.RETURN_STDOUT);
t.true((out as string).includes("A branch 'bar-branch' got created."));
// verify the target() and start() point are equal (in this case the
// start-point and target are still the same since the branch didn't move forward)
const repoAfter2 = await Repository.open(snowWorkdir);
const checkedOutBranch: string = repoAfter2.getHead().getName();
const fooBranch: Reference = repoAfter2.findReferenceByName(REFERENCE_TYPE.BRANCH, 'foo-branch');
t.is(secondCommit.hash, fooBranch.target());
t.is(secondCommit.hash, fooBranch.start());
const barBranch: Reference = repoAfter2.findReferenceByName(REFERENCE_TYPE.BRANCH, 'bar-branch');
t.is(firstCommit.hash, barBranch.target());
t.is(firstCommit.hash, barBranch.start());
// Delete foo-branch and bar branch
out = await exec(t, snow, ['branch', '--delete', 'foo-branch'], { cwd: snowWorkdir }, EXEC_OPTIONS.RETURN_STDOUT);
t.true((out as string).includes(`Deleted branch 'foo-branch' (was ${fooBranch.target()})`));
out = await exec(t, snow, ['branch', '--delete', 'bar-branch'], { cwd: snowWorkdir }, EXEC_OPTIONS.RETURN_STDOUT);
t.true((out as string).includes(`Deleted branch 'bar-branch' (was ${barBranch.target()})`));
// Try to delete the HEAD branch which must fail
error = await t.throwsAsync(async () =>
exec(t, snow, ['branch', '--delete', checkedOutBranch], { cwd: snowWorkdir }, EXEC_OPTIONS.RETURN_STDOUT));
if (process.platform === 'darwin') {
// on macOS 'process.cwd()' in the branch command returns /private/var/...
t.true(error.message.includes(`Cannot delete branch '${checkedOutBranch}' checked out at '/private${repoAfter2.workdir()}'`));
} else {
t.true(error.message.includes(`Cannot delete branch '${checkedOutBranch}' checked out at '${repoAfter2.workdir()}'`));
}
});
test('snow add .', async (t) => {
t.timeout(180000);
const snow: string = getSnowexec();
const snowWorkdir = generateUniqueTmpDirName();
const subdir = join(snowWorkdir, 'subdir');
await exec(t, snow, ['init', basename(snowWorkdir)], { cwd: dirname(snowWorkdir) });
t.log('Write foo.txt');
fse.writeFileSync(join(snowWorkdir, 'foo.txt'), 'foo');
t.log('Create subdir');
fse.mkdirpSync(subdir);
t.log('Write subdir/foo.txt');
fse.writeFileSync(join(subdir, 'bar.txt'), 'bar');
await exec(t, snow, ['add', '.'], { cwd: subdir });
// TODO: (Fix getStatus to differ between worktree and staging area)
// const stdout = await exec(t, snow, ['status', '--output=json-pretty'], { cwd: subdir }, EXEC_OPTIONS.RETURN_STDOUT);
t.is(true, true);
});
test('snow add *', async (t) => {
const snow: string = getSnowexec();
const snowWorkdir = generateUniqueTmpDirName();
const subdir = join(snowWorkdir, 'subdir');
await exec(t, snow, ['init', basename(snowWorkdir)], { cwd: dirname(snowWorkdir) });
t.log('Write foo.txt');
fse.writeFileSync(join(snowWorkdir, 'foo.txt'), 'foo');
t.log('Create subdir');
fse.mkdirpSync(subdir);
t.log('Write subdir/foo.txt');
fse.writeFileSync(join(subdir, 'bar.txt'), 'bar');
await exec(t, snow, ['add', '*'], { cwd: subdir });
// TODO: (Fix getStatus to differ between worktree and staging area)
// const stdout = await exec(t, snow, ['status', '--output=json-pretty'], { cwd: subdir }, EXEC_OPTIONS.RETURN_STDOUT);
t.is(true, true);
});
/**
* This test ensures that foo.txt is not added to the staging area because cwd is the subdirectory
*/
test('snow add foo.txt', async (t) => {
const snow: string = getSnowexec();
const snowWorkdir = generateUniqueTmpDirName();
const subdir = join(snowWorkdir, 'subdir');
await exec(t, snow, ['init', basename(snowWorkdir)], { cwd: dirname(snowWorkdir) });
t.log('Write foo.txt');
fse.writeFileSync(join(snowWorkdir, 'foo.txt'), 'foo');
t.log('Create subdir');
fse.mkdirpSync(subdir);
t.log('Write subdir/foo.txt');
fse.writeFileSync(join(subdir, 'bar.txt'), 'bar');
await exec(t, snow, ['add', 'foo.txt'], { cwd: subdir });
// TODO: (Fix getStatus to differ between worktree and staging area)
// const stdout = await exec(t, snow, ['status', '--output=json-pretty'], { cwd: subdir }, EXEC_OPTIONS.RETURN_STDOUT);
t.is(true, true);
});
test('snow add bar.txt', async (t) => {
const snow: string = getSnowexec();
const snowWorkdir = generateUniqueTmpDirName();
const subdir = join(snowWorkdir, 'subdir');
await exec(t, snow, ['init', basename(snowWorkdir)], { cwd: dirname(snowWorkdir) });
t.log('Write foo.txt');
fse.writeFileSync(join(snowWorkdir, 'foo.txt'), 'foo');
t.log('Create subdir');
fse.mkdirpSync(subdir);
t.log('Write subdir/foo.txt');
fse.writeFileSync(join(subdir, 'bar.txt'), 'bar');
await exec(t, snow, ['add', 'bar.txt'], { cwd: subdir });
// TODO: (Fix getStatus to differ between worktree and staging area)
// const stdout = await exec(t, snow, ['status', '--output=json-pretty'], { cwd: subdir }, EXEC_OPTIONS.RETURN_STDOUT);
t.is(true, true);
});
test('snow rm foo.txt', async (t) => {
const snow: string = getSnowexec();
const snowWorkdir = generateUniqueTmpDirName();
const subdir = join(snowWorkdir, 'subdir');
await exec(t, snow, ['init', basename(snowWorkdir)], { cwd: dirname(snowWorkdir) });
t.log('Write foo.txt');
fse.writeFileSync(join(snowWorkdir, 'foo.txt'), 'foo');
t.log('Create subdir');
fse.mkdirpSync(subdir);
t.log('Write subdir/foo.txt');
fse.writeFileSync(join(subdir, 'bar.txt'), 'bar');
await exec(t, snow, ['add', '*'], { cwd: snowWorkdir });
await exec(t, snow, ['commit', '-m', 'First commit'], { cwd: snowWorkdir });
// delete the file and commit
await exec(t, snow, ['rm', 'foo.txt'], { cwd: snowWorkdir });
await exec(t, snow, ['commit', '-m', 'Delete foo.txt'], { cwd: snowWorkdir });
// TODO: (Fix getStatus to differ between worktree and staging area)
// const stdout = await exec(t, snow, ['status', '--output=json-pretty'], { cwd: subdir }, EXEC_OPTIONS.RETURN_STDOUT);
t.is(true, true);
});
test('snow rm subdir', async (t) => {
const snow: string = getSnowexec();
const snowWorkdir = generateUniqueTmpDirName();
const subdir = join(snowWorkdir, 'subdir');
await exec(t, snow, ['init', basename(snowWorkdir)], { cwd: dirname(snowWorkdir) });
t.log('Write foo.txt');
fse.writeFileSync(join(snowWorkdir, 'foo.txt'), 'foo');
t.log('Create subdir');
fse.mkdirpSync(subdir);
t.log('Write subdir/foo.txt');
fse.writeFileSync(join(subdir, 'bar.txt'), 'bar');
await exec(t, snow, ['add', '*'], { cwd: snowWorkdir });
await exec(t, snow, ['commit', '-m', 'First commit'], { cwd: snowWorkdir });
// delete the file and commit
await exec(t, snow, ['rm', 'subdir'], { cwd: snowWorkdir });
await exec(t, snow, ['commit', '-m', 'Delete subdir'], { cwd: snowWorkdir });
// TODO: (Fix getStatus to differ between worktree and staging area)
// const stdout = await exec(t, snow, ['status', '--output=json-pretty'], { cwd: subdir }, EXEC_OPTIONS.RETURN_STDOUT);
t.is(true, true);
});
test('snow rm subdir/bar.txt', async (t) => {
const snow: string = getSnowexec();
const snowWorkdir = generateUniqueTmpDirName();
const subdir = join(snowWorkdir, 'subdir');
await exec(t, snow, ['init', basename(snowWorkdir)], { cwd: dirname(snowWorkdir) });
t.log('Write foo.txt');
fse.writeFileSync(join(snowWorkdir, 'foo.txt'), 'foo');
t.log('Create subdir');
fse.mkdirpSync(subdir);
t.log('Write subdir/foo.txt');
fse.writeFileSync(join(subdir, 'bar.txt'), 'bar');
await exec(t, snow, ['add', '*'], { cwd: snowWorkdir });
await exec(t, snow, ['commit', '-m', 'First commit'], { cwd: snowWorkdir });
// delete the file and commit
await exec(t, snow, ['rm', 'subdir/bar.txt'], { cwd: snowWorkdir });
await exec(t, snow, ['commit', '-m', 'Delete subdir/bar.txt'], { cwd: snowWorkdir });
// TODO: (Fix getStatus to differ between worktree and staging area)
// const stdout = await exec(t, snow, ['status', '--output=json-pretty'], { cwd: subdir }, EXEC_OPTIONS.RETURN_STDOUT);
t.is(true, true);
});
test('snow rm file-does-not-exist', async (t) => {
const snow: string = getSnowexec();
const snowWorkdir = generateUniqueTmpDirName();
const subdir = join(snowWorkdir, 'subdir');
await exec(t, snow, ['init', basename(snowWorkdir)], { cwd: dirname(snowWorkdir) });
t.log('Write foo.txt');
fse.writeFileSync(join(snowWorkdir, 'foo.txt'), 'foo');
t.log('Create subdir');
fse.mkdirpSync(subdir);
t.log('Write subdir/foo.txt');
fse.writeFileSync(join(subdir, 'bar.txt'), 'bar');
await exec(t, snow, ['add', '*'], { cwd: snowWorkdir });
await exec(t, snow, ['commit', '-m', 'First commit'], { cwd: snowWorkdir });
const error = await t.throwsAsync(async () => exec(t, snow, ['rm', 'file-does-not-exist'], { cwd: snowWorkdir }));
t.true(error.message.includes('fatal: ENOENT: no such file or directory, stat'));
// TODO: (Fix getStatus to differ between worktree and staging area)
// const stdout = await exec(t, snow, ['status', '--output=json-pretty'], { cwd: subdir }, EXEC_OPTIONS.RETURN_STDOUT);
t.is(true, true);
});
test('Commit User Data --- STORE AND LOAD IDENTICAL', async (t) => {
const snow: string = getSnowexec();
const snowWorkdir = generateUniqueTmpDirName();
const uData: any = { str_key: 'str_value', int_key: 3 };
await exec(t, snow, ['init', basename(snowWorkdir)], { cwd: dirname(snowWorkdir) });
await exec(t, snow,
['commit', '-m', 'unit test user data', '--allow-empty', '--input=stdin'], { cwd: snowWorkdir },
EXEC_OPTIONS.RETURN_STDOUT | EXEC_OPTIONS.WRITE_STDIN,
`--user-data: ${JSON.stringify(uData)}`);
const out = await exec(t, snow, ['log', '--output=json'], { cwd: snowWorkdir }, EXEC_OPTIONS.RETURN_STDOUT);
const c: any = JSON.parse(String(out));
let identical = false;
if (c.commits.length > 0) {
const d = c.commits[0].userData;
// eslint-disable-next-line guard-for-in
for (const key in d) {
if (!(key in uData)) {
identical = false;
break;
}
if (d[key] !== uData[key]) {
identical = false;
break;
}
identical = true;
}
}
t.is(true, identical);
});
test('Commit User Data --- FAIL INVALID INPUT', async (t) => {
const snow: string = getSnowexec();
const snowWorkdir = generateUniqueTmpDirName();
await exec(t, snow, ['init', basename(snowWorkdir)], { cwd: dirname(snowWorkdir) });
const error = await t.throwsAsync(async () => exec(t, snow,
['commit', '-m', 'unit test user data', '--allow-empty', '--input=stdin'], { cwd: snowWorkdir },
EXEC_OPTIONS.RETURN_STDOUT | EXEC_OPTIONS.WRITE_STDIN, '--user-data: garbage-because-json-object-expected'));
const errorMsgSub = 'fatal: invalid user-data: SyntaxError: Unexpected token g in JSON at position 0';
t.true(error.message.includes(errorMsgSub));
});
test('Commit Tags --- STORE AND LOAD IDENTICAL', async (t) => {
const snow: string = getSnowexec();
const snowWorkdir = generateUniqueTmpDirName();
const tag1 = 'FirstTag';
const tag2 = 'SecondTag';
await exec(t, snow, ['init', basename(snowWorkdir)], { cwd: dirname(snowWorkdir) });
await exec(t, snow, ['commit', '-m', 'unit test tags', '--allow-empty', `--tags=${tag1},${tag2}`], { cwd: snowWorkdir });
const out = await exec(t, snow, ['log', '--output=json'], { cwd: snowWorkdir }, EXEC_OPTIONS.RETURN_STDOUT);
const c: any = JSON.parse(String(out));
let identical = false;
if (c.commits.length > 0) {
const d = c.commits[0].tags;
identical = d.includes(tag1) && d.includes(tag2);
}
t.is(true, identical);
});
test('Commit Tags --- SPECIAL SYMBOLS INPUT', async (t) => {
const snow: string = getSnowexec();
const snowWorkdir = generateUniqueTmpDirName();
const tag1 = '[]}';
const tag2 = '\'%$[,.}}';
await exec(t, snow, ['init', basename(snowWorkdir)], { cwd: dirname(snowWorkdir) });
await exec(t, snow, ['commit', '-m', 'unit test tags', '--allow-empty', `--tags=${tag1},${tag2}`], { cwd: snowWorkdir });
const out = await exec(t, snow, ['log', '--output=json'], { cwd: snowWorkdir }, EXEC_OPTIONS.RETURN_STDOUT);
const c: any = JSON.parse(String(out));
let tags: string[] = [];
if (c.commits.length > 0) {
tags = c.commits[0].tags;
}
t.is(true, tags.length === 3); // === 3 due to comma in tag2
});
test('Commit Tags --- EMPTY INPUT', async (t) => {
const snow: string = getSnowexec();
const snowWorkdir = generateUniqueTmpDirName();
await exec(t, snow, ['init', basename(snowWorkdir)], { cwd: dirname(snowWorkdir) });
await exec(t, snow, ['commit', '-m', 'unit test tags', '--allow-empty', '--tags='], { cwd: snowWorkdir });
const out = await exec(t, snow, ['log'], { cwd: snowWorkdir }, EXEC_OPTIONS.RETURN_STDOUT);
// should not print tags as we never passed any
const tagsLog = 'Tags:';
t.is(true, !String(out).includes(tagsLog));
});
test('Branch User Data --- STORE AND LOAD IDENTICAL', async (t) => {
const snow: string = getSnowexec();
const snowWorkdir = generateUniqueTmpDirName();
const uData: any = { str_key: 'str_value', int_key: 3 };
const branchName = 'u-data-test';
await exec(t, snow, ['init', basename(snowWorkdir)], { cwd: dirname(snowWorkdir) });
await exec(t, snow,
['branch', branchName, '--input=stdin'], { cwd: snowWorkdir },
EXEC_OPTIONS.RETURN_STDOUT | EXEC_OPTIONS.WRITE_STDIN,
`--user-data:${JSON.stringify(uData)}`);
const out = await exec(t, snow, ['log', '--output=json'], { cwd: snowWorkdir }, EXEC_OPTIONS.RETURN_STDOUT);
const c: any = JSON.parse(String(out));
let identical = false;
if (c.refs.length > 1) {
const d = c.refs[1].userData;
// eslint-disable-next-line guard-for-in
for (const key in d) {
if (!(key in uData)) {
identical = false;
break;
}
if (d[key] !== uData[key]) {
identical = false;
break;
}
identical = true;
}
}
t.is(true, identical);
});
test('Branch User Data --- FAIL INVALID INPUT', async (t) => {
const snow: string = getSnowexec();
const snowWorkdir = generateUniqueTmpDirName();
const branchName = 'u-data-test';
await exec(t, snow, ['init', basename(snowWorkdir)], { cwd: dirname(snowWorkdir) });
const error = await t.throwsAsync(async () => exec(t, snow,
['branch', branchName, '--input=stdin'], { cwd: snowWorkdir },
EXEC_OPTIONS.RETURN_STDOUT | EXEC_OPTIONS.WRITE_STDIN, '--user-data: garbage-because-json-object-expected'));
const errorMsgSub = 'fatal: invalid user-data: SyntaxError: Unexpected token g in JSON at position 0';
t.true(error.message.includes(errorMsgSub));
t.log('Test failed as expected');
});
test('Multi-Index -- CREATE 2 INDEXES, COMMIT SEQUENTIALLY', async (t) => {
const snow: string = getSnowexec();
const snowWorkdir = generateUniqueTmpDirName();
await exec(t, snow, ['init', basename(snowWorkdir)], { cwd: dirname(snowWorkdir) });
t.log('Write a.txt');
fse.writeFileSync(join(snowWorkdir, 'a.txt'), 'a');
t.log('Write b.txt');
fse.writeFileSync(join(snowWorkdir, 'b.txt'), 'b');
const outAddA = await exec(t, snow, ['add', 'a.txt', '--index', 'create'], { cwd: snowWorkdir }, EXEC_OPTIONS.RETURN_STDOUT);
const outAddB = await exec(t, snow, ['add', 'b.txt', '--index', 'create'], { cwd: snowWorkdir }, EXEC_OPTIONS.RETURN_STDOUT);
const indexAMatch = /Created new index:\s\[(\w*)\]/.exec(outAddA as string);
t.true(Boolean(indexAMatch));
const indexBMatch = /Created new index:\s\[(\w*)\]/.exec(outAddB as string);
t.true(Boolean(indexBMatch));
t.log('Write dontcommit-c.txt'); // dummy file just to ensure file is not commited
fse.writeFileSync(join(snowWorkdir, 'dontcommit-c.txt'), 'dontcommit-c');
if (indexAMatch) {
const indexA: string = indexAMatch[1];
await exec(t, snow, ['commit', '-m', 'commit a.txt', '--index', indexA], { cwd: snowWorkdir });
}
t.log('Write dontcommit-d.txt'); // dummy file just to ensure file is not commited
fse.writeFileSync(join(snowWorkdir, 'dontcommit-d.txt'), 'dontcommit-d');
if (indexBMatch) {
const indexB: string = indexBMatch[1];
await exec(t, snow, ['commit', '-m', 'commit b.txt', '--index', indexB], { cwd: snowWorkdir });
}
t.log('Write dontcommit-e.txt'); // dummy file just to ensure file is not commited
fse.writeFileSync(join(snowWorkdir, 'dontcommit-e.txt'), 'dontcommit-e');
const repo = await Repository.open(snowWorkdir);
const allCommits = repo.getAllCommits(COMMIT_ORDER.OLDEST_FIRST);
t.is(allCommits.length, 3, 'all 3 commits'); // Dummy commit 'Created Project' + 'commit a.txt' + 'commit b.txt'
t.is(allCommits[1].message, 'commit a.txt');
t.is(allCommits[2].message, 'commit b.txt');
// ensure a.txt and b.txt are in their commits
t.true(allCommits[1].root.children.map((t) => t.path).includes('a.txt'));
t.true(allCommits[1].root.children.map((t) => t.path).includes('a.txt'));
t.true(allCommits[2].root.children.map((t) => t.path).includes('b.txt'));
// ensure the commits ONLY contain these files
t.is(allCommits[1].root.children.length, 1, '"First" commit shall contain 1 file (a.txt)');
t.is(allCommits[2].root.children.length, 2, 'Last commit shall contain 2 files (a.txt, b.txt)');
});
test('Multi-Index -- FAIL INVALID INPUT TEST 1', async (t) => {
t.timeout(180000);
const snow: string = getSnowexec();
const snowWorkdir = generateUniqueTmpDirName();
await exec(t, snow, ['init', basename(snowWorkdir)], { cwd: dirname(snowWorkdir) });
t.log('Write abc.txt');
fse.writeFileSync(join(snowWorkdir, 'abc.txt'), 'Hello World');
const error = await t.throwsAsync(async () => exec(t, snow, ['add', '.', '--index', 'non-existing-index'], { cwd: snowWorkdir }));
const errorMsgSub = 'fatal: unknown index: non-existing-index';
t.true(error.message.includes(errorMsgSub));
t.log('Test failed as expected');
});
test('driveinfo test', async (t) => {
const snow: string = getSnowexec();
const out1 = await exec(t, snow, ['driveinfo'], {}, EXEC_OPTIONS.RETURN_STDOUT) as string;
const parsedObj = JSON.parse(out1);
if (!Array.isArray(parsedObj) || parsedObj.length === 0) {
t.fail('expected array with minimum size of 1 element');
return;
}
t.log(out1);
t.true(parsedObj[0].description?.length > 0, 'stdout must be a JSON parsable string');
t.true(out1.includes(' '), 'driveinfo uses --output json-pretty as default and requires a 4-width space JSON output');
const out2 = await exec(t, snow, ['driveinfo', '--output', 'json'], {}, EXEC_OPTIONS.RETURN_STDOUT) as string;
t.true(JSON.parse(out2)[0].description?.length > 0, 'stdout must be a JSON parsable string');
t.true(out1.includes(' '), 'driveinfo --output json must return a minified JSON output');
const out3 = await exec(t, snow, ['driveinfo', '--output', 'json-pretty'], {}, EXEC_OPTIONS.RETURN_STDOUT) as string;
t.true(JSON.parse(out3)[0].description?.length > 0, 'stdout must be a JSON parsable string');
t.true(out1.includes(' '), 'driveinfo --output json-pretty requires a 4-width space JSON output');
});