From 4d6ee88d98612b97dbe5a6302fd63cefa86ae8a3 Mon Sep 17 00:00:00 2001 From: Johny Woller Skovdal Date: Mon, 15 Jun 2020 15:46:07 +0200 Subject: [PATCH 1/3] Initial editorconfig file: - For now, it only includes indentation rules for .cs files, to keep VS from "correcting" the indentation. --- .editorconfig | 8 ++++++++ Paket.sln | 21 +++++++++++++-------- 2 files changed, 21 insertions(+), 8 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000000..e87250d6a2 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,8 @@ +# EditorConfig is awesome: https://EditorConfig.org + +# top-most EditorConfig file +root = true + +[*.cs] +indent_style = space +indent_size = 4 diff --git a/Paket.sln b/Paket.sln index b2a3311092..7c3a7dca0f 100644 --- a/Paket.sln +++ b/Paket.sln @@ -1,6 +1,6 @@ Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.27009.1 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30204.135 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "docs", "docs", "{A6A6AF7D-D6E3-442D-9B1E-58CC91879BE1}" EndProject @@ -68,13 +68,13 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "content", "content", "{8E6D EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{ED8079DD-2B06-4030-9F0F-DC548F98E1C4}" EndProject -Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Paket.Tests", "tests\Paket.Tests\Paket.Tests.fsproj", "{E789C72A-5CFD-436B-8EF1-61AA2852A89F}" +Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Paket.Tests", "tests\Paket.Tests\Paket.Tests.fsproj", "{E789C72A-5CFD-436B-8EF1-61AA2852A89F}" EndProject -Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Paket", "src\Paket\Paket.fsproj", "{09B32F18-0C20-4489-8C83-5106D5C04C93}" +Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Paket", "src\Paket\Paket.fsproj", "{09B32F18-0C20-4489-8C83-5106D5C04C93}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Paket.Bootstrapper", "src\Paket.Bootstrapper\Paket.Bootstrapper.csproj", "{CE3F8B87-1ABD-462E-A35B-CDCEC695898B}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Paket.Bootstrapper", "src\Paket.Bootstrapper\Paket.Bootstrapper.csproj", "{CE3F8B87-1ABD-462E-A35B-CDCEC695898B}" EndProject -Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Paket.Core", "src\Paket.Core\Paket.Core.fsproj", "{7BAB0AE2-089F-4761-B138-A717AA2F86C5}" +Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Paket.Core", "src\Paket.Core\Paket.Core.fsproj", "{7BAB0AE2-089F-4761-B138-A717AA2F86C5}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "commands", "commands", "{4425A246-BD18-4622-86B5-0154F19165E4}" ProjectSection(SolutionItems) = preProject @@ -97,9 +97,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "commands", "commands", "{44 docs\content\commands\why.md = docs\content\commands\why.md EndProjectSection EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Paket.Bootstrapper.Tests", "tests\Paket.Bootstrapper.Tests\Paket.Bootstrapper.Tests.csproj", "{7C622582-E281-4EAB-AADA-B5893BB89B45}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Paket.Bootstrapper.Tests", "tests\Paket.Bootstrapper.Tests\Paket.Bootstrapper.Tests.csproj", "{7C622582-E281-4EAB-AADA-B5893BB89B45}" EndProject -Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Paket.IntegrationTests", "integrationtests\Paket.IntegrationTests\Paket.IntegrationTests.fsproj", "{7234B9B4-8CF5-4E68-AA29-050C087B9246}" +Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Paket.IntegrationTests", "integrationtests\Paket.IntegrationTests\Paket.IntegrationTests.fsproj", "{7234B9B4-8CF5-4E68-AA29-050C087B9246}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".paket", ".paket", "{62D18A14-5240-4CB1-AA8A-762D3EB0E19A}" ProjectSection(SolutionItems) = preProject @@ -108,6 +108,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".paket", ".paket", "{62D18A .paket\paket.targets = .paket\paket.targets EndProjectSection EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{1C9A57E4-9D94-44D1-A106-FC588B7B72DE}" + ProjectSection(SolutionItems) = preProject + .editorconfig = .editorconfig + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU From e8e8573b059b232f884477ba2a099b2e2c5cad27 Mon Sep 17 00:00:00 2001 From: Johny Woller Skovdal Date: Mon, 15 Jun 2020 17:14:41 +0200 Subject: [PATCH 2/3] Refactoring to remove EnvProxy as Singleton A provider has been introduced instead that, in most places, only creates a single instance as before, but for test scenarios now always creates a new, to allow parallel testing after .NET Core 3.0 removed the possibility of changing the singleton via reflection. --- src/Paket.Bootstrapper/BootstrapperHelper.cs | 12 +++---- .../HelperProxies/DefaultProxyProvider.cs | 22 ++++++++++++ .../{ => HelperProxies}/EnvProxy.cs | 11 +++--- .../HelperProxies/IEnvProxy.cs | 10 ++++++ .../HelperProxies/IProxyProvider.cs | 9 +++++ .../HelperProxies/WebRequestProxy.cs | 11 +++--- src/Paket.Bootstrapper/Program.cs | 20 +++++------ .../{ => HelperProxies}/EnvWebProxyShould.cs | 36 +++++-------------- .../Paket.Bootstrapper.Tests/ProgramTests.cs | 14 ++++---- tests/Paket.Bootstrapper.Tests/TestHelper.cs | 20 +++++++++++ 10 files changed, 105 insertions(+), 60 deletions(-) create mode 100644 src/Paket.Bootstrapper/HelperProxies/DefaultProxyProvider.cs rename src/Paket.Bootstrapper/{ => HelperProxies}/EnvProxy.cs (89%) create mode 100644 src/Paket.Bootstrapper/HelperProxies/IEnvProxy.cs create mode 100644 src/Paket.Bootstrapper/HelperProxies/IProxyProvider.cs rename tests/Paket.Bootstrapper.Tests/{ => HelperProxies}/EnvWebProxyShould.cs (78%) create mode 100644 tests/Paket.Bootstrapper.Tests/TestHelper.cs diff --git a/src/Paket.Bootstrapper/BootstrapperHelper.cs b/src/Paket.Bootstrapper/BootstrapperHelper.cs index 39225f8dc3..bbf1e9d69a 100644 --- a/src/Paket.Bootstrapper/BootstrapperHelper.cs +++ b/src/Paket.Bootstrapper/BootstrapperHelper.cs @@ -71,29 +71,29 @@ internal static string GetTempFile(string name) return fileName; } - internal static void PrepareWebClient(WebClient client, string url) + internal static void PrepareWebClient(WebClient client, string url, IEnvProxy envProxy) { client.Headers.Add("user-agent", PaketBootstrapperUserAgent); client.UseDefaultCredentials = true; - client.Proxy = GetDefaultWebProxyFor(url); + client.Proxy = GetDefaultWebProxyFor(url, envProxy); } - internal static HttpWebRequest PrepareWebRequest(string url) + internal static HttpWebRequest PrepareWebRequest(string url, IEnvProxy envProxy) { var request = (HttpWebRequest)HttpWebRequest.Create(url); request.UserAgent = PaketBootstrapperUserAgent; request.UseDefaultCredentials = true; - request.Proxy = GetDefaultWebProxyFor(url); + request.Proxy = GetDefaultWebProxyFor(url, envProxy); request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; return request; } - internal static IWebProxy GetDefaultWebProxyFor(String url) + internal static IWebProxy GetDefaultWebProxyFor(String url, IEnvProxy envProxy) { Uri uri = new Uri(url); IWebProxy result; - if (EnvProxy.TryGetProxyFor(uri, out result) && result.GetProxy(uri) != uri) + if (envProxy.TryGetProxyFor(uri, out result) && result.GetProxy(uri) != uri) return result; #if NO_SYSTEMWEBPROXY diff --git a/src/Paket.Bootstrapper/HelperProxies/DefaultProxyProvider.cs b/src/Paket.Bootstrapper/HelperProxies/DefaultProxyProvider.cs new file mode 100644 index 0000000000..89f10b030e --- /dev/null +++ b/src/Paket.Bootstrapper/HelperProxies/DefaultProxyProvider.cs @@ -0,0 +1,22 @@ +using System; + +namespace Paket.Bootstrapper.HelperProxies +{ + public class DefaultProxyProvider : IProxyProvider + { + private IProxyProvider This => this; + + IFileSystemProxy IProxyProvider.FileSystemProxy { get; } = new FileSystemProxy(); + + private readonly IWebRequestProxy webRequestProxy; + IWebRequestProxy IProxyProvider.WebRequestProxy => webRequestProxy; + + private Lazy _instance = new Lazy(); + IEnvProxy IProxyProvider.EnvProxy => _instance.Value; + + public DefaultProxyProvider() + { + webRequestProxy = new WebRequestProxy(This.EnvProxy); + } + } +} diff --git a/src/Paket.Bootstrapper/EnvProxy.cs b/src/Paket.Bootstrapper/HelperProxies/EnvProxy.cs similarity index 89% rename from src/Paket.Bootstrapper/EnvProxy.cs rename to src/Paket.Bootstrapper/HelperProxies/EnvProxy.cs index acb7f7a307..e0273baf5b 100644 --- a/src/Paket.Bootstrapper/EnvProxy.cs +++ b/src/Paket.Bootstrapper/HelperProxies/EnvProxy.cs @@ -4,11 +4,10 @@ using System.Text.RegularExpressions; using System.Linq; -namespace Paket.Bootstrapper +namespace Paket.Bootstrapper.HelperProxies { - internal class EnvProxy + internal class EnvProxy : IEnvProxy { - private static readonly EnvProxy instance = new EnvProxy(); private readonly Dictionary proxies = new Dictionary(StringComparer.OrdinalIgnoreCase); private static string GetEnvVarValue(string name) @@ -58,16 +57,16 @@ private void AddProxy(string scheme, string[] bypassList) } } - protected EnvProxy() + public EnvProxy() { var bypassList = GetBypassList(); AddProxy("http", bypassList); AddProxy("https", bypassList); } - public static bool TryGetProxyFor(Uri uri, out IWebProxy proxy) + public bool TryGetProxyFor(Uri uri, out IWebProxy proxy) { - return instance.proxies.TryGetValue(uri.Scheme, out proxy); + return proxies.TryGetValue(uri.Scheme, out proxy); } } } diff --git a/src/Paket.Bootstrapper/HelperProxies/IEnvProxy.cs b/src/Paket.Bootstrapper/HelperProxies/IEnvProxy.cs new file mode 100644 index 0000000000..4acb5d9023 --- /dev/null +++ b/src/Paket.Bootstrapper/HelperProxies/IEnvProxy.cs @@ -0,0 +1,10 @@ +using System; +using System.Net; + +namespace Paket.Bootstrapper.HelperProxies +{ + public interface IEnvProxy + { + bool TryGetProxyFor(Uri uri, out IWebProxy proxy); + } +} diff --git a/src/Paket.Bootstrapper/HelperProxies/IProxyProvider.cs b/src/Paket.Bootstrapper/HelperProxies/IProxyProvider.cs new file mode 100644 index 0000000000..710c272c17 --- /dev/null +++ b/src/Paket.Bootstrapper/HelperProxies/IProxyProvider.cs @@ -0,0 +1,9 @@ +namespace Paket.Bootstrapper.HelperProxies +{ + public interface IProxyProvider + { + IFileSystemProxy FileSystemProxy { get; } + IWebRequestProxy WebRequestProxy { get; } + IEnvProxy EnvProxy { get; } + } +} diff --git a/src/Paket.Bootstrapper/HelperProxies/WebRequestProxy.cs b/src/Paket.Bootstrapper/HelperProxies/WebRequestProxy.cs index fb15aa0ca0..4beae24e9c 100644 --- a/src/Paket.Bootstrapper/HelperProxies/WebRequestProxy.cs +++ b/src/Paket.Bootstrapper/HelperProxies/WebRequestProxy.cs @@ -5,22 +5,25 @@ namespace Paket.Bootstrapper.HelperProxies { public class WebRequestProxy : IWebRequestProxy { - public WebRequestProxy() + private readonly IEnvProxy envProxy; + + public WebRequestProxy(IEnvProxy envProxy) { Client = new WebClient(); + this.envProxy = envProxy; } private WebClient Client { get; set; } public string DownloadString(string address) { - BootstrapperHelper.PrepareWebClient(Client, address); + BootstrapperHelper.PrepareWebClient(Client, address, envProxy); return Client.DownloadString(address); } public Stream GetResponseStream(string url) { - var request = BootstrapperHelper.PrepareWebRequest(url); + var request = BootstrapperHelper.PrepareWebRequest(url, envProxy); using (var httpResponse = (HttpWebResponse)request.GetResponse()) { @@ -30,7 +33,7 @@ public Stream GetResponseStream(string url) public void DownloadFile(string url, string targetLocation) { - BootstrapperHelper.PrepareWebClient(Client, url); + BootstrapperHelper.PrepareWebClient(Client, url, envProxy); Client.DownloadFile(url, targetLocation); } } diff --git a/src/Paket.Bootstrapper/Program.cs b/src/Paket.Bootstrapper/Program.cs index 25266c4e19..97306193bf 100644 --- a/src/Paket.Bootstrapper/Program.cs +++ b/src/Paket.Bootstrapper/Program.cs @@ -24,7 +24,7 @@ static void Main(string[] args) executionWatch.Start(); Console.CancelKeyPress += CancelKeyPressed; - var fileProxy = new FileSystemProxy(); + IProxyProvider proxyProvider = new DefaultProxyProvider(); var appSettings = ConfigurationManager.AppSettings; @@ -45,14 +45,14 @@ static void Main(string[] args) } var optionsBeforeDependenciesFile = ArgumentParser.ParseArgumentsAndConfigurations(args, appSettings, - Environment.GetEnvironmentVariables(), fileProxy, Enumerable.Empty()); + Environment.GetEnvironmentVariables(), proxyProvider.FileSystemProxy, Enumerable.Empty()); ConsoleImpl.Verbosity = optionsBeforeDependenciesFile.Verbosity; var argumentsFromDependenciesFile = WindowsProcessArguments.Parse( - PaketDependencies.GetBootstrapperArgsForFolder(fileProxy)); + PaketDependencies.GetBootstrapperArgsForFolder(proxyProvider.FileSystemProxy)); var options = ArgumentParser.ParseArgumentsAndConfigurations(args, appSettings, - Environment.GetEnvironmentVariables(), fileProxy, argumentsFromDependenciesFile); + Environment.GetEnvironmentVariables(), proxyProvider.FileSystemProxy, argumentsFromDependenciesFile); if (options.ShowHelp) { ConsoleImpl.WriteAlways(BootstrapperHelper.HelpText); @@ -68,11 +68,11 @@ static void Main(string[] args) options.DownloadArguments.IgnoreCache = true; #endif - var effectiveStrategy = GetEffectiveDownloadStrategy(options.DownloadArguments, options.PreferNuget, options.ForceNuget); + var effectiveStrategy = GetEffectiveDownloadStrategy(options.DownloadArguments, options.PreferNuget, options.ForceNuget, proxyProvider); ConsoleImpl.WriteTrace("Using strategy: " + effectiveStrategy.Name); ConsoleImpl.WriteTrace("Using install kind: " + (options.DownloadArguments.AsTool? "tool": "exe")); - StartPaketBootstrapping(effectiveStrategy, options.DownloadArguments, fileProxy, () => OnSuccessfulDownload(options)); + StartPaketBootstrapping(effectiveStrategy, options.DownloadArguments, proxyProvider.FileSystemProxy, () => OnSuccessfulDownload(options)); } private static void OnSuccessfulDownload(BootstrapperOptions options) @@ -227,13 +227,13 @@ public static void StartPaketBootstrapping(IDownloadStrategy downloadStrategy, D } } - public static DownloadStrategy GetEffectiveDownloadStrategy(DownloadArguments dlArgs, bool preferNuget, bool forceNuget) + public static DownloadStrategy GetEffectiveDownloadStrategy(DownloadArguments dlArgs, bool preferNuget, bool forceNuget, IProxyProvider proxyProvider) { var gitHubDownloadStrategy = dlArgs.AsTool - ? new GitHubDownloadToolStrategy(new WebRequestProxy(), new FileSystemProxy()).AsCached(dlArgs.IgnoreCache) - : new GitHubDownloadStrategy(new WebRequestProxy(), new FileSystemProxy()).AsCached(dlArgs.IgnoreCache); - var nugetDownloadStrategy = new NugetDownloadStrategy(new WebRequestProxy(), new FileSystemProxy(), dlArgs.Folder, dlArgs.NugetSource, dlArgs.AsTool).AsCached(dlArgs.IgnoreCache); + ? new GitHubDownloadToolStrategy(proxyProvider.WebRequestProxy, proxyProvider.FileSystemProxy).AsCached(dlArgs.IgnoreCache) + : new GitHubDownloadStrategy(proxyProvider.WebRequestProxy, proxyProvider.FileSystemProxy).AsCached(dlArgs.IgnoreCache); + var nugetDownloadStrategy = new NugetDownloadStrategy(proxyProvider.WebRequestProxy, proxyProvider.FileSystemProxy, dlArgs.Folder, dlArgs.NugetSource, dlArgs.AsTool).AsCached(dlArgs.IgnoreCache); DownloadStrategy effectiveStrategy; if (forceNuget) diff --git a/tests/Paket.Bootstrapper.Tests/EnvWebProxyShould.cs b/tests/Paket.Bootstrapper.Tests/HelperProxies/EnvWebProxyShould.cs similarity index 78% rename from tests/Paket.Bootstrapper.Tests/EnvWebProxyShould.cs rename to tests/Paket.Bootstrapper.Tests/HelperProxies/EnvWebProxyShould.cs index f9c7c33310..2f9c046f7f 100644 --- a/tests/Paket.Bootstrapper.Tests/EnvWebProxyShould.cs +++ b/tests/Paket.Bootstrapper.Tests/HelperProxies/EnvWebProxyShould.cs @@ -1,25 +1,12 @@ using NUnit.Framework; using System; using System.Net; -using System.Reflection; -namespace Paket.Bootstrapper.Tests +namespace Paket.Bootstrapper.Tests.HelperProxies { [TestFixture] class EnvWebProxyShould { - private sealed class FakeEnvProxy : EnvProxy - { - public FakeEnvProxy() - { - var instanceField = typeof(EnvProxy).GetField("instance", BindingFlags.Static | BindingFlags.NonPublic); - instanceField.SetValue(null, this); - } - new public bool TryGetProxyFor(Uri uri, out IWebProxy proxy) - { - return EnvProxy.TryGetProxyFor(uri, out proxy); - } - } private sealed class DisposableEnvVar : IDisposable { private readonly string name; @@ -41,10 +28,10 @@ [Test] public void GetNoProxyIfNoneDefined() using (new DisposableEnvVar("http_proxy")) using (new DisposableEnvVar("https_proxy")) { - var envProxy = new FakeEnvProxy(); + var envProxy = TestHelper.TestProxyProvider.EnvProxy; IWebProxy proxy; - Assert.IsFalse(envProxy.TryGetProxyFor(new Uri("http://github.com"), out proxy)); - Assert.IsFalse(envProxy.TryGetProxyFor(new Uri("https://github.com"), out proxy)); + Assert.IsFalse(envProxy.TryGetProxyFor(new Uri("http://github.com"), out _)); + Assert.IsFalse(envProxy.TryGetProxyFor(new Uri("https://github.com"), out _)); } } @@ -53,9 +40,8 @@ [Test] public void GetHttpProxyWithNoPortNoCredentials() using (new DisposableEnvVar("http_proxy", "http://proxy.local")) using (new DisposableEnvVar("no_proxy")) { - var envProxy = new FakeEnvProxy(); IWebProxy proxy; - Assert.IsTrue(envProxy.TryGetProxyFor(new Uri("http://github.com"), out proxy)); + Assert.IsTrue(TestHelper.TestProxyProvider.EnvProxy.TryGetProxyFor(new Uri("http://github.com"), out proxy)); var webProxy = proxy as WebProxy; Assert.IsNotNull(webProxy); Assert.That(webProxy.Address, Is.EqualTo(new Uri("http://proxy.local"))); @@ -70,9 +56,8 @@ [Test] public void GetHttpProxyWithPortNoCredentials() using (new DisposableEnvVar("http_proxy", "http://proxy.local:8080")) using (new DisposableEnvVar("no_proxy")) { - var envProxy = new FakeEnvProxy(); IWebProxy proxy; - Assert.IsTrue(envProxy.TryGetProxyFor(new Uri("http://github.com"), out proxy)); + Assert.IsTrue(TestHelper.TestProxyProvider.EnvProxy.TryGetProxyFor(new Uri("http://github.com"), out proxy)); var webProxy = proxy as WebProxy; Assert.IsNotNull(webProxy); Assert.That(webProxy.Address, Is.EqualTo(new Uri("http://proxy.local:8080"))); @@ -88,9 +73,8 @@ [Test] public void GetHttpProxyWithPortAndCredentials() using (new DisposableEnvVar("http_proxy", string.Format("http://user:{0}@proxy.local:8080", Uri.EscapeDataString(password)))) using (new DisposableEnvVar("no_proxy")) { - var envProxy = new FakeEnvProxy(); IWebProxy proxy; - Assert.IsTrue(envProxy.TryGetProxyFor(new Uri("http://github.com"), out proxy)); + Assert.IsTrue(TestHelper.TestProxyProvider.EnvProxy.TryGetProxyFor(new Uri("http://github.com"), out proxy)); var webProxy = proxy as WebProxy; Assert.IsNotNull(webProxy); Assert.That(webProxy.Address, Is.EqualTo(new Uri("http://proxy.local:8080"))); @@ -109,9 +93,8 @@ [Test] public void GetHttpsProxyWithPortAndCredentials() using (new DisposableEnvVar("https_proxy", string.Format("https://user:{0}@proxy.local:8080", Uri.EscapeDataString(password)))) using (new DisposableEnvVar("no_proxy")) { - var envProxy = new FakeEnvProxy(); IWebProxy proxy; - Assert.IsTrue(envProxy.TryGetProxyFor(new Uri("https://github.com"), out proxy)); + Assert.IsTrue(TestHelper.TestProxyProvider.EnvProxy.TryGetProxyFor(new Uri("https://github.com"), out proxy)); var webProxy = proxy as WebProxy; Assert.IsNotNull(webProxy); Assert.That(webProxy.Address, Is.EqualTo(new Uri("http://proxy.local:8080"))); @@ -129,9 +112,8 @@ [Test] public void GetHttpProxyWithBypassList() using (new DisposableEnvVar("http_proxy", string.Format("http://proxy.local:8080"))) using (new DisposableEnvVar("no_proxy", ".local,127.0.0.1")) { - var envProxy = new FakeEnvProxy(); IWebProxy proxy; - Assert.IsTrue(envProxy.TryGetProxyFor(new Uri("http://github.com"), out proxy)); + Assert.IsTrue(TestHelper.TestProxyProvider.EnvProxy.TryGetProxyFor(new Uri("http://github.com"), out proxy)); var webProxy = proxy as WebProxy; Assert.IsNotNull(webProxy); Assert.That(webProxy.Address, Is.EqualTo(new Uri("http://proxy.local:8080"))); diff --git a/tests/Paket.Bootstrapper.Tests/ProgramTests.cs b/tests/Paket.Bootstrapper.Tests/ProgramTests.cs index 6973a4df45..df8eeea3ac 100644 --- a/tests/Paket.Bootstrapper.Tests/ProgramTests.cs +++ b/tests/Paket.Bootstrapper.Tests/ProgramTests.cs @@ -15,7 +15,7 @@ public void GetDownloadStrategy_Cached_Github_Nuget() //act var downloadArguments = new DownloadArguments(String.Empty, true, "any", "any", false, String.Empty, false, null); - var strategy = Program.GetEffectiveDownloadStrategy(downloadArguments, false, false); + var strategy = Program.GetEffectiveDownloadStrategy(downloadArguments, false, false, TestHelper.ProxyProvider); //assert Assert.That(strategy, Is.TypeOf()); @@ -32,7 +32,7 @@ public void GetDownloadStrategy_Cached_Nuget_Github() //act var downloadArguments = new DownloadArguments(String.Empty, true, "any", "any", false, String.Empty, false, null); - var strategy = Program.GetEffectiveDownloadStrategy(downloadArguments, true, false); + var strategy = Program.GetEffectiveDownloadStrategy(downloadArguments, true, false, TestHelper.ProxyProvider); //assert Assert.That(strategy, Is.TypeOf()); @@ -48,7 +48,7 @@ public void GetDownloadStrategy_Cached_Nuget_Nothing() //act var downloadArguments = new DownloadArguments(String.Empty, true, "any", "any", false, String.Empty, false, null); - var strategy = Program.GetEffectiveDownloadStrategy(downloadArguments, true, true); + var strategy = Program.GetEffectiveDownloadStrategy(downloadArguments, true, true, TestHelper.ProxyProvider); //assert Assert.That(strategy, Is.TypeOf()); @@ -63,7 +63,7 @@ public void GetDownloadStrategy_Cached_Nuget_Nothing_ForceOnly() //act var downloadArguments = new DownloadArguments(String.Empty, true, "any", "any", false, String.Empty, false, null); - var strategy = Program.GetEffectiveDownloadStrategy(downloadArguments, false, true); + var strategy = Program.GetEffectiveDownloadStrategy(downloadArguments, false, true, TestHelper.ProxyProvider); //assert Assert.That(strategy, Is.TypeOf()); @@ -78,7 +78,7 @@ public void GetDownloadStrategy_NotCached() //act var downloadArguments = new DownloadArguments(String.Empty, true, "any", "any", false, String.Empty, true, null); - var strategy = Program.GetEffectiveDownloadStrategy(downloadArguments, false, false); + var strategy = Program.GetEffectiveDownloadStrategy(downloadArguments, false, false, TestHelper.ProxyProvider); //assert Assert.That(strategy, Is.TypeOf()); @@ -93,7 +93,7 @@ public void GetDownloadStrategy_NotCached_TemporarilyIgnored() //act var downloadArguments = new DownloadArguments(String.Empty, true, "any", "any", false, String.Empty, true, 10); - var strategy = Program.GetEffectiveDownloadStrategy(downloadArguments, false, false); + var strategy = Program.GetEffectiveDownloadStrategy(downloadArguments, false, false, TestHelper.ProxyProvider); //assert Assert.That(strategy, Is.TypeOf()); @@ -111,7 +111,7 @@ public void GetDownloadStrategy_TemporarilyIgnored_Cached_Nuget_Nothing() //act var downloadArguments = new DownloadArguments(String.Empty, true, "any", "any", false, String.Empty, false, 10); - var strategy = Program.GetEffectiveDownloadStrategy(downloadArguments, true, true); + var strategy = Program.GetEffectiveDownloadStrategy(downloadArguments, true, true, TestHelper.ProxyProvider); //assert Assert.That(strategy, Is.TypeOf()); diff --git a/tests/Paket.Bootstrapper.Tests/TestHelper.cs b/tests/Paket.Bootstrapper.Tests/TestHelper.cs new file mode 100644 index 0000000000..9589b07206 --- /dev/null +++ b/tests/Paket.Bootstrapper.Tests/TestHelper.cs @@ -0,0 +1,20 @@ +using Paket.Bootstrapper.HelperProxies; + +namespace Paket.Bootstrapper.Tests +{ + internal static class TestHelper + { + public static IProxyProvider ProxyProvider { get; } = new DefaultProxyProvider(); + public static IProxyProvider TestProxyProvider { get; } = new NoSingletonProxyProvider(); + + private class NoSingletonProxyProvider : IProxyProvider + { + private IProxyProvider This => this; + IFileSystemProxy IProxyProvider.FileSystemProxy => new FileSystemProxy(); + + IWebRequestProxy IProxyProvider.WebRequestProxy => new WebRequestProxy(This.EnvProxy); + + IEnvProxy IProxyProvider.EnvProxy => new EnvProxy(); + } + } +} From db044d908c0e8e49a0407b4b6d0543d7e41f2e90 Mon Sep 17 00:00:00 2001 From: Johny Woller Skovdal Date: Mon, 15 Jun 2020 21:51:51 +0200 Subject: [PATCH 3/3] Refactoring to improve readability --- .../HelperProxies/DefaultProxyProvider.cs | 18 +++++++----------- .../HelperProxies/EnvWebProxyShould.cs | 12 ++++++------ .../NoSingletonProxyProvider.cs | 13 +++++++++++++ tests/Paket.Bootstrapper.Tests/ProgramTests.cs | 14 +++++++------- tests/Paket.Bootstrapper.Tests/TestHelper.cs | 16 +++------------- 5 files changed, 36 insertions(+), 37 deletions(-) create mode 100644 tests/Paket.Bootstrapper.Tests/NoSingletonProxyProvider.cs diff --git a/src/Paket.Bootstrapper/HelperProxies/DefaultProxyProvider.cs b/src/Paket.Bootstrapper/HelperProxies/DefaultProxyProvider.cs index 89f10b030e..372eef0730 100644 --- a/src/Paket.Bootstrapper/HelperProxies/DefaultProxyProvider.cs +++ b/src/Paket.Bootstrapper/HelperProxies/DefaultProxyProvider.cs @@ -1,22 +1,18 @@ -using System; - -namespace Paket.Bootstrapper.HelperProxies +namespace Paket.Bootstrapper.HelperProxies { public class DefaultProxyProvider : IProxyProvider { - private IProxyProvider This => this; - - IFileSystemProxy IProxyProvider.FileSystemProxy { get; } = new FileSystemProxy(); + public IFileSystemProxy FileSystemProxy { get; } - private readonly IWebRequestProxy webRequestProxy; - IWebRequestProxy IProxyProvider.WebRequestProxy => webRequestProxy; + public IWebRequestProxy WebRequestProxy { get; } - private Lazy _instance = new Lazy(); - IEnvProxy IProxyProvider.EnvProxy => _instance.Value; + public IEnvProxy EnvProxy { get; } public DefaultProxyProvider() { - webRequestProxy = new WebRequestProxy(This.EnvProxy); + FileSystemProxy = new FileSystemProxy(); + EnvProxy = new EnvProxy(); + WebRequestProxy = new WebRequestProxy(EnvProxy); } } } diff --git a/tests/Paket.Bootstrapper.Tests/HelperProxies/EnvWebProxyShould.cs b/tests/Paket.Bootstrapper.Tests/HelperProxies/EnvWebProxyShould.cs index 2f9c046f7f..ba5ed36e7b 100644 --- a/tests/Paket.Bootstrapper.Tests/HelperProxies/EnvWebProxyShould.cs +++ b/tests/Paket.Bootstrapper.Tests/HelperProxies/EnvWebProxyShould.cs @@ -28,7 +28,7 @@ [Test] public void GetNoProxyIfNoneDefined() using (new DisposableEnvVar("http_proxy")) using (new DisposableEnvVar("https_proxy")) { - var envProxy = TestHelper.TestProxyProvider.EnvProxy; + var envProxy = TestHelper.NoSingletonProxyProvider.EnvProxy; IWebProxy proxy; Assert.IsFalse(envProxy.TryGetProxyFor(new Uri("http://github.com"), out _)); Assert.IsFalse(envProxy.TryGetProxyFor(new Uri("https://github.com"), out _)); @@ -41,7 +41,7 @@ [Test] public void GetHttpProxyWithNoPortNoCredentials() using (new DisposableEnvVar("no_proxy")) { IWebProxy proxy; - Assert.IsTrue(TestHelper.TestProxyProvider.EnvProxy.TryGetProxyFor(new Uri("http://github.com"), out proxy)); + Assert.IsTrue(TestHelper.NoSingletonProxyProvider.EnvProxy.TryGetProxyFor(new Uri("http://github.com"), out proxy)); var webProxy = proxy as WebProxy; Assert.IsNotNull(webProxy); Assert.That(webProxy.Address, Is.EqualTo(new Uri("http://proxy.local"))); @@ -57,7 +57,7 @@ [Test] public void GetHttpProxyWithPortNoCredentials() using (new DisposableEnvVar("no_proxy")) { IWebProxy proxy; - Assert.IsTrue(TestHelper.TestProxyProvider.EnvProxy.TryGetProxyFor(new Uri("http://github.com"), out proxy)); + Assert.IsTrue(TestHelper.NoSingletonProxyProvider.EnvProxy.TryGetProxyFor(new Uri("http://github.com"), out proxy)); var webProxy = proxy as WebProxy; Assert.IsNotNull(webProxy); Assert.That(webProxy.Address, Is.EqualTo(new Uri("http://proxy.local:8080"))); @@ -74,7 +74,7 @@ [Test] public void GetHttpProxyWithPortAndCredentials() using (new DisposableEnvVar("no_proxy")) { IWebProxy proxy; - Assert.IsTrue(TestHelper.TestProxyProvider.EnvProxy.TryGetProxyFor(new Uri("http://github.com"), out proxy)); + Assert.IsTrue(TestHelper.NoSingletonProxyProvider.EnvProxy.TryGetProxyFor(new Uri("http://github.com"), out proxy)); var webProxy = proxy as WebProxy; Assert.IsNotNull(webProxy); Assert.That(webProxy.Address, Is.EqualTo(new Uri("http://proxy.local:8080"))); @@ -94,7 +94,7 @@ [Test] public void GetHttpsProxyWithPortAndCredentials() using (new DisposableEnvVar("no_proxy")) { IWebProxy proxy; - Assert.IsTrue(TestHelper.TestProxyProvider.EnvProxy.TryGetProxyFor(new Uri("https://github.com"), out proxy)); + Assert.IsTrue(TestHelper.NoSingletonProxyProvider.EnvProxy.TryGetProxyFor(new Uri("https://github.com"), out proxy)); var webProxy = proxy as WebProxy; Assert.IsNotNull(webProxy); Assert.That(webProxy.Address, Is.EqualTo(new Uri("http://proxy.local:8080"))); @@ -113,7 +113,7 @@ [Test] public void GetHttpProxyWithBypassList() using (new DisposableEnvVar("no_proxy", ".local,127.0.0.1")) { IWebProxy proxy; - Assert.IsTrue(TestHelper.TestProxyProvider.EnvProxy.TryGetProxyFor(new Uri("http://github.com"), out proxy)); + Assert.IsTrue(TestHelper.NoSingletonProxyProvider.EnvProxy.TryGetProxyFor(new Uri("http://github.com"), out proxy)); var webProxy = proxy as WebProxy; Assert.IsNotNull(webProxy); Assert.That(webProxy.Address, Is.EqualTo(new Uri("http://proxy.local:8080"))); diff --git a/tests/Paket.Bootstrapper.Tests/NoSingletonProxyProvider.cs b/tests/Paket.Bootstrapper.Tests/NoSingletonProxyProvider.cs new file mode 100644 index 0000000000..5a4680a2bb --- /dev/null +++ b/tests/Paket.Bootstrapper.Tests/NoSingletonProxyProvider.cs @@ -0,0 +1,13 @@ +using Paket.Bootstrapper.HelperProxies; + +namespace Paket.Bootstrapper.Tests +{ + internal class NoSingletonProxyProvider : IProxyProvider + { + public IFileSystemProxy FileSystemProxy => new FileSystemProxy(); + + public IWebRequestProxy WebRequestProxy => new WebRequestProxy(EnvProxy); + + public IEnvProxy EnvProxy => new EnvProxy(); + } +} diff --git a/tests/Paket.Bootstrapper.Tests/ProgramTests.cs b/tests/Paket.Bootstrapper.Tests/ProgramTests.cs index df8eeea3ac..b3d9bb6657 100644 --- a/tests/Paket.Bootstrapper.Tests/ProgramTests.cs +++ b/tests/Paket.Bootstrapper.Tests/ProgramTests.cs @@ -15,7 +15,7 @@ public void GetDownloadStrategy_Cached_Github_Nuget() //act var downloadArguments = new DownloadArguments(String.Empty, true, "any", "any", false, String.Empty, false, null); - var strategy = Program.GetEffectiveDownloadStrategy(downloadArguments, false, false, TestHelper.ProxyProvider); + var strategy = Program.GetEffectiveDownloadStrategy(downloadArguments, false, false, TestHelper.ProductionProxyProvider); //assert Assert.That(strategy, Is.TypeOf()); @@ -32,7 +32,7 @@ public void GetDownloadStrategy_Cached_Nuget_Github() //act var downloadArguments = new DownloadArguments(String.Empty, true, "any", "any", false, String.Empty, false, null); - var strategy = Program.GetEffectiveDownloadStrategy(downloadArguments, true, false, TestHelper.ProxyProvider); + var strategy = Program.GetEffectiveDownloadStrategy(downloadArguments, true, false, TestHelper.ProductionProxyProvider); //assert Assert.That(strategy, Is.TypeOf()); @@ -48,7 +48,7 @@ public void GetDownloadStrategy_Cached_Nuget_Nothing() //act var downloadArguments = new DownloadArguments(String.Empty, true, "any", "any", false, String.Empty, false, null); - var strategy = Program.GetEffectiveDownloadStrategy(downloadArguments, true, true, TestHelper.ProxyProvider); + var strategy = Program.GetEffectiveDownloadStrategy(downloadArguments, true, true, TestHelper.ProductionProxyProvider); //assert Assert.That(strategy, Is.TypeOf()); @@ -63,7 +63,7 @@ public void GetDownloadStrategy_Cached_Nuget_Nothing_ForceOnly() //act var downloadArguments = new DownloadArguments(String.Empty, true, "any", "any", false, String.Empty, false, null); - var strategy = Program.GetEffectiveDownloadStrategy(downloadArguments, false, true, TestHelper.ProxyProvider); + var strategy = Program.GetEffectiveDownloadStrategy(downloadArguments, false, true, TestHelper.ProductionProxyProvider); //assert Assert.That(strategy, Is.TypeOf()); @@ -78,7 +78,7 @@ public void GetDownloadStrategy_NotCached() //act var downloadArguments = new DownloadArguments(String.Empty, true, "any", "any", false, String.Empty, true, null); - var strategy = Program.GetEffectiveDownloadStrategy(downloadArguments, false, false, TestHelper.ProxyProvider); + var strategy = Program.GetEffectiveDownloadStrategy(downloadArguments, false, false, TestHelper.ProductionProxyProvider); //assert Assert.That(strategy, Is.TypeOf()); @@ -93,7 +93,7 @@ public void GetDownloadStrategy_NotCached_TemporarilyIgnored() //act var downloadArguments = new DownloadArguments(String.Empty, true, "any", "any", false, String.Empty, true, 10); - var strategy = Program.GetEffectiveDownloadStrategy(downloadArguments, false, false, TestHelper.ProxyProvider); + var strategy = Program.GetEffectiveDownloadStrategy(downloadArguments, false, false, TestHelper.ProductionProxyProvider); //assert Assert.That(strategy, Is.TypeOf()); @@ -111,7 +111,7 @@ public void GetDownloadStrategy_TemporarilyIgnored_Cached_Nuget_Nothing() //act var downloadArguments = new DownloadArguments(String.Empty, true, "any", "any", false, String.Empty, false, 10); - var strategy = Program.GetEffectiveDownloadStrategy(downloadArguments, true, true, TestHelper.ProxyProvider); + var strategy = Program.GetEffectiveDownloadStrategy(downloadArguments, true, true, TestHelper.ProductionProxyProvider); //assert Assert.That(strategy, Is.TypeOf()); diff --git a/tests/Paket.Bootstrapper.Tests/TestHelper.cs b/tests/Paket.Bootstrapper.Tests/TestHelper.cs index 9589b07206..d1065e8032 100644 --- a/tests/Paket.Bootstrapper.Tests/TestHelper.cs +++ b/tests/Paket.Bootstrapper.Tests/TestHelper.cs @@ -2,19 +2,9 @@ namespace Paket.Bootstrapper.Tests { - internal static class TestHelper + internal static partial class TestHelper { - public static IProxyProvider ProxyProvider { get; } = new DefaultProxyProvider(); - public static IProxyProvider TestProxyProvider { get; } = new NoSingletonProxyProvider(); - - private class NoSingletonProxyProvider : IProxyProvider - { - private IProxyProvider This => this; - IFileSystemProxy IProxyProvider.FileSystemProxy => new FileSystemProxy(); - - IWebRequestProxy IProxyProvider.WebRequestProxy => new WebRequestProxy(This.EnvProxy); - - IEnvProxy IProxyProvider.EnvProxy => new EnvProxy(); - } + public static IProxyProvider ProductionProxyProvider { get; } = new DefaultProxyProvider(); + public static IProxyProvider NoSingletonProxyProvider { get; } = new NoSingletonProxyProvider(); } }