Skip to content

Commit f500245

Browse files
author
Ryan Roden-Corrent
committed
Redirect config access for all test fixtures.
Begin each test by redirecting config access to a temporary location. End each test by restoring the config path to the default. This prevents any test from acidentally reading/writing to the user's 'real' system/global/xdg git config. Each test gets a fresh set of configs to avoid taking dependencies between tests.
1 parent 555c275 commit f500245

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

LibGit2Sharp.Tests/TestHelpers/BaseFixture.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,27 @@ 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+
22+
#if LEAKS_IDENTIFYING
2223
LeaksContainer.Clear();
23-
}
2424
#endif
25+
// redirect config access to a fresh directory before each test method
26+
var configPath = BuildSelfCleaningDirectory().RootedDirectoryPath;
27+
28+
var globalPath = Path.Combine(configPath, "fake_global_config");
29+
var systemPath = Path.Combine(configPath, "fake_system_config");
30+
var xdgPath = Path.Combine(configPath, "fake_xdg_config");
31+
32+
Touch(globalPath, ".gitconfig"); // faking ~/.gitconfig
33+
Touch(systemPath, "gitconfig"); // faking $(prefix)/etc/gitconfig
34+
Touch(xdgPath, "config"); // faking $XDG_CONFIG_HOME/git/config
35+
36+
GlobalSettings.SetConfigSearchPath(ConfigurationLevel.Global, globalPath);
37+
GlobalSettings.SetConfigSearchPath(ConfigurationLevel.System, systemPath);
38+
GlobalSettings.SetConfigSearchPath(ConfigurationLevel.Xdg, xdgPath);
39+
}
2540

2641
static BaseFixture()
2742
{

0 commit comments

Comments
 (0)