2016-03-24 21:52:46 -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.
2016-03-30 11:41:16 -07:00
using System ;
2016-03-24 21:52:46 -07:00
using System.IO ;
using System.Linq ;
using FluentAssertions ;
using Xunit ;
using Microsoft.DotNet.ProjectModel.Graph ;
using Microsoft.DotNet.Tools.Test.Utilities ;
2016-03-30 11:41:16 -07:00
using NuGet.ProjectModel ;
2016-03-24 21:52:46 -07:00
namespace Microsoft.DotNet.ProjectModel.Tests
{
public class LockFilePatchingTests : TestBase
{
2016-03-30 11:41:16 -07:00
private static string ExportFilesRoot = > Path . Combine ( RepoRoot , "TestAssets" , "LockFiles" , "ExportFiles" ) ;
2016-03-24 21:52:46 -07:00
[Fact]
public void TestValidPatching ( )
{
var lockFilePath = GetLockFilePath ( "valid" ) ;
var lockFile = LockFileReader . Read ( lockFilePath ) ;
var exportFile = lockFile . ExportFile ;
exportFile . Should ( ) . NotBeNull ( ) ;
exportFile . Exports . Count . Should ( ) . Be ( 3 ) ;
exportFile . Exports . Should ( ) . OnlyHaveUniqueItems ( ) ;
// check export structure
2016-03-30 11:41:16 -07:00
foreach ( var export in exportFile . Exports )
2016-03-24 21:52:46 -07:00
{
export . TargetFramework . Should ( ) . NotBeNull ( ) ;
2016-03-30 11:41:16 -07:00
AssertTargetLibrary ( export ) ;
2016-03-24 21:52:46 -07:00
}
lockFile . Targets . Count . Should ( ) . Be ( 3 ) ;
// check lock file structure is similar to export structure
foreach ( var target in lockFile . Targets )
{
target . Libraries . Count . Should ( ) . Be ( 3 ) ;
2016-03-30 11:41:16 -07:00
foreach ( var library in target . Libraries )
2016-03-24 21:52:46 -07:00
{
2016-03-30 11:41:16 -07:00
AssertTargetLibrary ( library ) ;
2016-03-24 21:52:46 -07:00
}
}
}
2016-03-30 10:58:30 -07:00
[Fact]
public void TestFragmentExistsButNoHolesInLockFile ( )
{
var lockFilePath = GetLockFilePath ( "valid_staleFragment" ) ;
var lockFile = LockFileReader . Read ( lockFilePath ) ;
var exportFile = lockFile . ExportFile ;
exportFile . Should ( ) . BeNull ( ) ;
lockFile . Targets . Count . Should ( ) . Be ( 1 ) ;
lockFile . Targets [ 0 ] . Libraries . Count . Should ( ) . Be ( 0 ) ;
}
2016-03-24 21:52:46 -07:00
[Fact]
public void TestMissingExportFileThrows ( )
{
var lockFilePath = GetLockFilePath ( "invalid_nofragment" ) ;
Assert . Throws < FileFormatException > ( ( ) = > LockFileReader . Read ( lockFilePath ) ) ;
}
[Fact]
public void TestMissingExportsThrow ( )
{
var lockFilePath = GetLockFilePath ( "invalid_missing-exports" ) ;
Assert . Throws < FileFormatException > ( ( ) = > LockFileReader . Read ( lockFilePath ) ) ;
}
[Fact]
public void TestMissmatchingFileVersionsThrows ( )
{
var lockFilePath = GetLockFilePath ( "invalid_missmatching-versions" ) ;
Assert . Throws < FileFormatException > ( ( ) = > LockFileReader . Read ( lockFilePath ) ) ;
}
2016-03-30 11:41:16 -07:00
private static int LibraryNumberFromName ( Microsoft . DotNet . ProjectModel . Graph . LockFileTargetLibrary library )
{
var libraryName = library . Name ;
return ( int ) char . GetNumericValue ( libraryName [ libraryName . Length - 1 ] ) ;
}
private static void AssertTargetLibrary ( Microsoft . DotNet . ProjectModel . Graph . LockFileTargetLibrary library )
2016-03-24 21:52:46 -07:00
{
2016-03-30 11:41:16 -07:00
var libraryNumber = LibraryNumberFromName ( library ) ;
library . Type . Should ( ) . Be ( "project" ) ;
2016-03-24 21:52:46 -07:00
2016-03-30 11:41:16 -07:00
library . Name . Should ( ) . Be ( "ClassLibrary" + libraryNumber ) ;
library . Version . ToNormalizedString ( ) . Should ( ) . Be ( "1.0.0" ) ;
2016-03-24 21:52:46 -07:00
2016-03-30 11:41:16 -07:00
var dll = $"bin/Debug/ClassLibrary{libraryNumber}.dll" ;
2016-03-24 21:52:46 -07:00
dll = dll . Replace ( '/' , Path . DirectorySeparatorChar ) ;
2016-03-30 11:41:16 -07:00
library . CompileTimeAssemblies . Count . Should ( ) . Be ( 1 ) ;
library . CompileTimeAssemblies . ElementAt ( 0 ) . Path . Should ( ) . Be ( dll ) ;
2016-03-24 21:52:46 -07:00
2016-03-30 11:41:16 -07:00
library . RuntimeAssemblies . Count . Should ( ) . Be ( 1 ) ;
library . RuntimeAssemblies . ElementAt ( 0 ) . Path . Should ( ) . Be ( dll ) ;
2016-03-24 21:52:46 -07:00
}
2016-03-30 11:41:16 -07:00
private static string GetLockFilePath ( string exportSample )
2016-03-24 21:52:46 -07:00
{
return Path . Combine ( ExportFilesRoot , exportSample , "project.lock.json" ) ;
}
}
}