|
| 1 | +using System; |
| 2 | +using System.IO; |
1 | 3 | using System.Linq;
|
| 4 | +using System.Text; |
2 | 5 | using LibGit2Sharp.Tests.TestHelpers;
|
3 | 6 | using Xunit;
|
| 7 | +using Xunit.Extensions; |
4 | 8 |
|
5 | 9 | namespace LibGit2Sharp.Tests
|
6 | 10 | {
|
@@ -36,6 +40,129 @@ public void CanCompareTheWorkDirAgainstTheIndex()
|
36 | 40 | }
|
37 | 41 | }
|
38 | 42 |
|
| 43 | + [Theory] |
| 44 | + [InlineData("new_untracked_file.txt", FileStatus.Untracked)] |
| 45 | + [InlineData("really-i-cant-exist.txt", FileStatus.Nonexistent)] |
| 46 | + public void CanCompareTheWorkDirAgainstTheIndexWithLaxUnmatchedExplicitPathsValidation(string relativePath, FileStatus currentStatus) |
| 47 | + { |
| 48 | + using (var repo = new Repository(StandardTestRepoPath)) |
| 49 | + { |
| 50 | + Assert.Equal(currentStatus, repo.Index.RetrieveStatus(relativePath)); |
| 51 | + |
| 52 | + TreeChanges changes = repo.Diff.Compare(new[] { relativePath }, false, new ExplicitPathsOptions { ShouldFailOnUnmatchedPath = false }); |
| 53 | + Assert.Equal(0, changes.Count()); |
| 54 | + |
| 55 | + changes = repo.Diff.Compare(new[] { relativePath }); |
| 56 | + Assert.Equal(0, changes.Count()); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + [Theory] |
| 61 | + [InlineData("new_untracked_file.txt", FileStatus.Untracked)] |
| 62 | + [InlineData("really-i-cant-exist.txt", FileStatus.Nonexistent)] |
| 63 | + public void ComparingTheWorkDirAgainstTheIndexWithStrictUnmatchedExplicitPathsValidationAndANonExistentPathspecThrows(string relativePath, FileStatus currentStatus) |
| 64 | + { |
| 65 | + using (var repo = new Repository(StandardTestRepoPath)) |
| 66 | + { |
| 67 | + Assert.Equal(currentStatus, repo.Index.RetrieveStatus(relativePath)); |
| 68 | + |
| 69 | + Assert.Throws<UnmatchedPathException>(() => repo.Diff.Compare(new[] { relativePath }, false, new ExplicitPathsOptions())); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + [Theory] |
| 74 | + [InlineData("new_untracked_file.txt", FileStatus.Untracked)] |
| 75 | + [InlineData("where-am-I.txt", FileStatus.Nonexistent)] |
| 76 | + public void CallbackForUnmatchedExplicitPathsIsCalledWhenSet(string relativePath, FileStatus currentStatus) |
| 77 | + { |
| 78 | + var callback = new AssertUnmatchedPathspecsCallbackIsCalled(); |
| 79 | + |
| 80 | + using (var repo = new Repository(StandardTestRepoPath)) |
| 81 | + { |
| 82 | + Assert.Equal(currentStatus, repo.Index.RetrieveStatus(relativePath)); |
| 83 | + |
| 84 | + repo.Diff.Compare(new[] { relativePath }, false, new ExplicitPathsOptions { ShouldFailOnUnmatchedPath = false, |
| 85 | + OnUnmatchedPath = callback.OnUnmatchedPath }); |
| 86 | + |
| 87 | + Assert.True(callback.WasCalled); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private class AssertUnmatchedPathspecsCallbackIsCalled |
| 92 | + { |
| 93 | + public bool WasCalled; |
| 94 | + |
| 95 | + public void OnUnmatchedPath(string unmatchedpath) |
| 96 | + { |
| 97 | + WasCalled = true; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + [Fact] |
| 102 | + public void ComparingReliesOnProvidedConfigEntriesIfAny() |
| 103 | + { |
| 104 | + const string file = "1/branch_file.txt"; |
| 105 | + |
| 106 | + string path = CloneStandardTestRepo(); |
| 107 | + using (var repo = new Repository(path)) |
| 108 | + { |
| 109 | + TreeEntry entry = repo.Head[file]; |
| 110 | + Assert.Equal(Mode.ExecutableFile, entry.Mode); |
| 111 | + |
| 112 | + // Recreate the file in the workdir without the executable bit |
| 113 | + string fullpath = Path.Combine(repo.Info.WorkingDirectory, file); |
| 114 | + File.Delete(fullpath); |
| 115 | + File.WriteAllBytes(fullpath, ((Blob)(entry.Target)).Content); |
| 116 | + |
| 117 | + // Unset the local core.filemode, if any. |
| 118 | + repo.Config.Unset("core.filemode", ConfigurationLevel.Local); |
| 119 | + } |
| 120 | + |
| 121 | + SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); |
| 122 | + |
| 123 | + var options = BuildFakeSystemConfigFilemodeOption(scd, true); |
| 124 | + |
| 125 | + using (var repo = new Repository(path, options)) |
| 126 | + { |
| 127 | + TreeChanges changes = repo.Diff.Compare(new[] { file }); |
| 128 | + |
| 129 | + Assert.Equal(1, changes.Count()); |
| 130 | + |
| 131 | + var change = changes.Modified.Single(); |
| 132 | + Assert.Equal(Mode.ExecutableFile, change.OldMode); |
| 133 | + Assert.Equal(Mode.NonExecutableFile, change.Mode); |
| 134 | + } |
| 135 | + |
| 136 | + options = BuildFakeSystemConfigFilemodeOption(scd, false); |
| 137 | + |
| 138 | + using (var repo = new Repository(path, options)) |
| 139 | + { |
| 140 | + TreeChanges changes = repo.Diff.Compare(new[] { file }); |
| 141 | + |
| 142 | + Assert.Equal(0, changes.Count()); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + private RepositoryOptions BuildFakeSystemConfigFilemodeOption( |
| 147 | + SelfCleaningDirectory scd, |
| 148 | + bool value) |
| 149 | + { |
| 150 | + Directory.CreateDirectory(scd.DirectoryPath); |
| 151 | + |
| 152 | + var options = new RepositoryOptions |
| 153 | + { |
| 154 | + SystemConfigurationLocation = Path.Combine( |
| 155 | + scd.RootedDirectoryPath, "fake-system.config") |
| 156 | + }; |
| 157 | + |
| 158 | + StringBuilder sb = new StringBuilder() |
| 159 | + .AppendFormat("[core]{0}", Environment.NewLine) |
| 160 | + .AppendFormat("filemode = {1}{0}", Environment.NewLine, value); |
| 161 | + File.WriteAllText(options.SystemConfigurationLocation, sb.ToString()); |
| 162 | + |
| 163 | + return options; |
| 164 | + } |
| 165 | + |
39 | 166 | [Fact]
|
40 | 167 | public void CanCompareTheWorkDirAgainstTheIndexWithUntrackedFiles()
|
41 | 168 | {
|
|
0 commit comments