Skip to content

Commit 7988edd

Browse files
authored
Merge pull request #176 from microsoft/master
Fix previous release (linux support) by adding config bug fix
2 parents a87e1e5 + c0214d2 commit 7988edd

File tree

3 files changed

+157
-1
lines changed

3 files changed

+157
-1
lines changed

src/shared/Microsoft.Git.CredentialManager.Tests/GitConfigurationTests.cs

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,142 @@ public void GitConfiguration_GetString_SectionScopeProperty_DoesNotExists_Throws
324324
Assert.Throws<KeyNotFoundException>(() => config.GetValue(randomSection, randomScope, randomProperty));
325325
}
326326

327+
[Fact]
328+
public void GitConfiguration_SetValue_Local_SetsLocalConfig()
329+
{
330+
string repoPath = CreateRepository(out string workDirPath);
331+
332+
string gitPath = GetGitPath();
333+
var trace = new NullTrace();
334+
var git = new GitProcess(trace, gitPath, repoPath);
335+
IGitConfiguration config = git.GetConfiguration(GitConfigurationLevel.Local);
336+
337+
config.SetValue("core.foobar", "foo123");
338+
339+
GitResult localResult = Git(repoPath, workDirPath, "config --local core.foobar");
340+
341+
Assert.Equal("foo123", localResult.StandardOutput.Trim());
342+
}
343+
344+
[Fact]
345+
public void GitConfiguration_SetValue_All_ThrowsException()
346+
{
347+
string repoPath = CreateRepository(out _);
348+
349+
string gitPath = GetGitPath();
350+
var trace = new NullTrace();
351+
var git = new GitProcess(trace, gitPath, repoPath);
352+
IGitConfiguration config = git.GetConfiguration(GitConfigurationLevel.All);
353+
354+
Assert.Throws<InvalidOperationException>(() => config.SetValue("core.foobar", "test123"));
355+
}
356+
357+
[Fact]
358+
public void GitConfiguration_Unset_Global_UnsetsGlobalConfig()
359+
{
360+
string repoPath = CreateRepository(out string workDirPath);
361+
try
362+
{
363+
364+
Git(repoPath, workDirPath, "config --global core.foobar alice").AssertSuccess();
365+
Git(repoPath, workDirPath, "config --local core.foobar bob").AssertSuccess();
366+
367+
string gitPath = GetGitPath();
368+
var trace = new NullTrace();
369+
var git = new GitProcess(trace, gitPath, repoPath);
370+
IGitConfiguration config = git.GetConfiguration(GitConfigurationLevel.Global);
371+
372+
config.Unset("core.foobar");
373+
374+
GitResult globalResult = Git(repoPath, workDirPath, "config --global core.foobar");
375+
GitResult localResult = Git(repoPath, workDirPath, "config --local core.foobar");
376+
377+
Assert.Equal(string.Empty, globalResult.StandardOutput.Trim());
378+
Assert.Equal("bob", localResult.StandardOutput.Trim());
379+
}
380+
finally
381+
{
382+
// Cleanup global config changes
383+
Git(repoPath, workDirPath, "config --global --unset core.foobar");
384+
}
385+
}
386+
387+
[Fact]
388+
public void GitConfiguration_Unset_Local_UnsetsLocalConfig()
389+
{
390+
string repoPath = CreateRepository(out string workDirPath);
391+
392+
try
393+
{
394+
Git(repoPath, workDirPath, "config --global core.foobar alice").AssertSuccess();
395+
Git(repoPath, workDirPath, "config --local core.foobar bob").AssertSuccess();
396+
397+
string gitPath = GetGitPath();
398+
var trace = new NullTrace();
399+
var git = new GitProcess(trace, gitPath, repoPath);
400+
IGitConfiguration config = git.GetConfiguration(GitConfigurationLevel.Local);
401+
402+
config.Unset("core.foobar");
403+
404+
GitResult globalResult = Git(repoPath, workDirPath, "config --global core.foobar");
405+
GitResult localResult = Git(repoPath, workDirPath, "config --local core.foobar");
406+
407+
Assert.Equal("alice", globalResult.StandardOutput.Trim());
408+
Assert.Equal(string.Empty, localResult.StandardOutput.Trim());
409+
}
410+
finally
411+
{
412+
// Cleanup global config changes
413+
Git(repoPath, workDirPath, "config --global --unset core.foobar");
414+
}
415+
}
416+
417+
[Fact]
418+
public void GitConfiguration_Unset_All_ThrowsException()
419+
{
420+
string repoPath = CreateRepository(out _);
421+
422+
string gitPath = GetGitPath();
423+
var trace = new NullTrace();
424+
var git = new GitProcess(trace, gitPath, repoPath);
425+
IGitConfiguration config = git.GetConfiguration(GitConfigurationLevel.All);
426+
427+
Assert.Throws<InvalidOperationException>(() => config.Unset("core.foobar"));
428+
}
429+
430+
[Fact]
431+
public void GitConfiguration_UnsetAll_UnsetsAllConfig()
432+
{
433+
string repoPath = CreateRepository(out string workDirPath);
434+
Git(repoPath, workDirPath, "config --local --add core.foobar foo1").AssertSuccess();
435+
Git(repoPath, workDirPath, "config --local --add core.foobar foo2").AssertSuccess();
436+
Git(repoPath, workDirPath, "config --local --add core.foobar bar1").AssertSuccess();
437+
438+
string gitPath = GetGitPath();
439+
var trace = new NullTrace();
440+
var git = new GitProcess(trace, gitPath, repoPath);
441+
IGitConfiguration config = git.GetConfiguration(GitConfigurationLevel.Local);
442+
443+
config.UnsetAll("core.foobar", "foo*");
444+
445+
GitResult result = Git(repoPath, workDirPath, "config --local --get-all core.foobar");
446+
447+
Assert.Equal("bar1", result.StandardOutput.Trim());
448+
}
449+
450+
[Fact]
451+
public void GitConfiguration_UnsetAll_All_ThrowsException()
452+
{
453+
string repoPath = CreateRepository(out _);
454+
455+
string gitPath = GetGitPath();
456+
var trace = new NullTrace();
457+
var git = new GitProcess(trace, gitPath, repoPath);
458+
IGitConfiguration config = git.GetConfiguration(GitConfigurationLevel.All);
459+
460+
Assert.Throws<InvalidOperationException>(() => config.UnsetAll("core.foobar", Constants.RegexPatterns.Any));
461+
}
462+
327463
#region Test helpers
328464

329465
private static string GetGitPath()

src/shared/Microsoft.Git.CredentialManager/Git.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public GitProcess(ITrace trace, string gitPath, string workingDirectory = null)
3232

3333
public IGitConfiguration GetConfiguration(GitConfigurationLevel level)
3434
{
35-
return new GitProcessConfiguration(_trace, this);
35+
return new GitProcessConfiguration(_trace, this, level);
3636
}
3737

3838
public Process CreateProcess(string args)

src/shared/Microsoft.Git.CredentialManager/GitConfiguration.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@ public bool TryGetValue(string name, out string value)
165165

166166
public void SetValue(string name, string value)
167167
{
168+
if (_filterLevel == GitConfigurationLevel.All)
169+
{
170+
throw new InvalidOperationException("Must have a specific configuration level filter to modify values.");
171+
}
172+
168173
string level = GetLevelFilterArg();
169174
using (Process git = _git.CreateProcess($"config {level} {name} \"{value}\""))
170175
{
@@ -184,6 +189,11 @@ public void SetValue(string name, string value)
184189

185190
public void Unset(string name)
186191
{
192+
if (_filterLevel == GitConfigurationLevel.All)
193+
{
194+
throw new InvalidOperationException("Must have a specific configuration level filter to modify values.");
195+
}
196+
187197
string level = GetLevelFilterArg();
188198
using (Process git = _git.CreateProcess($"config {level} --unset {name}"))
189199
{
@@ -236,6 +246,11 @@ public IEnumerable<string> GetRegex(string nameRegex, string valueRegex)
236246

237247
public void ReplaceAll(string name, string valueRegex, string value)
238248
{
249+
if (_filterLevel == GitConfigurationLevel.All)
250+
{
251+
throw new InvalidOperationException("Must have a specific configuration level filter to modify values.");
252+
}
253+
239254
string level = GetLevelFilterArg();
240255
using (Process git = _git.CreateProcess($"config {level} --replace-all {name} {value} {valueRegex}"))
241256
{
@@ -255,6 +270,11 @@ public void ReplaceAll(string name, string valueRegex, string value)
255270

256271
public void UnsetAll(string name, string valueRegex)
257272
{
273+
if (_filterLevel == GitConfigurationLevel.All)
274+
{
275+
throw new InvalidOperationException("Must have a specific configuration level filter to modify values.");
276+
}
277+
258278
string level = GetLevelFilterArg();
259279
using (Process git = _git.CreateProcess($"config {level} --unset-all {name} {valueRegex}"))
260280
{

0 commit comments

Comments
 (0)