diff --git a/src/shared/Core.Tests/HostProviderRegistryTests.cs b/src/shared/Core.Tests/HostProviderRegistryTests.cs index b61aa7a3a..8bae48d33 100644 --- a/src/shared/Core.Tests/HostProviderRegistryTests.cs +++ b/src/shared/Core.Tests/HostProviderRegistryTests.cs @@ -69,7 +69,7 @@ public async Task HostProviderRegistry_GetProvider_Auto_HasProviders_ReturnsSupp } [Fact] - public async Task HostProviderRegistry_GetProvider_Auto_HasProviders_SetsProviderGlobalConfig() + public async Task HostProviderRegistry_GetProvider_Auto_HasProviders_StaticMatch_DoesNotSetProviderGlobalConfig() { var context = new TestCommandContext(); var registry = new HostProviderRegistry(context); @@ -90,6 +90,33 @@ public async Task HostProviderRegistry_GetProvider_Auto_HasProviders_SetsProvide IHostProvider result = await registry.GetProviderAsync(input); + Assert.Same(providerMock.Object, result); + Assert.False(context.Git.Configuration.Global.TryGetValue(configKey, out _)); + } + + [Fact] + public async Task HostProviderRegistry_GetProvider_Auto_HasProviders_DynamicMatch_SetsProviderGlobalConfig() + { + var context = new TestCommandContext(); + var registry = new HostProviderRegistry(context); + var remote = new Uri("https://example.com"); + InputArguments input = CreateInputArguments(remote); + + string providerId = "myProvider"; + string configKey = string.Format(CultureInfo.InvariantCulture, + "{0}.https://example.com.{1}", + Constants.GitConfiguration.Credential.SectionName, + Constants.GitConfiguration.Credential.Provider); + + var providerMock = new Mock(); + providerMock.Setup(x => x.Id).Returns(providerId); + providerMock.Setup(x => x.IsSupported(It.IsAny())).Returns(false); + providerMock.Setup(x => x.IsSupported(It.IsAny())).Returns(true); + + registry.Register(providerMock.Object, HostProviderPriority.Normal); + + IHostProvider result = await registry.GetProviderAsync(input); + Assert.Same(providerMock.Object, result); Assert.True(context.Git.Configuration.Global.TryGetValue(configKey, out IList config)); Assert.Equal(1, config.Count); @@ -97,7 +124,7 @@ public async Task HostProviderRegistry_GetProvider_Auto_HasProviders_SetsProvide } [Fact] - public async Task HostProviderRegistry_GetProvider_Auto_HasProviders_SetsProviderGlobalConfig_HostWithPath() + public async Task HostProviderRegistry_GetProvider_Auto_HasProviders_DynamicMatch_SetsProviderGlobalConfig_HostWithPath() { var context = new TestCommandContext(); var registry = new HostProviderRegistry(context); @@ -112,7 +139,8 @@ public async Task HostProviderRegistry_GetProvider_Auto_HasProviders_SetsProvide var providerMock = new Mock(); providerMock.Setup(x => x.Id).Returns(providerId); - providerMock.Setup(x => x.IsSupported(It.IsAny())).Returns(true); + providerMock.Setup(x => x.IsSupported(It.IsAny())).Returns(false); + providerMock.Setup(x => x.IsSupported(It.IsAny())).Returns(true); registry.Register(providerMock.Object, HostProviderPriority.Normal); diff --git a/src/shared/Core/HostProviderRegistry.cs b/src/shared/Core/HostProviderRegistry.cs index 51ff13b34..d45d0678f 100644 --- a/src/shared/Core/HostProviderRegistry.cs +++ b/src/shared/Core/HostProviderRegistry.cs @@ -220,24 +220,28 @@ await MatchProviderAsync(HostProviderPriority.Normal) ?? await MatchProviderAsync(HostProviderPriority.Low) ?? throw new Exception("No host provider available to service this request."); - // Set the host provider explicitly for future calls - IGitConfiguration gitConfig = _context.Git.GetConfiguration(); - var keyName = string.Format(CultureInfo.InvariantCulture, "{0}.{1}.{2}", - Constants.GitConfiguration.Credential.SectionName, uri.ToString().TrimEnd('/'), - Constants.GitConfiguration.Credential.Provider); - - try - { - _context.Trace.WriteLine($"Remembering host provider for '{uri}' as '{match.Id}'..."); - gitConfig.Set(GitConfigurationLevel.Global, keyName, match.Id); - } - catch (Exception ex) + // If we ended up making a network call then set the host provider explicitly + // to avoid future calls! + if (probeResponse != null) { - _context.Trace.WriteLine("Failed to set host provider!"); - _context.Trace.WriteException(ex); + IGitConfiguration gitConfig = _context.Git.GetConfiguration(); + var keyName = string.Format(CultureInfo.InvariantCulture, "{0}.{1}.{2}", + Constants.GitConfiguration.Credential.SectionName, uri.ToString().TrimEnd('/'), + Constants.GitConfiguration.Credential.Provider); + + try + { + _context.Trace.WriteLine($"Remembering host provider for '{uri}' as '{match.Id}'..."); + gitConfig.Set(GitConfigurationLevel.Global, keyName, match.Id); + } + catch (Exception ex) + { + _context.Trace.WriteLine("Failed to set host provider!"); + _context.Trace.WriteException(ex); - _context.Streams.Error.WriteLine("warning: failed to remember result of host provider detection!"); - _context.Streams.Error.WriteLine($"warning: try setting this manually: `git config --global {keyName} {match.Id}`"); + _context.Streams.Error.WriteLine("warning: failed to remember result of host provider detection!"); + _context.Streams.Error.WriteLine($"warning: try setting this manually: `git config --global {keyName} {match.Id}`"); + } } return match;