2016-06-04 00:36:40 +00:00
|
|
|
|
// 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.
|
|
|
|
|
|
2017-03-30 07:11:17 +00:00
|
|
|
|
using System;
|
2016-06-04 00:36:40 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2017-05-20 04:35:45 +00:00
|
|
|
|
using System.IO;
|
2016-06-04 00:36:40 +00:00
|
|
|
|
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
|
|
|
|
|
{
|
2016-06-10 03:52:49 +00:00
|
|
|
|
private const string COMPRESSED_ARCHIVE_PATH = "a path to somewhere";
|
2016-06-04 00:36:40 +00:00
|
|
|
|
private const string TEMPORARY_FOLDER_PATH = "some path";
|
|
|
|
|
private const string PACKAGES_ARCHIVE_PATH = "some other path";
|
|
|
|
|
|
|
|
|
|
private IFileSystem _fileSystemMock;
|
|
|
|
|
|
|
|
|
|
private Mock<INuGetPackagesArchiver> _nugetPackagesArchiverMock;
|
2016-06-06 19:51:27 +00:00
|
|
|
|
private Mock<INuGetCacheSentinel> _nugetCacheSentinel;
|
2017-04-04 05:15:40 +00:00
|
|
|
|
private CliFallbackFolderPathCalculator _cliFallbackFolderPathCalculator;
|
2016-06-04 00:36:40 +00:00
|
|
|
|
|
|
|
|
|
public GivenANuGetCachePrimer()
|
|
|
|
|
{
|
|
|
|
|
var fileSystemMockBuilder = FileSystemMockBuilder.Create();
|
|
|
|
|
fileSystemMockBuilder.TemporaryFolder = TEMPORARY_FOLDER_PATH;
|
2016-06-10 03:52:49 +00:00
|
|
|
|
fileSystemMockBuilder.AddFile(COMPRESSED_ARCHIVE_PATH);
|
2016-06-04 00:36:40 +00:00
|
|
|
|
_fileSystemMock = fileSystemMockBuilder.Build();
|
2017-03-30 07:11:17 +00:00
|
|
|
|
|
2016-06-04 00:36:40 +00:00
|
|
|
|
_nugetPackagesArchiverMock = new Mock<INuGetPackagesArchiver>();
|
2016-06-10 03:52:49 +00:00
|
|
|
|
_nugetPackagesArchiverMock.Setup(n => n.NuGetPackagesArchive).Returns(COMPRESSED_ARCHIVE_PATH);
|
2016-06-04 00:36:40 +00:00
|
|
|
|
|
2016-06-06 19:51:27 +00:00
|
|
|
|
_nugetCacheSentinel = new Mock<INuGetCacheSentinel>();
|
|
|
|
|
|
2017-04-04 05:15:40 +00:00
|
|
|
|
_cliFallbackFolderPathCalculator = new CliFallbackFolderPathCalculator();
|
2017-03-30 18:28:01 +00:00
|
|
|
|
|
2016-06-04 00:36:40 +00:00
|
|
|
|
var nugetCachePrimer = new NuGetCachePrimer(
|
|
|
|
|
_nugetPackagesArchiverMock.Object,
|
2016-06-06 19:51:27 +00:00
|
|
|
|
_nugetCacheSentinel.Object,
|
2017-03-30 18:28:01 +00:00
|
|
|
|
_cliFallbackFolderPathCalculator,
|
2016-06-10 03:52:49 +00:00
|
|
|
|
_fileSystemMock.File);
|
2016-06-04 00:36:40 +00:00
|
|
|
|
|
2017-03-30 07:11:17 +00:00
|
|
|
|
nugetCachePrimer.PrimeCache();
|
2016-06-04 00:36:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-10 03:52:49 +00:00
|
|
|
|
[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,
|
2017-03-30 18:28:01 +00:00
|
|
|
|
_cliFallbackFolderPathCalculator,
|
2016-06-10 03:52:49 +00:00
|
|
|
|
fileSystemMock.File);
|
|
|
|
|
|
|
|
|
|
nugetCachePrimer.PrimeCache();
|
|
|
|
|
|
2017-03-30 07:11:17 +00:00
|
|
|
|
nugetPackagesArchiverMock.Verify(n => n.ExtractArchive(It.IsAny<string>()), Times.Never);
|
2016-06-04 00:36:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2017-03-30 07:11:17 +00:00
|
|
|
|
public void It_extracts_the_archive_to_the_fallback_folder()
|
2016-06-04 00:36:40 +00:00
|
|
|
|
{
|
2017-03-30 18:28:01 +00:00
|
|
|
|
_nugetPackagesArchiverMock.Verify(n =>
|
2017-04-04 05:15:40 +00:00
|
|
|
|
n.ExtractArchive(_cliFallbackFolderPathCalculator.CliFallbackFolderPath),
|
2017-03-30 18:28:01 +00:00
|
|
|
|
Times.Exactly(1));
|
2016-06-04 00:36:40 +00:00
|
|
|
|
}
|
2016-06-06 19:51:27 +00:00
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void It_creates_a_sentinel_when_restore_succeeds()
|
|
|
|
|
{
|
|
|
|
|
_nugetCacheSentinel.Verify(n => n.CreateIfNotExists(), Times.Once);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2017-03-30 07:11:17 +00:00
|
|
|
|
public void It_does_not_create_a_sentinel_when_extracting_the_archive_fails()
|
2016-06-06 19:51:27 +00:00
|
|
|
|
{
|
|
|
|
|
var nugetCacheSentinel = new Mock<INuGetCacheSentinel>();
|
2017-03-30 07:11:17 +00:00
|
|
|
|
var nugetPackagesArchiveMock = new Mock<INuGetPackagesArchiver>();
|
|
|
|
|
nugetPackagesArchiveMock.Setup(n => n.ExtractArchive(It.IsAny<string>())).Throws<Exception>();
|
2016-06-06 19:51:27 +00:00
|
|
|
|
|
|
|
|
|
var nugetCachePrimer = new NuGetCachePrimer(
|
2017-03-30 07:11:17 +00:00
|
|
|
|
nugetPackagesArchiveMock.Object,
|
2016-06-06 19:51:27 +00:00
|
|
|
|
nugetCacheSentinel.Object,
|
2017-03-30 18:28:01 +00:00
|
|
|
|
_cliFallbackFolderPathCalculator,
|
2016-06-10 03:52:49 +00:00
|
|
|
|
_fileSystemMock.File);
|
2016-06-06 19:51:27 +00:00
|
|
|
|
|
2017-03-30 07:11:17 +00:00
|
|
|
|
Action action = () => nugetCachePrimer.PrimeCache();
|
2016-06-06 19:51:27 +00:00
|
|
|
|
|
2017-03-30 07:11:17 +00:00
|
|
|
|
action.ShouldThrow<Exception>();
|
2016-06-06 19:51:27 +00:00
|
|
|
|
nugetCacheSentinel.Verify(n => n.CreateIfNotExists(), Times.Never);
|
|
|
|
|
}
|
2016-06-04 00:36:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|