Almost all of the code to prime the NuGet cache from the archive.

- Created a Configurer class that is responsible for deciding when to run the dotnet first time use
experience and invoke the NuGetCachePrimer.
- Added the NuGetCachePrimer which extract the archive and primes the cache.
-- This is just missing creating the sentinel once restore succeeds.
- Added a shell for the NugetPackagesArchiver, which will be responsible for expanding the archive (likely
replaced in the future by an abstraction from Eric's code or its implementation will simply call Eric's code).
- Added a TemporaryFolder abstration to Internal Abstractions that handles deleting the temporary folder once
we are done with it.
This commit is contained in:
Livar Cunha 2016-06-03 17:36:40 -07:00
parent a3447617f9
commit 84f63029fe
25 changed files with 698 additions and 6 deletions

View file

@ -0,0 +1,69 @@
// 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.DotNet.Tools.Test;
using Microsoft.Extensions.DependencyModel.Tests;
using Microsoft.Extensions.EnvironmentAbstractions;
using Moq;
using Xunit;
namespace Microsoft.DotNet.Configurer.UnitTests
{
public class GivenADotnetFirstTimeUseConfigurer
{
private const string NUGET_CACHE_PATH = "some path";
private Mock<INuGetCachePrimer> _nugetCachePrimerMock;
private Mock<INuGetCacheResolver> _nugetCacheResolverMock;
public GivenADotnetFirstTimeUseConfigurer()
{
_nugetCachePrimerMock = new Mock<INuGetCachePrimer>();
_nugetCacheResolverMock = new Mock<INuGetCacheResolver>();
_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]
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();
var dotnetFirstTimeUseConfigurer = new DotnetFirstTimeUseConfigurer(
_nugetCachePrimerMock.Object,
_nugetCacheResolverMock.Object,
fileSystemMock.File);
dotnetFirstTimeUseConfigurer.Configure();
_nugetCachePrimerMock.Verify(r => r.PrimeCache(), Times.Never);
}
[Fact]
public void It_primes_the_cache_if_the_sentinel_does_not_exist()
{
var fileSystemMockBuilder = FileSystemMockBuilder.Create();
var fileSystemMock = fileSystemMockBuilder.Build();
var dotnetFirstTimeUseConfigurer = new DotnetFirstTimeUseConfigurer(
_nugetCachePrimerMock.Object,
_nugetCacheResolverMock.Object,
fileSystemMock.File);
dotnetFirstTimeUseConfigurer.Configure();
_nugetCachePrimerMock.Verify(r => r.PrimeCache(), Times.Once);
}
}
}

View file

@ -0,0 +1,170 @@
// 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.Collections.Generic;
using System.Linq;
using FluentAssertions;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools.Test.Utilities.Mock;
using Microsoft.Extensions.DependencyModel.Tests;
using Microsoft.Extensions.EnvironmentAbstractions;
using Moq;
using NuGet.Frameworks;
using Xunit;
namespace Microsoft.DotNet.Configurer.UnitTests
{
public class GivenANuGetCachePrimer
{
private const string TEMPORARY_FOLDER_PATH = "some path";
private const string PACKAGES_ARCHIVE_PATH = "some other path";
private IFileSystem _fileSystemMock;
private ITemporaryDirectoryMock _temporaryDirectoryMock;
private Mock<ICommandFactory> _commandFactoryMock;
private Mock<ICommand> _dotnetNewCommandMock;
private Mock<ICommand> _dotnetRestoreCommandMock;
private Mock<INuGetPackagesArchiver> _nugetPackagesArchiverMock;
public GivenANuGetCachePrimer()
{
var fileSystemMockBuilder = FileSystemMockBuilder.Create();
fileSystemMockBuilder.TemporaryFolder = TEMPORARY_FOLDER_PATH;
_fileSystemMock = fileSystemMockBuilder.Build();
_temporaryDirectoryMock = (ITemporaryDirectoryMock)_fileSystemMock.Directory.CreateTemporaryDirectory();
_commandFactoryMock = SetupCommandFactoryMock();
_nugetPackagesArchiverMock = new Mock<INuGetPackagesArchiver>();
_nugetPackagesArchiverMock.Setup(n => n.ExtractArchive()).Returns(PACKAGES_ARCHIVE_PATH);
var nugetCachePrimer = new NuGetCachePrimer(
_commandFactoryMock.Object,
_nugetPackagesArchiverMock.Object,
_fileSystemMock.Directory);
nugetCachePrimer.PrimeCache();
}
private Mock<ICommandFactory> SetupCommandFactoryMock()
{
var commandFactoryMock = new Mock<ICommandFactory>();
_dotnetNewCommandMock = new Mock<ICommand>();
SetupCommandMock(_dotnetNewCommandMock);
commandFactoryMock
.Setup(c => c.Create("dotnet new", Enumerable.Empty<string>(), null, Constants.DefaultConfiguration))
.Returns(_dotnetNewCommandMock.Object);
_dotnetRestoreCommandMock = new Mock<ICommand>();
SetupCommandMock(_dotnetRestoreCommandMock);
commandFactoryMock
.Setup(c => c.Create(
"dotnet restore",
It.IsAny<IEnumerable<string>>(),
null,
Constants.DefaultConfiguration))
.Returns(_dotnetRestoreCommandMock.Object);
return commandFactoryMock;
}
private void SetupCommandMock(Mock<ICommand> commandMock)
{
commandMock
.Setup(c => c.WorkingDirectory(TEMPORARY_FOLDER_PATH))
.Returns(commandMock.Object);
commandMock.Setup(c => c.CaptureStdOut()).Returns(commandMock.Object);
commandMock.Setup(c => c.CaptureStdErr()).Returns(commandMock.Object);
}
[Fact]
public void It_disposes_the_temporary_directory_created_for_the_temporary_project_used_to_prime_the_cache()
{
_temporaryDirectoryMock.DisposedTemporaryDirectory.Should().BeTrue();
}
[Fact]
public void It_runs_dotnet_new_using_the_temporary_folder()
{
_dotnetNewCommandMock.Verify(c => c.WorkingDirectory(TEMPORARY_FOLDER_PATH), Times.Once);
}
[Fact]
public void It_runs_dotnet_new_capturing_stdout()
{
_dotnetNewCommandMock.Verify(c => c.CaptureStdOut(), Times.Once);
}
[Fact]
public void It_runs_dotnet_new_capturing_stderr()
{
_dotnetNewCommandMock.Verify(c => c.CaptureStdErr(), Times.Once);
}
[Fact]
public void It_actually_runs_dotnet_new()
{
_dotnetNewCommandMock.Verify(c => c.Execute(), Times.Once);
}
[Fact]
public void It_uses_the_packages_archive_with_dotnet_restore()
{
_commandFactoryMock.Verify(
c => c.Create(
"dotnet restore",
new [] {"-s", $"{PACKAGES_ARCHIVE_PATH}"},
null,
Constants.DefaultConfiguration),
Times.Once);
}
[Fact]
public void It_does_not_run_restore_if_dotnet_new_fails()
{
var commandFactoryMock = SetupCommandFactoryMock();;
_dotnetNewCommandMock.Setup(c => c.Execute()).Returns(new CommandResult(null, -1, null, null));
var nugetCachePrimer = new NuGetCachePrimer(
commandFactoryMock.Object,
_nugetPackagesArchiverMock.Object,
_fileSystemMock.Directory);
nugetCachePrimer.PrimeCache();
commandFactoryMock.Verify(
c => c.Create(
"dotnet restore",
It.IsAny<IEnumerable<string>>(),
It.IsAny<NuGetFramework>(),
It.IsAny<string>()),
Times.Never);
}
[Fact]
public void It_runs_dotnet_restore_using_the_temporary_folder()
{
_dotnetRestoreCommandMock.Verify(c => c.WorkingDirectory(TEMPORARY_FOLDER_PATH), Times.Once);
}
[Fact]
public void It_runs_dotnet_restore_capturing_stdout()
{
_dotnetRestoreCommandMock.Verify(c => c.CaptureStdOut(), Times.Once);
}
[Fact]
public void It_runs_dotnet_restore_capturing_stderr()
{
_dotnetRestoreCommandMock.Verify(c => c.CaptureStdErr(), Times.Once);
}
[Fact]
public void It_actually_runs_dotnet_restore()
{
_dotnetRestoreCommandMock.Verify(c => c.Execute(), Times.Once);
}
}
}

View file

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0.23107" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0.23107</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>4c3b06d5-b6d5-4e5b-a44f-3ebe52a1c759</ProjectGuid>
<RootNamespace>Microsoft.DotNet.Configurer.UnitTests</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin</OutputPath>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>

View file

@ -0,0 +1,35 @@
{
"version": "1.0.0-*",
"buildOptions": {
"keyFile": "../../tools/Key.snk"
},
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0-rc3-004363"
},
"System.Diagnostics.TraceSource": "4.0.0-rc3-24131-00",
"Microsoft.DotNet.Configurer": {
"target": "project"
},
"Microsoft.DotNet.Tools.Tests.Utilities": {
"target": "project"
},
"Microsoft.DotNet.Cli.Utils": {
"target": "project"
},
"FluentAssertions": "4.0.0",
"moq.netcore": "4.4.0-beta8",
"xunit": "2.1.0",
"dotnet-test-xunit": "1.0.0-rc2-192208-24"
},
"frameworks": {
"netcoreapp1.0": {
"imports": [
"dotnet5.4",
"portable-net451+win8"
]
}
},
"testRunner": "xunit"
}

View file

@ -1,10 +1,12 @@
// 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.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.DotNet.Tools.Test.Utilities.Mock;
using Microsoft.Extensions.EnvironmentAbstractions;
namespace Microsoft.Extensions.DependencyModel.Tests
@ -13,6 +15,8 @@ namespace Microsoft.Extensions.DependencyModel.Tests
{
private Dictionary<string, string> _files = new Dictionary<string, string>();
public string TemporaryFolder { get; set; }
internal static IFileSystem Empty { get; } = Create().Build();
public static FileSystemMockBuilder Create()
@ -37,15 +41,15 @@ namespace Microsoft.Extensions.DependencyModel.Tests
internal IFileSystem Build()
{
return new FileSystemMock(_files);
return new FileSystemMock(_files, TemporaryFolder);
}
private class FileSystemMock : IFileSystem
{
public FileSystemMock(Dictionary<string, string> files)
public FileSystemMock(Dictionary<string, string> files, string temporaryFolder)
{
File = new FileMock(files);
Directory = new DirectoryMock(files);
Directory = new DirectoryMock(files, temporaryFolder);
}
public IFile File { get; }
@ -85,9 +89,17 @@ namespace Microsoft.Extensions.DependencyModel.Tests
private class DirectoryMock : IDirectory
{
private Dictionary<string, string> _files;
public DirectoryMock(Dictionary<string, string> files)
private readonly TemporaryDirectoryMock _temporaryDirectory;
public DirectoryMock(Dictionary<string, string> files, string temporaryDirectory)
{
_files = files;
_temporaryDirectory = new TemporaryDirectoryMock(temporaryDirectory);
}
public ITemporaryDirectory CreateTemporaryDirectory()
{
return _temporaryDirectory;
}
public bool Exists(string path)
@ -95,6 +107,23 @@ namespace Microsoft.Extensions.DependencyModel.Tests
return _files.Keys.Any(k => k.StartsWith(path));
}
}
private class TemporaryDirectoryMock : ITemporaryDirectoryMock
{
public bool DisposedTemporaryDirectory { get; private set; }
public TemporaryDirectoryMock(string temporaryDirectory)
{
DirectoryPath = temporaryDirectory;
}
public string DirectoryPath { get; }
public void Dispose()
{
DisposedTemporaryDirectory = true;
}
}
}
}

View file

@ -0,0 +1,12 @@
// 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.Extensions.EnvironmentAbstractions;
namespace Microsoft.DotNet.Tools.Test.Utilities.Mock
{
internal interface ITemporaryDirectoryMock : ITemporaryDirectory
{
bool DisposedTemporaryDirectory { get; }
}
}

View file

@ -1,3 +1,4 @@
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("Microsoft.Extensions.DependencyModel.Tests , PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
[assembly: InternalsVisibleTo("Microsoft.Extensions.DependencyModel.Tests , PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
[assembly: InternalsVisibleTo("Microsoft.DotNet.Configurer.UnitTests , PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]