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