diff options
author | nulltoken <emeric.fermas@gmail.com> | 2012-06-06 21:46:02 +0400 |
---|---|---|
committer | nulltoken <emeric.fermas@gmail.com> | 2012-06-06 22:39:26 +0400 |
commit | b93a9e88cd99daad720e75793261b750df24c997 (patch) | |
tree | fe285394e2840e23952faa7e6b4db7b2959966c1 /LibGit2Sharp.Tests | |
parent | 9ea588955c8c971f39447a6cdcf7036caff654f3 (diff) |
Add repo.Reset() overload to allow replacing of index entries with commit entries
Fix #165
Diffstat (limited to 'LibGit2Sharp.Tests')
-rw-r--r-- | LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj | 3 | ||||
-rw-r--r-- | LibGit2Sharp.Tests/ResetHeadFixture.cs (renamed from LibGit2Sharp.Tests/ResetFixture.cs) | 2 | ||||
-rw-r--r-- | LibGit2Sharp.Tests/ResetIndexFixture.cs | 103 |
3 files changed, 106 insertions, 2 deletions
diff --git a/LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj b/LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj index 7020fd38..3f85a748 100644 --- a/LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj +++ b/LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj @@ -62,9 +62,10 @@ <Compile Include="ObjectDatabaseFixture.cs" /> <Compile Include="DiffTreeToTreeFixture.cs" /> <Compile Include="RepositoryOptionsFixture.cs" /> - <Compile Include="ResetFixture.cs" /> + <Compile Include="ResetHeadFixture.cs" /> <Compile Include="LazyFixture.cs" /> <Compile Include="RemoteFixture.cs" /> + <Compile Include="ResetIndexFixture.cs" /> <Compile Include="StatusFixture.cs" /> <Compile Include="TestHelpers\BaseFixture.cs" /> <Compile Include="BlobFixture.cs" /> diff --git a/LibGit2Sharp.Tests/ResetFixture.cs b/LibGit2Sharp.Tests/ResetHeadFixture.cs index b2912701..44d43f06 100644 --- a/LibGit2Sharp.Tests/ResetFixture.cs +++ b/LibGit2Sharp.Tests/ResetHeadFixture.cs @@ -6,7 +6,7 @@ using Xunit.Extensions; namespace LibGit2Sharp.Tests { - public class ResetFixture : BaseFixture + public class ResetHeadFixture : BaseFixture { [Theory] [InlineData(true)] diff --git a/LibGit2Sharp.Tests/ResetIndexFixture.cs b/LibGit2Sharp.Tests/ResetIndexFixture.cs new file mode 100644 index 00000000..ac9f6815 --- /dev/null +++ b/LibGit2Sharp.Tests/ResetIndexFixture.cs @@ -0,0 +1,103 @@ +using System.IO; +using System.Linq; +using LibGit2Sharp.Tests.TestHelpers; +using Xunit; +using Xunit.Extensions; + +namespace LibGit2Sharp.Tests +{ + public class ResetIndexFixture : BaseFixture + { + [Theory] + [InlineData(true)] + [InlineData(false)] + public void ResetANewlyInitializedRepositoryThrows(bool isBare) + { + SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); + + using (Repository repo = Repository.Init(scd.DirectoryPath, isBare)) + { + Assert.Throws<LibGit2SharpException>(() => repo.Reset()); + } + } + + [Fact] + public void ResettingInABareRepositoryThrows() + { + using (var repo = new Repository(BareTestRepoPath)) + { + Assert.Throws<LibGit2SharpException>(() => repo.Reset()); + } + } + + private bool IsStaged(StatusEntry entry) + { + if ((entry.State & FileStatus.Added) == FileStatus.Added) + { + return true; + } + + if ((entry.State & FileStatus.Staged) == FileStatus.Staged) + { + return true; + } + + if ((entry.State & FileStatus.Removed) == FileStatus.Removed) + { + return true; + } + + return false; + } + + [Fact] + public void ResetTheIndexWithTheHeadUnstagesEverything() + { + TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath); + + using (var repo = new Repository(path.DirectoryPath)) + { + RepositoryStatus oldStatus = repo.Index.RetrieveStatus(); + Assert.Equal(3, oldStatus.Where(IsStaged).Count()); + + repo.Reset(); + + RepositoryStatus newStatus = repo.Index.RetrieveStatus(); + Assert.Equal(0, newStatus.Where(IsStaged).Count()); + } + } + + [Fact] + public void CanResetTheIndexToTheContentOfACommit() + { + TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath); + + using (var repo = new Repository(path.DirectoryPath)) + { + repo.Reset("be3563a"); + + RepositoryStatus newStatus = repo.Index.RetrieveStatus(); + + var expected = new[] { "1.txt", "1" + Path.DirectorySeparatorChar + "branch_file.txt", "deleted_staged_file.txt", + "deleted_unstaged_file.txt", "modified_staged_file.txt", "modified_unstaged_file.txt" }; + + Assert.Equal(expected.Length, newStatus.Where(IsStaged).Count()); + Assert.Equal(expected, newStatus.Removed); + } + } + + [Fact] + public void CanResetTheIndexToASubsetOfTheContentOfACommit() + { + TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath); + + using (var repo = new Repository(path.DirectoryPath)) + { + repo.Reset("5b5b025", new[]{ "new.txt" }); + + Assert.Equal("a8233120f6ad708f843d861ce2b7228ec4e3dec6", repo.Index["README"].Id.Sha); + Assert.Equal("fa49b077972391ad58037050f2a75f74e3671e92", repo.Index["new.txt"].Id.Sha); + } + } + } +} |