2016-04-29 12:11:27 -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.
2015-12-09 09:57:45 -08:00
using System ;
2016-03-27 22:43:37 -07:00
using System.Collections.Generic ;
2015-12-09 09:57:45 -08:00
using System.IO ;
using System.Linq ;
2016-06-08 16:28:52 -07:00
using System.Runtime.InteropServices ;
2016-03-10 00:47:13 -08:00
using System.Threading ;
using System.Threading.Tasks ;
2016-05-02 11:32:24 -07:00
using Microsoft.DotNet.Cli.Utils ;
2016-03-10 00:47:13 -08:00
using Microsoft.DotNet.ProjectModel.Graph ;
2016-02-29 21:34:48 -08:00
using Microsoft.DotNet.TestFramework ;
2016-02-17 16:49:34 -08:00
using Microsoft.DotNet.Tools.Test.Utilities ;
2015-12-09 09:57:45 -08:00
using Newtonsoft.Json ;
using Newtonsoft.Json.Linq ;
using Xunit ;
namespace Microsoft.DotNet.ProjectModel.Server.Tests
{
2016-02-29 21:34:48 -08:00
public class DthTests : TestBase
2015-12-09 09:57:45 -08:00
{
2016-02-29 21:34:48 -08:00
private readonly TestAssetsManager _testAssetsManager ;
2016-03-10 00:47:13 -08:00
2016-02-29 21:34:48 -08:00
public DthTests ( )
2015-12-09 09:57:45 -08:00
{
2016-02-29 21:34:48 -08:00
_testAssetsManager = new TestAssetsManager (
Path . Combine ( RepoRoot , "TestAssets" , "ProjectModelServer" , "DthTestProjects" , "src" ) ) ;
2015-12-09 09:57:45 -08:00
}
[Fact]
public void DthStartup_GetProjectInformation ( )
{
2016-02-29 21:34:48 -08:00
var projectPath = Path . Combine ( _testAssetsManager . AssetsRoot , "EmptyConsoleApp" ) ;
2015-12-09 09:57:45 -08:00
Assert . NotNull ( projectPath ) ;
2016-04-28 10:01:30 -07:00
using ( var server = new DthTestServer ( ) )
using ( var client = new DthTestClient ( server ) )
2015-12-09 09:57:45 -08:00
{
client . Initialize ( projectPath ) ;
var projectInformation = client . DrainTillFirst ( MessageTypes . ProjectInformation )
. EnsureSource ( server , client )
. RetrievePayloadAs < JObject > ( )
. AssertProperty ( "Name" , "EmptyConsoleApp" ) ;
projectInformation . RetrievePropertyAs < JArray > ( "Configurations" )
. AssertJArrayCount ( 2 )
. AssertJArrayContains ( "Debug" )
. AssertJArrayContains ( "Release" ) ;
var frameworkShortNames = projectInformation . RetrievePropertyAs < JArray > ( "Frameworks" )
. AssertJArrayCount ( 2 )
. Select ( f = > f [ "ShortName" ] . Value < string > ( ) ) ;
2016-04-12 17:29:07 -07:00
Assert . Contains ( "netcoreapp1.0" , frameworkShortNames ) ;
2015-12-09 09:57:45 -08:00
Assert . Contains ( "dnx451" , frameworkShortNames ) ;
}
}
2016-04-12 14:32:45 -07:00
[Fact]
public void DependencyDiagnsoticsAfterDependencies ( )
{
var projectPath = Path . Combine ( _testAssetsManager . AssetsRoot , "EmptyConsoleApp" ) ;
Assert . NotNull ( projectPath ) ;
2016-04-28 10:01:30 -07:00
using ( var server = new DthTestServer ( ) )
using ( var client = new DthTestClient ( server ) )
2016-04-12 14:32:45 -07:00
{
client . Initialize ( projectPath ) ;
2016-05-23 14:57:33 -07:00
var messages = client . DrainMessage ( 12 )
2016-04-12 14:32:45 -07:00
. Select ( message = > message . MessageType )
. ToArray ( ) ;
var expectDependencies = true ;
var expectDependencyDiagnostics = false ;
for ( var i = 0 ; i < messages . Length ; + + i )
{
if ( messages [ i ] = = MessageTypes . Dependencies )
{
Assert . True ( expectDependencies ) ;
expectDependencies = false ;
expectDependencyDiagnostics = true ;
}
else if ( messages [ i ] = = MessageTypes . DependencyDiagnostics )
{
Assert . True ( expectDependencyDiagnostics ) ;
expectDependencyDiagnostics = false ;
break ;
}
}
Assert . False ( expectDependencies ) ;
Assert . False ( expectDependencyDiagnostics ) ;
}
}
2015-12-09 09:57:45 -08:00
[Theory]
[InlineData(4, 4)]
[InlineData(5, 4)]
[InlineData(3, 3)]
public void DthStartup_ProtocolNegotiation ( int requestVersion , int expectVersion )
{
2016-04-28 10:01:30 -07:00
using ( var server = new DthTestServer ( ) )
using ( var client = new DthTestClient ( server ) )
2015-12-09 09:57:45 -08:00
{
client . SetProtocolVersion ( requestVersion ) ;
var response = client . DrainTillFirst ( MessageTypes . ProtocolVersion , TimeSpan . FromDays ( 1 ) ) ;
response . EnsureSource ( server , client ) ;
Assert . Equal ( expectVersion , response . Payload [ "Version" ] ? . Value < int > ( ) ) ;
}
}
2016-01-11 22:26:34 -08:00
[Fact]
2015-12-09 09:57:45 -08:00
public void DthStartup_ProtocolNegotiation_ZeroIsNoAllowed ( )
{
2016-04-28 10:01:30 -07:00
using ( var server = new DthTestServer ( ) )
using ( var client = new DthTestClient ( server ) )
2015-12-09 09:57:45 -08:00
{
client . SetProtocolVersion ( 0 ) ;
Assert . Throws < TimeoutException > ( ( ) = >
{
client . DrainTillFirst ( MessageTypes . ProtocolVersion , timeout : TimeSpan . FromSeconds ( 1 ) ) ;
} ) ;
}
}
[Theory]
[InlineData("Project", "UnresolvedProjectSample", "EmptyLibrary", "Project")]
[InlineData("Package", "UnresolvedPackageSample", "NoSuchPackage", null)]
2016-01-29 01:49:56 -08:00
[InlineData("Package", "IncompatiblePackageSample", "Microsoft.Web.Administration", "Package")]
2015-12-09 09:57:45 -08:00
public void DthCompilation_Initialize_UnresolvedDependency ( string referenceType ,
string testProjectName ,
string expectedUnresolvedDependency ,
string expectedUnresolvedType )
{
2016-06-08 16:28:52 -07:00
if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) )
2016-02-17 16:49:34 -08:00
{
Console . WriteLine ( "Test is skipped on Linux" ) ;
return ;
}
2016-03-10 00:47:13 -08:00
2016-02-29 21:34:48 -08:00
var projectPath = Path . Combine ( _testAssetsManager . AssetsRoot , testProjectName ) ;
2015-12-09 09:57:45 -08:00
Assert . NotNull ( projectPath ) ;
2016-04-28 10:01:30 -07:00
using ( var server = new DthTestServer ( ) )
using ( var client = new DthTestClient ( server ) )
2015-12-09 09:57:45 -08:00
{
client . Initialize ( projectPath ) ;
2016-01-14 16:24:59 -08:00
var referencesMessage = client . DrainTillFirst ( MessageTypes . References , TimeSpan . FromDays ( 1 ) )
2015-12-09 09:57:45 -08:00
. EnsureSource ( server , client ) ;
if ( referenceType = = "Project" )
{
var expectedUnresolvedProjectPath = Path . Combine ( Path . GetDirectoryName ( projectPath ) ,
expectedUnresolvedDependency ,
Project . FileName ) ;
referencesMessage . RetrievePayloadAs < JObject > ( )
. RetrievePropertyAs < JArray > ( "ProjectReferences" )
. AssertJArrayCount ( 1 )
. RetrieveArraryElementAs < JObject > ( 0 )
. AssertProperty ( "Name" , expectedUnresolvedDependency )
2016-03-30 15:54:40 -07:00
. AssertProperty ( "Path" , expectedUnresolvedProjectPath ) ;
2015-12-09 09:57:45 -08:00
}
else if ( referenceType = = "Package" )
{
referencesMessage . RetrievePayloadAs < JObject > ( )
. RetrievePropertyAs < JArray > ( "ProjectReferences" )
. AssertJArrayCount ( 0 ) ;
}
2016-01-14 16:24:59 -08:00
var unresolveDependency = client . DrainTillFirst ( MessageTypes . Dependencies )
. EnsureSource ( server , client )
. RetrieveDependency ( expectedUnresolvedDependency ) ;
unresolveDependency . AssertProperty ( "Name" , expectedUnresolvedDependency )
. AssertProperty ( "DisplayName" , expectedUnresolvedDependency )
. AssertProperty ( "Resolved" , false )
. AssertProperty ( "Type" , expectedUnresolvedType ) ;
if ( expectedUnresolvedType = = "Project" )
{
unresolveDependency . AssertProperty ( "Path" , Path . Combine ( Path . GetDirectoryName ( projectPath ) ,
expectedUnresolvedDependency ,
Project . FileName ) ) ;
}
else
{
Assert . False ( unresolveDependency [ "Path" ] . HasValues ) ;
}
2015-12-09 09:57:45 -08:00
}
}
[Fact]
public void DthNegative_BrokenProjectPathInLockFile ( )
{
2016-04-28 10:01:30 -07:00
using ( var server = new DthTestServer ( ) )
using ( var client = new DthTestClient ( server ) )
2015-12-09 09:57:45 -08:00
{
// After restore the project is copied to another place so that
// the relative path in project lock file is invalid.
2016-02-29 21:34:48 -08:00
var movedProjectPath = _testAssetsManager . CreateTestInstance ( "BrokenProjectPathSample" )
. WithLockFiles ( )
. TestRoot ;
2015-12-09 09:57:45 -08:00
client . Initialize ( movedProjectPath ) ;
client . DrainTillFirst ( "Dependencies" )
. RetrieveDependency ( "EmptyLibrary" )
. AssertProperty < JArray > ( "Errors" , errorsArray = > errorsArray . Count = = 1 )
. AssertProperty < JArray > ( "Warnings" , warningsArray = > warningsArray . Count = = 0 )
. AssertProperty ( "Name" , "EmptyLibrary" )
. AssertProperty ( "Resolved" , false ) ;
2016-04-12 14:32:45 -07:00
client . DrainTillFirst ( "DependencyDiagnostics" )
. RetrieveDependencyDiagnosticsCollection ( )
. RetrieveDependencyDiagnosticsErrorAt ( 0 )
. AssertProperty < string > ( "FormattedMessage" , message = > message . Contains ( "error NU1002" ) )
. RetrievePropertyAs < JObject > ( "Source" )
. AssertProperty ( "Name" , "EmptyLibrary" ) ;
2015-12-09 09:57:45 -08:00
}
}
[Fact(Skip = "Require dotnet restore integration test")]
public void DthDependencies_UpdateGlobalJson_RefreshDependencies ( )
{
2016-02-29 21:34:48 -08:00
var assets = new TestAssetsManager ( Path . Combine ( AppContext . BaseDirectory , "TestAssets" , "ProjectModelServer" ) ) ;
var projectPath = assets . CreateTestInstance ( "DthUpdateSearchPathSample" ) . WithLockFiles ( ) . TestRoot ;
2015-12-09 09:57:45 -08:00
Assert . True ( Directory . Exists ( projectPath ) ) ;
2016-04-28 10:01:30 -07:00
using ( var server = new DthTestServer ( ) )
using ( var client = new DthTestClient ( server ) )
2015-12-09 09:57:45 -08:00
{
var testProject = Path . Combine ( projectPath , "home" , "src" , "MainProject" ) ;
client . Initialize ( testProject ) ;
client . DrainTillFirst ( "ProjectInformation" )
. RetrievePayloadAs < JObject > ( )
. RetrievePropertyAs < JArray > ( "ProjectSearchPaths" )
. AssertJArrayCount ( 2 ) ;
client . DrainTillFirst ( "Dependencies" )
. RetrieveDependency ( "Newtonsoft.Json" )
. AssertProperty ( "Type" , "Project" )
. AssertProperty ( "Resolved" , true )
. AssertProperty < JArray > ( "Errors" , array = > array . Count = = 0 , _ = > "Dependency shouldn't contain any error." ) ;
2016-04-12 14:32:45 -07:00
client . DrainTillFirst ( "DependencyDiagnostics" )
. RetrievePayloadAs < JObject > ( )
. AssertProperty < JArray > ( "Errors" , array = > array . Count = = 0 )
. AssertProperty < JArray > ( "Warnings" , array = > array . Count = = 0 ) ;
2015-12-09 09:57:45 -08:00
// Overwrite the global.json to remove search path to ext
File . WriteAllText (
Path . Combine ( projectPath , "home" , GlobalSettings . FileName ) ,
JsonConvert . SerializeObject ( new { project = new string [ ] { "src" } } ) ) ;
2016-05-02 11:32:24 -07:00
client . SendPayload ( testProject , "RefreshDependencies" ) ;
2015-12-09 09:57:45 -08:00
client . DrainTillFirst ( "ProjectInformation" )
. RetrievePayloadAs < JObject > ( )
. RetrievePropertyAs < JArray > ( "ProjectSearchPaths" )
. AssertJArrayCount ( 1 )
. AssertJArrayElement ( 0 , Path . Combine ( projectPath , "home" , "src" ) ) ;
client . DrainTillFirst ( "Dependencies" )
. RetrieveDependency ( "Newtonsoft.Json" )
. AssertProperty ( "Type" , "" )
. AssertProperty ( "Resolved" , false )
. RetrievePropertyAs < JArray > ( "Errors" )
. AssertJArrayCount ( 1 )
. RetrieveArraryElementAs < JObject > ( 0 )
. AssertProperty ( "ErrorCode" , "NU1010" ) ;
2016-04-12 14:32:45 -07:00
client . DrainTillFirst ( "DependencyDiagnostics" )
. RetrieveDependencyDiagnosticsCollection ( )
. RetrieveDependencyDiagnosticsErrorAt < JObject > ( 0 )
. AssertProperty ( "ErrorCode" , "NU1010" ) ;
2015-12-09 09:57:45 -08:00
}
}
2016-01-14 16:04:27 -08:00
[Fact]
public void DthStartup_OpenProjectBeforeRestore ( )
{
2016-02-29 21:34:48 -08:00
var projectPath = _testAssetsManager . CreateTestInstance ( "EmptyConsoleApp" ) . TestRoot ;
2016-01-14 16:04:27 -08:00
2016-04-28 10:01:30 -07:00
using ( var server = new DthTestServer ( ) )
using ( var client = new DthTestClient ( server ) )
2016-01-14 16:04:27 -08:00
{
client . Initialize ( projectPath ) ;
2016-05-23 14:57:33 -07:00
var messages = client . DrainMessage ( 12 ) ;
2016-01-14 16:04:27 -08:00
Assert . False ( messages . Any ( msg = > msg . MessageType = = MessageTypes . Error ) ) ;
var dependencyDiagnostics = messages . Where ( msg = > msg . MessageType = = MessageTypes . DependencyDiagnostics ) ;
Assert . Equal ( 2 , dependencyDiagnostics . Count ( ) ) ;
foreach ( var message in dependencyDiagnostics )
{
message . RetrievePayloadAs < JObject > ( )
. RetrievePropertyAs < JArray > ( "Errors" )
. AssertJArrayContains < JObject > ( error = > error [ "ErrorCode" ] . Value < string > ( ) = = ErrorCodes . NU1009 ) ;
}
}
}
2016-02-29 10:46:13 -08:00
[Fact]
public void InvalidProjectJson ( )
{
2016-02-29 22:09:38 -08:00
var testAssetsPath = Path . Combine ( RepoRoot , "TestAssets" , "ProjectModelServer" ) ;
var assetsManager = new TestAssetsManager ( testAssetsPath ) ;
var testSource = assetsManager . CreateTestInstance ( "IncorrectProjectJson" ) . TestRoot ;
2016-03-10 00:47:13 -08:00
2016-04-28 10:01:30 -07:00
using ( var server = new DthTestServer ( ) )
using ( var client = new DthTestClient ( server ) )
2016-02-29 10:46:13 -08:00
{
client . Initialize ( Path . Combine ( _testAssetsManager . AssetsRoot , "EmptyLibrary" ) ) ;
2016-02-29 22:09:38 -08:00
client . Initialize ( testSource ) ;
2016-02-29 10:46:13 -08:00
// Error for invalid project.json
2016-05-23 14:57:33 -07:00
var messages = client . DrainMessage ( 8 ) ;
2016-02-29 10:46:13 -08:00
messages . Single ( msg = > msg . MessageType = = MessageTypes . Error )
. Payload . AsJObject ( )
2016-02-29 22:09:38 -08:00
. AssertProperty < string > ( "Path" , v = > v . Contains ( "IncorrectProjectJson" ) ) ;
2016-02-29 10:46:13 -08:00
// Successfully initialize the other project
messages . Single ( msg = > msg . MessageType = = MessageTypes . ProjectInformation )
. Payload . AsJObject ( )
. AssertProperty < string > ( "Name" , v = > string . Equals ( v , "EmptyLibrary" , StringComparison . Ordinal ) ) ;
// Successfully initialize another project afterwards
client . Initialize ( Path . Combine ( _testAssetsManager . AssetsRoot , "EmptyConsoleApp" ) ) ;
2016-05-23 14:57:33 -07:00
client . DrainTillFirst ( MessageTypes . ProjectInformation )
. Payload . AsJObject ( )
. AssertProperty < string > ( "Name" , v = > string . Equals ( v , "EmptyConsoleApp" , StringComparison . Ordinal ) ) ;
2016-02-29 10:46:13 -08:00
}
}
[Fact]
public void InvalidGlobalJson ( )
{
var testAssetsPath = Path . Combine ( RepoRoot , "TestAssets" , "ProjectModelServer" ) ;
var assetsManager = new TestAssetsManager ( testAssetsPath ) ;
var testSource = assetsManager . CreateTestInstance ( "IncorrectGlobalJson" ) ;
2016-04-28 10:01:30 -07:00
using ( var server = new DthTestServer ( ) )
using ( var client = new DthTestClient ( server ) )
2016-02-29 10:46:13 -08:00
{
client . Initialize ( Path . Combine ( testSource . TestRoot , "src" , "Project1" ) ) ;
2016-05-23 14:57:33 -07:00
client . DrainTillFirst ( MessageTypes . Error )
. Payload . AsJObject ( )
. AssertProperty < string > ( "Path" , v = > v . Contains ( "InvalidGlobalJson" ) ) ;
2016-02-29 10:46:13 -08:00
}
}
2016-03-10 00:47:13 -08:00
2016-03-03 13:12:33 -08:00
[Fact]
public void RecoverFromGlobalError ( )
{
var testProject = _testAssetsManager . CreateTestInstance ( "EmptyConsoleApp" )
. WithLockFiles ( )
. TestRoot ;
2016-03-10 00:47:13 -08:00
2016-04-28 10:01:30 -07:00
using ( var server = new DthTestServer ( ) )
using ( var client = new DthTestClient ( server ) )
2016-03-03 13:12:33 -08:00
{
var projectFile = Path . Combine ( testProject , Project . FileName ) ;
var content = File . ReadAllText ( projectFile ) ;
File . WriteAllText ( projectFile , content + "}" ) ;
2016-03-10 00:47:13 -08:00
2016-03-03 13:12:33 -08:00
client . Initialize ( testProject ) ;
2016-05-23 14:57:33 -07:00
client . DrainTillFirst ( MessageTypes . Error ) ;
2016-03-10 00:47:13 -08:00
2016-03-03 13:12:33 -08:00
File . WriteAllText ( projectFile , content ) ;
2016-05-02 11:32:24 -07:00
client . SendPayload ( testProject , MessageTypes . FilesChanged ) ;
2016-05-23 14:57:33 -07:00
client . DrainTillFirst ( MessageTypes . Error )
. Payload . AsJObject ( )
. AssertProperty ( "Message" , null as string ) ;
2016-03-03 13:12:33 -08:00
}
}
2016-03-10 00:47:13 -08:00
[Theory]
[InlineData(500, true)]
[InlineData(3000, false)]
public void WaitForLockFileReleased ( int occupyFileFor , bool expectSuccess )
{
var testProject = _testAssetsManager . CreateTestInstance ( "EmptyConsoleApp" )
. WithLockFiles ( )
. TestRoot ;
2016-04-28 10:01:30 -07:00
using ( var server = new DthTestServer ( ) )
using ( var client = new DthTestClient ( server ) )
2016-03-10 00:47:13 -08:00
{
var lockFilePath = Path . Combine ( testProject , LockFile . FileName ) ;
var lockFileContent = File . ReadAllText ( lockFilePath ) ;
var fs = new FileStream ( lockFilePath , FileMode . Create , FileAccess . Write , FileShare . None ) ;
// Test the platform
// A sharing violation is expected in following code. Otherwise the FileSteam is not implemented correctly.
Assert . ThrowsAny < IOException > ( ( ) = >
{
new FileStream ( lockFilePath , FileMode . Open , FileAccess . Read , FileShare . Read ) ;
} ) ;
var task = Task . Run ( ( ) = >
{
// WorkspaceContext will try to open the lock file for 3 times with 500 ms interval in between.
Thread . Sleep ( occupyFileFor ) ;
fs . Dispose ( ) ;
} ) ;
client . Initialize ( testProject ) ;
if ( expectSuccess )
{
2016-05-23 14:57:33 -07:00
client . DrainMessage ( 12 ) . AssertDoesNotContain ( MessageTypes . Error ) ;
2016-03-10 00:47:13 -08:00
}
else
{
2016-05-23 14:57:33 -07:00
client . DrainTillFirst ( MessageTypes . Error ) ;
2016-03-10 00:47:13 -08:00
}
}
}
2016-04-03 19:29:23 -07:00
2016-04-03 20:52:36 -07:00
[Fact]
public void AddMSBuildReferenceBeforeRestore ( )
{
var tam = new TestAssetsManager (
Path . Combine ( RepoRoot , "TestAssets" , "ProjectModelServer" , "MSBuildReferencesProjects" ) ) ;
// var appName = "EmptyNetCoreApp";
var projectPath = tam . CreateTestInstance ( "ValidCase01" ) . WithLockFiles ( ) . TestRoot ;
projectPath = Path . Combine ( projectPath , "src" , "MainApp" ) ;
var projectFilePath = Path . Combine ( projectPath , Project . FileName ) ;
var projectJson = JsonConvert . DeserializeObject < JObject > ( File . ReadAllText ( projectFilePath ) ) ;
( ( JObject ) projectJson [ "frameworks" ] [ "net46" ] [ "dependencies" ] )
. Add ( "ClassLibrary4" , JToken . FromObject ( new { target = "project" } ) ) ;
File . WriteAllText ( projectFilePath , JsonConvert . SerializeObject ( projectJson ) ) ;
2016-04-28 10:01:30 -07:00
using ( var server = new DthTestServer ( ) )
using ( var client = new DthTestClient ( server ) )
2016-04-03 20:52:36 -07:00
{
client . Initialize ( projectPath ) ;
2016-05-23 14:57:33 -07:00
var messages = client . DrainMessage ( 7 ) ;
2016-04-03 20:52:36 -07:00
messages . AssertDoesNotContain ( MessageTypes . Error ) ;
messages . RetrieveSingleMessage ( MessageTypes . Dependencies )
. RetrieveDependency ( "ClassLibrary4" )
. AssertProperty < object > (
"Version" ,
v = > ! string . IsNullOrEmpty ( v . ToString ( ) ) ,
2016-04-03 22:03:06 -07:00
v = > $"Version string shouldn't be empty. Value [{v.ToString()}]" ) ;
2016-04-03 20:52:36 -07:00
}
}
2016-03-27 22:43:37 -07:00
[Fact]
public void MSBuildReferenceTest ( )
{
var testProject = Path . Combine ( RepoRoot , "TestAssets" ,
"ProjectModelServer" ,
2016-04-03 19:29:23 -07:00
"MSBuildReferencesProjects" ,
2016-03-30 11:10:06 -07:00
"ValidCase01" ,
2016-03-27 22:43:37 -07:00
"src" ,
2016-03-30 11:10:06 -07:00
"MainApp" ) ;
2016-03-27 22:43:37 -07:00
2016-04-28 10:01:30 -07:00
using ( var server = new DthTestServer ( ) )
using ( var client = new DthTestClient ( server ) )
2016-03-27 22:43:37 -07:00
{
client . Initialize ( testProject ) ;
2016-05-23 14:57:33 -07:00
var messages = client . DrainMessage ( 7 ) ;
2016-04-03 19:29:23 -07:00
2016-03-27 22:43:37 -07:00
var classLibraries = new HashSet < string > ( new string [ ] { "ClassLibrary1" , "ClassLibrary2" , "ClassLibrary3" } ) ;
var dependencies = messages . RetrieveSingleMessage ( MessageTypes . Dependencies ) ;
2016-03-30 15:54:40 -07:00
var testProjectRoot = Path . Combine ( RepoRoot , "TestAssets" , "ProjectModelServer" , "MSBuildReferencesProjects" , "ValidCase01" ) ;
2016-03-30 11:32:38 -07:00
foreach ( var classLibrary in classLibraries )
2016-04-03 19:29:23 -07:00
{
2016-04-22 15:01:56 -07:00
var dependency = dependencies . RetrieveDependency ( classLibrary ) ;
dependency . AssertProperty ( "Type" , LibraryType . MSBuildProject . ToString ( ) ) ;
dependency . AssertProperty ( "Path" , NormalizePathString ( Path . Combine ( testProjectRoot , classLibrary , $"{classLibrary}.csproj" ) ) ) ;
dependency . AssertProperty < bool > ( "Resolved" , true ) ;
dependency . AssertProperty ( "Name" , classLibrary ) ;
dependency . AssertProperty < JArray > ( "Errors" , array = > array . Count = = 0 ) ;
dependency . AssertProperty < JArray > ( "Warnings" , array = > array . Count = = 0 ) ;
2016-03-27 22:43:37 -07:00
}
2016-04-03 19:29:23 -07:00
2016-03-27 22:43:37 -07:00
var references = messages . RetrieveSingleMessage ( MessageTypes . References )
. RetrievePayloadAs < JObject > ( ) ;
2016-04-03 19:29:23 -07:00
2016-03-27 22:43:37 -07:00
var projectReferences = references . RetrievePropertyAs < JArray > ( "ProjectReferences" ) ;
Assert . Equal ( 3 , projectReferences . Count ) ;
for ( int i = 0 ; i < 3 ; + + i )
{
var projectRef = projectReferences . RetrieveArraryElementAs < JObject > ( i ) ;
var name = projectRef [ "Name" ] . Value < string > ( ) ;
2016-04-03 19:29:23 -07:00
2016-03-27 22:43:37 -07:00
Assert . True ( classLibraries . Contains ( name ) ) ;
2016-03-30 15:54:40 -07:00
projectRef . AssertProperty ( "Path" , NormalizePathString ( Path . Combine ( testProjectRoot , name , $"{name}.csproj" ) ) ) ;
2016-03-27 22:43:37 -07:00
}
2016-04-03 19:29:23 -07:00
2016-03-27 22:43:37 -07:00
var fileReferences = references . RetrievePropertyAs < JArray > ( "FileReferences" )
. Select ( each = > each . Value < string > ( ) )
. ToArray ( ) ;
foreach ( var each in classLibraries )
{
2016-03-30 11:10:06 -07:00
fileReferences . Contains ( Path . Combine ( "ValidCase01" , "ClassLibrary1" , "bin" , "Debug" , $"{each}.dll" ) ) ;
2016-03-27 22:43:37 -07:00
}
}
}
2016-03-29 11:32:33 -07:00
2016-04-03 19:29:23 -07:00
[Fact]
public void RemovePackageDependencyFromProjectJson ( )
{
// Remove a package dependency from project.json and then request refreshing dependency before
// restore.
var appName = "EmptyNetCoreApp" ;
var projectPath = _testAssetsManager . CreateTestInstance ( appName )
. WithLockFiles ( )
. TestRoot ;
2016-04-28 10:01:30 -07:00
using ( var server = new DthTestServer ( ) )
using ( var client = new DthTestClient ( server ) )
2016-04-03 19:29:23 -07:00
{
client . Initialize ( projectPath ) ;
2016-05-23 14:57:33 -07:00
client . DrainMessage ( 7 )
2016-04-03 19:29:23 -07:00
. AssertDoesNotContain ( MessageTypes . Error )
. RetrieveSingleMessage ( MessageTypes . Dependencies )
. RetrieveDependency ( appName )
. RetrievePropertyAs < JArray > ( "Dependencies" )
. AssertJArrayCount ( 2 ) ;
var projectFilePath = Path . Combine ( projectPath , Project . FileName ) ;
var projectJson = JsonConvert . DeserializeObject < JObject > ( File . ReadAllText ( projectFilePath ) ) ;
// Remove newtonsoft.json dependency
var dependencies = projectJson [ "frameworks" ] [ "netcoreapp1.0" ] [ "dependencies" ] as JObject ;
dependencies . Remove ( "Newtonsoft.Json" ) ;
File . WriteAllText ( projectFilePath , JsonConvert . SerializeObject ( projectJson ) ) ;
2016-05-02 11:32:24 -07:00
client . SendPayload ( projectPath , MessageTypes . RefreshDependencies ) ;
2016-04-03 19:29:23 -07:00
var afterDependencies = client . DrainTillFirst ( MessageTypes . Dependencies ) ;
afterDependencies . RetrieveDependency ( appName )
. RetrievePropertyAs < JArray > ( "Dependencies" )
. AssertJArrayCount ( 1 )
. RetrieveArraryElementAs < JObject > ( 0 )
. AssertProperty ( "Name" , "Microsoft.NETCore.App" ) ;
afterDependencies . RetrieveDependency ( "Newtonsoft.Json" ) ;
}
}
[Fact]
public void RemoveMSBuildDependencyFromProjectJson ( )
{
// Remove a msbuild project dependency from project.json and then request refreshing dependency before
// restore.
var tam = new TestAssetsManager (
Path . Combine ( RepoRoot , "TestAssets" , "ProjectModelServer" , "MSBuildReferencesProjects" ) ) ;
// var appName = "EmptyNetCoreApp";
var projectPath = tam . CreateTestInstance ( "ValidCase01" ) . WithLockFiles ( ) . TestRoot ;
projectPath = Path . Combine ( projectPath , "src" , "MainApp" ) ;
2016-04-28 10:01:30 -07:00
using ( var server = new DthTestServer ( ) )
using ( var client = new DthTestClient ( server ) )
2016-04-03 19:29:23 -07:00
{
client . Initialize ( projectPath ) ;
2016-05-23 14:57:33 -07:00
client . DrainMessage ( 7 )
2016-04-03 19:29:23 -07:00
. AssertDoesNotContain ( MessageTypes . Error )
. RetrieveSingleMessage ( MessageTypes . Dependencies )
. RetrieveDependency ( "MainApp" )
. RetrievePropertyAs < JArray > ( "Dependencies" )
. AssertJArrayContains < JObject > ( dep = > dep [ "Name" ] . Value < string > ( ) = = "ClassLibrary1" )
. AssertJArrayContains < JObject > ( dep = > dep [ "Name" ] . Value < string > ( ) = = "ClassLibrary2" )
. AssertJArrayContains < JObject > ( dep = > dep [ "Name" ] . Value < string > ( ) = = "ClassLibrary3" ) ;
var projectFilePath = Path . Combine ( projectPath , Project . FileName ) ;
var projectJson = JsonConvert . DeserializeObject < JObject > ( File . ReadAllText ( projectFilePath ) ) ;
// Remove ClassLibrary2 and ClassLibrary3 dependency
var dependencies = projectJson [ "frameworks" ] [ "net46" ] [ "dependencies" ] as JObject ;
dependencies . Remove ( "ClassLibrary2" ) ;
dependencies . Remove ( "ClassLibrary3" ) ;
File . WriteAllText ( projectFilePath , JsonConvert . SerializeObject ( projectJson ) ) ;
2016-05-02 11:32:24 -07:00
client . SendPayload ( projectPath , MessageTypes . RefreshDependencies ) ;
2016-04-03 19:29:23 -07:00
var afterDependencies = client . DrainTillFirst ( MessageTypes . Dependencies ) ;
afterDependencies . RetrieveDependency ( "MainApp" )
. RetrievePropertyAs < JArray > ( "Dependencies" )
. AssertJArrayNotContains < JObject > ( dep = > dep [ "Name" ] . Value < string > ( ) = = "ClassLibrary2" )
. AssertJArrayNotContains < JObject > ( dep = > dep [ "Name" ] . Value < string > ( ) = = "ClassLibrary3" ) ;
afterDependencies . RetrieveDependency ( "ClassLibrary2" ) ;
afterDependencies . RetrieveDependency ( "ClassLibrary3" ) ;
}
}
2016-04-22 15:01:56 -07:00
2016-04-07 20:28:55 -07:00
[Fact]
public void TestMscorlibLibraryDuplication ( )
{
var projectPath = Path . Combine ( RepoRoot , "TestAssets" , "ProjectModelServer" , "MscorlibLibraryDuplication" ) ;
2016-04-22 15:01:56 -07:00
2016-04-28 10:01:30 -07:00
using ( var server = new DthTestServer ( ) )
using ( var client = new DthTestClient ( server ) )
2016-04-07 20:28:55 -07:00
{
client . Initialize ( projectPath ) ;
2016-05-23 14:57:33 -07:00
client . DrainMessage ( 7 ) . AssertDoesNotContain ( MessageTypes . Error ) ;
2016-04-07 20:28:55 -07:00
}
}
2016-04-03 19:29:23 -07:00
2016-05-02 11:32:24 -07:00
[Fact]
public void TestTargetFrameworkChange ( )
{
using ( var server = new DthTestServer ( ) )
using ( var client = new DthTestClient ( server ) )
{
var testProject = _testAssetsManager . CreateTestInstance ( "EmptyLibrary" )
. WithLockFiles ( )
. TestRoot ;
// initialize the project and drain all messages (7 message for project with one framework)
client . Initialize ( testProject ) ;
2016-05-23 14:57:33 -07:00
client . DrainMessage ( 7 ) ;
2016-05-02 11:32:24 -07:00
// update the target framework from netstandard1.3 to netstandard 1.5 so as to invalidate all
// dependencies
var projectJsonPath = Path . Combine ( testProject , "project.json" ) ;
File . WriteAllText ( projectJsonPath ,
File . ReadAllText ( projectJsonPath ) . Replace ( "netstandard1.3" , "netstandard1.5" ) ) ;
// send files change request to server to prompt update
client . SendPayload ( testProject , MessageTypes . FilesChanged ) ;
// assert project information is updated
client . DrainTillFirst ( MessageTypes . ProjectInformation )
. RetrievePayloadAs < JObject > ( )
. RetrievePropertyAs < JArray > ( "Frameworks" )
. AssertJArrayCount ( 1 )
. RetrieveArraryElementAs < JObject > ( 0 )
. AssertProperty ( "ShortName" , "netstandard1.5" ) ;
// the NETStandard.Library dependency should turn unresolved
var dependencies = client . DrainTillFirst ( MessageTypes . Dependencies ) ;
dependencies . RetrievePayloadAs < JObject > ( )
. RetrievePropertyAs < JObject > ( "Framework" )
. AssertProperty ( "ShortName" , "netstandard1.5" ) ;
dependencies . RetrieveDependency ( "NETStandard.Library" )
. RetrievePropertyAs < JArray > ( "Errors" )
. AssertJArrayCount ( 1 )
. RetrieveArraryElementAs < JObject > ( 0 )
. AssertProperty ( "ErrorCode" , "NU1001" ) ;
// warning for project.json and project.lock.json out of sync
var diagnostics = client . DrainTillFirst ( MessageTypes . DependencyDiagnostics ) ;
diagnostics . RetrievePayloadAs < JObject > ( )
. RetrievePropertyAs < JObject > ( "Framework" )
. AssertProperty ( "ShortName" , "netstandard1.5" ) ;
diagnostics . RetrievePayloadAs < JObject > ( )
. RetrievePropertyAs < JArray > ( "Warnings" )
. AssertJArrayCount ( 1 )
. RetrieveArraryElementAs < JObject > ( 0 )
. AssertProperty ( "ErrorCode" , "NU1006" ) ;
// restore again
var restoreCommand = new RestoreCommand ( ) ;
restoreCommand . WorkingDirectory = testProject ;
restoreCommand . Execute ( ) . Should ( ) . Pass ( ) ;
client . SendPayload ( testProject , MessageTypes . RefreshDependencies ) ;
client . DrainTillFirst ( MessageTypes . Dependencies )
. RetrieveDependency ( "NETStandard.Library" )
. RetrievePropertyAs < JArray > ( "Errors" )
. AssertJArrayCount ( 0 ) ;
client . DrainTillFirst ( MessageTypes . DependencyDiagnostics )
. RetrievePayloadAs < JObject > ( )
. RetrievePropertyAs < JArray > ( "Warnings" )
. AssertJArrayCount ( 0 ) ;
}
}
2016-03-30 15:54:40 -07:00
private static string NormalizePathString ( string original )
2016-03-29 11:32:33 -07:00
{
2016-03-30 15:54:40 -07:00
return original . Replace ( '/' , Path . DirectorySeparatorChar ) . Replace ( '\\' , Path . DirectorySeparatorChar ) ;
2016-03-29 11:32:33 -07:00
}
2016-04-03 19:29:23 -07:00
private static void PrintAllMessages ( IEnumerable < DthMessage > messages )
{
foreach ( var message in messages )
{
Console . WriteLine ( $"{message.MessageType} => {message.Payload.ToString()}" ) ;
}
}
2015-12-09 09:57:45 -08:00
}
}