Skip to content

Commit 1bd1cd5

Browse files
committed
Remove the explicit repository isolation in tests
We now isolate all tests by setting the config search paths globally at the start of the fixture.
1 parent 3c3674a commit 1bd1cd5

File tree

9 files changed

+66
-87
lines changed

9 files changed

+66
-87
lines changed

LibGit2Sharp.Tests/AttributesFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class AttributesFixture : BaseFixture
99
[Fact]
1010
public void StagingHonorsTheAttributesFiles()
1111
{
12-
using (var repo = InitIsolatedRepository())
12+
using (var repo = new Repository(InitNewRepository()))
1313
{
1414
CreateAttributesFile(repo);
1515

LibGit2Sharp.Tests/BranchFixture.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -447,11 +447,10 @@ public void QueryRemoteForRemoteBranch()
447447
[Fact]
448448
public void QueryUnresolvableRemoteForRemoteBranch()
449449
{
450-
var path = SandboxStandardTestRepo();
451-
452450
var fetchRefSpecs = new string[] { "+refs/heads/notfound/*:refs/remotes/origin/notfound/*" };
453451

454-
using (var repo = InitIsolatedRepository(path))
452+
var path = SandboxStandardTestRepo();
453+
using (var repo = new Repository(path))
455454
{
456455
// Update the remote config such that the remote for a
457456
// remote branch cannot be resolved
@@ -472,12 +471,11 @@ public void QueryUnresolvableRemoteForRemoteBranch()
472471
[Fact]
473472
public void QueryAmbigousRemoteForRemoteBranch()
474473
{
475-
var path = SandboxStandardTestRepo();
476-
477474
const string fetchRefSpec = "+refs/heads/*:refs/remotes/origin/*";
478475
const string url = "http://github.com/libgit2/TestGitRepository";
479476

480-
using (var repo = InitIsolatedRepository(path))
477+
var path = SandboxStandardTestRepo();
478+
using (var repo = new Repository(path))
481479
{
482480
// Add a second remote so that it is ambiguous which remote
483481
// the remote-tracking branch tracks.

LibGit2Sharp.Tests/ConfigurationFixture.cs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,8 @@ public void CanUnsetAnEntryFromTheLocalConfiguration()
3636
[Fact]
3737
public void CanUnsetAnEntryFromTheGlobalConfiguration()
3838
{
39-
SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
40-
41-
var options = BuildFakeConfigs(scd);
42-
4339
string path = SandboxBareTestRepo();
44-
using (var repo = new Repository(path, options))
40+
using (var repo = new Repository(path))
4541
{
4642
Assert.True(repo.Config.HasConfig(ConfigurationLevel.Global));
4743
Assert.Equal(42, repo.Config.Get<int>("Wow.Man-I-am-totally-global").Value);
@@ -331,12 +327,8 @@ public void SettingUnsupportedTypeThrows()
331327
[Fact]
332328
public void CanGetAnEntryFromASpecificStore()
333329
{
334-
SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
335-
336-
var options = BuildFakeConfigs(scd);
337-
338330
string path = SandboxStandardTestRepo();
339-
using (var repo = new Repository(path, options))
331+
using (var repo = new Repository(path))
340332
{
341333
Assert.True(repo.Config.HasConfig(ConfigurationLevel.Local));
342334
Assert.True(repo.Config.HasConfig(ConfigurationLevel.Global));
@@ -356,12 +348,8 @@ public void CanGetAnEntryFromASpecificStore()
356348
[Fact]
357349
public void CanTellIfASpecificStoreContainsAKey()
358350
{
359-
SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
360-
361-
var options = BuildFakeConfigs(scd);
362-
363351
string path = SandboxBareTestRepo();
364-
using (var repo = new Repository(path, options))
352+
using (var repo = new Repository(path))
365353
{
366354
Assert.True(repo.Config.HasConfig(ConfigurationLevel.System));
367355

LibGit2Sharp.Tests/FetchFixture.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,7 @@ public void FetchHonorsTheFetchPruneConfigurationEntry()
213213

214214
string clonedRepoPath = Repository.Clone(url, scd.DirectoryPath);
215215

216-
var options = BuildFakeConfigs(BuildSelfCleaningDirectory());
217-
218-
using (var clonedRepo = new Repository(clonedRepoPath, options))
216+
using (var clonedRepo = new Repository(clonedRepoPath))
219217
{
220218
Assert.Equal(5, clonedRepo.Branches.Count(b => b.IsRemote));
221219

LibGit2Sharp.Tests/MergeFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ public void MergeCanSpecifyMergeFileFavorOption(MergeFileFavor fileFavorFlag)
678678
const string conflictBranchName = "conflicts";
679679

680680
string path = SandboxMergeTestRepo();
681-
using (var repo = InitIsolatedRepository(path))
681+
using (var repo = new Repository(path))
682682
{
683683
Branch branch = repo.Branches[conflictBranchName];
684684
Assert.NotNull(branch);

LibGit2Sharp.Tests/ObjectDatabaseFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public void CanCreateABlobFromAStream(string expectedSha, string hintPath)
109109
sb.Append("libgit2\n\r\n");
110110
}
111111

112-
using (var repo = InitIsolatedRepository())
112+
using (var repo = new Repository(InitNewRepository()))
113113
{
114114
CreateAttributesFiles(Path.Combine(repo.Info.Path, "info"), "attributes");
115115

LibGit2Sharp.Tests/RefSpecFixture.cs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class RefSpecFixture : BaseFixture
1111
public void CanCountRefSpecs()
1212
{
1313
var path = SandboxStandardTestRepo();
14-
using (var repo = InitIsolatedRepository(path))
14+
using (var repo = new Repository(path))
1515
{
1616
var remote = repo.Network.Remotes["origin"];
1717
Assert.Equal(1, remote.RefSpecs.Count());
@@ -22,7 +22,7 @@ public void CanCountRefSpecs()
2222
public void CanIterateOverRefSpecs()
2323
{
2424
var path = SandboxStandardTestRepo();
25-
using (var repo = InitIsolatedRepository(path))
25+
using (var repo = new Repository(path))
2626
{
2727
var remote = repo.Network.Remotes["origin"];
2828
int count = 0;
@@ -39,7 +39,7 @@ public void CanIterateOverRefSpecs()
3939
public void FetchAndPushRefSpecsComposeRefSpecs()
4040
{
4141
var path = SandboxStandardTestRepo();
42-
using (var repo = InitIsolatedRepository(path))
42+
using (var repo = new Repository(path))
4343
{
4444
var remote = repo.Network.Remotes["origin"];
4545

@@ -53,7 +53,7 @@ public void FetchAndPushRefSpecsComposeRefSpecs()
5353
public void CanReadRefSpecDetails()
5454
{
5555
var path = SandboxStandardTestRepo();
56-
using (var repo = InitIsolatedRepository(path))
56+
using (var repo = new Repository(path))
5757
{
5858
var remote = repo.Network.Remotes["origin"];
5959

@@ -73,7 +73,7 @@ public void CanReadRefSpecDetails()
7373
public void CanReplaceRefSpecs(string[] newFetchRefSpecs, string[] newPushRefSpecs)
7474
{
7575
var path = SandboxStandardTestRepo();
76-
using (var repo = InitIsolatedRepository(path))
76+
using (var repo = new Repository(path))
7777
{
7878
var remote = repo.Network.Remotes["origin"];
7979
var oldRefSpecs = remote.RefSpecs.ToList();
@@ -101,15 +101,15 @@ public void CanReplaceRefSpecs(string[] newFetchRefSpecs, string[] newPushRefSpe
101101
public void RemoteUpdaterSavesRefSpecsPermanently()
102102
{
103103
var fetchRefSpecs = new string[] { "refs/their/heads/*:refs/my/heads/*", "+refs/their/tag:refs/my/tag" };
104-
105104
var path = SandboxStandardTestRepo();
106-
using (var repo = InitIsolatedRepository(path))
105+
106+
using (var repo = new Repository(path))
107107
{
108108
var remote = repo.Network.Remotes["origin"];
109109
repo.Network.Remotes.Update(remote, r => r.FetchRefSpecs = fetchRefSpecs);
110110
}
111111

112-
using (var repo = InitIsolatedRepository(path))
112+
using (var repo = new Repository(path))
113113
{
114114
var remote = repo.Network.Remotes["origin"];
115115
var actualRefSpecs = remote.RefSpecs
@@ -124,9 +124,9 @@ public void RemoteUpdaterSavesRefSpecsPermanently()
124124
public void CanAddAndRemoveRefSpecs()
125125
{
126126
string newRefSpec = "+refs/heads/test:refs/heads/other-test";
127-
128127
var path = SandboxStandardTestRepo();
129-
using (var repo = InitIsolatedRepository(path))
128+
129+
using (var repo = new Repository(path))
130130
{
131131
var remote = repo.Network.Remotes["origin"];
132132

@@ -150,7 +150,7 @@ public void CanAddAndRemoveRefSpecs()
150150
public void CanClearRefSpecs()
151151
{
152152
var path = SandboxStandardTestRepo();
153-
using (var repo = InitIsolatedRepository(path))
153+
using (var repo = new Repository(path))
154154
{
155155
var remote = repo.Network.Remotes["origin"];
156156

@@ -178,7 +178,7 @@ public void CanClearRefSpecs()
178178
public void SettingInvalidRefSpecsThrows(string refSpec)
179179
{
180180
var path = SandboxStandardTestRepo();
181-
using (var repo = InitIsolatedRepository(path))
181+
using (var repo = new Repository(path))
182182
{
183183
var remote = repo.Network.Remotes["origin"];
184184
var oldRefSpecs = remote.RefSpecs.Select(r => r.Specification).ToList();
@@ -198,8 +198,7 @@ public void SettingInvalidRefSpecsThrows(string refSpec)
198198
[InlineData("refs/tags/foo", false, false)]
199199
public void CanCheckForMatches(string reference, bool shouldMatchSource, bool shouldMatchDest)
200200
{
201-
var path = SandboxStandardTestRepo();
202-
using (var repo = InitIsolatedRepository(path))
201+
using (var repo = new Repository(InitNewRepository()))
203202
{
204203
var remote = repo.Network.Remotes.Add("foo", "blahblah", "refs/heads/*:refs/remotes/foo/*");
205204
var refspec = remote.RefSpecs.Single();
@@ -215,8 +214,7 @@ public void CanCheckForMatches(string reference, bool shouldMatchSource, bool sh
215214
[InlineData("refs/heads/master", "refs/remotes/foo/master")]
216215
public void CanTransformRefspecs(string lhs, string rhs)
217216
{
218-
var path = SandboxStandardTestRepo();
219-
using (var repo = InitIsolatedRepository(path))
217+
using (var repo = new Repository(InitNewRepository()))
220218
{
221219
var remote = repo.Network.Remotes.Add("foo", "blahblah", "refs/heads/*:refs/remotes/foo/*");
222220
var refspec = remote.RefSpecs.Single();

LibGit2Sharp.Tests/RemoteFixture.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,9 +377,8 @@ public void CanNotRenameWhenRemoteWithSameNameExists()
377377
public void ShoudlPruneOnFetchReflectsTheConfiguredSetting(bool? fetchPrune, bool? remotePrune, bool expectedFetchPrune)
378378
{
379379
var path = SandboxStandardTestRepo();
380-
var scd = BuildSelfCleaningDirectory();
381380

382-
using (var repo = new Repository(path, BuildFakeConfigs(scd)))
381+
using (var repo = new Repository(path))
383382
{
384383
Assert.Null(repo.Config.Get<bool>("fetch.prune"));
385384
Assert.Null(repo.Config.Get<bool>("remote.origin.prune"));

LibGit2Sharp.Tests/TestHelpers/BaseFixture.cs

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ public class BaseFixture : IPostTestDirectoryRemover, IDisposable
1616
{
1717
private readonly List<string> directories = new List<string>();
1818

19-
#if LEAKS_IDENTIFYING
2019
public BaseFixture()
2120
{
21+
BuildFakeConfigs(this);
22+
23+
#if LEAKS_IDENTIFYING
2224
LeaksContainer.Clear();
23-
}
2425
#endif
26+
}
2527

2628
static BaseFixture()
2729
{
@@ -80,6 +82,35 @@ private static void SetUpTestEnvironment()
8082
CleanupTestReposOlderThan(TimeSpan.FromMinutes(15));
8183
}
8284

85+
public static void BuildFakeConfigs(IPostTestDirectoryRemover dirRemover)
86+
{
87+
var scd = new SelfCleaningDirectory(dirRemover);
88+
89+
string global = null, xdg = null, system = null;
90+
BuildFakeRepositoryOptions(scd, out global, out xdg, out system);
91+
92+
StringBuilder sb = new StringBuilder()
93+
.AppendFormat("[Woot]{0}", Environment.NewLine)
94+
.AppendFormat("this-rocks = global{0}", Environment.NewLine)
95+
.AppendFormat("[Wow]{0}", Environment.NewLine)
96+
.AppendFormat("Man-I-am-totally-global = 42{0}", Environment.NewLine);
97+
File.WriteAllText(Path.Combine(global, ".gitconfig"), sb.ToString());
98+
99+
sb = new StringBuilder()
100+
.AppendFormat("[Woot]{0}", Environment.NewLine)
101+
.AppendFormat("this-rocks = system{0}", Environment.NewLine);
102+
File.WriteAllText(Path.Combine(system, "gitconfig"), sb.ToString());
103+
104+
sb = new StringBuilder()
105+
.AppendFormat("[Woot]{0}", Environment.NewLine)
106+
.AppendFormat("this-rocks = xdg{0}", Environment.NewLine);
107+
File.WriteAllText(Path.Combine(xdg, "config"), sb.ToString());
108+
109+
GlobalSettings.SetConfigSearchPaths(ConfigurationLevel.Global, global);
110+
GlobalSettings.SetConfigSearchPaths(ConfigurationLevel.Xdg, xdg);
111+
GlobalSettings.SetConfigSearchPaths(ConfigurationLevel.System, system);
112+
}
113+
83114
private static void CleanupTestReposOlderThan(TimeSpan olderThan)
84115
{
85116
var oldTestRepos = new DirectoryInfo(Constants.TemporaryReposPath)
@@ -207,14 +238,6 @@ protected string InitNewRepository(bool isBare = false)
207238
return Repository.Init(scd.DirectoryPath, isBare);
208239
}
209240

210-
protected Repository InitIsolatedRepository(string path = null, bool isBare = false, RepositoryOptions options = null)
211-
{
212-
path = path ?? InitNewRepository(isBare);
213-
options = BuildFakeConfigs(BuildSelfCleaningDirectory(), options);
214-
215-
return new Repository(path, options);
216-
}
217-
218241
public void Register(string directoryPath)
219242
{
220243
directories.Add(directoryPath);
@@ -293,42 +316,17 @@ protected static void AssertValueInConfigFile(string configFilePath, string rege
293316
Assert.True(r.Success, text);
294317
}
295318

296-
public RepositoryOptions BuildFakeConfigs(SelfCleaningDirectory scd, RepositoryOptions options = null)
297-
{
298-
options = BuildFakeRepositoryOptions(scd, options);
299-
300-
StringBuilder sb = new StringBuilder()
301-
.AppendFormat("[Woot]{0}", Environment.NewLine)
302-
.AppendFormat("this-rocks = global{0}", Environment.NewLine)
303-
.AppendFormat("[Wow]{0}", Environment.NewLine)
304-
.AppendFormat("Man-I-am-totally-global = 42{0}", Environment.NewLine);
305-
File.WriteAllText(options.GlobalConfigurationLocation, sb.ToString());
306-
307-
sb = new StringBuilder()
308-
.AppendFormat("[Woot]{0}", Environment.NewLine)
309-
.AppendFormat("this-rocks = system{0}", Environment.NewLine);
310-
File.WriteAllText(options.SystemConfigurationLocation, sb.ToString());
311-
312-
sb = new StringBuilder()
313-
.AppendFormat("[Woot]{0}", Environment.NewLine)
314-
.AppendFormat("this-rocks = xdg{0}", Environment.NewLine);
315-
File.WriteAllText(options.XdgConfigurationLocation, sb.ToString());
316-
317-
return options;
318-
}
319-
320-
private static RepositoryOptions BuildFakeRepositoryOptions(SelfCleaningDirectory scd, RepositoryOptions options = null)
319+
private static void BuildFakeRepositoryOptions(SelfCleaningDirectory scd, out string global, out string xdg, out string system)
321320
{
322-
options = options ?? new RepositoryOptions();
323-
324321
string confs = Path.Combine(scd.DirectoryPath, "confs");
325322
Directory.CreateDirectory(confs);
326323

327-
options.GlobalConfigurationLocation = Path.Combine(confs, "my-global-config");
328-
options.XdgConfigurationLocation = Path.Combine(confs, "my-xdg-config");
329-
options.SystemConfigurationLocation = Path.Combine(confs, "my-system-config");
330-
331-
return options;
324+
global = Path.Combine(confs, "my-global-config");
325+
Directory.CreateDirectory(global);
326+
xdg = Path.Combine(confs, "my-xdg-config");
327+
Directory.CreateDirectory(xdg);
328+
system = Path.Combine(confs, "my-system-config");
329+
Directory.CreateDirectory(system);
332330
}
333331

334332
/// <summary>

0 commit comments

Comments
 (0)