Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 37 additions & 42 deletions src/shared/Core.Tests/GitConfigurationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,63 +324,58 @@ public void GitConfiguration_Set_All_ThrowsException()
public void GitConfiguration_Unset_Global_UnsetsGlobalConfig()
{
string repoPath = CreateRepository(out string workDirPath);
try
{
ExecGit(repoPath, workDirPath, "config --global core.foobar alice").AssertSuccess();
ExecGit(repoPath, workDirPath, "config --local core.foobar bob").AssertSuccess();

string gitPath = GetGitPath();
var trace = new NullTrace();
var trace2 = new NullTrace2();
var processManager = new TestProcessManager();
var git = new GitProcess(trace, trace2, processManager, gitPath, repoPath);
IGitConfiguration config = git.GetConfiguration();
// Use an isolated global config file so tests work even when HOME is not set
// to a valid directory (e.g., in package build environments like AUR).
using var _ = new GitTestUtilities.EnvVarScope(
"GIT_CONFIG_GLOBAL", Path.Combine(workDirPath, ".gitconfig-global"));

config.Unset(GitConfigurationLevel.Global, "core.foobar");
ExecGit(repoPath, workDirPath, "config --global core.foobar alice").AssertSuccess();
ExecGit(repoPath, workDirPath, "config --local core.foobar bob").AssertSuccess();

GitResult globalResult = ExecGit(repoPath, workDirPath, "config --global core.foobar");
GitResult localResult = ExecGit(repoPath, workDirPath, "config --local core.foobar");
string gitPath = GetGitPath();
var trace = new NullTrace();
var trace2 = new NullTrace2();
var processManager = new TestProcessManager();
var git = new GitProcess(trace, trace2, processManager, gitPath, repoPath);
IGitConfiguration config = git.GetConfiguration();

Assert.Equal(string.Empty, globalResult.StandardOutput.Trim());
Assert.Equal("bob", localResult.StandardOutput.Trim());
}
finally
{
// Cleanup global config changes
ExecGit(repoPath, workDirPath, "config --global --unset core.foobar");
}
config.Unset(GitConfigurationLevel.Global, "core.foobar");

GitResult globalResult = ExecGit(repoPath, workDirPath, "config --global core.foobar");
GitResult localResult = ExecGit(repoPath, workDirPath, "config --local core.foobar");

Assert.Equal(string.Empty, globalResult.StandardOutput.Trim());
Assert.Equal("bob", localResult.StandardOutput.Trim());
}

[Fact]
public void GitConfiguration_Unset_Local_UnsetsLocalConfig()
{
string repoPath = CreateRepository(out string workDirPath);

try
{
ExecGit(repoPath, workDirPath, "config --global core.foobar alice").AssertSuccess();
ExecGit(repoPath, workDirPath, "config --local core.foobar bob").AssertSuccess();
// Use an isolated global config file so tests work even when HOME is not set
// to a valid directory (e.g., in package build environments like AUR).
using var _ = new GitTestUtilities.EnvVarScope(
"GIT_CONFIG_GLOBAL", Path.Combine(workDirPath, ".gitconfig-global"));

ExecGit(repoPath, workDirPath, "config --global core.foobar alice").AssertSuccess();
ExecGit(repoPath, workDirPath, "config --local core.foobar bob").AssertSuccess();

string gitPath = GetGitPath();
var trace = new NullTrace();
var trace2 = new NullTrace2();
var processManager = new TestProcessManager();
var git = new GitProcess(trace, trace2, processManager, gitPath, repoPath);
IGitConfiguration config = git.GetConfiguration();
string gitPath = GetGitPath();
var trace = new NullTrace();
var trace2 = new NullTrace2();
var processManager = new TestProcessManager();
var git = new GitProcess(trace, trace2, processManager, gitPath, repoPath);
IGitConfiguration config = git.GetConfiguration();

config.Unset(GitConfigurationLevel.Local, "core.foobar");
config.Unset(GitConfigurationLevel.Local, "core.foobar");

GitResult globalResult = ExecGit(repoPath, workDirPath, "config --global core.foobar");
GitResult localResult = ExecGit(repoPath, workDirPath, "config --local core.foobar");
GitResult globalResult = ExecGit(repoPath, workDirPath, "config --global core.foobar");
GitResult localResult = ExecGit(repoPath, workDirPath, "config --local core.foobar");

Assert.Equal("alice", globalResult.StandardOutput.Trim());
Assert.Equal(string.Empty, localResult.StandardOutput.Trim());
}
finally
{
// Cleanup global config changes
ExecGit(repoPath, workDirPath, "config --global --unset core.foobar");
}
Assert.Equal("alice", globalResult.StandardOutput.Trim());
Assert.Equal(string.Empty, localResult.StandardOutput.Trim());
}

[Fact]
Expand Down
31 changes: 31 additions & 0 deletions src/shared/TestInfrastructure/GitTestUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ public static GitResult ExecGit(string repositoryPath, string workingDirectory,

procInfo.Environment["GIT_DIR"] = repositoryPath;

// Use an isolated global config file in the test's working directory to avoid
// failures when HOME is not set to a valid directory (e.g., in package build
// environments). This also prevents tests from polluting the user's real global config.
procInfo.Environment["GIT_CONFIG_GLOBAL"] = Path.Combine(workingDirectory, ".gitconfig-global");

var proc = ChildProcess.Start(new NullTrace2(), procInfo, Trace2ProcessClass.None);
if (proc is null)
{
Expand Down Expand Up @@ -109,5 +114,31 @@ public void AssertSuccess()
Assert.Equal(0, ExitCode);
}
}

/// <summary>
/// Sets an environment variable for the scope of a using block, restoring the original value on dispose.
/// </summary>
/// <remarks>
/// This class modifies the process-level environment, which is shared across all threads.
/// To avoid interference, tests using this class should not run concurrently. Tests within
/// the same xUnit test class are guaranteed to run serially by default.
/// </remarks>
public sealed class EnvVarScope : IDisposable
{
private readonly string _name;
private readonly string _previousValue;

public EnvVarScope(string name, string value)
{
_name = name;
_previousValue = Environment.GetEnvironmentVariable(name);
Environment.SetEnvironmentVariable(name, value);
}

public void Dispose()
{
Environment.SetEnvironmentVariable(_name, _previousValue);
}
}
}
}
Loading