forked from microsoft/VFSForGit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDehydrateTests.cs
More file actions
674 lines (560 loc) · 33.9 KB
/
DehydrateTests.cs
File metadata and controls
674 lines (560 loc) · 33.9 KB
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
using GVFS.FunctionalTests.FileSystemRunners;
using GVFS.FunctionalTests.Should;
using GVFS.FunctionalTests.Tools;
using GVFS.Tests.Should;
using Microsoft.Win32.SafeHandles;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
namespace GVFS.FunctionalTests.Tests.EnlistmentPerFixture
{
[TestFixture]
[Category(Categories.ExtraCoverage)]
public class DehydrateTests : TestsWithEnlistmentPerFixture
{
private const string FolderDehydrateSuccessfulMessage = "folder dehydrate successful.";
private const int GVFSGenericError = 3;
private const uint FileFlagBackupSemantics = 0x02000000;
private FileSystemRunner fileSystem;
// Set forcePerRepoObjectCache to true so that DehydrateShouldSucceedEvenIfObjectCacheIsDeleted does
// not delete the shared local cache
public DehydrateTests()
: base(forcePerRepoObjectCache: true)
{
this.fileSystem = new SystemIORunner();
}
[TearDown]
public void TearDown()
{
string backupFolder = Path.Combine(this.Enlistment.EnlistmentRoot, "dehydrate_backup");
if (this.fileSystem.DirectoryExists(backupFolder))
{
this.fileSystem.DeleteDirectory(backupFolder);
}
if (!this.Enlistment.IsMounted())
{
this.Enlistment.MountGVFS();
}
}
[TestCase]
public void DehydrateShouldExitWithoutConfirm()
{
this.DehydrateShouldSucceed(new[] { "To actually execute the dehydrate, run 'gvfs dehydrate --confirm'" }, confirm: false, noStatus: false);
}
[TestCase]
public void DehydrateShouldSucceedInCommonCase()
{
this.DehydrateShouldSucceed(new[] { "folder dehydrate successful." }, confirm: true, noStatus: false);
}
[TestCase]
public void FullDehydrateShouldExitWithoutConfirm()
{
this.DehydrateShouldSucceed(new[] { "To actually execute the dehydrate, run 'gvfs dehydrate --confirm --full'" }, confirm: false, noStatus: false, full: true);
}
[TestCase]
public void FullDehydrateShouldSucceedInCommonCase()
{
this.DehydrateShouldSucceed(new[] { "The repo was successfully dehydrated and remounted" }, confirm: true, noStatus: false, full: true);
}
[TestCase]
public void DehydrateShouldFailOnUnmountedRepoWithStatus()
{
this.Enlistment.UnmountGVFS();
this.DehydrateShouldFail(new[] { "Failed to run git status because the repo is not mounted" }, noStatus: false);
}
[TestCase]
public void DehydrateShouldSucceedEvenIfObjectCacheIsDeleted()
{
this.Enlistment.UnmountGVFS();
RepositoryHelpers.DeleteTestDirectory(this.Enlistment.GetObjectRoot(this.fileSystem));
this.DehydrateShouldSucceed(new[] { "The repo was successfully dehydrated and remounted" }, confirm: true, noStatus: true, full: true);
}
[TestCase]
public void DehydrateShouldBackupFiles()
{
this.DehydrateShouldSucceed(new[] { "The repo was successfully dehydrated and remounted" }, confirm: true, noStatus: false, full: true);
string backupFolder = Path.Combine(this.Enlistment.EnlistmentRoot, "dehydrate_backup");
backupFolder.ShouldBeADirectory(this.fileSystem);
string[] backupFolderItems = this.fileSystem.EnumerateDirectory(backupFolder).Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
backupFolderItems.Length.ShouldEqual(1);
this.DirectoryShouldContain(backupFolderItems[0], ".git", GVFSTestConfig.DotGVFSRoot, "src");
// .git folder items
string gitFolder = Path.Combine(backupFolderItems[0], ".git");
this.DirectoryShouldContain(gitFolder, "index");
// .gvfs folder items
string gvfsFolder = Path.Combine(backupFolderItems[0], GVFSTestConfig.DotGVFSRoot);
this.DirectoryShouldContain(gvfsFolder, "databases", "GVFS_projection");
string gvfsDatabasesFolder = Path.Combine(gvfsFolder, "databases");
this.DirectoryShouldContain(gvfsDatabasesFolder, "BackgroundGitOperations.dat", "ModifiedPaths.dat", "VFSForGit.sqlite");
}
[TestCase]
public void DehydrateShouldFailIfLocalCacheNotInMetadata()
{
this.Enlistment.UnmountGVFS();
string majorVersion;
string minorVersion;
GVFSHelpers.GetPersistedDiskLayoutVersion(this.Enlistment.DotGVFSRoot, out majorVersion, out minorVersion);
string objectsRoot = GVFSHelpers.GetPersistedGitObjectsRoot(this.Enlistment.DotGVFSRoot).ShouldNotBeNull();
string metadataPath = Path.Combine(this.Enlistment.DotGVFSRoot, GVFSHelpers.RepoMetadataName);
string metadataBackupPath = metadataPath + ".backup";
this.fileSystem.MoveFile(metadataPath, metadataBackupPath);
this.fileSystem.CreateEmptyFile(metadataPath);
GVFSHelpers.SaveDiskLayoutVersion(this.Enlistment.DotGVFSRoot, majorVersion, minorVersion);
GVFSHelpers.SaveGitObjectsRoot(this.Enlistment.DotGVFSRoot, objectsRoot);
this.DehydrateShouldFail(new[] { "Failed to determine local cache path from repo metadata" }, noStatus: true, full: true);
this.fileSystem.DeleteFile(metadataPath);
this.fileSystem.MoveFile(metadataBackupPath, metadataPath);
}
[TestCase]
public void DehydrateShouldFailIfGitObjectsRootNotInMetadata()
{
this.Enlistment.UnmountGVFS();
string majorVersion;
string minorVersion;
GVFSHelpers.GetPersistedDiskLayoutVersion(this.Enlistment.DotGVFSRoot, out majorVersion, out minorVersion);
string localCacheRoot = GVFSHelpers.GetPersistedLocalCacheRoot(this.Enlistment.DotGVFSRoot).ShouldNotBeNull();
string metadataPath = Path.Combine(this.Enlistment.DotGVFSRoot, GVFSHelpers.RepoMetadataName);
string metadataBackupPath = metadataPath + ".backup";
this.fileSystem.MoveFile(metadataPath, metadataBackupPath);
this.fileSystem.CreateEmptyFile(metadataPath);
GVFSHelpers.SaveDiskLayoutVersion(this.Enlistment.DotGVFSRoot, majorVersion, minorVersion);
GVFSHelpers.SaveLocalCacheRoot(this.Enlistment.DotGVFSRoot, localCacheRoot);
this.DehydrateShouldFail(new[] { "Failed to determine git objects root from repo metadata" }, noStatus: true, full: true);
this.fileSystem.DeleteFile(metadataPath);
this.fileSystem.MoveFile(metadataBackupPath, metadataPath);
}
[TestCase]
public void DehydrateShouldFailOnWrongDiskLayoutVersion()
{
this.Enlistment.UnmountGVFS();
string majorVersion;
string minorVersion;
GVFSHelpers.GetPersistedDiskLayoutVersion(this.Enlistment.DotGVFSRoot, out majorVersion, out minorVersion);
int majorVersionNum;
int minorVersionNum;
int.TryParse(majorVersion.ShouldNotBeNull(), out majorVersionNum).ShouldEqual(true);
int.TryParse(minorVersion.ShouldNotBeNull(), out minorVersionNum).ShouldEqual(true);
int previousMajorVersionNum = majorVersionNum - 1;
if (previousMajorVersionNum >= GVFSHelpers.GetCurrentDiskLayoutMinimumMajorVersion())
{
GVFSHelpers.SaveDiskLayoutVersion(this.Enlistment.DotGVFSRoot, previousMajorVersionNum.ToString(), "0");
this.DehydrateShouldFail(new[] { "disk layout version doesn't match current version" }, noStatus: true, full: true);
}
GVFSHelpers.SaveDiskLayoutVersion(this.Enlistment.DotGVFSRoot, (majorVersionNum + 1).ToString(), "0");
this.DehydrateShouldFail(new[] { "Changes to GVFS disk layout do not allow mounting after downgrade." }, noStatus: true, full: true);
GVFSHelpers.SaveDiskLayoutVersion(this.Enlistment.DotGVFSRoot, majorVersionNum.ToString(), minorVersionNum.ToString());
}
[TestCase]
public void FolderDehydrateFolderThatWasEnumerated()
{
string folderToDehydrate = "GVFS";
TestPath folderToEnumerate = new TestPath(this.Enlistment, folderToDehydrate);
TestPath subFolderToEnumerate = new TestPath(this.Enlistment, Path.Combine(folderToDehydrate, "GVFS"));
this.fileSystem.EnumerateDirectory(folderToEnumerate.VirtualPath);
this.fileSystem.EnumerateDirectory(subFolderToEnumerate.VirtualPath);
this.DehydrateShouldSucceed(new[] { $"{folderToDehydrate} {FolderDehydrateSuccessfulMessage}" }, confirm: true, noStatus: false, foldersToDehydrate: folderToDehydrate);
this.Enlistment.UnmountGVFS();
// Use the backing path because on some platforms
// the virtual path is no longer accessible after unmounting.
this.CheckDehydratedFolderAfterUnmount(folderToEnumerate.BackingPath);
subFolderToEnumerate.BackingPath.ShouldNotExistOnDisk(this.fileSystem);
}
[TestCase]
public void FolderDehydrateFolderWithFilesThatWerePlaceholders()
{
string folderToDehydrate = "GVFS";
TestPath folderToReadFiles = new TestPath(this.Enlistment, folderToDehydrate);
TestPath fileToRead = new TestPath(this.Enlistment, Path.Combine(folderToDehydrate, "GVFS", "Program.cs"));
using (File.OpenRead(fileToRead.VirtualPath))
{
}
this.DehydrateShouldSucceed(new[] { $"{folderToDehydrate} {FolderDehydrateSuccessfulMessage}" }, confirm: true, noStatus: false, foldersToDehydrate: folderToDehydrate);
this.Enlistment.UnmountGVFS();
// Use the backing path because on some platforms
// the virtual path is no longer accessible after unmounting.
this.CheckDehydratedFolderAfterUnmount(folderToReadFiles.BackingPath);
fileToRead.BackingPath.ShouldNotExistOnDisk(this.fileSystem);
}
[TestCase]
public void FolderDehydrateFolderWithFilesThatWereRead()
{
string folderToDehydrate = "GVFS";
TestPath folderToReadFiles = new TestPath(this.Enlistment, folderToDehydrate);
TestPath fileToRead = new TestPath(this.Enlistment, Path.Combine(folderToDehydrate, "GVFS", "Program.cs"));
this.fileSystem.ReadAllText(fileToRead.VirtualPath);
this.fileSystem.EnumerateDirectory(folderToReadFiles.VirtualPath);
this.DehydrateShouldSucceed(new[] { $"{folderToDehydrate} {FolderDehydrateSuccessfulMessage}" }, confirm: true, noStatus: false, foldersToDehydrate: folderToDehydrate);
this.Enlistment.UnmountGVFS();
// Use the backing path because on some platforms
// the virtual path is no longer accessible after unmounting.
this.CheckDehydratedFolderAfterUnmount(folderToReadFiles.BackingPath);
fileToRead.BackingPath.ShouldNotExistOnDisk(this.fileSystem);
}
[TestCase]
public void FolderDehydrateFolderWithFilesThatWereWrittenTo()
{
string folderToDehydrate = "GVFS";
TestPath folderToWriteFiles = new TestPath(this.Enlistment, folderToDehydrate);
TestPath fileToWrite = new TestPath(this.Enlistment, Path.Combine(folderToDehydrate, "GVFS", "Program.cs"));
this.fileSystem.AppendAllText(fileToWrite.VirtualPath, "Append content");
GitProcess.Invoke(this.Enlistment.RepoRoot, "add .");
GitProcess.Invoke(this.Enlistment.RepoRoot, "commit -m Test");
this.DehydrateShouldSucceed(new[] { $"{folderToDehydrate} {FolderDehydrateSuccessfulMessage}" }, confirm: true, noStatus: false, foldersToDehydrate: folderToDehydrate);
this.Enlistment.UnmountGVFS();
// Use the backing path because on some platforms
// the virtual path is no longer accessible after unmounting.
this.CheckDehydratedFolderAfterUnmount(folderToWriteFiles.BackingPath);
fileToWrite.BackingPath.ShouldNotExistOnDisk(this.fileSystem);
}
[TestCase]
public void FolderDehydrateFolderThatWasDeleted()
{
string folderToDehydrate = "Scripts";
TestPath folderToDelete = new TestPath(this.Enlistment, folderToDehydrate);
this.fileSystem.DeleteDirectory(folderToDelete.VirtualPath);
GitProcess.Invoke(this.Enlistment.RepoRoot, $"checkout -- {folderToDehydrate}");
this.DehydrateShouldSucceed(new[] { $"{folderToDehydrate} {FolderDehydrateSuccessfulMessage}" }, confirm: true, noStatus: false, foldersToDehydrate: folderToDehydrate);
this.Enlistment.UnmountGVFS();
// Use the backing path because on some platforms
// the virtual path is no longer accessible after unmounting.
this.CheckDehydratedFolderAfterUnmount(folderToDelete.BackingPath);
Path.Combine(folderToDelete.BackingPath, "RunUnitTests.bat").ShouldNotExistOnDisk(this.fileSystem);
}
[TestCase]
public void FolderDehydrateFolderThatIsLocked()
{
const string folderToDehydrate = "GVFS";
const string folderToLock = "GVFS.Service";
TestPath folderPathDehydrated = new TestPath(this.Enlistment, folderToDehydrate);
TestPath folderPathToLock = new TestPath(this.Enlistment, Path.Combine(folderToDehydrate, folderToLock));
TestPath fileToWrite = new TestPath(this.Enlistment, Path.Combine(folderToDehydrate, folderToLock, "Program.cs"));
this.fileSystem.AppendAllText(fileToWrite.VirtualPath, "Append content");
GitProcess.Invoke(this.Enlistment.RepoRoot, $"reset --hard");
using (SafeFileHandle handle = this.OpenFolderHandle(folderPathToLock.VirtualPath))
{
handle.IsInvalid.ShouldEqual(false);
this.DehydrateShouldSucceed(new[] { $"{folderToDehydrate} {FolderDehydrateSuccessfulMessage}" }, confirm: true, noStatus: false, foldersToDehydrate: folderToDehydrate);
}
this.Enlistment.UnmountGVFS();
folderPathToLock.BackingPath.ShouldBeADirectory(this.fileSystem).WithNoItems();
folderPathDehydrated.BackingPath.ShouldBeADirectory(this.fileSystem).WithOneItem().Name.ShouldEqual(folderToLock);
}
[TestCase]
public void FolderDehydrateFolderThatIsSubstringOfExistingFolder()
{
string folderToDehydrate = Path.Combine("GVFS", "GVFS");
TestPath fileToReadThenDehydrate = new TestPath(this.Enlistment, Path.Combine(folderToDehydrate, "Program.cs"));
TestPath fileToWriteThenDehydrate = new TestPath(this.Enlistment, Path.Combine(folderToDehydrate, "App.config"));
this.fileSystem.ReadAllText(fileToReadThenDehydrate.VirtualPath);
this.fileSystem.AppendAllText(fileToWriteThenDehydrate.VirtualPath, "Append content");
string folderToNotDehydrate = Path.Combine("GVFS", "GVFS.Common");
TestPath fileToReadThenNotDehydrate = new TestPath(this.Enlistment, Path.Combine(folderToNotDehydrate, "GVFSLock.cs"));
TestPath fileToWriteThenNotDehydrate = new TestPath(this.Enlistment, Path.Combine(folderToNotDehydrate, "Enlistment.cs"));
this.fileSystem.ReadAllText(fileToReadThenNotDehydrate.VirtualPath);
this.fileSystem.AppendAllText(fileToWriteThenNotDehydrate.VirtualPath, "Append content");
GitProcess.Invoke(this.Enlistment.RepoRoot, $"reset --hard");
this.DehydrateShouldSucceed(new[] { $"{folderToDehydrate} {FolderDehydrateSuccessfulMessage}" }, confirm: true, noStatus: false, foldersToDehydrate: folderToDehydrate);
this.PlaceholdersShouldNotContain(folderToDehydrate, fileToReadThenDehydrate.BasePath);
GVFSHelpers.ModifiedPathsShouldNotContain(this.Enlistment, this.fileSystem, fileToWriteThenDehydrate.BasePath.Replace(Path.DirectorySeparatorChar, TestConstants.GitPathSeparator));
this.PlaceholdersShouldContain(folderToNotDehydrate, fileToReadThenNotDehydrate.BasePath);
GVFSHelpers.ModifiedPathsShouldContain(this.Enlistment, this.fileSystem, fileToWriteThenNotDehydrate.BasePath.Replace(Path.DirectorySeparatorChar, TestConstants.GitPathSeparator));
this.Enlistment.UnmountGVFS();
// Use the backing path because on some platforms
// the virtual path is no longer accessible after unmounting.
fileToReadThenDehydrate.BackingPath.ShouldNotExistOnDisk(this.fileSystem);
fileToWriteThenDehydrate.BackingPath.ShouldNotExistOnDisk(this.fileSystem);
fileToReadThenNotDehydrate.BackingPath.ShouldBeAFile(this.fileSystem);
fileToWriteThenNotDehydrate.BackingPath.ShouldBeAFile(this.fileSystem);
}
[TestCase]
public void FolderDehydrateNestedFoldersChildBeforeParent()
{
string parentFolderToDehydrate = "GVFS";
string childFolderToDehydrate = Path.Combine(parentFolderToDehydrate, "GVFS.Mount");
TestPath fileToReadInChildFolder = new TestPath(this.Enlistment, Path.Combine(childFolderToDehydrate, "Program.cs"));
TestPath fileToReadInOtherChildFolder = new TestPath(this.Enlistment, Path.Combine(parentFolderToDehydrate, "GVFS.UnitTests", "Program.cs"));
this.fileSystem.ReadAllText(fileToReadInChildFolder.VirtualPath);
this.fileSystem.ReadAllText(fileToReadInOtherChildFolder.VirtualPath);
this.DehydrateShouldSucceed(
new[] { $"{childFolderToDehydrate} {FolderDehydrateSuccessfulMessage}", $"{parentFolderToDehydrate} {FolderDehydrateSuccessfulMessage}" },
confirm: true,
noStatus: false,
foldersToDehydrate: string.Join(";", childFolderToDehydrate, parentFolderToDehydrate));
this.Enlistment.UnmountGVFS();
// Use the backing path because on some platforms
// the virtual path is no longer accessible after unmounting.
fileToReadInChildFolder.BackingPath.ShouldNotExistOnDisk(this.fileSystem);
fileToReadInOtherChildFolder.BackingPath.ShouldNotExistOnDisk(this.fileSystem);
}
[TestCase]
public void FolderDehydrateNestedFoldersParentBeforeChild()
{
string parentFolderToDehydrate = "GVFS";
string childFolderToDehydrate = Path.Combine(parentFolderToDehydrate, "GVFS.Mount");
TestPath fileToReadInChildFolder = new TestPath(this.Enlistment, Path.Combine(childFolderToDehydrate, "Program.cs"));
TestPath fileToReadInOtherChildFolder = new TestPath(this.Enlistment, Path.Combine(parentFolderToDehydrate, "GVFS.UnitTests", "Program.cs"));
this.fileSystem.ReadAllText(fileToReadInChildFolder.VirtualPath);
this.fileSystem.ReadAllText(fileToReadInOtherChildFolder.VirtualPath);
this.DehydrateShouldSucceed(
new[] { $"{parentFolderToDehydrate} {FolderDehydrateSuccessfulMessage}", $"Cannot dehydrate folder '{childFolderToDehydrate}': '{childFolderToDehydrate}' does not exist." },
confirm: true,
noStatus: false,
foldersToDehydrate: string.Join(";", parentFolderToDehydrate, childFolderToDehydrate));
this.Enlistment.UnmountGVFS();
// Use the backing path because on some platforms
// the virtual path is no longer accessible after unmounting.
fileToReadInChildFolder.BackingPath.ShouldNotExistOnDisk(this.fileSystem);
fileToReadInOtherChildFolder.BackingPath.ShouldNotExistOnDisk(this.fileSystem);
}
[TestCase]
public void FolderDehydrateParentFolderInModifiedPathsShouldOutputMessage()
{
string folderToDehydrateParentFolder = "GitCommandsTests";
TestPath folderToDelete = new TestPath(this.Enlistment, folderToDehydrateParentFolder);
this.fileSystem.DeleteDirectory(folderToDelete.VirtualPath);
GitProcess.Invoke(this.Enlistment.RepoRoot, "reset --hard");
string folderToDehydrate = Path.Combine(folderToDehydrateParentFolder, "DeleteFileTests");
this.Enlistment.GetVirtualPathTo(folderToDehydrate).ShouldBeADirectory(this.fileSystem);
this.DehydrateShouldSucceed(new[] { $"Cannot dehydrate folder '{folderToDehydrate}': Must dehydrate parent folder '{folderToDehydrateParentFolder}/'." }, confirm: true, noStatus: false, foldersToDehydrate: folderToDehydrate);
}
[TestCase]
public void FolderDehydrateDirtyStatusShouldFail()
{
string folderToDehydrate = "GVFS";
TestPath fileToCreate = new TestPath(this.Enlistment, Path.Combine(folderToDehydrate, $"{nameof(this.FolderDehydrateDirtyStatusShouldFail)}.txt"));
this.fileSystem.WriteAllText(fileToCreate.VirtualPath, "new file contents");
fileToCreate.VirtualPath.ShouldBeAFile(this.fileSystem);
this.DehydrateShouldFail(new[] { "Running git status...Failed", "Untracked files:", "git status reported that you have dirty files" }, noStatus: false, foldersToDehydrate: folderToDehydrate);
GitProcess.Invoke(this.Enlistment.RepoRoot, "clean -xdf");
}
[TestCase]
public void FolderDehydrateDirtyStatusWithNoStatusShouldFail()
{
string folderToDehydrate = "GVFS";
TestPath fileToCreate = new TestPath(this.Enlistment, Path.Combine(folderToDehydrate, $"{nameof(this.FolderDehydrateDirtyStatusWithNoStatusShouldFail)}.txt"));
this.fileSystem.WriteAllText(fileToCreate.VirtualPath, "new file contents");
fileToCreate.VirtualPath.ShouldBeAFile(this.fileSystem);
this.DehydrateShouldFail(new[] { "Dehydrate --no-status not valid with --folders" }, noStatus: true, foldersToDehydrate: folderToDehydrate);
GitProcess.Invoke(this.Enlistment.RepoRoot, "clean -xdf");
}
[TestCase]
public void FolderDehydrateCannotDehydrateDotGitFolder()
{
this.DehydrateShouldSucceed(new[] { $"Cannot dehydrate folder '{TestConstants.DotGit.Root}': invalid folder path." }, confirm: true, noStatus: false, foldersToDehydrate: TestConstants.DotGit.Root);
this.DehydrateShouldSucceed(new[] { $"Cannot dehydrate folder '{TestConstants.DotGit.Info.Root}': invalid folder path." }, confirm: true, noStatus: false, foldersToDehydrate: TestConstants.DotGit.Info.Root);
}
[TestCase]
public void FolderDehydratePreviouslyDeletedFolders()
{
string folderToDehydrate = "TrailingSlashTests";
TestPath folderToDelete = new TestPath(this.Enlistment, folderToDehydrate);
string secondFolderToDehydrate = "FilenameEncoding";
TestPath secondFolderToDelete = new TestPath(this.Enlistment, secondFolderToDehydrate);
this.fileSystem.DeleteDirectory(folderToDelete.VirtualPath);
this.fileSystem.DeleteDirectory(secondFolderToDelete.VirtualPath);
GitProcess.Invoke(this.Enlistment.RepoRoot, "commit -a -m \"Delete directories\"");
folderToDelete.VirtualPath.ShouldNotExistOnDisk(this.fileSystem);
secondFolderToDelete.VirtualPath.ShouldNotExistOnDisk(this.fileSystem);
GitProcess.Invoke(this.Enlistment.RepoRoot, "checkout -f HEAD~1");
folderToDelete.VirtualPath.ShouldBeADirectory(this.fileSystem);
secondFolderToDelete.VirtualPath.ShouldBeADirectory(this.fileSystem);
this.DehydrateShouldSucceed(
new[]
{
$"{folderToDehydrate} {FolderDehydrateSuccessfulMessage}",
$"{secondFolderToDehydrate} {FolderDehydrateSuccessfulMessage}",
},
confirm: true,
noStatus: false,
foldersToDehydrate: new[] { folderToDehydrate, secondFolderToDehydrate });
folderToDelete.VirtualPath.ShouldBeADirectory(this.fileSystem);
secondFolderToDelete.VirtualPath.ShouldBeADirectory(this.fileSystem);
}
[TestCase]
public void FolderDehydrateTombstone()
{
string folderToDehydrate = "TrailingSlashTests";
TestPath folderToDelete = new TestPath(this.Enlistment, folderToDehydrate);
this.fileSystem.DeleteDirectory(folderToDelete.VirtualPath);
GitProcess.Invoke(this.Enlistment.RepoRoot, "commit -a -m \"Delete a directory\"");
this.DehydrateShouldSucceed(new[] { $"{folderToDehydrate} {FolderDehydrateSuccessfulMessage}" }, confirm: true, noStatus: false, foldersToDehydrate: folderToDehydrate);
folderToDelete.VirtualPath.ShouldNotExistOnDisk(this.fileSystem);
GitProcess.Invoke(this.Enlistment.RepoRoot, "checkout HEAD~1");
folderToDelete.VirtualPath.ShouldBeADirectory(this.fileSystem);
}
[TestCase]
public void FolderDehydrateRelativePaths()
{
string[] foldersToDehydrate = new[]
{
Path.Combine("..", ".gvfs"),
Path.DirectorySeparatorChar + Path.Combine("..", ".gvfs"),
Path.Combine("GVFS", "..", "..", ".gvfs"),
Path.Combine("GVFS/../../.gvfs"),
};
List<string> errorMessages = new List<string>();
foreach (string path in foldersToDehydrate)
{
errorMessages.Add($"Cannot dehydrate folder '{path}': invalid folder path.");
}
this.DehydrateShouldSucceed(
errorMessages.ToArray(),
confirm: true,
noStatus: false,
foldersToDehydrate: foldersToDehydrate);
}
[TestCase]
public void FolderDehydrateFolderThatDoesNotExist()
{
string folderToDehydrate = "DoesNotExist";
this.DehydrateShouldSucceed(new[] { $"Cannot dehydrate folder '{folderToDehydrate}': '{folderToDehydrate}' does not exist." }, confirm: true, noStatus: false, foldersToDehydrate: folderToDehydrate);
}
[TestCase]
public void FolderDehydrateNewlyCreatedFolderAndFile()
{
string folderToDehydrate = "NewFolder";
TestPath folderToCreate = new TestPath(this.Enlistment, folderToDehydrate);
this.fileSystem.CreateDirectory(folderToCreate.VirtualPath);
TestPath fileToCreate = new TestPath(this.Enlistment, Path.Combine(folderToDehydrate, "newfile.txt"));
this.fileSystem.WriteAllText(fileToCreate.VirtualPath, "Test content");
GitProcess.Invoke(this.Enlistment.RepoRoot, "add .");
GitProcess.Invoke(this.Enlistment.RepoRoot, "commit -m Test");
this.DehydrateShouldSucceed(new[] { $"{folderToDehydrate} {FolderDehydrateSuccessfulMessage}" }, confirm: true, noStatus: false, foldersToDehydrate: folderToDehydrate);
this.Enlistment.UnmountGVFS();
// Use the backing path because on some platforms
// the virtual path is no longer accessible after unmounting.
fileToCreate.BackingPath.ShouldNotExistOnDisk(this.fileSystem);
this.CheckDehydratedFolderAfterUnmount(folderToCreate.BackingPath);
}
private void PlaceholdersShouldContain(params string[] paths)
{
string[] placeholderLines = this.GetPlaceholderDatabaseLines();
foreach (string path in paths)
{
placeholderLines.ShouldContain(x => x.StartsWith(path + GVFSHelpers.PlaceholderFieldDelimiter, FileSystemHelpers.PathComparison));
}
}
private void PlaceholdersShouldNotContain(params string[] paths)
{
string[] placeholderLines = this.GetPlaceholderDatabaseLines();
foreach (string path in paths)
{
placeholderLines.ShouldNotContain(x => x.StartsWith(path + Path.DirectorySeparatorChar, FileSystemHelpers.PathComparison) || x.Equals(path, FileSystemHelpers.PathComparison));
}
}
private string[] GetPlaceholderDatabaseLines()
{
string placeholderDatabase = Path.Combine(this.Enlistment.DotGVFSRoot, "databases", "VFSForGit.sqlite");
return GVFSHelpers.GetAllSQLitePlaceholdersAsString(placeholderDatabase).Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
}
private void DirectoryShouldContain(string directory, params string[] fileOrFolders)
{
IEnumerable<string> onDiskItems =
this.fileSystem.EnumerateDirectory(directory)
.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
.Select(path => Path.GetFileName(path))
.OrderByDescending(x => x);
onDiskItems.ShouldMatchInOrder(fileOrFolders.OrderByDescending(x => x));
}
private void CheckDehydratedFolderAfterUnmount(string path)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
path.ShouldNotExistOnDisk(this.fileSystem);
}
else
{
path.ShouldBeADirectory(this.fileSystem);
}
}
private void DehydrateShouldSucceed(string[] expectedInOutput, bool confirm, bool noStatus, bool full = false, params string[] foldersToDehydrate)
{
ProcessResult result = this.RunDehydrateProcess(confirm, noStatus, full, foldersToDehydrate);
result.ExitCode.ShouldEqual(0, $"mount exit code was {result.ExitCode}. Output: {result.Output}");
if (result.Output.Contains("Failed to move the src folder: Access to the path"))
{
string output = this.RunHandleProcess(Path.Combine(this.Enlistment.EnlistmentRoot, "src"));
TestContext.Out.WriteLine(output);
}
result.Output.ShouldContain(expectedInOutput);
}
private void DehydrateShouldFail(string[] expectedErrorMessages, bool noStatus, bool full = false, params string[] foldersToDehydrate)
{
ProcessResult result = this.RunDehydrateProcess(confirm: true, noStatus: noStatus, full: full, foldersToDehydrate: foldersToDehydrate);
result.ExitCode.ShouldEqual(GVFSGenericError, $"mount exit code was not {GVFSGenericError}");
result.Output.ShouldContain(expectedErrorMessages);
}
private ProcessResult RunDehydrateProcess(bool confirm, bool noStatus, bool full = false, params string[] foldersToDehydrate)
{
string dehydrateFlags = string.Empty;
if (confirm)
{
dehydrateFlags += " --confirm ";
}
if (noStatus)
{
dehydrateFlags += " --no-status ";
}
if (full)
{
dehydrateFlags += " --full ";
}
if (foldersToDehydrate.Length > 0)
{
dehydrateFlags += $" --folders {string.Join(";", foldersToDehydrate)}";
}
string enlistmentRoot = this.Enlistment.EnlistmentRoot;
ProcessStartInfo processInfo = new ProcessStartInfo(GVFSTestConfig.PathToGVFS);
processInfo.Arguments = "dehydrate " + dehydrateFlags + " " + TestConstants.InternalUseOnlyFlag + " " + GVFSHelpers.GetInternalParameter();
processInfo.WindowStyle = ProcessWindowStyle.Hidden;
processInfo.WorkingDirectory = enlistmentRoot;
processInfo.UseShellExecute = false;
processInfo.RedirectStandardOutput = true;
return ProcessHelper.Run(processInfo);
}
private SafeFileHandle OpenFolderHandle(string path)
{
return NativeMethods.CreateFile(
path,
(uint)FileAccess.Read,
FileShare.Read,
IntPtr.Zero,
FileMode.Open,
FileFlagBackupSemantics,
IntPtr.Zero);
}
private string RunHandleProcess(string path)
{
try
{
ProcessStartInfo processInfo = new ProcessStartInfo("handle.exe");
processInfo.Arguments = "-p " + path;
processInfo.WindowStyle = ProcessWindowStyle.Hidden;
processInfo.WorkingDirectory = this.Enlistment.EnlistmentRoot;
processInfo.UseShellExecute = false;
processInfo.RedirectStandardOutput = true;
return "handle.exe output: " + ProcessHelper.Run(processInfo).Output;
}
catch (Exception ex)
{
return $"Exception running handle.exe - {ex.Message}";
}
}
private class TestPath
{
public TestPath(GVFSFunctionalTestEnlistment enlistment, string basePath)
{
this.BasePath = basePath;
this.VirtualPath = enlistment.GetVirtualPathTo(basePath);
this.BackingPath = enlistment.GetBackingPathTo(basePath);
}
public string BasePath { get; }
public string VirtualPath { get; }
public string BackingPath { get; }
}
}
}