2016-08-22 12:24:10 -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.
using System.Linq ;
using FluentAssertions ;
using Microsoft.DotNet.Tools.Test.Utilities ;
using Xunit ;
using Microsoft.DotNet.ProjectJsonMigration ;
using System ;
using System.IO ;
using Microsoft.Build.Construction ;
2016-08-23 13:50:05 -07:00
using Microsoft.DotNet.ProjectJsonMigration.Rules ;
2016-08-22 12:24:10 -07:00
namespace Microsoft.DotNet.ProjectJsonMigration.Tests
{
public class GivenThatIWantToMigrateScripts : TestBase
{
2017-01-03 17:15:39 -08:00
private const bool IsMultiTFM = true ;
2016-08-22 12:24:10 -07:00
[Theory]
2016-09-15 15:54:10 -07:00
[InlineData("compile:TargetFramework", "$(TargetFramework)")]
[InlineData("publish:TargetFramework", "$(TargetFramework)")]
2016-08-23 13:50:05 -07:00
[InlineData("compile:FullTargetFramework", "$(TargetFrameworkIdentifier),Version=$(TargetFrameworkVersion)")]
2016-08-22 12:24:10 -07:00
[InlineData("compile:Configuration", "$(Configuration)")]
[InlineData("compile:OutputFile", "$(TargetPath)")]
[InlineData("compile:OutputDir", "$(TargetDir)")]
[InlineData("publish:ProjectPath", "$(MSBuildThisFileDirectory)")]
[InlineData("publish:Configuration", "$(Configuration)")]
[InlineData("publish:OutputPath", "$(TargetDir)")]
2016-08-23 13:50:05 -07:00
[InlineData("publish:FullTargetFramework", "$(TargetFrameworkIdentifier),Version=$(TargetFrameworkVersion)")]
2016-08-22 12:24:10 -07:00
[InlineData("project:Version", "$(Version)")]
2016-08-23 13:50:05 -07:00
[InlineData("project:Name", "$(AssemblyName)")]
2016-08-22 12:24:10 -07:00
[InlineData("project:Directory", "$(MSBuildProjectDirectory)")]
2016-08-23 13:50:05 -07:00
[InlineData("publish:Runtime", "$(RuntimeIdentifier)")]
2016-12-02 10:41:25 -08:00
public void FormattingScriptCommandsReplacesVariablesWithTheRightMSBuildProperties (
2016-08-22 12:24:10 -07:00
string variable ,
string msbuildReplacement )
{
var scriptMigrationRule = new MigrateScriptsRule ( ) ;
scriptMigrationRule . ReplaceScriptVariables ( $"%{variable}%" ) . Should ( ) . Be ( msbuildReplacement ) ;
}
[Theory]
[InlineData("compile:ResponseFile")]
[InlineData("compile:CompilerExitCode")]
[InlineData("compile:RuntimeOutputDir")]
[InlineData("compile:RuntimeIdentifier")]
2016-12-02 10:41:25 -08:00
public void FormattingScriptCommandsThrowsWhenVariableIsUnsupported ( string unsupportedVariable )
2016-08-22 12:24:10 -07:00
{
var scriptMigrationRule = new MigrateScriptsRule ( ) ;
Action formatScriptAction = ( ) = > scriptMigrationRule . ReplaceScriptVariables ( $"%{unsupportedVariable}%" ) ;
formatScriptAction . ShouldThrow < Exception > ( )
. Where ( exc = > exc . Message . Contains ( "is currently an unsupported script variable for project migration" ) ) ;
}
[Theory]
2016-11-10 18:24:35 -08:00
[InlineData("precompile", "BeforeBuild")]
[InlineData("prepublish", "PrepareForPublish")]
2016-12-02 10:41:25 -08:00
public void MigratingPreScriptsPopulatesBeforeTargetsWithAppropriateTarget (
string scriptName ,
string targetName )
2016-08-22 12:24:10 -07:00
{
var scriptMigrationRule = new MigrateScriptsRule ( ) ;
ProjectRootElement mockProj = ProjectRootElement . Create ( ) ;
2017-01-11 16:51:45 -08:00
ProjectPropertyGroupElement commonPropertyGroup = mockProj . AddPropertyGroup ( ) ;
2016-08-22 12:24:10 -07:00
var commands = new string [ ] { "fakecommand" } ;
2016-12-02 10:41:25 -08:00
var target = scriptMigrationRule . MigrateScriptSet (
mockProj ,
2017-01-11 16:51:45 -08:00
commonPropertyGroup ,
2016-12-02 10:41:25 -08:00
commands ,
2017-01-03 17:15:39 -08:00
scriptName ,
IsMultiTFM ) ;
2016-08-22 12:24:10 -07:00
target . BeforeTargets . Should ( ) . Be ( targetName ) ;
}
[Theory]
[InlineData("postcompile", "Build")]
[InlineData("postpublish", "Publish")]
2016-12-02 10:41:25 -08:00
public void MigratingPostScriptsPopulatesAfterTargetsWithAppropriateTarget (
string scriptName ,
string targetName )
2016-08-22 12:24:10 -07:00
{
var scriptMigrationRule = new MigrateScriptsRule ( ) ;
ProjectRootElement mockProj = ProjectRootElement . Create ( ) ;
2017-01-11 16:51:45 -08:00
ProjectPropertyGroupElement commonPropertyGroup = mockProj . AddPropertyGroup ( ) ;
2016-08-30 15:39:27 -07:00
var commands = new [ ] { "fakecommand" } ;
2016-08-22 12:24:10 -07:00
2016-12-02 10:41:25 -08:00
var target = scriptMigrationRule . MigrateScriptSet (
mockProj ,
2017-01-11 16:51:45 -08:00
commonPropertyGroup ,
2016-12-02 10:41:25 -08:00
commands ,
2017-01-03 17:15:39 -08:00
scriptName ,
IsMultiTFM ) ;
2016-08-22 12:24:10 -07:00
target . AfterTargets . Should ( ) . Be ( targetName ) ;
}
[Theory]
[InlineData("precompile")]
[InlineData("postcompile")]
[InlineData("prepublish")]
[InlineData("postpublish")]
2016-12-02 10:41:25 -08:00
public void MigratingScriptsWithMultipleCommandsCreatesExecTaskForEach ( string scriptName )
2016-08-22 12:24:10 -07:00
{
var scriptMigrationRule = new MigrateScriptsRule ( ) ;
ProjectRootElement mockProj = ProjectRootElement . Create ( ) ;
2017-01-11 16:51:45 -08:00
ProjectPropertyGroupElement commonPropertyGroup = mockProj . AddPropertyGroup ( ) ;
2016-08-22 12:24:10 -07:00
2016-08-30 15:39:27 -07:00
var commands = new [ ] { "fakecommand1" , "fakecommand2" , "mockcommand3" } ;
2016-08-22 12:24:10 -07:00
var commandsInTask = commands . ToDictionary ( c = > c , c = > false ) ;
2016-12-02 10:41:25 -08:00
var target = scriptMigrationRule . MigrateScriptSet (
mockProj ,
2017-01-11 16:51:45 -08:00
commonPropertyGroup ,
2016-12-02 10:41:25 -08:00
commands ,
2017-01-03 17:15:39 -08:00
scriptName ,
IsMultiTFM ) ;
2016-08-22 12:24:10 -07:00
foreach ( var task in target . Tasks )
{
var taskCommand = task . GetParameter ( "Command" ) ;
var originalCommandCandidates = commands . Where ( c = > taskCommand . Contains ( c ) ) ;
originalCommandCandidates . Count ( ) . Should ( ) . Be ( 1 ) ;
var command = originalCommandCandidates . First ( ) ;
2016-12-02 10:41:25 -08:00
commandsInTask [ command ]
. Should ( ) . Be ( false , "Expected to find each element from commands Array once" ) ;
2016-08-22 12:24:10 -07:00
commandsInTask [ command ] = true ;
}
commandsInTask . All ( commandInTask = > commandInTask . Value )
. Should ( )
. BeTrue ( "Expected each element from commands array to be found in a task" ) ;
}
[Theory]
[InlineData("precompile")]
[InlineData("postcompile")]
[InlineData("prepublish")]
[InlineData("postpublish")]
2016-12-02 10:41:25 -08:00
public void MigratedScriptSetHasExecAndReplacesVariables ( string scriptName )
2016-08-22 12:24:10 -07:00
{
var scriptMigrationRule = new MigrateScriptsRule ( ) ;
ProjectRootElement mockProj = ProjectRootElement . Create ( ) ;
2017-01-11 16:51:45 -08:00
ProjectPropertyGroupElement commonPropertyGroup = mockProj . AddPropertyGroup ( ) ;
2016-08-22 12:24:10 -07:00
2016-09-15 15:54:10 -07:00
var commands = new [ ] { "%compile:FullTargetFramework%" , "%compile:Configuration%" } ;
2016-08-22 12:24:10 -07:00
2016-12-02 10:41:25 -08:00
var target = scriptMigrationRule . MigrateScriptSet (
mockProj ,
2017-01-11 16:51:45 -08:00
commonPropertyGroup ,
2016-12-02 10:41:25 -08:00
commands ,
2017-01-03 17:15:39 -08:00
scriptName ,
IsMultiTFM ) ;
2016-08-22 12:24:10 -07:00
target . Tasks . Count ( ) . Should ( ) . Be ( commands . Length ) ;
foreach ( var task in target . Tasks )
{
var taskCommand = task . GetParameter ( "Command" ) ;
var commandIndex = Array . IndexOf ( commands , taskCommand ) ;
2016-12-02 10:41:25 -08:00
commandIndex . Should ( ) . Be (
- 1 ,
"Expected command array elements to be replaced by appropriate msbuild properties" ) ;
2016-08-22 12:24:10 -07:00
}
}
2016-09-15 15:54:10 -07:00
[Fact]
2016-12-02 10:41:25 -08:00
public void PublishIISCommandDoesNotGetMigratedBecauseItIsNowInTheWebSDK ( )
{
var scriptMigrationRule = new MigrateScriptsRule ( ) ;
ProjectRootElement mockProj = ProjectRootElement . Create ( ) ;
2017-01-11 16:51:45 -08:00
ProjectPropertyGroupElement commonPropertyGroup = mockProj . AddPropertyGroup ( ) ;
2016-12-02 10:41:25 -08:00
var commands = new [ ]
{
"dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%"
} ;
var target = scriptMigrationRule . MigrateScriptSet (
mockProj ,
2017-01-11 16:51:45 -08:00
commonPropertyGroup ,
commands ,
"postpublish" ,
IsMultiTFM ) ;
target . Tasks . Should ( ) . BeEmpty ( ) ;
}
[Fact]
public void MigratingScriptsReplacesRazorPrecompileWithProperty ( )
{
var scriptMigrationRule = new MigrateScriptsRule ( ) ;
ProjectRootElement mockProj = ProjectRootElement . Create ( ) ;
ProjectPropertyGroupElement commonPropertyGroup = mockProj . AddPropertyGroup ( ) ;
var commands = new string [ ] { "dotnet razor-precompile --configuration %publish:Configuration% --framework %publish:TargetFramework% --output-path %publish:OutputPath% %publish:ProjectPath%" } ;
var target = scriptMigrationRule . MigrateScriptSet (
mockProj ,
commonPropertyGroup ,
2016-12-02 10:41:25 -08:00
commands ,
2017-01-03 17:15:39 -08:00
"postpublish" ,
IsMultiTFM ) ;
2017-01-11 16:51:45 -08:00
2016-12-02 10:41:25 -08:00
target . Tasks . Should ( ) . BeEmpty ( ) ;
2017-01-11 16:51:45 -08:00
commonPropertyGroup . Properties . Count ( ) . Should ( ) . Be ( 1 ) ;
var propertyElement = commonPropertyGroup . Properties . First ( ) ;
propertyElement . Name . Should ( ) . Be ( "MvcRazorCompileOnPublish" ) ;
propertyElement . Value . Should ( ) . Be ( "true" ) ;
2016-12-02 10:41:25 -08:00
}
[Fact]
public void FormattingScriptCommandsReplacesUnknownVariablesWithMSBuildPropertyForEnvironmentVariableSupport ( )
2016-08-22 12:24:10 -07:00
{
var scriptMigrationRule = new MigrateScriptsRule ( ) ;
2016-09-15 15:54:10 -07:00
scriptMigrationRule . ReplaceScriptVariables ( $"%UnknownVariable%" ) . Should ( ) . Be ( "$(UnknownVariable)" ) ;
2016-08-22 12:24:10 -07:00
}
2016-09-15 15:54:10 -07:00
[Fact]
2017-02-16 16:10:19 -05:00
public void MigratingScriptsWithMultiTFMCreatesTargetWithIsCrosstargetingBuildNotEqualTrueCondition ( )
2016-08-22 12:24:10 -07:00
{
var scriptMigrationRule = new MigrateScriptsRule ( ) ;
2016-09-15 15:54:10 -07:00
ProjectRootElement mockProj = ProjectRootElement . Create ( ) ;
2017-01-11 16:51:45 -08:00
ProjectPropertyGroupElement commonPropertyGroup = mockProj . AddPropertyGroup ( ) ;
2016-08-22 12:24:10 -07:00
2016-09-15 15:54:10 -07:00
var commands = new [ ] { "compile:FullTargetFramework" , "compile:Configuration" } ;
2016-08-30 15:39:27 -07:00
2016-12-02 10:41:25 -08:00
var target = scriptMigrationRule . MigrateScriptSet (
mockProj ,
2017-01-11 16:51:45 -08:00
commonPropertyGroup ,
2016-12-02 10:41:25 -08:00
commands ,
2017-01-03 17:15:39 -08:00
"prepublish" ,
IsMultiTFM ) ;
2016-09-15 15:54:10 -07:00
target . Condition . Should ( ) . Be ( " '$(IsCrossTargetingBuild)' != 'true' " ) ;
2016-08-30 15:39:27 -07:00
}
2016-12-01 05:36:13 +01:00
2017-01-03 17:15:39 -08:00
[Fact]
2017-02-16 16:10:19 -05:00
public void MigratingScriptsWithSingleTFMDoesNotCreateTargetWithIsCrosstargetingBuild ( )
2017-01-03 17:15:39 -08:00
{
var scriptMigrationRule = new MigrateScriptsRule ( ) ;
ProjectRootElement mockProj = ProjectRootElement . Create ( ) ;
2017-01-11 16:51:45 -08:00
ProjectPropertyGroupElement commonPropertyGroup = mockProj . AddPropertyGroup ( ) ;
2017-01-03 17:15:39 -08:00
var commands = new [ ] { "compile:FullTargetFramework" , "compile:Configuration" } ;
var target = scriptMigrationRule . MigrateScriptSet (
mockProj ,
2017-01-11 16:51:45 -08:00
commonPropertyGroup ,
2017-01-03 17:15:39 -08:00
commands ,
"prepublish" ,
false ) ;
target . Condition . Should ( ) . BeEmpty ( ) ;
}
2016-12-01 05:36:13 +01:00
[Fact]
2016-12-02 10:41:25 -08:00
public void MigratingScriptsThrowsOnInvalidScriptSet ( )
2016-12-01 05:36:13 +01:00
{
var scriptMigrationRule = new MigrateScriptsRule ( ) ;
ProjectRootElement mockProj = ProjectRootElement . Create ( ) ;
2017-01-11 16:51:45 -08:00
ProjectPropertyGroupElement commonPropertyGroup = mockProj . AddPropertyGroup ( ) ;
2016-12-01 05:36:13 +01:00
var commands = new string [ ] { "fakecommand" } ;
2016-12-02 10:41:25 -08:00
Action action = ( ) = > scriptMigrationRule . MigrateScriptSet (
mockProj ,
2017-01-11 16:51:45 -08:00
commonPropertyGroup ,
2016-12-02 10:41:25 -08:00
commands ,
2017-01-03 17:15:39 -08:00
"invalidScriptSet" ,
IsMultiTFM ) ;
2016-12-01 05:36:13 +01:00
action . ShouldThrow < MigrationException > ( )
. WithMessage ( "MIGRATE1019::Unsupported Script Event Hook: invalidScriptSet is an unsupported script event hook for project migration" ) ;
}
2016-08-22 12:24:10 -07:00
}
}