2016-10-29 01:58:37 -07:00
using FluentAssertions ;
using Microsoft.DotNet.TestFramework ;
using Microsoft.DotNet.Tools.Common ;
using Microsoft.DotNet.Tools.Test.Utilities ;
using System ;
using System.Collections.Generic ;
using System.IO ;
using Xunit ;
namespace Microsoft.DotNet.Migration.Tests
{
public class GivenThatAnAppWasMigrated : TestBase
{
[Theory]
[InlineData("TestAppWithLibrary")]
2016-12-20 14:19:37 -08:00
public void WhenProjectMigrationSucceedsThenProjectJsonArtifactsGetMovedToBackup ( string testProjectName )
2016-10-29 01:58:37 -07:00
{
2017-01-22 14:40:00 -08:00
var testRoot = TestAssets
. GetProjectJson ( testProjectName )
. CreateInstance ( )
. WithSourceFiles ( )
. Root ;
2016-10-29 01:58:37 -07:00
2017-01-22 14:40:00 -08:00
var backupRoot = testRoot . GetDirectory ( "backup" ) ;
2016-10-29 01:58:37 -07:00
var migratableArtifacts = GetProjectJsonArtifacts ( testRoot ) ;
2016-12-20 14:19:37 -08:00
new MigrateCommand ( )
. WithWorkingDirectory ( testRoot )
. Execute ( )
. Should ( ) . Pass ( ) ;
var backupArtifacts = GetProjectJsonArtifacts ( backupRoot ) ;
backupArtifacts . Should ( ) . Equal ( migratableArtifacts , "Because all of and only these artifacts should have been moved" ) ;
2017-01-22 14:40:00 -08:00
testRoot . Should ( ) . NotHaveFiles ( backupArtifacts . Keys ) ;
2016-12-20 14:19:37 -08:00
2017-01-22 14:40:00 -08:00
backupRoot . Should ( ) . HaveTextFiles ( backupArtifacts ) ;
2016-12-20 14:19:37 -08:00
}
[Theory]
[InlineData("PJTestAppSimple")]
public void WhenFolderMigrationSucceedsThenProjectJsonArtifactsGetMovedToBackup ( string testProjectName )
{
2017-01-22 14:40:00 -08:00
var testRoot = TestAssets
. GetProjectJson ( testProjectName )
. CreateInstance ( )
. WithSourceFiles ( )
. Root ;
2016-12-20 14:19:37 -08:00
2017-01-22 14:40:00 -08:00
var backupRoot = testRoot . GetDirectory ( "backup" ) ;
2016-12-20 14:19:37 -08:00
var migratableArtifacts = GetProjectJsonArtifacts ( testRoot ) ;
2016-10-29 01:58:37 -07:00
new MigrateCommand ( )
. WithWorkingDirectory ( testRoot )
. Execute ( )
. Should ( ) . Pass ( ) ;
var backupArtifacts = GetProjectJsonArtifacts ( backupRoot ) ;
backupArtifacts . Should ( ) . Equal ( migratableArtifacts , "Because all of and only these artifacts should have been moved" ) ;
2017-01-22 14:40:00 -08:00
testRoot . Should ( ) . NotHaveFiles ( backupArtifacts . Keys ) ;
2016-10-29 01:58:37 -07:00
2017-01-22 14:40:00 -08:00
backupRoot . Should ( ) . HaveTextFiles ( backupArtifacts ) ;
2016-10-29 01:58:37 -07:00
}
[Theory]
[InlineData("TestAppWithLibraryAndMissingP2P")]
2016-12-20 14:19:37 -08:00
public void WhenMigrationFailsThenProjectJsonArtifactsDoNotGetMovedToBackup ( string testProjectName )
2016-10-29 01:58:37 -07:00
{
2017-01-22 14:40:00 -08:00
var testRoot = TestAssets
. GetProjectJson ( TestAssetKinds . NonRestoredTestProjects , testProjectName )
. CreateInstance ( identifier : testProjectName )
. WithSourceFiles ( )
. Root ;
2016-10-29 01:58:37 -07:00
2017-01-22 14:40:00 -08:00
var backupRoot = testRoot . GetDirectory ( "backup" ) ;
2016-10-29 01:58:37 -07:00
var migratableArtifacts = GetProjectJsonArtifacts ( testRoot ) ;
new MigrateCommand ( )
. WithWorkingDirectory ( testRoot )
. Execute ( )
. Should ( ) . Fail ( ) ;
2016-12-20 14:19:37 -08:00
2017-01-22 14:40:00 -08:00
backupRoot . Should ( ) . NotExist ( "Because migration failed and therefore no backup is needed." ) ;
2016-10-29 01:58:37 -07:00
2017-01-22 14:40:00 -08:00
testRoot . Should ( ) . HaveTextFiles ( migratableArtifacts , "Because migration failed so nothing was moved to backup." ) ;
2016-10-29 01:58:37 -07:00
}
[Theory]
[InlineData("PJTestAppSimple")]
2016-12-20 14:19:37 -08:00
public void WhenSkipbackupSpecifiedThenProjectJsonArtifactsDoNotGetMovedToBackup ( string testProjectName )
2016-10-29 01:58:37 -07:00
{
2017-01-22 14:40:00 -08:00
var testRoot = TestAssets
. GetProjectJson ( testProjectName )
. CreateInstance ( identifier : testProjectName )
. WithSourceFiles ( )
. Root ;
var backupRoot = testRoot . GetDirectory ( "backup" ) ;
2016-10-29 01:58:37 -07:00
var migratableArtifacts = GetProjectJsonArtifacts ( testRoot ) ;
new MigrateCommand ( )
. WithWorkingDirectory ( testRoot )
. Execute ( "--skip-backup" )
. Should ( ) . Pass ( ) ;
2016-12-20 14:19:37 -08:00
2017-01-22 14:40:00 -08:00
backupRoot . Should ( ) . NotExist ( "Because --skip-backup was specified." ) ;
2016-10-29 01:58:37 -07:00
2017-01-22 14:40:00 -08:00
testRoot . Should ( ) . HaveTextFiles ( migratableArtifacts , "Because --skip-backup was specified." ) ;
2016-10-29 01:58:37 -07:00
}
2017-01-22 14:40:00 -08:00
private Dictionary < string , string > GetProjectJsonArtifacts ( DirectoryInfo root )
2016-10-29 01:58:37 -07:00
{
var catalog = new Dictionary < string , string > ( ) ;
2016-12-20 14:19:37 -08:00
var patterns = new [ ] { "global.json" , "project.json" , "project.lock.json" , "*.xproj" , "*.xproj.user" } ;
2016-10-29 01:58:37 -07:00
foreach ( var pattern in patterns )
{
2017-01-22 14:40:00 -08:00
AddArtifactsToCatalog ( catalog , root , pattern ) ;
2016-10-29 01:58:37 -07:00
}
2016-12-20 14:19:37 -08:00
2016-10-29 01:58:37 -07:00
return catalog ;
}
2017-01-22 14:40:00 -08:00
private void AddArtifactsToCatalog ( Dictionary < string , string > catalog , DirectoryInfo root , string pattern )
2016-10-29 01:58:37 -07:00
{
2017-01-22 14:40:00 -08:00
var files = root . GetFiles ( pattern , SearchOption . AllDirectories ) ;
2016-10-29 01:58:37 -07:00
foreach ( var file in files )
{
2017-01-22 14:40:00 -08:00
var key = PathUtility . GetRelativePath ( root , file ) ;
2016-12-20 14:19:37 -08:00
catalog . Add ( key , File . ReadAllText ( file . FullName ) ) ;
2016-10-29 01:58:37 -07:00
}
}
}
}