2016-08-23 13:50:05 -07:00
using System ;
using System.Collections.Generic ;
using System.IO ;
using System.Linq ;
using FluentAssertions ;
using Microsoft.Build.Construction ;
using Microsoft.DotNet.ProjectJsonMigration.Rules ;
2016-10-27 18:46:43 -07:00
using Microsoft.DotNet.Internal.ProjectModel ;
2016-08-23 13:50:05 -07:00
using Microsoft.DotNet.Tools.Common ;
using Microsoft.DotNet.Tools.Test.Utilities ;
using NuGet.Frameworks ;
using Xunit ;
namespace Microsoft.DotNet.ProjectJsonMigration.Tests
{
public class GivenAProjectMigrator : TestBase
{
[Fact]
2016-12-08 19:53:35 -08:00
public void ItCopiesProjectDirectoryContentsToOutputDirectoryWhenTheDirectoriesAreDifferent ( )
2016-08-23 13:50:05 -07:00
{
2016-10-27 18:46:43 -07:00
var testProjectDirectory = TestAssetsManager
. CreateTestInstance ( "PJTestAppSimple" , callingMethod : "z" )
2016-09-21 21:23:50 -07:00
. Path ;
2016-10-27 18:46:43 -07:00
2016-08-23 13:50:05 -07:00
var outputDirectory = Temp . CreateDirectory ( ) . Path ;
var projectDirectoryRelativeFilePaths = EnumerateFilesWithRelativePath ( testProjectDirectory ) ;
var mockProj = ProjectRootElement . Create ( ) ;
2016-12-07 11:49:15 -10:00
var testSettings = MigrationSettings . CreateMigrationSettingsTestHook ( testProjectDirectory , outputDirectory , mockProj ) ;
2016-08-23 13:50:05 -07:00
var projectMigrator = new ProjectMigrator ( new FakeEmptyMigrationRule ( ) ) ;
projectMigrator . Migrate ( testSettings ) ;
foreach ( var projectDirectoryRelativeFilePath in projectDirectoryRelativeFilePaths )
{
File . Exists ( Path . Combine ( outputDirectory , projectDirectoryRelativeFilePath ) ) . Should ( ) . BeTrue ( ) ;
}
}
[Fact]
2016-12-08 19:53:35 -08:00
public void ItHasErrorWhenMigratingANonCsharpApp ( )
2016-08-23 13:50:05 -07:00
{
var testProjectDirectory =
TestAssetsManager . CreateTestInstance ( "FSharpTestProjects/TestApp" , callingMethod : "z" )
2016-09-21 21:23:50 -07:00
. Path ;
2016-08-23 13:50:05 -07:00
var mockProj = ProjectRootElement . Create ( ) ;
2016-12-07 11:49:15 -10:00
var testSettings = MigrationSettings . CreateMigrationSettingsTestHook ( testProjectDirectory , testProjectDirectory , mockProj ) ;
2016-08-23 13:50:05 -07:00
var projectMigrator = new ProjectMigrator ( new FakeEmptyMigrationRule ( ) ) ;
2016-10-04 14:59:04 -07:00
var report = projectMigrator . Migrate ( testSettings ) ;
var projectReport = report . ProjectMigrationReports . First ( ) ;
2016-08-23 13:50:05 -07:00
2016-10-04 14:59:04 -07:00
var errorMessage = projectReport . Errors . First ( ) . GetFormattedErrorMessage ( ) ;
errorMessage . Should ( ) . Contain ( "MIGRATE20013::Non-Csharp App: Cannot migrate project" ) ;
2016-08-23 13:50:05 -07:00
}
2016-12-08 19:53:35 -08:00
[Fact]
public void ItHasErrorWhenMigratingAProjectJsonWithoutAFrameworks ( )
{
2016-12-08 20:06:55 -08:00
var testInstance = TestAssets . Get (
"NonRestoredTestProjects" ,
"TestLibraryWithProjectFileWithoutFrameworks" )
. CreateInstance ( )
2016-12-08 19:53:35 -08:00
. WithSourceFiles ( ) ;
var testProjectDirectory = testInstance . Root . FullName ;
var mockProj = ProjectRootElement . Create ( ) ;
var testSettings = MigrationSettings . CreateMigrationSettingsTestHook (
testProjectDirectory ,
testProjectDirectory ,
mockProj ) ;
var projectMigrator = new ProjectMigrator ( new FakeEmptyMigrationRule ( ) ) ;
var report = projectMigrator . Migrate ( testSettings ) ;
var projectReport = report . ProjectMigrationReports . First ( ) ;
2016-12-08 20:06:55 -08:00
projectReport . Errors . First ( ) . GetFormattedErrorMessage ( )
. Should ( ) . Contain ( "MIGRATE1013::No Project:" )
. And . Contain ( $"The project.json specifies no target frameworks in {testProjectDirectory}" ) ;
2016-12-08 19:53:35 -08:00
}
2016-08-23 13:50:05 -07:00
private IEnumerable < string > EnumerateFilesWithRelativePath ( string testProjectDirectory )
{
return
Directory . EnumerateFiles ( testProjectDirectory , "*" , SearchOption . AllDirectories )
. Select ( file = > PathUtility . GetRelativePath ( testProjectDirectory , file ) ) ;
}
private class FakeEmptyMigrationRule : IMigrationRule
{
public void Apply ( MigrationSettings migrationSettings , MigrationRuleInputs migrationRuleInputs )
{
return ;
}
}
}
}