Skip to content

Commit

Permalink
Merge pull request #176 from microsoft/master
Browse files Browse the repository at this point in the history
Fix previous release (linux support) by adding config bug fix
  • Loading branch information
mjcheetham authored Sep 22, 2020
2 parents a87e1e5 + c0214d2 commit 7988edd
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,142 @@ public void GitConfiguration_GetString_SectionScopeProperty_DoesNotExists_Throws
Assert.Throws<KeyNotFoundException>(() => config.GetValue(randomSection, randomScope, randomProperty));
}

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

string gitPath = GetGitPath();
var trace = new NullTrace();
var git = new GitProcess(trace, gitPath, repoPath);
IGitConfiguration config = git.GetConfiguration(GitConfigurationLevel.Local);

config.SetValue("core.foobar", "foo123");

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

Assert.Equal("foo123", localResult.StandardOutput.Trim());
}

[Fact]
public void GitConfiguration_SetValue_All_ThrowsException()
{
string repoPath = CreateRepository(out _);

string gitPath = GetGitPath();
var trace = new NullTrace();
var git = new GitProcess(trace, gitPath, repoPath);
IGitConfiguration config = git.GetConfiguration(GitConfigurationLevel.All);

Assert.Throws<InvalidOperationException>(() => config.SetValue("core.foobar", "test123"));
}

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

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

string gitPath = GetGitPath();
var trace = new NullTrace();
var git = new GitProcess(trace, gitPath, repoPath);
IGitConfiguration config = git.GetConfiguration(GitConfigurationLevel.Global);

config.Unset("core.foobar");

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

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

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

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

string gitPath = GetGitPath();
var trace = new NullTrace();
var git = new GitProcess(trace, gitPath, repoPath);
IGitConfiguration config = git.GetConfiguration(GitConfigurationLevel.Local);

config.Unset("core.foobar");

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

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

[Fact]
public void GitConfiguration_Unset_All_ThrowsException()
{
string repoPath = CreateRepository(out _);

string gitPath = GetGitPath();
var trace = new NullTrace();
var git = new GitProcess(trace, gitPath, repoPath);
IGitConfiguration config = git.GetConfiguration(GitConfigurationLevel.All);

Assert.Throws<InvalidOperationException>(() => config.Unset("core.foobar"));
}

[Fact]
public void GitConfiguration_UnsetAll_UnsetsAllConfig()
{
string repoPath = CreateRepository(out string workDirPath);
Git(repoPath, workDirPath, "config --local --add core.foobar foo1").AssertSuccess();
Git(repoPath, workDirPath, "config --local --add core.foobar foo2").AssertSuccess();
Git(repoPath, workDirPath, "config --local --add core.foobar bar1").AssertSuccess();

string gitPath = GetGitPath();
var trace = new NullTrace();
var git = new GitProcess(trace, gitPath, repoPath);
IGitConfiguration config = git.GetConfiguration(GitConfigurationLevel.Local);

config.UnsetAll("core.foobar", "foo*");

GitResult result = Git(repoPath, workDirPath, "config --local --get-all core.foobar");

Assert.Equal("bar1", result.StandardOutput.Trim());
}

[Fact]
public void GitConfiguration_UnsetAll_All_ThrowsException()
{
string repoPath = CreateRepository(out _);

string gitPath = GetGitPath();
var trace = new NullTrace();
var git = new GitProcess(trace, gitPath, repoPath);
IGitConfiguration config = git.GetConfiguration(GitConfigurationLevel.All);

Assert.Throws<InvalidOperationException>(() => config.UnsetAll("core.foobar", Constants.RegexPatterns.Any));
}

#region Test helpers

private static string GetGitPath()
Expand Down
2 changes: 1 addition & 1 deletion src/shared/Microsoft.Git.CredentialManager/Git.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public GitProcess(ITrace trace, string gitPath, string workingDirectory = null)

public IGitConfiguration GetConfiguration(GitConfigurationLevel level)
{
return new GitProcessConfiguration(_trace, this);
return new GitProcessConfiguration(_trace, this, level);
}

public Process CreateProcess(string args)
Expand Down
20 changes: 20 additions & 0 deletions src/shared/Microsoft.Git.CredentialManager/GitConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ public bool TryGetValue(string name, out string value)

public void SetValue(string name, string value)
{
if (_filterLevel == GitConfigurationLevel.All)
{
throw new InvalidOperationException("Must have a specific configuration level filter to modify values.");
}

string level = GetLevelFilterArg();
using (Process git = _git.CreateProcess($"config {level} {name} \"{value}\""))
{
Expand All @@ -184,6 +189,11 @@ public void SetValue(string name, string value)

public void Unset(string name)
{
if (_filterLevel == GitConfigurationLevel.All)
{
throw new InvalidOperationException("Must have a specific configuration level filter to modify values.");
}

string level = GetLevelFilterArg();
using (Process git = _git.CreateProcess($"config {level} --unset {name}"))
{
Expand Down Expand Up @@ -236,6 +246,11 @@ public IEnumerable<string> GetRegex(string nameRegex, string valueRegex)

public void ReplaceAll(string name, string valueRegex, string value)
{
if (_filterLevel == GitConfigurationLevel.All)
{
throw new InvalidOperationException("Must have a specific configuration level filter to modify values.");
}

string level = GetLevelFilterArg();
using (Process git = _git.CreateProcess($"config {level} --replace-all {name} {value} {valueRegex}"))
{
Expand All @@ -255,6 +270,11 @@ public void ReplaceAll(string name, string valueRegex, string value)

public void UnsetAll(string name, string valueRegex)
{
if (_filterLevel == GitConfigurationLevel.All)
{
throw new InvalidOperationException("Must have a specific configuration level filter to modify values.");
}

string level = GetLevelFilterArg();
using (Process git = _git.CreateProcess($"config {level} --unset-all {name} {valueRegex}"))
{
Expand Down

0 comments on commit 7988edd

Please sign in to comment.