Skip to content

Commit 3db1f92

Browse files
jamillnulltoken
authored andcommitted
Update Checkout tests to reset and clean working directory
Checkout tests were originally written under the assumption that git reset would remove untracked files (reset, as implemented in LibGit2Sharp, originally mistakenly removed untracked files). This updates the tests to reset and clean the working directory.
1 parent afbc000 commit 3db1f92

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

LibGit2Sharp.Tests/CheckoutFixture.cs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ public void CanCheckoutAnExistingBranch(string branchName)
2323
Branch master = repo.Branches["master"];
2424
Assert.True(master.IsCurrentRepositoryHead);
2525

26-
// Hard reset to ensure that working directory, index, and HEAD match
27-
repo.Reset(ResetOptions.Hard);
26+
// Set the working directory to the current head
27+
ResetAndCleanWorkingDirectory(repo);
28+
2829
Assert.False(repo.Index.RetrieveStatus().IsDirty);
2930

3031
Branch branch = repo.Branches[branchName];
@@ -55,8 +56,9 @@ public void CanCheckoutAnExistingBranchByName(string branchName)
5556
Branch master = repo.Branches["master"];
5657
Assert.True(master.IsCurrentRepositoryHead);
5758

58-
// Hard reset to ensure that working directory, index, and HEAD match
59-
repo.Reset(ResetOptions.Hard);
59+
// Set the working directory to the current head
60+
ResetAndCleanWorkingDirectory(repo);
61+
6062
Assert.False(repo.Index.RetrieveStatus().IsDirty);
6163

6264
Branch test = repo.Checkout(branchName);
@@ -84,8 +86,9 @@ public void CanCheckoutAnArbitraryCommit(string commitPointer)
8486
Branch master = repo.Branches["master"];
8587
Assert.True(master.IsCurrentRepositoryHead);
8688

87-
// Hard reset to ensure that working directory, index, and HEAD match
88-
repo.Reset(ResetOptions.Hard);
89+
// Set the working directory to the current head
90+
ResetAndCleanWorkingDirectory(repo);
91+
8992
Assert.False(repo.Index.RetrieveStatus().IsDirty);
9093

9194
Branch detachedHead = repo.Checkout(commitPointer);
@@ -197,8 +200,9 @@ public void CanForcefullyCheckoutWithStagedChanges()
197200
Branch master = repo.Branches["master"];
198201
Assert.True(master.IsCurrentRepositoryHead);
199202

200-
// Hard reset to ensure that working directory, index, and HEAD match
201-
repo.Reset(ResetOptions.Hard);
203+
// Set the working directory to the current head
204+
ResetAndCleanWorkingDirectory(repo);
205+
202206
Assert.False(repo.Index.RetrieveStatus().IsDirty);
203207

204208
// Add local change
@@ -335,5 +339,19 @@ private void PopulateBasicRepository(Repository repo)
335339

336340
repo.CreateBranch(otherBranchName);
337341
}
342+
343+
/// <summary>
344+
/// Reset and clean current working directory. This will ensure that the current
345+
/// working directory matches the current Head commit.
346+
/// </summary>
347+
/// <param name="repo">Repository whose current working directory should be operated on.</param>
348+
private void ResetAndCleanWorkingDirectory(Repository repo)
349+
{
350+
// Reset the index and the working tree.
351+
repo.Reset(ResetOptions.Hard);
352+
353+
// Clean the working directory.
354+
repo.RemoveUntrackedFiles();
355+
}
338356
}
339357
}

0 commit comments

Comments
 (0)