|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using LibGit2Sharp.Tests.TestHelpers; |
| 4 | +using Xunit; |
| 5 | + |
| 6 | +namespace LibGit2Sharp.Tests |
| 7 | +{ |
| 8 | + public class StashFixture : BaseFixture |
| 9 | + { |
| 10 | + [Fact] |
| 11 | + public void CannotAddStashAgainstBareRepository() |
| 12 | + { |
| 13 | + TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(); |
| 14 | + using (var repo = new Repository(path.RepositoryPath)) |
| 15 | + { |
| 16 | + var stasher = DummySignature; |
| 17 | + |
| 18 | + Assert.Throws<BareRepositoryException>(() => repo.Stashes.Add(stasher, "My very first stash")); |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + [Fact] |
| 23 | + public void CanAddStash() |
| 24 | + { |
| 25 | + TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath); |
| 26 | + using (var repo = new Repository(path.RepositoryPath)) |
| 27 | + { |
| 28 | + var stasher = DummySignature; |
| 29 | + |
| 30 | + Assert.True(repo.Index.RetrieveStatus().IsDirty); |
| 31 | + |
| 32 | + Stash stash = repo.Stashes.Add(stasher, "My very first stash", StashOptions.IncludeUntracked); |
| 33 | + |
| 34 | + // Check that untracked files are deleted from working directory |
| 35 | + Assert.False(File.Exists(Path.Combine(repo.Info.WorkingDirectory, "new_untracked_file.txt"))); |
| 36 | + |
| 37 | + Assert.NotNull(stash); |
| 38 | + Assert.Equal("stash@{0}", stash.CanonicalName); |
| 39 | + Assert.Contains("My very first stash", stash.Message); |
| 40 | + |
| 41 | + var stashRef = repo.Refs["refs/stash"]; |
| 42 | + Assert.Equal(stash.Target.Sha, stashRef.TargetIdentifier); |
| 43 | + |
| 44 | + Assert.False(repo.Index.RetrieveStatus().IsDirty); |
| 45 | + |
| 46 | + // Create extra file |
| 47 | + string newFileFullPath = Path.Combine(repo.Info.WorkingDirectory, "stash_candidate.txt"); |
| 48 | + File.WriteAllText(newFileFullPath, "Oh, I'm going to be stashed!\n"); |
| 49 | + |
| 50 | + Stash secondStash = repo.Stashes.Add(stasher, "My second stash", StashOptions.IncludeUntracked); |
| 51 | + |
| 52 | + Assert.NotNull(stash); |
| 53 | + Assert.Equal("stash@{0}", stash.CanonicalName); |
| 54 | + Assert.Contains("My second stash", secondStash.Message); |
| 55 | + |
| 56 | + // Stash history has been shifted |
| 57 | + Assert.Equal(repo.Lookup<Commit>("stash@{0}").Sha, secondStash.Target.Sha); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + [Fact] |
| 62 | + public void AddingAStashWithNoMessageGeneratesADefaultOne() |
| 63 | + { |
| 64 | + TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath); |
| 65 | + using (var repo = new Repository(path.RepositoryPath)) |
| 66 | + { |
| 67 | + var stasher = DummySignature; |
| 68 | + |
| 69 | + Stash stash = repo.Stashes.Add(stasher); |
| 70 | + |
| 71 | + Assert.NotNull(stash); |
| 72 | + Assert.Equal("stash@{0}", stash.CanonicalName); |
| 73 | + Assert.NotEmpty(stash.Target.Message); |
| 74 | + |
| 75 | + var stashRef = repo.Refs["refs/stash"]; |
| 76 | + Assert.Equal(stash.Target.Sha, stashRef.TargetIdentifier); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + [Fact] |
| 81 | + public void AddStashWithBadParamsShouldThrows() |
| 82 | + { |
| 83 | + TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath); |
| 84 | + using (var repo = new Repository(path.RepositoryPath)) |
| 85 | + { |
| 86 | + Assert.Throws<ArgumentNullException>(() => repo.Stashes.Add(null)); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + [Fact] |
| 91 | + public void StashingAgainstCleanWorkDirShouldReturnANullStash() |
| 92 | + { |
| 93 | + TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath); |
| 94 | + using (var repo = new Repository(path.RepositoryPath)) |
| 95 | + { |
| 96 | + var stasher = DummySignature; |
| 97 | + |
| 98 | + Stash stash = repo.Stashes.Add(stasher, "My very first stash", StashOptions.IncludeUntracked); |
| 99 | + |
| 100 | + Assert.NotNull(stash); |
| 101 | + |
| 102 | + //Stash against clean working directory |
| 103 | + Assert.Null(repo.Stashes.Add(stasher)); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + [Fact] |
| 108 | + public void CanStashWithoutOptions() |
| 109 | + { |
| 110 | + TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath); |
| 111 | + using (var repo = new Repository(path.RepositoryPath)) |
| 112 | + { |
| 113 | + var stasher = DummySignature; |
| 114 | + |
| 115 | + var untrackedFilePath = Path.Combine(repo.Info.WorkingDirectory, "new_untracked_file.txt"); |
| 116 | + File.WriteAllText(untrackedFilePath, "I'm untracked\n"); |
| 117 | + |
| 118 | + string stagedfilePath = Path.Combine(repo.Info.WorkingDirectory, "staged_file_path.txt"); |
| 119 | + File.WriteAllText(stagedfilePath, "I'm staged\n"); |
| 120 | + repo.Index.Stage(stagedfilePath); |
| 121 | + |
| 122 | + Stash stash = repo.Stashes.Add(stasher, "Stash with default options"); |
| 123 | + |
| 124 | + Assert.NotNull(stash); |
| 125 | + |
| 126 | + //It should not keep staged files |
| 127 | + Assert.Equal(FileStatus.Nonexistent, repo.Index.RetrieveStatus("staged_file_path.txt")); |
| 128 | + |
| 129 | + //It should leave untracked files untracked |
| 130 | + Assert.Equal(FileStatus.Untracked, repo.Index.RetrieveStatus("new_untracked_file.txt")); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + [Fact] |
| 135 | + public void CanStashAndKeepIndex() |
| 136 | + { |
| 137 | + TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath); |
| 138 | + using (var repo = new Repository(path.RepositoryPath)) |
| 139 | + { |
| 140 | + var stasher = DummySignature; |
| 141 | + |
| 142 | + string stagedfilePath = Path.Combine(repo.Info.WorkingDirectory, "staged_file_path.txt"); |
| 143 | + File.WriteAllText(stagedfilePath, "I'm staged\n"); |
| 144 | + repo.Index.Stage(stagedfilePath); |
| 145 | + |
| 146 | + Stash stash = repo.Stashes.Add(stasher, "This stash wil keep index", StashOptions.KeepIndex); |
| 147 | + |
| 148 | + Assert.NotNull(stash); |
| 149 | + Assert.Equal(FileStatus.Added, repo.Index.RetrieveStatus("staged_file_path.txt")); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + [Fact] |
| 154 | + public void CanStashIgnoredFiles() |
| 155 | + { |
| 156 | + TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath); |
| 157 | + |
| 158 | + using (var repo = new Repository(path.RepositoryPath)) |
| 159 | + { |
| 160 | + string gitIgnoreFilePath = Path.Combine(repo.Info.WorkingDirectory, ".gitignore"); |
| 161 | + File.WriteAllText(gitIgnoreFilePath, "ignored_file.txt"); |
| 162 | + repo.Index.Stage(gitIgnoreFilePath); |
| 163 | + repo.Commit("Modify gitignore", Constants.Signature, Constants.Signature); |
| 164 | + |
| 165 | + string ignoredFilePath = Path.Combine(repo.Info.WorkingDirectory, "ignored_file.txt"); |
| 166 | + File.WriteAllText(ignoredFilePath, "I'm ignored\n"); |
| 167 | + |
| 168 | + Assert.True(repo.Ignore.IsPathIgnored("ignored_file.txt")); |
| 169 | + |
| 170 | + var stasher = DummySignature; |
| 171 | + repo.Stashes.Add(stasher, "This stash includes ignore files", StashOptions.IncludeIgnored); |
| 172 | + |
| 173 | + //TODO : below assertion doesn't pass. Bug? |
| 174 | + //Assert.False(File.Exists(ignoredFilePath)); |
| 175 | + |
| 176 | + var blob = repo.Lookup<Blob>("stash^3:ignored_file.txt"); |
| 177 | + Assert.NotNull(blob); |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments