-
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
Open
tyrielv
wants to merge
1
commit into
microsoft:master
Choose a base branch
from
tyrielv:tyrielv/shared-libgit2-config-lookup
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
127 changes: 127 additions & 0 deletions
127
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,127 @@ | ||
| 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 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new MockConfigRepo does not override Dispose(bool), so disposing it runs the base finalizable path that calls native libgit2 Native.Repo.Free/Native.Shutdown without a matching Init, unlike the other LibGit2Repo mocks which override Dispose to avoid the base native calls.