Adding a create method to the sentinel and invoking it from the Primer when appropriate.

This commit is contained in:
Livar Cunha 2016-06-06 12:51:27 -07:00
parent ac2e21452f
commit 461c26b78d
8 changed files with 131 additions and 23 deletions

View file

@ -6,5 +6,7 @@ namespace Microsoft.DotNet.Configurer
public interface INuGetCacheSentinel
{
bool Exists();
void CreateIfNotExists();
}
}

View file

@ -14,20 +14,26 @@ namespace Microsoft.DotNet.Configurer
private readonly ICommandFactory _commandFactory;
private readonly IDirectory _directory;
private readonly INuGetPackagesArchiver _nugetPackagesArchiver;
private readonly INuGetCacheSentinel _nuGetCacheSentinel;
public NuGetCachePrimer(ICommandFactory commandFactory, INuGetPackagesArchiver nugetPackagesArchiver)
: this(commandFactory, nugetPackagesArchiver, FileSystemWrapper.Default.Directory)
public NuGetCachePrimer(
ICommandFactory commandFactory,
INuGetPackagesArchiver nugetPackagesArchiver,
INuGetCacheSentinel nuGetCacheSentinel)
: this(commandFactory, nugetPackagesArchiver, nuGetCacheSentinel, FileSystemWrapper.Default.Directory)
{
}
internal NuGetCachePrimer(
ICommandFactory commandFactory,
INuGetPackagesArchiver nugetPackagesArchiver,
INuGetCacheSentinel nuGetCacheSentinel,
IDirectory directory)
{
_commandFactory = commandFactory;
_directory = directory;
_nugetPackagesArchiver = nugetPackagesArchiver;
_nuGetCacheSentinel = nuGetCacheSentinel;
}
public void PrimeCache()
@ -42,18 +48,17 @@ namespace Microsoft.DotNet.Configurer
using (var temporaryDotnetNewDirectory = _directory.CreateTemporaryDirectory())
{
var workingDirectory = temporaryDotnetNewDirectory.DirectoryPath;
var dotnetNewSucceeded = CreateTemporaryProject(workingDirectory);
var createProjectSucceeded = CreateTemporaryProject(workingDirectory);
if (dotnetNewSucceeded)
if (createProjectSucceeded)
{
RestoreTemporaryProject(pathToPackagesArchive, workingDirectory);
var restoreProjectSucceeded = RestoreTemporaryProject(pathToPackagesArchive, workingDirectory);
if (restoreProjectSucceeded)
{
_nuGetCacheSentinel.CreateIfNotExists();
}
}
}
// -- PrimeCache(<path to archive>)
// (done) Create temporary project under a temporary folder using dotnet new
// (done) Restore that project using dotnet restore -s parameter pointing to the <path to archive>
// Create sentinel
// (done) Delete temporary folder (should be done automatically if using abstraction).
}
private bool CreateTemporaryProject(string workingDirectory)

View file

@ -14,21 +14,44 @@ namespace Microsoft.DotNet.Configurer
private readonly IFile _file;
public NuGetCacheSentinel() : this(FileSystemWrapper.Default.File)
private string _nugetCachePath;
private string NuGetCachePath
{
get
{
if (string.IsNullOrEmpty(_nugetCachePath))
{
_nugetCachePath = PackageDependencyProvider.ResolvePackagesPath(null, null);
}
return _nugetCachePath;
}
}
private string Sentinel => Path.Combine(NuGetCachePath, SENTINEL);
public NuGetCacheSentinel() : this(string.Empty, FileSystemWrapper.Default.File)
{
}
internal NuGetCacheSentinel(IFile file)
internal NuGetCacheSentinel(string nugetCachePath, IFile file)
{
_file = file;
_nugetCachePath = nugetCachePath;
}
public bool Exists()
{
var nugetCachePath = PackageDependencyProvider.ResolvePackagesPath(null, null);
var sentinel = Path.Combine(nugetCachePath, SENTINEL);
return _file.Exists(Sentinel);
}
return !_file.Exists(sentinel);
public void CreateIfNotExists()
{
if (!Exists())
{
_file.CreateEmptyFile(Sentinel);
}
}
}
}

View file

@ -1,6 +1,7 @@
// 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;
using System.IO;
namespace Microsoft.Extensions.EnvironmentAbstractions
@ -21,5 +22,10 @@ namespace Microsoft.Extensions.EnvironmentAbstractions
{
return File.OpenRead(path);
}
public void CreateEmptyFile(string path)
{
File.Create(path).Dispose();
}
}
}

View file

@ -12,5 +12,7 @@ namespace Microsoft.Extensions.EnvironmentAbstractions
string ReadAllText(string path);
Stream OpenRead(string path);
void CreateEmptyFile(string path);
}
}

View file

@ -26,6 +26,7 @@ namespace Microsoft.DotNet.Configurer.UnitTests
private Mock<ICommand> _dotnetNewCommandMock;
private Mock<ICommand> _dotnetRestoreCommandMock;
private Mock<INuGetPackagesArchiver> _nugetPackagesArchiverMock;
private Mock<INuGetCacheSentinel> _nugetCacheSentinel;
public GivenANuGetCachePrimer()
{
@ -39,9 +40,12 @@ namespace Microsoft.DotNet.Configurer.UnitTests
_nugetPackagesArchiverMock = new Mock<INuGetPackagesArchiver>();
_nugetPackagesArchiverMock.Setup(n => n.ExtractArchive()).Returns(PACKAGES_ARCHIVE_PATH);
_nugetCacheSentinel = new Mock<INuGetCacheSentinel>();
var nugetCachePrimer = new NuGetCachePrimer(
_commandFactoryMock.Object,
_nugetPackagesArchiverMock.Object,
_nugetCacheSentinel.Object,
_fileSystemMock.Directory);
nugetCachePrimer.PrimeCache();
@ -124,12 +128,13 @@ namespace Microsoft.DotNet.Configurer.UnitTests
[Fact]
public void It_does_not_run_restore_if_dotnet_new_fails()
{
var commandFactoryMock = SetupCommandFactoryMock();;
var commandFactoryMock = SetupCommandFactoryMock();
_dotnetNewCommandMock.Setup(c => c.Execute()).Returns(new CommandResult(null, -1, null, null));
var nugetCachePrimer = new NuGetCachePrimer(
commandFactoryMock.Object,
_nugetPackagesArchiverMock.Object,
_nugetCacheSentinel.Object,
_fileSystemMock.Directory);
nugetCachePrimer.PrimeCache();
@ -166,5 +171,28 @@ namespace Microsoft.DotNet.Configurer.UnitTests
{
_dotnetRestoreCommandMock.Verify(c => c.Execute(), Times.Once);
}
[Fact]
public void It_creates_a_sentinel_when_restore_succeeds()
{
_nugetCacheSentinel.Verify(n => n.CreateIfNotExists(), Times.Once);
}
[Fact]
public void It_does_not_create_a_sentinel_when_restore_fails()
{
var nugetCacheSentinel = new Mock<INuGetCacheSentinel>();
_dotnetRestoreCommandMock.Setup(c => c.Execute()).Returns(new CommandResult(null, -1, null, null));
var nugetCachePrimer = new NuGetCachePrimer(
_commandFactoryMock.Object,
_nugetPackagesArchiverMock.Object,
nugetCacheSentinel.Object,
_fileSystemMock.Directory);
nugetCachePrimer.PrimeCache();
nugetCacheSentinel.Verify(n => n.CreateIfNotExists(), Times.Never);
}
}
}

View file

@ -1,6 +1,7 @@
// 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 FluentAssertions;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Configurer;
@ -11,6 +12,13 @@ namespace Microsoft.DotNet.Configurer.UnitTests
{
public class GivenANuGetCacheSentinel
{
private FileSystemMockBuilder _fileSystemMockBuilder;
public GivenANuGetCacheSentinel()
{
_fileSystemMockBuilder = FileSystemMockBuilder.Create();
}
private const string NUGET_CACHE_PATH = "some path";
[Fact]
@ -22,12 +30,11 @@ namespace Microsoft.DotNet.Configurer.UnitTests
[Fact]
public void It_returns_true_if_the_sentinel_exists()
{
var fileSystemMockBuilder = FileSystemMockBuilder.Create();
fileSystemMockBuilder.AddFiles(NUGET_CACHE_PATH, NuGetCacheSentinel.SENTINEL);
_fileSystemMockBuilder.AddFiles(NUGET_CACHE_PATH, NuGetCacheSentinel.SENTINEL);
var fileSystemMock = fileSystemMockBuilder.Build();
var fileSystemMock = _fileSystemMockBuilder.Build();
var nugetCacheSentinel = new NuGetCacheSentinel(fileSystemMock.File);
var nugetCacheSentinel = new NuGetCacheSentinel(NUGET_CACHE_PATH, fileSystemMock.File);
nugetCacheSentinel.Exists().Should().BeTrue();
}
@ -35,12 +42,42 @@ namespace Microsoft.DotNet.Configurer.UnitTests
[Fact]
public void It_returns_false_if_the_sentinel_does_not_exist()
{
var fileSystemMockBuilder = FileSystemMockBuilder.Create();
var fileSystemMock = fileSystemMockBuilder.Build();
var fileSystemMock = _fileSystemMockBuilder.Build();
var nugetCacheSentinel = new NuGetCacheSentinel(fileSystemMock.File);
var nugetCacheSentinel = new NuGetCacheSentinel(NUGET_CACHE_PATH, fileSystemMock.File);
nugetCacheSentinel.Exists().Should().BeFalse();
}
[Fact]
public void It_creates_the_sentinel_in_the_nuget_cache_path_if_it_does_not_exist_already()
{
var fileSystemMock = _fileSystemMockBuilder.Build();
var nugetCacheSentinel = new NuGetCacheSentinel(NUGET_CACHE_PATH, fileSystemMock.File);
nugetCacheSentinel.Exists().Should().BeFalse();
nugetCacheSentinel.CreateIfNotExists();
nugetCacheSentinel.Exists().Should().BeTrue();
}
[Fact]
public void It_does_not_create_the_sentinel_again_if_it_already_exists_in_the_nuget_cache_path()
{
const string contentToValidateSentinalWasNotReplaced = "some string";
var sentinel = Path.Combine(NUGET_CACHE_PATH, NuGetCacheSentinel.SENTINEL);
_fileSystemMockBuilder.AddFile(sentinel, contentToValidateSentinalWasNotReplaced);
var fileSystemMock = _fileSystemMockBuilder.Build();
var nugetCacheSentinel = new NuGetCacheSentinel(NUGET_CACHE_PATH, fileSystemMock.File);
nugetCacheSentinel.Exists().Should().BeTrue();
nugetCacheSentinel.CreateIfNotExists();
fileSystemMock.File.ReadAllText(sentinel).Should().Be(contentToValidateSentinalWasNotReplaced);
}
}
}

View file

@ -84,6 +84,11 @@ namespace Microsoft.Extensions.DependencyModel.Tests
{
return new MemoryStream(Encoding.UTF8.GetBytes(ReadAllText(path)));
}
public void CreateEmptyFile(string path)
{
_files.Add(path, string.Empty);
}
}
private class DirectoryMock : IDirectory