2016-02-10 20:13:56 -08: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.
using System ;
using System.Collections.Generic ;
using System.IO ;
using FluentAssertions ;
using Microsoft.Extensions.EnvironmentAbstractions ;
using Microsoft.Extensions.DependencyModel.Resolution ;
using Xunit ;
using F = Microsoft . Extensions . DependencyModel . Tests . TestLibraryFactory ;
namespace Microsoft.Extensions.DependencyModel.Tests
{
public class PackageCacheResolverTest
{
private static string CachePath = Path . Combine ( "cache" , "directory" , "location" ) ;
[Fact]
public void SholdUseEnvironmentVariableToGetDefaultLocation ( )
{
2016-07-26 13:20:23 -05:00
var result = PackageCacheCompilationAssemblyResolver . GetDefaultPackageCacheDirectory ( GetDefaultEnvironment ( ) ) ;
2016-02-10 20:13:56 -08:00
result . Should ( ) . Be ( CachePath ) ;
}
[Fact]
public void SkipsNonPackage ( )
{
var resolver = new PackageCacheCompilationAssemblyResolver ( ) ;
var library = F . Create (
F . PackageType ,
assemblies : F . EmptyAssemblies ) ;
var result = resolver . TryResolveAssemblyPaths ( library , null ) ;
result . Should ( ) . BeFalse ( ) ;
}
[Theory]
[InlineData("INVALIDHASHVALUE")]
[InlineData("INVALIDHASHVALUE-")]
[InlineData("-INVALIDHASHVALUE")]
public void FailsOnInvalidHash ( string hash )
{
var resolver = new PackageCacheCompilationAssemblyResolver ( FileSystemMockBuilder . Empty , CachePath ) ;
var library = F . Create ( hash : hash ) ;
var exception = Assert . Throws < InvalidOperationException > ( ( ) = > resolver . TryResolveAssemblyPaths ( library , null ) ) ;
exception . Message . Should ( )
. Contain ( library . Hash )
2016-03-04 14:12:16 -08:00
. And . Contain ( library . Name ) ;
2016-02-10 20:13:56 -08:00
}
[Fact]
public void ChecksHashFile ( )
{
2016-07-26 13:20:23 -05:00
var packagePath = GetPackagesPath ( F . DefaultPackageName , F . DefaultVersion ) ;
2016-02-10 20:13:56 -08:00
var fileSystem = FileSystemMockBuilder . Create ( )
. AddFile (
2016-07-26 13:20:23 -05:00
GetHashFilePath ( packagePath ) ,
2016-02-10 20:13:56 -08:00
"WRONGHASH"
)
. AddFiles ( packagePath , F . DefaultAssemblies )
. Build ( ) ;
var resolver = new PackageCacheCompilationAssemblyResolver ( fileSystem , CachePath ) ;
var assemblies = new List < string > ( ) ;
var result = resolver . TryResolveAssemblyPaths ( F . Create ( ) , assemblies ) ;
result . Should ( ) . BeFalse ( ) ;
}
[Fact]
public void ResolvesAllAssemblies ( )
{
2016-07-26 13:20:23 -05:00
var packagePath = GetPackagesPath ( F . DefaultPackageName , F . DefaultVersion ) ;
2016-02-10 20:13:56 -08:00
var fileSystem = FileSystemMockBuilder . Create ( )
. AddFile (
2016-07-26 13:20:23 -05:00
GetHashFilePath ( packagePath ) ,
2016-02-10 20:13:56 -08:00
F . DefaultHashValue
)
. AddFiles ( packagePath , F . TwoAssemblies )
. Build ( ) ;
var library = F . Create ( assemblies : F . TwoAssemblies ) ;
var resolver = new PackageCacheCompilationAssemblyResolver ( fileSystem , CachePath ) ;
var assemblies = new List < string > ( ) ;
var result = resolver . TryResolveAssemblyPaths ( library , assemblies ) ;
assemblies . Should ( ) . HaveCount ( 2 ) ;
assemblies . Should ( ) . Contain ( Path . Combine ( packagePath , F . DefaultAssemblyPath ) ) ;
assemblies . Should ( ) . Contain ( Path . Combine ( packagePath , F . SecondAssemblyPath ) ) ;
}
[Fact]
public void FailsWhenOneOfAssembliesNotFound ( )
{
2016-07-26 13:20:23 -05:00
var packagePath = GetPackagesPath ( F . DefaultPackageName , F . DefaultVersion ) ;
2016-02-10 20:13:56 -08:00
var fileSystem = FileSystemMockBuilder . Create ( )
. AddFile (
2016-07-26 13:20:23 -05:00
GetHashFilePath ( packagePath ) ,
2016-02-10 20:13:56 -08:00
F . DefaultHashValue
)
. AddFiles ( packagePath , F . DefaultAssemblyPath )
. Build ( ) ;
var library = F . Create ( assemblies : F . TwoAssemblies ) ;
var resolver = new PackageCacheCompilationAssemblyResolver ( fileSystem , CachePath ) ;
var assemblies = new List < string > ( ) ;
var exception = Assert . Throws < InvalidOperationException > ( ( ) = > resolver . TryResolveAssemblyPaths ( library , assemblies ) ) ;
exception . Message . Should ( )
. Contain ( F . SecondAssemblyPath )
2016-03-04 14:12:16 -08:00
. And . Contain ( library . Name ) ;
2016-02-10 20:13:56 -08:00
}
2016-07-26 13:20:23 -05:00
private IEnvironment GetDefaultEnvironment ( )
2016-02-10 20:13:56 -08:00
{
return EnvironmentMockBuilder . Create ( )
. AddVariable ( "DOTNET_PACKAGES_CACHE" , CachePath )
. Build ( ) ;
}
2016-07-26 13:20:23 -05:00
private static string GetPackagesPath ( string id , string version )
{
return PackageResolverTest . GetPackagesPath ( CachePath , id , version ) ;
}
2016-02-10 20:13:56 -08:00
2016-07-26 13:20:23 -05:00
private static string GetHashFilePath ( string packagePath )
{
return Path . Combine (
packagePath ,
$"{F.DefaultPackageName.ToLowerInvariant()}.{F.DefaultVersion.ToLowerInvariant()}.nupkg.{F.DefaultHashAlgoritm}" ) ;
}
2016-02-10 20:13:56 -08:00
}
}