diff --git a/src/Microsoft.DotNet.Configurer/DotnetFirstTimeUseConfigurer.cs b/src/Microsoft.DotNet.Configurer/DotnetFirstTimeUseConfigurer.cs index d8fdd4cbd..e86f600aa 100644 --- a/src/Microsoft.DotNet.Configurer/DotnetFirstTimeUseConfigurer.cs +++ b/src/Microsoft.DotNet.Configurer/DotnetFirstTimeUseConfigurer.cs @@ -9,25 +9,13 @@ namespace Microsoft.DotNet.Configurer { public class DotnetFirstTimeUseConfigurer { - public static readonly string SENTINEL = $"{Product.Version}.dotnetSentinel"; - - private IFile _file; private INuGetCachePrimer _nugetCachePrimer; - private INuGetCacheResolver _nugetCacheResolver; + private INuGetCacheSentinel _nugetCacheSentinel; - public DotnetFirstTimeUseConfigurer(INuGetCachePrimer nugetCachePrimer, INuGetCacheResolver nugetCacheResolver) - : this(nugetCachePrimer, nugetCacheResolver, FileSystemWrapper.Default.File) + public DotnetFirstTimeUseConfigurer(INuGetCachePrimer nugetCachePrimer, INuGetCacheSentinel nugetCacheSentinel) { - } - - internal DotnetFirstTimeUseConfigurer( - INuGetCachePrimer nugetCachePrimer, - INuGetCacheResolver nugetCacheResolver, - IFile file) - { - _file = file; _nugetCachePrimer = nugetCachePrimer; - _nugetCacheResolver = nugetCacheResolver; + _nugetCacheSentinel = nugetCacheSentinel; } public void Configure() @@ -40,10 +28,7 @@ namespace Microsoft.DotNet.Configurer private bool ShouldPrimeNugetCache() { - var nugetCachePath = _nugetCacheResolver.ResolveNugetCachePath(); - var sentinel = Path.Combine(nugetCachePath, SENTINEL); - - return !_file.Exists(sentinel); + return !_nugetCacheSentinel.Exists(); } } } diff --git a/src/Microsoft.DotNet.Configurer/INuGetCacheResolver.cs b/src/Microsoft.DotNet.Configurer/INuGetCacheSentinel.cs similarity index 75% rename from src/Microsoft.DotNet.Configurer/INuGetCacheResolver.cs rename to src/Microsoft.DotNet.Configurer/INuGetCacheSentinel.cs index 715762479..9c58cdd39 100644 --- a/src/Microsoft.DotNet.Configurer/INuGetCacheResolver.cs +++ b/src/Microsoft.DotNet.Configurer/INuGetCacheSentinel.cs @@ -3,8 +3,8 @@ namespace Microsoft.DotNet.Configurer { - public interface INuGetCacheResolver + public interface INuGetCacheSentinel { - string ResolveNugetCachePath(); + bool Exists(); } } \ No newline at end of file diff --git a/src/Microsoft.DotNet.Configurer/NuGetCacheResolver.cs b/src/Microsoft.DotNet.Configurer/NuGetCacheResolver.cs deleted file mode 100644 index 2a31e5d45..000000000 --- a/src/Microsoft.DotNet.Configurer/NuGetCacheResolver.cs +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright (c) .NET Foundation and contributors. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using Microsoft.DotNet.ProjectModel.Resolution; - -namespace Microsoft.DotNet.Configurer -{ - public class NuGetCacheResolver : INuGetCacheResolver - { - public string ResolveNugetCachePath() - { - return PackageDependencyProvider.ResolvePackagesPath(null, null); - } - } -} diff --git a/src/Microsoft.DotNet.Configurer/NuGetCacheSentinel.cs b/src/Microsoft.DotNet.Configurer/NuGetCacheSentinel.cs new file mode 100644 index 000000000..f1411f177 --- /dev/null +++ b/src/Microsoft.DotNet.Configurer/NuGetCacheSentinel.cs @@ -0,0 +1,34 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.IO; +using Microsoft.DotNet.Cli.Utils; +using Microsoft.Extensions.EnvironmentAbstractions; +using Microsoft.DotNet.ProjectModel.Resolution; + +namespace Microsoft.DotNet.Configurer +{ + public class NuGetCacheSentinel : INuGetCacheSentinel + { + public static readonly string SENTINEL = $"{Product.Version}.dotnetSentinel"; + + private readonly IFile _file; + + public NuGetCacheSentinel() : this(FileSystemWrapper.Default.File) + { + } + + internal NuGetCacheSentinel(IFile file) + { + _file = file; + } + + public bool Exists() + { + var nugetCachePath = PackageDependencyProvider.ResolvePackagesPath(null, null); + var sentinel = Path.Combine(nugetCachePath, SENTINEL); + + return !_file.Exists(sentinel); + } + } +} diff --git a/test/Microsoft.DotNet.Configurer.UnitTests/GivenADotnetFirstTimeUseConfigurer.cs b/test/Microsoft.DotNet.Configurer.UnitTests/GivenADotnetFirstTimeUseConfigurer.cs index 46f36f53d..2c5c85703 100644 --- a/test/Microsoft.DotNet.Configurer.UnitTests/GivenADotnetFirstTimeUseConfigurer.cs +++ b/test/Microsoft.DotNet.Configurer.UnitTests/GivenADotnetFirstTimeUseConfigurer.cs @@ -6,7 +6,6 @@ using Microsoft.DotNet.Cli.Utils; using Microsoft.DotNet.Configurer; using Microsoft.DotNet.Tools.Test; using Microsoft.Extensions.DependencyModel.Tests; -using Microsoft.Extensions.EnvironmentAbstractions; using Moq; using Xunit; @@ -14,36 +13,23 @@ namespace Microsoft.DotNet.Configurer.UnitTests { public class GivenADotnetFirstTimeUseConfigurer { - private const string NUGET_CACHE_PATH = "some path"; - private Mock _nugetCachePrimerMock; - private Mock _nugetCacheResolverMock; + private Mock _nugetCacheSentinelMock; public GivenADotnetFirstTimeUseConfigurer() { _nugetCachePrimerMock = new Mock(); - _nugetCacheResolverMock = new Mock(); - _nugetCacheResolverMock.Setup(n => n.ResolveNugetCachePath()).Returns(NUGET_CACHE_PATH); - } - - [Fact] - public void The_sentinel_has_the_current_version_in_its_name() - { - DotnetFirstTimeUseConfigurer.SENTINEL.Should().Contain($"{Product.Version}"); + _nugetCacheSentinelMock = new Mock(); } [Fact] public void It_does_not_prime_the_cache_if_the_sentinel_exists() { - var fileSystemMockBuilder = FileSystemMockBuilder.Create(); - fileSystemMockBuilder.AddFiles(NUGET_CACHE_PATH, DotnetFirstTimeUseConfigurer.SENTINEL); - - var fileSystemMock = fileSystemMockBuilder.Build(); + _nugetCacheSentinelMock.Setup(n => n.Exists()).Returns(true); var dotnetFirstTimeUseConfigurer = new DotnetFirstTimeUseConfigurer( _nugetCachePrimerMock.Object, - _nugetCacheResolverMock.Object, - fileSystemMock.File); + _nugetCacheSentinelMock.Object); dotnetFirstTimeUseConfigurer.Configure(); @@ -53,13 +39,11 @@ namespace Microsoft.DotNet.Configurer.UnitTests [Fact] public void It_primes_the_cache_if_the_sentinel_does_not_exist() { - var fileSystemMockBuilder = FileSystemMockBuilder.Create(); - var fileSystemMock = fileSystemMockBuilder.Build(); + _nugetCacheSentinelMock.Setup(n => n.Exists()).Returns(false); var dotnetFirstTimeUseConfigurer = new DotnetFirstTimeUseConfigurer( _nugetCachePrimerMock.Object, - _nugetCacheResolverMock.Object, - fileSystemMock.File); + _nugetCacheSentinelMock.Object); dotnetFirstTimeUseConfigurer.Configure(); diff --git a/test/Microsoft.DotNet.Configurer.UnitTests/GivenANuGetCacheSentinel.cs b/test/Microsoft.DotNet.Configurer.UnitTests/GivenANuGetCacheSentinel.cs new file mode 100644 index 000000000..d6a2fe9e0 --- /dev/null +++ b/test/Microsoft.DotNet.Configurer.UnitTests/GivenANuGetCacheSentinel.cs @@ -0,0 +1,46 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using FluentAssertions; +using Microsoft.DotNet.Cli.Utils; +using Microsoft.DotNet.Configurer; +using Microsoft.Extensions.DependencyModel.Tests; +using Xunit; + +namespace Microsoft.DotNet.Configurer.UnitTests +{ + public class GivenANuGetCacheSentinel + { + private const string NUGET_CACHE_PATH = "some path"; + + [Fact] + public void The_sentinel_has_the_current_version_in_its_name() + { + NuGetCacheSentinel.SENTINEL.Should().Contain($"{Product.Version}"); + } + + [Fact] + public void It_returns_true_if_the_sentinel_exists() + { + var fileSystemMockBuilder = FileSystemMockBuilder.Create(); + fileSystemMockBuilder.AddFiles(NUGET_CACHE_PATH, NuGetCacheSentinel.SENTINEL); + + var fileSystemMock = fileSystemMockBuilder.Build(); + + var nugetCacheSentinel = new NuGetCacheSentinel(fileSystemMock.File); + + nugetCacheSentinel.Exists().Should().BeTrue(); + } + + [Fact] + public void It_returns_false_if_the_sentinel_does_not_exist() + { + var fileSystemMockBuilder = FileSystemMockBuilder.Create(); + var fileSystemMock = fileSystemMockBuilder.Build(); + + var nugetCacheSentinel = new NuGetCacheSentinel(fileSystemMock.File); + + nugetCacheSentinel.Exists().Should().BeTrue(); + } + } +}