dotnet-installer/test/Microsoft.DotNet.Configurer.UnitTests/GivenANuGetCachePrimer.cs
Livar Cunha f4bb13acaa Merge branch 'rel/1.1.0' into merge_rel_110
* rel/1.1.0: (41 commits)
  Updating the Sdk to one that includes the error surfacing work.
  Update NuGet to 4.3.0-preview2-4082
  Update NuGet to 4.3.0-preview1-4081 and SDK to corresponding 1.1.0 based version
  Updating MSBuild to 15.3.0-preview-000246-05 to match VS.
  Updating the global.json creation to use the IFile interface and adding a unit test to cover it.
  Making restore use a config file so that it does not use fallback folders that may exist in the machine.
  Dropping a global.json when running the first run experience with a version that matches the version of the CLI being used in the command that triggered the first run.
  Updating the websdk version for 1.0
  Trying to fix the opensuse42 test failure, where we tried to invoke a tool that target 1.0.4 where the 1.0 runtime is not available.
  Pinning the stage0 to the last build out of rel/1.0.1 and adding a project to download 1.0 dependencies for test assets.
  Updating the branding to rel/1.1.0
  Dummy commit.
  Adding the access token to the lzma url.
  Dummy change to force a build.
  Reverting the msbuild version to the release version.
  Adding the web feed to nuget.config, as some packages failed to mirror and we need a build ASAP.
  Updating the msbuild, SDK and Web SDK versions.
  Dummy commit to kick off the build.
  the FSharp.NET.Sdk version `-bundled` contains only the Sdk dir
  bump f# sdk package version
  ...
2017-06-01 09:43:31 -07:00

121 lines
4.7 KiB
C#

// 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.Linq;
using System.IO;
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 COMPRESSED_ARCHIVE_PATH = "a path to somewhere";
private const string TEMPORARY_FOLDER_PATH = "some path";
private const string PACKAGES_ARCHIVE_PATH = "some other path";
private IFileSystem _fileSystemMock;
private Mock<INuGetPackagesArchiver> _nugetPackagesArchiverMock;
private Mock<INuGetCacheSentinel> _nugetCacheSentinel;
private Mock<INuGetConfig> _nugetConfigMock;
private CliFallbackFolderPathCalculator _cliFallbackFolderPathCalculator;
public GivenANuGetCachePrimer()
{
var fileSystemMockBuilder = FileSystemMockBuilder.Create();
fileSystemMockBuilder.TemporaryFolder = TEMPORARY_FOLDER_PATH;
fileSystemMockBuilder.AddFile(COMPRESSED_ARCHIVE_PATH);
_fileSystemMock = fileSystemMockBuilder.Build();
_nugetPackagesArchiverMock = new Mock<INuGetPackagesArchiver>();
_nugetPackagesArchiverMock.Setup(n => n.NuGetPackagesArchive).Returns(COMPRESSED_ARCHIVE_PATH);
_nugetCacheSentinel = new Mock<INuGetCacheSentinel>();
_nugetConfigMock = new Mock<INuGetConfig>();
_cliFallbackFolderPathCalculator = new CliFallbackFolderPathCalculator();
var nugetCachePrimer = new NuGetCachePrimer(
_nugetPackagesArchiverMock.Object,
_nugetCacheSentinel.Object,
_nugetConfigMock.Object,
_cliFallbackFolderPathCalculator,
_fileSystemMock.File);
nugetCachePrimer.PrimeCache();
}
[Fact]
public void It_does_not_prime_the_NuGet_cache_if_the_archive_is_not_found_so_that_we_do_not_need_to_generate_the_archive_for_stage1()
{
var fileSystemMockBuilder = FileSystemMockBuilder.Create();
var fileSystemMock = fileSystemMockBuilder.Build();
var nugetPackagesArchiverMock = new Mock<INuGetPackagesArchiver>();
nugetPackagesArchiverMock.Setup(n => n.NuGetPackagesArchive).Returns(COMPRESSED_ARCHIVE_PATH);
var nugetCachePrimer = new NuGetCachePrimer(
nugetPackagesArchiverMock.Object,
_nugetCacheSentinel.Object,
_nugetConfigMock.Object,
_cliFallbackFolderPathCalculator,
fileSystemMock.File);
nugetCachePrimer.PrimeCache();
nugetPackagesArchiverMock.Verify(n => n.ExtractArchive(It.IsAny<string>()), Times.Never);
}
[Fact]
public void It_adds_the_fallback_folder_to_NuGet_Config()
{
_nugetConfigMock.Verify(n =>
n.AddCliFallbackFolder(_cliFallbackFolderPathCalculator.CliFallbackFolderPath),
Times.Exactly(1));
}
[Fact]
public void It_extracts_the_archive_to_the_fallback_folder()
{
_nugetPackagesArchiverMock.Verify(n =>
n.ExtractArchive(_cliFallbackFolderPathCalculator.CliFallbackFolderPath),
Times.Exactly(1));
}
[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_extracting_the_archive_fails()
{
var nugetCacheSentinel = new Mock<INuGetCacheSentinel>();
var nugetPackagesArchiveMock = new Mock<INuGetPackagesArchiver>();
nugetPackagesArchiveMock.Setup(n => n.ExtractArchive(It.IsAny<string>())).Throws<Exception>();
var nugetCachePrimer = new NuGetCachePrimer(
nugetPackagesArchiveMock.Object,
nugetCacheSentinel.Object,
_nugetConfigMock.Object,
_cliFallbackFolderPathCalculator,
_fileSystemMock.File);
Action action = () => nugetCachePrimer.PrimeCache();
action.ShouldThrow<Exception>();
nugetCacheSentinel.Verify(n => n.CreateIfNotExists(), Times.Never);
}
}
}