Skip to content

Commit 247e385

Browse files
authored
Fix running tests on macOS (#422)
* Fix running tests on macOS On macOS, the temporary directory contains a symlink and all tests using `DisposableRepository` would fail with the following error: > System.ArgumentException Unable to process file '/var/folders/62/pz7b9x0x7hv54vy47pllqlvw0000gn/T/edic1ukd/.gitignore'. This file is not located under the working directory of the repository ('/private/var/folders/62/pz7b9x0x7hv54vy47pllqlvw0000gn/T/edic1ukd/'). at LibGit2Sharp.RepositoryExtensions.BuildRelativePathFrom(IRepository repo, String path) in /_/LibGit2Sharp/RepositoryExtensions.cs:line 215 at LibGit2Sharp.Repository.ToFilePaths(IEnumerable`1 paths) in /_/LibGit2Sharp/Repository.cs:line 1676 at LibGit2Sharp.Diff.BuildDiffList(ObjectId oldTreeId, ObjectId newTreeId, TreeComparisonHandleRetriever comparisonHandleRetriever, DiffModifiers diffOptions, IEnumerable`1 paths, ExplicitPathsOptions explicitPathsOptions, CompareOptions compareOptions) in /_/LibGit2Sharp/Diff.cs:line 544 at LibGit2Sharp.Diff.Compare[T](DiffModifiers diffOptions, IEnumerable`1 paths, ExplicitPathsOptions explicitPathsOptions, CompareOptions compareOptions) in /_/LibGit2Sharp/Diff.cs:line 483 at LibGit2Sharp.Commands.Stage(IRepository repository, IEnumerable`1 paths, StageOptions stageOptions) in /_/LibGit2Sharp/Commands/Stage.cs:line 77 at LibGit2Sharp.Commands.Stage(IRepository repository, String path) in /_/LibGit2Sharp/Commands/Stage.cs:line 25 at Incrementalist.Tests.Helpers.DisposableRepository.WriteFile(String fileName, String fileText) in ~/Incrementalist/src/Incrementalist.Tests/Helpers/DisposableRepository.cs:line 131 at Incrementalist.Tests.Helpers.DisposableRepository.Init() in ~/Incrementalist/src/Incrementalist.Tests/Helpers/DisposableRepository.cs:line 93 at Incrementalist.Tests.Helpers.DisposableRepository..ctor(AbsolutePath basePath) in ~/Incrementalist/src/Incrementalist.Tests/Helpers/DisposableRepository.cs:line 56 at Incrementalist.Tests.Helpers.DisposableRepository..ctor() in ~/Incrementalist/src/Incrementalist.Tests/Helpers/DisposableRepository.cs:line 46 at Incrementalist.Tests.Git.GitBranchDetectionSpecs..ctor() in ~/Incrementalist/src/Incrementalist.Tests/Git/GitBranchDetectionSpecs.cs:line 18 at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean wrapExceptions) See also libgit2/libgit2sharp#1945 * Ensure that the default branch is named `master` Many tests are assuming the default branch is `master`. But if the `~/.gitconfig` file contains another default branch name, then all these tests will fail. ```ini [init] defaultBranch = "main" ``` See [How to control the name of the default branch from Repository.Init?][1] on the LibGit2Sharp repository. [1]: libgit2/libgit2sharp#1964
1 parent 97b6c47 commit 247e385

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/Incrementalist.Tests/Helpers/DisposableRepository.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,19 @@ public DisposableRepository() : this(CreateTempDirectory())
4949

5050
public DisposableRepository(AbsolutePath basePath)
5151
{
52-
BasePath = basePath;
52+
var repoPath = new DirectoryInfo(Repository.Init(basePath.Path));
53+
BasePath = new AbsolutePath(repoPath.Parent?.FullName ?? "");
54+
Repository = new Repository(repoPath.FullName);
5355
Init();
5456
}
5557

5658
public AbsolutePath BasePath { get; }
5759

58-
// Gets created via CTOR method call, so can't be null unless catastrophic failure
59-
public Repository Repository { get; private set; } = null!;
60+
public Repository Repository { get; }
6061

6162
public void Dispose()
6263
{
63-
Repository?.Dispose();
64+
Repository.Dispose();
6465
for (var attempt = 1; attempt <= MaxDeleteAttempts; attempt++)
6566
try
6667
{
@@ -86,13 +87,16 @@ public static AbsolutePath CreateTempDirectory()
8687

8788
private void Init()
8889
{
89-
var repoPath = Repository.Init(BasePath.Path);
90-
Repository = new Repository(repoPath);
9190
var sig = CreateSignature();
9291
// add a .gitignore file to the repository immediately
9392
WriteFile(GitIgnoreFileName, GitIgnoreContent);
9493
Repository.Commit("First", sig, sig);
95-
//Repository.CreateBranch("master"); // setup the master branch initially
94+
if (Repository.Refs.Head.TargetIdentifier != "refs/heads/master")
95+
{
96+
// ensure the default branch is named "master", see https://github.com/libgit2/libgit2sharp/issues/1964
97+
var master = Repository.CreateBranch("master");
98+
Repository.Refs.UpdateTarget(Repository.Refs.Head, master.Reference);
99+
}
96100
}
97101

98102
/// <summary>

0 commit comments

Comments
 (0)