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

@ -115,6 +115,10 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.DotNet.ProjectMod
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.DotNet.ProjectModel.Loader.Tests", "test\Microsoft.DotNet.ProjectModel.Loader.Tests\Microsoft.DotNet.ProjectModel.Loader.Tests.xproj", "{5DF6C9DA-6909-4EC0-909E-6913580BB4A4}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.DotNet.Configurer", "src\Microsoft.DotNet.Configurer\Microsoft.DotNet.Configurer.xproj", "{E5ED47EF-BF25-4DA9-A7FE-290C642CBF0F}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.DotNet.Configurer.UnitTests", "test\Microsoft.DotNet.Configurer.UnitTests\Microsoft.DotNet.Configurer.UnitTests.xproj", "{4C3B06D5-B6D5-4E5B-A44F-3EBE52A1C759}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -847,6 +851,38 @@ Global
{5DF6C9DA-6909-4EC0-909E-6913580BB4A4}.RelWithDebInfo|Any CPU.Build.0 = Release|Any CPU
{5DF6C9DA-6909-4EC0-909E-6913580BB4A4}.RelWithDebInfo|x64.ActiveCfg = Release|Any CPU
{5DF6C9DA-6909-4EC0-909E-6913580BB4A4}.RelWithDebInfo|x64.Build.0 = Release|Any CPU
{E5ED47EF-BF25-4DA9-A7FE-290C642CBF0F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E5ED47EF-BF25-4DA9-A7FE-290C642CBF0F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E5ED47EF-BF25-4DA9-A7FE-290C642CBF0F}.Debug|x64.ActiveCfg = Debug|Any CPU
{E5ED47EF-BF25-4DA9-A7FE-290C642CBF0F}.Debug|x64.Build.0 = Debug|Any CPU
{E5ED47EF-BF25-4DA9-A7FE-290C642CBF0F}.MinSizeRel|Any CPU.ActiveCfg = Debug|Any CPU
{E5ED47EF-BF25-4DA9-A7FE-290C642CBF0F}.MinSizeRel|Any CPU.Build.0 = Debug|Any CPU
{E5ED47EF-BF25-4DA9-A7FE-290C642CBF0F}.MinSizeRel|x64.ActiveCfg = Debug|Any CPU
{E5ED47EF-BF25-4DA9-A7FE-290C642CBF0F}.MinSizeRel|x64.Build.0 = Debug|Any CPU
{E5ED47EF-BF25-4DA9-A7FE-290C642CBF0F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E5ED47EF-BF25-4DA9-A7FE-290C642CBF0F}.Release|Any CPU.Build.0 = Release|Any CPU
{E5ED47EF-BF25-4DA9-A7FE-290C642CBF0F}.Release|x64.ActiveCfg = Release|Any CPU
{E5ED47EF-BF25-4DA9-A7FE-290C642CBF0F}.Release|x64.Build.0 = Release|Any CPU
{E5ED47EF-BF25-4DA9-A7FE-290C642CBF0F}.RelWithDebInfo|Any CPU.ActiveCfg = Release|Any CPU
{E5ED47EF-BF25-4DA9-A7FE-290C642CBF0F}.RelWithDebInfo|Any CPU.Build.0 = Release|Any CPU
{E5ED47EF-BF25-4DA9-A7FE-290C642CBF0F}.RelWithDebInfo|x64.ActiveCfg = Release|Any CPU
{E5ED47EF-BF25-4DA9-A7FE-290C642CBF0F}.RelWithDebInfo|x64.Build.0 = Release|Any CPU
{4C3B06D5-B6D5-4E5B-A44F-3EBE52A1C759}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4C3B06D5-B6D5-4E5B-A44F-3EBE52A1C759}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4C3B06D5-B6D5-4E5B-A44F-3EBE52A1C759}.Debug|x64.ActiveCfg = Debug|Any CPU
{4C3B06D5-B6D5-4E5B-A44F-3EBE52A1C759}.Debug|x64.Build.0 = Debug|Any CPU
{4C3B06D5-B6D5-4E5B-A44F-3EBE52A1C759}.MinSizeRel|Any CPU.ActiveCfg = Debug|Any CPU
{4C3B06D5-B6D5-4E5B-A44F-3EBE52A1C759}.MinSizeRel|Any CPU.Build.0 = Debug|Any CPU
{4C3B06D5-B6D5-4E5B-A44F-3EBE52A1C759}.MinSizeRel|x64.ActiveCfg = Debug|Any CPU
{4C3B06D5-B6D5-4E5B-A44F-3EBE52A1C759}.MinSizeRel|x64.Build.0 = Debug|Any CPU
{4C3B06D5-B6D5-4E5B-A44F-3EBE52A1C759}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4C3B06D5-B6D5-4E5B-A44F-3EBE52A1C759}.Release|Any CPU.Build.0 = Release|Any CPU
{4C3B06D5-B6D5-4E5B-A44F-3EBE52A1C759}.Release|x64.ActiveCfg = Release|Any CPU
{4C3B06D5-B6D5-4E5B-A44F-3EBE52A1C759}.Release|x64.Build.0 = Release|Any CPU
{4C3B06D5-B6D5-4E5B-A44F-3EBE52A1C759}.RelWithDebInfo|Any CPU.ActiveCfg = Release|Any CPU
{4C3B06D5-B6D5-4E5B-A44F-3EBE52A1C759}.RelWithDebInfo|Any CPU.Build.0 = Release|Any CPU
{4C3B06D5-B6D5-4E5B-A44F-3EBE52A1C759}.RelWithDebInfo|x64.ActiveCfg = Release|Any CPU
{4C3B06D5-B6D5-4E5B-A44F-3EBE52A1C759}.RelWithDebInfo|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -902,5 +938,7 @@ Global
{1DBB7542-0345-4F4B-A84B-3B00B185D416} = {88278B81-7649-45DC-8A6A-D3A645C5AFC3}
{1C599FFD-FB52-4279-A8E5-465D3EC499E1} = {ED2FE3E2-F7E7-4389-8231-B65123F2076F}
{5DF6C9DA-6909-4EC0-909E-6913580BB4A4} = {17735A9D-BFD9-4585-A7CB-3208CA6EA8A7}
{E5ED47EF-BF25-4DA9-A7FE-290C642CBF0F} = {ED2FE3E2-F7E7-4389-8231-B65123F2076F}
{4C3B06D5-B6D5-4E5B-A44F-3EBE52A1C759} = {17735A9D-BFD9-4585-A7CB-3208CA6EA8A7}
EndGlobalSection
EndGlobal

View file

@ -40,6 +40,7 @@ namespace Microsoft.DotNet.Cli.Build
"Microsoft.DotNet.Compiler.Common.Tests",
"Microsoft.DotNet.ProjectModel.Tests",
"Microsoft.Extensions.DependencyModel.Tests",
"Microsoft.DotNet.Configurer.UnitTests",
"Performance"
};

View file

@ -0,0 +1,49 @@
// 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;
namespace Microsoft.DotNet.Configurer
{
public class DotnetFirstTimeUseConfigurer
{
public static readonly string SENTINEL = $"{Product.Version}.dotnetSentinel";
private IFile _file;
private INuGetCachePrimer _nugetCachePrimer;
private INuGetCacheResolver _nugetCacheResolver;
public DotnetFirstTimeUseConfigurer(INuGetCachePrimer nugetCachePrimer, INuGetCacheResolver nugetCacheResolver)
: this(nugetCachePrimer, nugetCacheResolver, FileSystemWrapper.Default.File)
{
}
internal DotnetFirstTimeUseConfigurer(
INuGetCachePrimer nugetCachePrimer,
INuGetCacheResolver nugetCacheResolver,
IFile file)
{
_file = file;
_nugetCachePrimer = nugetCachePrimer;
_nugetCacheResolver = nugetCacheResolver;
}
public void Configure()
{
if(ShouldPrimeNugetCache())
{
_nugetCachePrimer.PrimeCache();
}
}
private bool ShouldPrimeNugetCache()
{
var nugetCachePath = _nugetCacheResolver.ResolveNugetCachePath();
var sentinel = Path.Combine(nugetCachePath, SENTINEL);
return !_file.Exists(sentinel);
}
}
}

View file

@ -0,0 +1,10 @@
// 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.
namespace Microsoft.DotNet.Configurer
{
public interface INuGetCachePrimer
{
void PrimeCache();
}
}

View file

@ -0,0 +1,10 @@
// 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.
namespace Microsoft.DotNet.Configurer
{
public interface INuGetCacheResolver
{
string ResolveNugetCachePath();
}
}

View file

@ -0,0 +1,10 @@
// 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.
namespace Microsoft.DotNet.Configurer
{
public interface INuGetPackagesArchiver
{
string ExtractArchive();
}
}

View file

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0.25123" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0.25123</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>e5ed47ef-bf25-4da9-a7fe-290c642cbf0f</ProjectGuid>
<RootNamespace>Microsoft.DotNet.Configurer</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>

View file

@ -0,0 +1,92 @@
// 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 Microsoft.DotNet.Cli.Utils;
using Microsoft.Extensions.EnvironmentAbstractions;
namespace Microsoft.DotNet.Configurer
{
public class NuGetCachePrimer : INuGetCachePrimer
{
private const string NUGET_SOURCE_PARAMETER = "-s";
private readonly ICommandFactory _commandFactory;
private readonly IDirectory _directory;
private readonly INuGetPackagesArchiver _nugetPackagesArchiver;
public NuGetCachePrimer(ICommandFactory commandFactory, INuGetPackagesArchiver nugetPackagesArchiver)
: this(commandFactory, nugetPackagesArchiver, FileSystemWrapper.Default.Directory)
{
}
internal NuGetCachePrimer(
ICommandFactory commandFactory,
INuGetPackagesArchiver nugetPackagesArchiver,
IDirectory directory)
{
_commandFactory = commandFactory;
_directory = directory;
_nugetPackagesArchiver = nugetPackagesArchiver;
}
public void PrimeCache()
{
var pathToPackagesArchive = _nugetPackagesArchiver.ExtractArchive();
PrimeCacheUsingArchive(pathToPackagesArchive);
}
private void PrimeCacheUsingArchive(string pathToPackagesArchive)
{
using (var temporaryDotnetNewDirectory = _directory.CreateTemporaryDirectory())
{
var workingDirectory = temporaryDotnetNewDirectory.DirectoryPath;
var dotnetNewSucceeded = CreateTemporaryProject(workingDirectory);
if (dotnetNewSucceeded)
{
RestoreTemporaryProject(pathToPackagesArchive, workingDirectory);
}
}
// -- 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)
{
return RunCommand("dotnet new", Enumerable.Empty<string>(), workingDirectory);
}
private bool RestoreTemporaryProject(string pathToPackagesArchive, string workingDirectory)
{
return RunCommand(
"dotnet restore",
new[] {NUGET_SOURCE_PARAMETER, $"{pathToPackagesArchive}"},
workingDirectory);
}
private bool RunCommand(string commandToExecute, IEnumerable<string> args, string workingDirectory)
{
var command = _commandFactory
.Create(commandToExecute, args)
.WorkingDirectory(workingDirectory)
.CaptureStdOut()
.CaptureStdErr();
var commandResult = command.Execute();
if (commandResult.ExitCode != 0)
{
Reporter.Verbose.WriteLine(commandResult.StdErr);
Reporter.Error.WriteLine(
$"Failed to create prime the NuGet cache. {commandToExecute} failed with: {commandResult.ExitCode}");
}
return commandResult.ExitCode == 0;
}
}
}

View file

@ -0,0 +1,15 @@
// 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,20 @@
// 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.
namespace Microsoft.DotNet.Configurer
{
public class NuGetPackagesArchiver : INuGetPackagesArchiver
{
public string ExtractArchive()
{
// -- ExtractArchive
// find archive
// extract archive to temporary folder
// Path.GetTempPath();
// Path.GetRandomFileName();
// Consider putting this inside an abstraction that will delete the folder automatically once it is done.
return @"C:\Users\licavalc\git\temp\feed";
}
}
}

View file

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

View file

@ -0,0 +1,34 @@
{
"version": "1.0.0-rc3-*",
"buildOptions": {
"warningsAsErrors": true,
"keyFile": "../../tools/Key.snk",
},
"dependencies": {
"Microsoft.DotNet.InternalAbstractions": {
"target": "project"
},
"Microsoft.DotNet.Cli.Utils": {
"target": "project"
},
"Microsoft.DotNet.ProjectModel": {
"target": "project"
}
},
"frameworks": {
"netcoreapp1.0": {
"imports": [
"dnxcore50",
"netstandardapp1.5",
"portable-net45+win8",
"portable-net45+wp80+win8+wpa81+dnxcore50"
]
}
},
"packOptions": {
"repository": {
"type": "git",
"url": "git://github.com/dotnet/cli"
}
}
}

View file

@ -2,6 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.IO;
using Microsoft.DotNet.InternalAbstractions;
namespace Microsoft.Extensions.EnvironmentAbstractions
{
@ -11,5 +12,10 @@ namespace Microsoft.Extensions.EnvironmentAbstractions
{
return Directory.Exists(path);
}
public ITemporaryDirectory CreateTemporaryDirectory()
{
return new TemporaryDirectory();
}
}
}

View file

@ -6,5 +6,7 @@ namespace Microsoft.Extensions.EnvironmentAbstractions
internal interface IDirectory
{
bool Exists(string path);
ITemporaryDirectory CreateTemporaryDirectory();
}
}

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 System;
namespace Microsoft.Extensions.EnvironmentAbstractions
{
internal interface ITemporaryDirectory : IDisposable
{
string DirectoryPath { get; }
}
}

View file

@ -2,4 +2,7 @@
[assembly: InternalsVisibleTo("Microsoft.Extensions.DependencyModel, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
[assembly: InternalsVisibleTo("Microsoft.DotNet.Tools.Tests.Utilities, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
[assembly: InternalsVisibleTo("Microsoft.Extensions.DependencyModel.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
[assembly: InternalsVisibleTo("Microsoft.Extensions.DependencyModel.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
[assembly: InternalsVisibleTo("Microsoft.DotNet.Configurer, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
[assembly: InternalsVisibleTo("Microsoft.DotNet.Configurer.UnitTests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]

View file

@ -0,0 +1,31 @@
// 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;
using System.IO;
namespace Microsoft.DotNet.InternalAbstractions
{
internal class TemporaryDirectory : ITemporaryDirectory
{
public string DirectoryPath { get; }
public TemporaryDirectory()
{
DirectoryPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Directory.CreateDirectory(DirectoryPath);
}
public void Dispose()
{
try
{
Directory.Delete(DirectoryPath, true);
}
catch
{
// Ignore failures here.
}
}
}
}

View file

@ -43,6 +43,9 @@
"Microsoft.Extensions.Testing.Abstractions": {
"target": "project"
},
"Microsoft.DotNet.Configurer": {
"target": "project"
},
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0-rc3-004449-00"

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")]