-
Notifications
You must be signed in to change notification settings - Fork 474
Share transient libgit2 config lookup helper #2066
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tyrielv
merged 1 commit into
microsoft:master
from
tyrielv:tyrielv/shared-libgit2-config-lookup
Aug 27, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -118,4 +118,3 @@ | |
|
|
||
|
|
||
| </Project> | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
GVFS/GVFS.UnitTests/Common/LibGit2RepoConfigLookupTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| using GVFS.Common.Git; | ||
| using GVFS.Tests.Should; | ||
| using GVFS.UnitTests.Mock.Common; | ||
| using NUnit.Framework; | ||
| using System; | ||
| using System.IO; | ||
|
|
||
| namespace GVFS.UnitTests.Common | ||
| { | ||
| [TestFixture] | ||
| public class LibGit2RepoConfigLookupTests | ||
| { | ||
| [TestCase] | ||
| public void GetConfigBoolOrDefaultOnRepoReturnsConfiguredValue() | ||
| { | ||
| MockTracer tracer = new MockTracer(); | ||
|
|
||
| using (MockConfigRepo repo = new MockConfigRepo(tracer, true)) | ||
| { | ||
| bool value = repo.GetConfigBoolOrDefault("gvfs.test", false); | ||
|
|
||
| value.ShouldEqual(true); | ||
| tracer.RelatedWarningEvents.Count.ShouldEqual(0); | ||
| } | ||
| } | ||
|
|
||
| [TestCase] | ||
| public void GetConfigBoolOrDefaultOnRepoReturnsDefaultWhenKeyIsUnset() | ||
| { | ||
| MockTracer tracer = new MockTracer(); | ||
|
|
||
| using (MockConfigRepo repo = new MockConfigRepo(tracer, (bool?)null)) | ||
| { | ||
| bool value = repo.GetConfigBoolOrDefault("gvfs.test", true); | ||
|
|
||
| value.ShouldEqual(true); | ||
| tracer.RelatedWarningEvents.Count.ShouldEqual(0); | ||
| } | ||
| } | ||
|
|
||
| [TestCase] | ||
| public void GetConfigBoolOrDefaultOnRepoReturnsDefaultOnLibGit2ExceptionAndLogsOnce() | ||
| { | ||
| MockTracer tracer = new MockTracer(); | ||
|
|
||
| using (MockConfigRepo repo = new MockConfigRepo(tracer, new LibGit2Exception("boom"))) | ||
| { | ||
| bool value = repo.GetConfigBoolOrDefault("gvfs.test", false); | ||
|
|
||
| value.ShouldEqual(false); | ||
| tracer.RelatedWarningEvents.Count.ShouldEqual(1); | ||
| tracer.RelatedWarningEvents[0].ShouldContain("Failed to read gvfs.test config, using default: boom"); | ||
| } | ||
| } | ||
|
|
||
| [TestCase] | ||
| public void GetConfigBoolOrDefaultOnRepoReturnsDefaultOnInvalidDataExceptionAndLogsOnce() | ||
| { | ||
| MockTracer tracer = new MockTracer(); | ||
|
|
||
| using (MockConfigRepo repo = new MockConfigRepo(tracer, new InvalidDataException("corrupt config"))) | ||
| { | ||
| bool value = repo.GetConfigBoolOrDefault("gvfs.test", false); | ||
|
|
||
| value.ShouldEqual(false); | ||
| tracer.RelatedWarningEvents.Count.ShouldEqual(1); | ||
| tracer.RelatedWarningEvents[0].ShouldContain("Failed to read gvfs.test config, using default: corrupt config"); | ||
| } | ||
| } | ||
|
|
||
| [TestCase] | ||
| public void GetConfigBoolOrDefaultOnPathReturnsDefaultForMissingRepoAndLogsExactlyOnce() | ||
| { | ||
| MockTracer tracer = new MockTracer(); | ||
|
|
||
| // A GUID-suffixed path under the OS temp directory is guaranteed not to exist and | ||
| // does not depend on any particular drive letter being unmapped (unlike a | ||
| // hardcoded "Z:\..." path, which could resolve on a host with that drive mapped). | ||
| string missingRepoPath = Path.Combine( | ||
| Path.GetTempPath(), | ||
| "LibGit2RepoConfigLookupTests_" + Guid.NewGuid().ToString("N")); | ||
|
|
||
| bool value = LibGit2Repo.GetConfigBoolOrDefault( | ||
| tracer, | ||
| missingRepoPath, | ||
| "gvfs.test", | ||
| false); | ||
|
|
||
| value.ShouldEqual(false); | ||
|
|
||
| // The LibGit2Repo constructor logs a RelatedWarning with the native open-failure | ||
| // reason before throwing InvalidDataException; the static helper's catch does not | ||
| // log a second time for that case (see LibGit2Repo.GetConfigBoolOrDefault), so | ||
| // exactly one warning is expected here. | ||
| tracer.RelatedWarningEvents.Count.ShouldEqual(1); | ||
| tracer.RelatedWarningEvents[0].ShouldContain("Couldn't open repo at"); | ||
| } | ||
|
|
||
| private class MockConfigRepo : LibGit2Repo | ||
| { | ||
| private readonly bool? value; | ||
| private readonly Exception exceptionToThrow; | ||
|
|
||
| public MockConfigRepo(MockTracer tracer, bool? value) | ||
| : base(tracer) | ||
| { | ||
| this.value = value; | ||
| } | ||
|
|
||
| public MockConfigRepo(MockTracer tracer, Exception exceptionToThrow) | ||
| : base(tracer) | ||
| { | ||
| this.exceptionToThrow = exceptionToThrow; | ||
| } | ||
|
|
||
| public override bool? GetConfigBool(string name) | ||
| { | ||
| if (this.exceptionToThrow != null) | ||
| { | ||
| throw this.exceptionToThrow; | ||
| } | ||
|
|
||
| return this.value; | ||
| } | ||
|
|
||
| // This mock never calls the base LibGit2Repo(tracer, repoPath) constructor, so it | ||
| // never initializes native libgit2 state or a real RepoHandle. Override Dispose(bool) | ||
| // to skip the base implementation's native Free/Shutdown calls, which would otherwise | ||
| // run on an uninitialized handle without a matching Init (same pattern as | ||
| // LibGit2RepoInvokerTests.MockLibGit2Repo and LibGit2RepoSafeDirectoryTests.MockSafeDirectoryRepo). | ||
| protected override void Dispose(bool disposing) | ||
| { | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.