Adding a separate abstraction for the NuGet sentinel.

This commit is contained in:
Livar Cunha 2016-06-06 10:39:05 -07:00
parent 8db7c1c4fb
commit ac2e21452f
6 changed files with 92 additions and 58 deletions

View file

@ -9,25 +9,13 @@ namespace Microsoft.DotNet.Configurer
{ {
public class DotnetFirstTimeUseConfigurer public class DotnetFirstTimeUseConfigurer
{ {
public static readonly string SENTINEL = $"{Product.Version}.dotnetSentinel";
private IFile _file;
private INuGetCachePrimer _nugetCachePrimer; private INuGetCachePrimer _nugetCachePrimer;
private INuGetCacheResolver _nugetCacheResolver; private INuGetCacheSentinel _nugetCacheSentinel;
public DotnetFirstTimeUseConfigurer(INuGetCachePrimer nugetCachePrimer, INuGetCacheResolver nugetCacheResolver) public DotnetFirstTimeUseConfigurer(INuGetCachePrimer nugetCachePrimer, INuGetCacheSentinel nugetCacheSentinel)
: this(nugetCachePrimer, nugetCacheResolver, FileSystemWrapper.Default.File)
{ {
}
internal DotnetFirstTimeUseConfigurer(
INuGetCachePrimer nugetCachePrimer,
INuGetCacheResolver nugetCacheResolver,
IFile file)
{
_file = file;
_nugetCachePrimer = nugetCachePrimer; _nugetCachePrimer = nugetCachePrimer;
_nugetCacheResolver = nugetCacheResolver; _nugetCacheSentinel = nugetCacheSentinel;
} }
public void Configure() public void Configure()
@ -40,10 +28,7 @@ namespace Microsoft.DotNet.Configurer
private bool ShouldPrimeNugetCache() private bool ShouldPrimeNugetCache()
{ {
var nugetCachePath = _nugetCacheResolver.ResolveNugetCachePath(); return !_nugetCacheSentinel.Exists();
var sentinel = Path.Combine(nugetCachePath, SENTINEL);
return !_file.Exists(sentinel);
} }
} }
} }

View file

@ -3,8 +3,8 @@
namespace Microsoft.DotNet.Configurer namespace Microsoft.DotNet.Configurer
{ {
public interface INuGetCacheResolver public interface INuGetCacheSentinel
{ {
string ResolveNugetCachePath(); bool Exists();
} }
} }

View file

@ -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);
}
}
}

View file

@ -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);
}
}
}

View file

@ -6,7 +6,6 @@ using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Configurer; using Microsoft.DotNet.Configurer;
using Microsoft.DotNet.Tools.Test; using Microsoft.DotNet.Tools.Test;
using Microsoft.Extensions.DependencyModel.Tests; using Microsoft.Extensions.DependencyModel.Tests;
using Microsoft.Extensions.EnvironmentAbstractions;
using Moq; using Moq;
using Xunit; using Xunit;
@ -14,36 +13,23 @@ namespace Microsoft.DotNet.Configurer.UnitTests
{ {
public class GivenADotnetFirstTimeUseConfigurer public class GivenADotnetFirstTimeUseConfigurer
{ {
private const string NUGET_CACHE_PATH = "some path";
private Mock<INuGetCachePrimer> _nugetCachePrimerMock; private Mock<INuGetCachePrimer> _nugetCachePrimerMock;
private Mock<INuGetCacheResolver> _nugetCacheResolverMock; private Mock<INuGetCacheSentinel> _nugetCacheSentinelMock;
public GivenADotnetFirstTimeUseConfigurer() public GivenADotnetFirstTimeUseConfigurer()
{ {
_nugetCachePrimerMock = new Mock<INuGetCachePrimer>(); _nugetCachePrimerMock = new Mock<INuGetCachePrimer>();
_nugetCacheResolverMock = new Mock<INuGetCacheResolver>(); _nugetCacheSentinelMock = new Mock<INuGetCacheSentinel>();
_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}");
} }
[Fact] [Fact]
public void It_does_not_prime_the_cache_if_the_sentinel_exists() public void It_does_not_prime_the_cache_if_the_sentinel_exists()
{ {
var fileSystemMockBuilder = FileSystemMockBuilder.Create(); _nugetCacheSentinelMock.Setup(n => n.Exists()).Returns(true);
fileSystemMockBuilder.AddFiles(NUGET_CACHE_PATH, DotnetFirstTimeUseConfigurer.SENTINEL);
var fileSystemMock = fileSystemMockBuilder.Build();
var dotnetFirstTimeUseConfigurer = new DotnetFirstTimeUseConfigurer( var dotnetFirstTimeUseConfigurer = new DotnetFirstTimeUseConfigurer(
_nugetCachePrimerMock.Object, _nugetCachePrimerMock.Object,
_nugetCacheResolverMock.Object, _nugetCacheSentinelMock.Object);
fileSystemMock.File);
dotnetFirstTimeUseConfigurer.Configure(); dotnetFirstTimeUseConfigurer.Configure();
@ -53,13 +39,11 @@ namespace Microsoft.DotNet.Configurer.UnitTests
[Fact] [Fact]
public void It_primes_the_cache_if_the_sentinel_does_not_exist() public void It_primes_the_cache_if_the_sentinel_does_not_exist()
{ {
var fileSystemMockBuilder = FileSystemMockBuilder.Create(); _nugetCacheSentinelMock.Setup(n => n.Exists()).Returns(false);
var fileSystemMock = fileSystemMockBuilder.Build();
var dotnetFirstTimeUseConfigurer = new DotnetFirstTimeUseConfigurer( var dotnetFirstTimeUseConfigurer = new DotnetFirstTimeUseConfigurer(
_nugetCachePrimerMock.Object, _nugetCachePrimerMock.Object,
_nugetCacheResolverMock.Object, _nugetCacheSentinelMock.Object);
fileSystemMock.File);
dotnetFirstTimeUseConfigurer.Configure(); dotnetFirstTimeUseConfigurer.Configure();

View file

@ -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();
}
}
}