2016-08-22 12:24:10 -07:00
using Microsoft.Build.Construction ;
using Microsoft.DotNet.ProjectJsonMigration ;
2016-10-27 18:46:43 -07:00
using Microsoft.DotNet.Internal.ProjectModel ;
2016-08-22 12:24:10 -07:00
using Microsoft.DotNet.Tools.Test.Utilities ;
using NuGet.Frameworks ;
using System ;
using System.IO ;
using System.Linq ;
using Xunit ;
using FluentAssertions ;
2016-08-23 13:50:05 -07:00
using Microsoft.DotNet.ProjectJsonMigration.Rules ;
2016-10-27 18:46:43 -07:00
using Microsoft.DotNet.Internal.ProjectModel.Files ;
2016-10-12 16:01:22 -07:00
using System.Collections.Generic ;
2016-08-22 12:24:10 -07:00
namespace Microsoft.DotNet.ProjectJsonMigration.Tests
{
public class GivenThatIWantToMigrateBuildOptions : TestBase
{
2017-02-08 00:20:04 -08:00
[Fact]
public void MigratingDeprecatedCompilationOptionsWithEmitEntryPointPopulatesOutputTypeField ( )
{
var mockProj = RunBuildOptionsRuleOnPj ( @ "
{
"" compilationOptions "" : {
"" emitEntryPoint "" : "" true ""
} ,
"" exclude "" : [
"" node_modules ""
]
} ");
mockProj . Properties . Count ( p = > p . Name = = "OutputType" ) . Should ( ) . Be ( 1 ) ;
mockProj . Properties . First ( p = > p . Name = = "OutputType" ) . Value . Should ( ) . Be ( "Exe" ) ;
}
2016-08-22 12:24:10 -07:00
[Fact]
2016-12-05 20:34:11 -08:00
public void SpecifiedDefaultPropertiesAreRemovedWhenTheyExistInTheCsprojTemplate ( )
2016-08-22 12:24:10 -07:00
{
// Setup project with default properties
var defaultPropertiesExpectedToBeRemoved = new string [ ]
{
"OutputType" ,
"TargetExt"
} ;
var defaultValue = "defaultValue" ;
var templateProj = ProjectRootElement . Create ( ) ;
var defaultPropertyGroup = templateProj . AddPropertyGroup ( ) ;
foreach ( var defaultPropertyName in defaultPropertiesExpectedToBeRemoved )
{
defaultPropertyGroup . AddProperty ( defaultPropertyName , defaultValue ) ;
}
// Setup projectcontext
var testProjectDirectory = TestAssetsManager . CreateTestInstance ( "TestAppWithRuntimeOptions" ) . Path ;
var projectContext = ProjectContext . Create ( testProjectDirectory , FrameworkConstants . CommonFrameworks . NetCoreApp10 ) ;
2016-12-07 11:49:15 -10:00
var testSettings = MigrationSettings . CreateMigrationSettingsTestHook ( testProjectDirectory , testProjectDirectory , templateProj ) ;
2016-08-22 12:24:10 -07:00
var testInputs = new MigrationRuleInputs ( new [ ] { projectContext } , templateProj , templateProj . AddItemGroup ( ) ,
templateProj . AddPropertyGroup ( ) ) ;
new MigrateBuildOptionsRule ( ) . Apply ( testSettings , testInputs ) ;
defaultPropertyGroup . Properties . Count . Should ( ) . Be ( 0 ) ;
}
[Fact]
2017-01-04 14:17:43 -08:00
public void MigratingEmptyBuildOptionsGeneratesAnEmptyCSProj ( )
2016-08-22 12:24:10 -07:00
{
var mockProj = RunBuildOptionsRuleOnPj ( @ "
{
"" buildOptions "" : { }
} ");
2017-01-04 14:17:43 -08:00
mockProj . Items . Count ( ) . Should ( ) . Be ( 0 ) ;
2016-08-22 12:24:10 -07:00
}
2016-12-09 00:30:02 -08:00
[Fact]
public void MigratingWebProjectWithoutCustomSourcesOrResourcesDoesNotEmitCompileAndEmbeddedResource ( )
{
var mockProj = RunBuildOptionsRuleOnPj ( @ "
{
"" buildOptions "" : {
"" emitEntryPoint "" : true
} ,
"" dependencies "" : {
"" Microsoft . AspNetCore . Mvc "" : {
"" version "" : "" 1.0 . 0 ""
}
} ,
"" frameworks "" : {
"" netcoreapp1 . 0 "" : { }
}
} ");
mockProj . Items . Count ( ) . Should ( ) . Be ( 0 ) ;
}
2017-01-04 14:17:43 -08:00
[Fact]
public void MigratingConsoleProjectWithoutCustomSourcesOrResourcesDoesNotEmitCompileAndEmbeddedResource ( )
{
var mockProj = RunBuildOptionsRuleOnPj ( @ "
{
"" buildOptions "" : {
"" emitEntryPoint "" : true
} ,
"" frameworks "" : {
"" netcoreapp1 . 0 "" : { }
}
} ");
mockProj . Items . Count ( ) . Should ( ) . Be ( 0 ) ;
}
2016-12-09 16:10:16 -08:00
public void MigratingOutputNamePopulatesAssemblyName ( )
{
var mockProj = RunBuildOptionsRuleOnPj ( @ "
{
"" buildOptions "" : {
"" outputName "" : "" some name ""
}
} ");
mockProj . Properties . Count ( p = > p . Name = = "AssemblyName" ) . Should ( ) . Be ( 1 ) ;
mockProj . Properties . First ( p = > p . Name = = "AssemblyName" ) . Value . Should ( ) . Be ( "some name" ) ;
}
[Fact]
public void MigratingOutputNamePopulatesPackageIdWithTheProjectContainingFolderName ( )
{
var testDirectoryPath = Temp . CreateDirectory ( ) . Path ;
var testDirectoryName = new DirectoryInfo ( testDirectoryPath ) . Name ;
var mockProj = RunBuildOptionsRuleOnPj ( @ "
{
"" buildOptions "" : {
"" outputName "" : "" some name ""
}
} ",
testDirectoryPath ) ;
mockProj . Properties . Count ( p = > p . Name = = "PackageId" ) . Should ( ) . Be ( 1 ) ;
mockProj . Properties . First ( p = > p . Name = = "PackageId" ) . Value . Should ( ) . Be ( testDirectoryName ) ;
}
2016-08-22 12:24:10 -07:00
[Fact]
2016-12-05 20:34:11 -08:00
public void MigratingEmitEntryPointTruePopulatesOutputTypeField ( )
2016-08-22 12:24:10 -07:00
{
var mockProj = RunBuildOptionsRuleOnPj ( @ "
{
"" buildOptions "" : {
"" emitEntryPoint "" : "" true ""
}
} ");
mockProj . Properties . Count ( p = > p . Name = = "OutputType" ) . Should ( ) . Be ( 1 ) ;
mockProj . Properties . First ( p = > p . Name = = "OutputType" ) . Value . Should ( ) . Be ( "Exe" ) ;
}
[Fact]
2016-12-05 20:34:11 -08:00
public void MigratingEmitEntryPointFalsePopulatesOutputTypeFields ( )
2016-08-22 12:24:10 -07:00
{
var mockProj = RunBuildOptionsRuleOnPj ( @ "
{
"" buildOptions "" : {
"" emitEntryPoint "" : "" false ""
}
} ");
mockProj . Properties . Count ( p = > p . Name = = "OutputType" ) . Should ( ) . Be ( 1 ) ;
mockProj . Properties . First ( p = > p . Name = = "OutputType" ) . Value . Should ( ) . Be ( "Library" ) ;
}
[Fact]
2016-12-05 20:34:11 -08:00
public void MigratingDefinePopulatesDefineConstants ( )
2016-08-22 12:24:10 -07:00
{
var mockProj = RunBuildOptionsRuleOnPj ( @ "
{
"" buildOptions "" : {
"" define "" : [ "" DEBUG "" , "" TRACE "" ]
}
} ");
mockProj . Properties . Count ( p = > p . Name = = "DefineConstants" ) . Should ( ) . Be ( 1 ) ;
2016-09-08 14:40:46 -07:00
mockProj . Properties . First ( p = > p . Name = = "DefineConstants" )
. Value . Should ( ) . Be ( "$(DefineConstants);DEBUG;TRACE" ) ;
2016-08-22 12:24:10 -07:00
}
[Fact]
2016-12-05 20:34:11 -08:00
public void MigratingNowarnPopulatesNoWarn ( )
2016-08-22 12:24:10 -07:00
{
var mockProj = RunBuildOptionsRuleOnPj ( @ "
{
"" buildOptions "" : {
"" nowarn "" : [ "" CS0168 "" , "" CS0219 "" ]
}
} ");
mockProj . Properties . Count ( p = > p . Name = = "NoWarn" ) . Should ( ) . Be ( 1 ) ;
2016-09-08 14:40:46 -07:00
mockProj . Properties . First ( p = > p . Name = = "NoWarn" ) . Value . Should ( ) . Be ( "$(NoWarn);CS0168;CS0219" ) ;
2016-08-22 12:24:10 -07:00
}
[Fact]
2016-12-05 20:34:11 -08:00
public void MigratingWarningsAsErrorsPopulatesWarningsAsErrors ( )
2016-08-22 12:24:10 -07:00
{
var mockProj = RunBuildOptionsRuleOnPj ( @ "
{
"" buildOptions "" : {
"" warningsAsErrors "" : "" true ""
}
} ");
2017-01-30 14:34:19 -10:00
mockProj . Properties . Count ( p = > p . Name = = "TreatWarningsAsErrors" ) . Should ( ) . Be ( 1 ) ;
mockProj . Properties . First ( p = > p . Name = = "TreatWarningsAsErrors" ) . Value . Should ( ) . Be ( "true" ) ;
2016-08-22 12:24:10 -07:00
mockProj = RunBuildOptionsRuleOnPj ( @ "
{
"" buildOptions "" : {
"" warningsAsErrors "" : "" false ""
}
} ");
2017-01-30 14:34:19 -10:00
mockProj . Properties . Count ( p = > p . Name = = "TreatWarningsAsErrors" ) . Should ( ) . Be ( 0 ) ;
2016-08-22 12:24:10 -07:00
}
[Fact]
2016-12-05 20:34:11 -08:00
public void MigratingAllowUnsafePopulatesAllowUnsafeBlocks ( )
2016-08-22 12:24:10 -07:00
{
var mockProj = RunBuildOptionsRuleOnPj ( @ "
{
"" buildOptions "" : {
"" allowUnsafe "" : "" true ""
}
} ");
mockProj . Properties . Count ( p = > p . Name = = "AllowUnsafeBlocks" ) . Should ( ) . Be ( 1 ) ;
mockProj . Properties . First ( p = > p . Name = = "AllowUnsafeBlocks" ) . Value . Should ( ) . Be ( "true" ) ;
mockProj = RunBuildOptionsRuleOnPj ( @ "
{
"" buildOptions "" : {
"" allowUnsafe "" : "" false ""
}
} ");
mockProj . Properties . Count ( p = > p . Name = = "AllowUnsafeBlocks" ) . Should ( ) . Be ( 0 ) ;
}
[Fact]
2016-12-05 20:34:11 -08:00
public void MigratingOptimizePopulatesOptimize ( )
2016-08-22 12:24:10 -07:00
{
var mockProj = RunBuildOptionsRuleOnPj ( @ "
{
"" buildOptions "" : {
"" optimize "" : "" true ""
}
} ");
mockProj . Properties . Count ( p = > p . Name = = "Optimize" ) . Should ( ) . Be ( 1 ) ;
mockProj . Properties . First ( p = > p . Name = = "Optimize" ) . Value . Should ( ) . Be ( "true" ) ;
mockProj = RunBuildOptionsRuleOnPj ( @ "
{
"" buildOptions "" : {
"" optimize "" : "" false ""
}
} ");
mockProj . Properties . Count ( p = > p . Name = = "Optimize" ) . Should ( ) . Be ( 0 ) ;
}
[Fact]
2016-12-05 20:34:11 -08:00
public void MigratingPlatformPopulatesPlatformTarget ( )
2016-08-22 12:24:10 -07:00
{
var mockProj = RunBuildOptionsRuleOnPj ( @ "
{
"" buildOptions "" : {
"" platform "" : "" x64 ""
}
} ");
mockProj . Properties . Count ( p = > p . Name = = "PlatformTarget" ) . Should ( ) . Be ( 1 ) ;
mockProj . Properties . First ( p = > p . Name = = "PlatformTarget" ) . Value . Should ( ) . Be ( "x64" ) ;
mockProj = RunBuildOptionsRuleOnPj ( @ "
{
"" buildOptions "" : {
"" platform "" : "" x86 ""
}
} ");
mockProj . Properties . Count ( p = > p . Name = = "PlatformTarget" ) . Should ( ) . Be ( 1 ) ;
mockProj . Properties . First ( p = > p . Name = = "PlatformTarget" ) . Value . Should ( ) . Be ( "x86" ) ;
mockProj = RunBuildOptionsRuleOnPj ( @ "
{
"" buildOptions "" : {
"" platform "" : "" foo ""
}
} ");
mockProj . Properties . Count ( p = > p . Name = = "PlatformTarget" ) . Should ( ) . Be ( 1 ) ;
mockProj . Properties . First ( p = > p . Name = = "PlatformTarget" ) . Value . Should ( ) . Be ( "foo" ) ;
}
[Fact]
2016-12-05 20:34:11 -08:00
public void MigratingLanguageVersionPopulatesLangVersion ( )
2016-08-22 12:24:10 -07:00
{
var mockProj = RunBuildOptionsRuleOnPj ( @ "
{
"" buildOptions "" : {
"" languageVersion "" : "" 5 ""
}
} ");
mockProj . Properties . Count ( p = > p . Name = = "LangVersion" ) . Should ( ) . Be ( 1 ) ;
mockProj . Properties . First ( p = > p . Name = = "LangVersion" ) . Value . Should ( ) . Be ( "5" ) ;
}
2016-10-12 14:59:36 -07:00
[Fact]
2016-12-05 20:34:11 -08:00
public void MigratingLanguageVersionRemovesCsharpInLangVersion ( )
2016-10-12 14:59:36 -07:00
{
var mockProj = RunBuildOptionsRuleOnPj ( @ "
{
"" buildOptions "" : {
"" languageVersion "" : "" csharp5 ""
}
} ");
mockProj . Properties . Count ( p = > p . Name = = "LangVersion" ) . Should ( ) . Be ( 1 ) ;
mockProj . Properties . First ( p = > p . Name = = "LangVersion" ) . Value . Should ( ) . Be ( "5" ) ;
}
2016-08-22 12:24:10 -07:00
[Fact]
2016-12-05 20:34:11 -08:00
public void MigratingKeyFilePopulatesAssemblyOriginatorKeyFileAndSignAssembly ( )
2016-08-22 12:24:10 -07:00
{
var mockProj = RunBuildOptionsRuleOnPj ( @ "
{
"" buildOptions "" : {
"" keyFile "" : "" . . / keyfile . snk ""
}
} ");
2016-10-20 20:15:42 -07:00
mockProj . Properties . Count ( p = > p . Name = = "AssemblyOriginatorKeyFile" ) . Should ( ) . Be ( 1 ) ;
mockProj . Properties . First ( p = > p . Name = = "AssemblyOriginatorKeyFile" ) . Value . Should ( ) . Be ( "../keyfile.snk" ) ;
2016-08-22 12:24:10 -07:00
mockProj . Properties . Count ( p = > p . Name = = "SignAssembly" ) . Should ( ) . Be ( 1 ) ;
mockProj . Properties . First ( p = > p . Name = = "SignAssembly" ) . Value . Should ( ) . Be ( "true" ) ;
2016-10-04 16:06:45 -07:00
mockProj . Properties . Count ( p = > p . Name = = "PublicSign" ) . Should ( ) . Be ( 1 ) ;
mockProj . Properties . First ( p = > p . Name = = "PublicSign" ) . Value . Should ( ) . Be ( "true" ) ;
mockProj . Properties . First ( p = > p . Name = = "PublicSign" ) . Condition . Should ( ) . Be ( " '$(OS)' != 'Windows_NT' " ) ;
2016-08-22 12:24:10 -07:00
}
[Fact]
2016-12-05 20:34:11 -08:00
public void MigratingDelaySignPopulatesDelaySign ( )
2016-08-22 12:24:10 -07:00
{
var mockProj = RunBuildOptionsRuleOnPj ( @ "
{
"" buildOptions "" : {
"" delaySign "" : "" true ""
}
} ");
mockProj . Properties . Count ( p = > p . Name = = "DelaySign" ) . Should ( ) . Be ( 1 ) ;
mockProj . Properties . First ( p = > p . Name = = "DelaySign" ) . Value . Should ( ) . Be ( "true" ) ;
mockProj = RunBuildOptionsRuleOnPj ( @ "
{
"" buildOptions "" : {
"" delaySign "" : "" false ""
}
} ");
mockProj . Properties . Count ( p = > p . Name = = "DelaySign" ) . Should ( ) . Be ( 0 ) ;
}
[Fact]
2016-12-05 20:34:11 -08:00
public void MigratingPublicSignPopulatesPublicSign ( )
2016-08-22 12:24:10 -07:00
{
var mockProj = RunBuildOptionsRuleOnPj ( @ "
{
"" buildOptions "" : {
"" publicSign "" : "" true ""
}
} ");
mockProj . Properties . Count ( p = > p . Name = = "PublicSign" ) . Should ( ) . Be ( 1 ) ;
mockProj . Properties . First ( p = > p . Name = = "PublicSign" ) . Value . Should ( ) . Be ( "true" ) ;
mockProj = RunBuildOptionsRuleOnPj ( @ "
{
"" buildOptions "" : {
"" publicSign "" : "" false ""
}
} ");
mockProj . Properties . Count ( p = > p . Name = = "PublicSign" ) . Should ( ) . Be ( 0 ) ;
}
[Fact]
2016-12-05 20:34:11 -08:00
public void MigratingDebugTypePopulatesDebugType ( )
2016-08-22 12:24:10 -07:00
{
var mockProj = RunBuildOptionsRuleOnPj ( @ "
{
"" buildOptions "" : {
"" debugType "" : "" full ""
}
} ");
mockProj . Properties . Count ( p = > p . Name = = "DebugType" ) . Should ( ) . Be ( 1 ) ;
mockProj . Properties . First ( p = > p . Name = = "DebugType" ) . Value . Should ( ) . Be ( "full" ) ;
mockProj = RunBuildOptionsRuleOnPj ( @ "
{
"" buildOptions "" : {
"" debugType "" : "" foo ""
}
} ");
mockProj . Properties . Count ( p = > p . Name = = "DebugType" ) . Should ( ) . Be ( 1 ) ;
mockProj . Properties . First ( p = > p . Name = = "DebugType" ) . Value . Should ( ) . Be ( "foo" ) ;
}
[Fact]
2016-12-05 20:34:11 -08:00
public void MigratingXmlDocPopulatesGenerateDocumentationFile ( )
2016-08-22 12:24:10 -07:00
{
var mockProj = RunBuildOptionsRuleOnPj ( @ "
{
"" buildOptions "" : {
"" xmlDoc "" : "" true ""
}
} ");
mockProj . Properties . Count ( p = > p . Name = = "GenerateDocumentationFile" ) . Should ( ) . Be ( 1 ) ;
mockProj . Properties . First ( p = > p . Name = = "GenerateDocumentationFile" ) . Value . Should ( ) . Be ( "true" ) ;
}
[Theory]
2017-01-28 15:14:17 -10:00
[InlineData("compile", "Compile", 3, "")]
[InlineData("embed", "EmbeddedResource", 3, ";rootfile.cs")]
[InlineData("copyToOutput", "Content", 2, ";rootfile.cs")]
2016-12-05 20:34:11 -08:00
private void MigratingGroupIncludeExcludePopulatesAppropriateProjectItemElement (
2016-08-22 12:24:10 -07:00
string group ,
2017-01-27 18:39:44 -08:00
string itemName ,
2017-01-28 15:14:17 -10:00
int expectedNumberOfCompileItems ,
2017-01-27 18:39:44 -08:00
string expectedRootFiles )
2016-08-22 12:24:10 -07:00
{
var testDirectory = Temp . CreateDirectory ( ) . Path ;
2016-10-12 16:01:22 -07:00
WriteExtraFiles ( testDirectory ) ;
2016-08-22 12:24:10 -07:00
var pj = @ "
{
"" buildOptions "" : {
"" < group > "" : {
"" include "" : [ "" root "" , "" src "" , "" rootfile . cs "" ] ,
"" exclude "" : [ "" src "" , "" rootfile . cs "" ] ,
"" includeFiles "" : [ "" src / file1 . cs "" , "" src / file2 . cs "" ] ,
"" excludeFiles "" : [ "" src / file2 . cs "" ]
}
}
} ".Replace(" < group > ", group);
var mockProj = RunBuildOptionsRuleOnPj ( pj ,
testDirectory : testDirectory ) ;
2017-01-28 15:14:17 -10:00
mockProj . Items . Count ( i = > i . ItemType . Equals ( itemName , StringComparison . Ordinal ) )
. Should ( ) . Be ( expectedNumberOfCompileItems ) ;
2016-08-22 12:24:10 -07:00
2016-10-12 16:01:22 -07:00
var defaultIncludePatterns = GetDefaultIncludePatterns ( group ) ;
var defaultExcludePatterns = GetDefaultExcludePatterns ( group ) ;
2016-08-22 12:24:10 -07:00
foreach ( var item in mockProj . Items . Where ( i = > i . ItemType . Equals ( itemName , StringComparison . Ordinal ) ) )
{
2016-10-12 16:01:22 -07:00
VerifyContentMetadata ( item ) ;
2016-08-23 13:50:05 -07:00
2017-01-28 15:14:17 -10:00
if ( string . IsNullOrEmpty ( item . Include ) )
{
item . Remove . Should ( )
. Be ( @"src\**\*;rootfile.cs;src\file2.cs" ) ;
}
else if ( item . Include . Contains ( @"src\file1.cs" ) )
2016-08-22 12:24:10 -07:00
{
item . Include . Should ( ) . Be ( @"src\file1.cs;src\file2.cs" ) ;
item . Exclude . Should ( ) . Be ( @"src\file2.cs" ) ;
}
else
{
if ( defaultIncludePatterns . Any ( ) )
{
item . Include . Should ( )
2017-01-27 18:39:44 -08:00
. Be ( $@"root\**\*;src\**\*{expectedRootFiles};" + string . Join ( ";" , defaultIncludePatterns ) . Replace ( "/" , "\\" ) ) ;
2016-08-22 12:24:10 -07:00
}
else
{
item . Include . Should ( )
2017-01-27 18:39:44 -08:00
. Be ( $@"root\**\*;src\**\*{expectedRootFiles}" ) ;
2016-08-22 12:24:10 -07:00
}
if ( defaultExcludePatterns . Any ( ) )
{
item . Exclude . Should ( )
. Be ( @"src\**\*;rootfile.cs;" + string . Join ( ";" , defaultExcludePatterns ) . Replace ( "/" , "\\" ) +
@";src\file2.cs" ) ;
}
else
{
item . Exclude . Should ( )
. Be ( @"src\**\*;rootfile.cs;src\file2.cs" ) ;
}
}
}
}
2016-10-12 16:01:22 -07:00
[Theory]
2017-01-27 18:39:44 -08:00
[InlineData("compile", "Compile", "")]
[InlineData("embed", "EmbeddedResource", ";rootfile.cs")]
[InlineData("copyToOutput", "Content", ";rootfile.cs")]
2016-12-05 20:34:11 -08:00
private void MigratingGroupIncludeOnlyPopulatesAppropriateProjectItemElement (
2016-10-12 16:01:22 -07:00
string group ,
2017-01-27 18:39:44 -08:00
string itemName ,
string expectedRootFiles )
2016-10-12 16:01:22 -07:00
{
var testDirectory = Temp . CreateDirectory ( ) . Path ;
WriteExtraFiles ( testDirectory ) ;
var pj = @ "
{
"" buildOptions "" : {
"" < group > "" : [ "" root "" , "" src "" , "" rootfile . cs "" ]
}
} ".Replace(" < group > ", group);
var mockProj = RunBuildOptionsRuleOnPj ( pj ,
testDirectory : testDirectory ) ;
Console . WriteLine ( mockProj . RawXml ) ;
mockProj . Items . Count ( i = > i . ItemType . Equals ( itemName , StringComparison . Ordinal ) ) . Should ( ) . Be ( 1 ) ;
var defaultIncludePatterns = GetDefaultIncludePatterns ( group ) ;
var defaultExcludePatterns = GetDefaultExcludePatterns ( group ) ;
foreach ( var item in mockProj . Items . Where ( i = > i . ItemType . Equals ( itemName , StringComparison . Ordinal ) ) )
{
VerifyContentMetadata ( item ) ;
if ( defaultIncludePatterns . Any ( ) )
{
item . Include . Should ( )
2017-01-27 18:39:44 -08:00
. Be ( $@"root\**\*;src\**\*{expectedRootFiles};" + string . Join ( ";" , defaultIncludePatterns ) . Replace ( "/" , "\\" ) ) ;
2016-10-12 16:01:22 -07:00
}
else
{
item . Include . Should ( )
2017-01-27 18:39:44 -08:00
. Be ( $@"root\**\*;src\**\*{expectedRootFiles}" ) ;
2016-10-12 16:01:22 -07:00
}
if ( defaultExcludePatterns . Any ( ) )
{
item . Exclude . Should ( )
. Be ( string . Join ( ";" , defaultExcludePatterns ) . Replace ( "/" , "\\" ) ) ;
}
else
{
item . Exclude . Should ( )
. Be ( string . Empty ) ;
}
}
}
2016-12-05 18:23:34 -08:00
[Fact]
public void MigratingTestProjectAddsGenerateRuntimeConfigurationFiles ( )
{
var mockProj = RunBuildOptionsRuleOnPj ( @ "
{
"" testRunner "" : "" xunit ""
} ");
mockProj . Properties . Count ( p = > p . Name = = "GenerateRuntimeConfigurationFiles" ) . Should ( ) . Be ( 1 ) ;
mockProj . Properties . First ( p = > p . Name = = "GenerateRuntimeConfigurationFiles" ) . Value . Should ( ) . Be ( "true" ) ;
}
[Fact]
public void MigratingANonTestProjectDoesNotAddGenerateRuntimeConfigurationFiles ( )
{
var mockProj = RunBuildOptionsRuleOnPj ( @ "
{
} ");
mockProj . Properties . Count ( p = > p . Name = = "GenerateRuntimeConfigurationFiles" ) . Should ( ) . Be ( 0 ) ;
}
2016-12-19 15:22:21 -08:00
[Fact]
public void MigratingAnAppWithAppConfigAddsItAsNoneToTheCsProj ( )
{
var tempDirectory = Temp . CreateDirectory ( ) . Path ;
File . Create ( Path . Combine ( tempDirectory , "App.config" ) ) . Dispose ( ) ;
var mockProj = RunBuildOptionsRuleOnPj ( @ "
{
} ",
tempDirectory ) ;
mockProj . Items . Count ( i = > i . ItemType = = "None" ) . Should ( ) . Be ( 1 ) ;
mockProj . Items . First ( i = > i . ItemType = = "None" ) . Include . Should ( ) . Be ( "App.config" ) ;
}
2017-01-27 18:39:44 -08:00
[Fact]
public void MigratingCompileIncludeWithPlainFileNamesRemovesThem ( )
{
var mockProj = RunBuildOptionsRuleOnPj ( @ "
{
"" buildOptions "" : {
"" compile "" : {
"" include "" : [ "" filename1 . cs "" , "" filename2 . cs "" ] ,
}
}
} ");
mockProj . Items . Count ( i = > i . ItemType . Equals ( "Compile" , StringComparison . Ordinal ) ) . Should ( ) . Be ( 0 ) ;
}
[Fact]
public void MigratingCompileIncludeFilesWithPlainFileNamesRemovesThem ( )
{
var mockProj = RunBuildOptionsRuleOnPj ( @ "
{
"" buildOptions "" : {
"" compile "" : {
"" includeFiles "" : [ "" filename1 . cs "" , "" filename2 . cs "" ] ,
}
}
} ");
mockProj . Items . Count ( i = > i . ItemType . Equals ( "Compile" , StringComparison . Ordinal ) ) . Should ( ) . Be ( 0 ) ;
}
2017-01-31 18:30:37 -10:00
[Fact]
public void MigratingProjectWithCompileResourcesPopulatesAppropriateProjectItemElements ( )
{
var testDirectory = Temp . CreateDirectory ( ) . Path ;
WriteCompileResourceFiles ( testDirectory ) ;
var pj = @ "
{
"" version "" : "" 1.0 . 0 - * "" ,
"" dependencies "" : {
"" NETStandard . Library "" : "" 1.6 . 0 ""
} ,
"" frameworks "" : {
"" netstandard1 . 5 "" : { }
}
} ";
var mockProj = RunBuildOptionsRuleOnPj ( pj , testDirectory ) ;
var compileItems = mockProj . Items . Where ( i = > i . ItemType . Equals ( "Compile" , StringComparison . Ordinal ) ) ;
compileItems . Count ( ) . Should ( ) . Be ( 1 ) ;
var compileItem = compileItems . Single ( ) ;
compileItem . Include . Should ( ) . BeEmpty ( ) ;
compileItem . Exclude . Should ( ) . BeEmpty ( ) ;
compileItem . Remove . Should ( ) . Be ( @"compiler\resources\*" ) ;
var embeddedResourceItems = mockProj . Items . Where (
i = > i . ItemType . Equals ( "EmbeddedResource" , StringComparison . Ordinal ) ) ;
embeddedResourceItems . Count ( ) . Should ( ) . Be ( 1 ) ;
var embeddedResourceItem = embeddedResourceItems . Single ( ) ;
embeddedResourceItem . Include . Should ( ) . Be ( @"compiler\resources\*" ) ;
embeddedResourceItem . Exclude . Should ( ) . BeEmpty ( ) ;
embeddedResourceItem . Remove . Should ( ) . BeEmpty ( ) ;
}
[Fact]
public void MigratingProjectWithoutCompileResourcesDoesNotAddProjectItemElements ( )
{
var testDirectory = Temp . CreateDirectory ( ) . Path ;
var pj = @ "
{
"" version "" : "" 1.0 . 0 - * "" ,
"" dependencies "" : {
"" NETStandard . Library "" : "" 1.6 . 0 ""
} ,
"" frameworks "" : {
"" netstandard1 . 5 "" : { }
}
} ";
var mockProj = RunBuildOptionsRuleOnPj ( pj , testDirectory ) ;
mockProj . Items . Count . Should ( ) . Be ( 0 ) ;
}
2016-10-12 16:01:22 -07:00
private static IEnumerable < string > GetDefaultExcludePatterns ( string group )
{
2016-12-14 13:59:01 -08:00
var defaultExcludePatterns = new List < string > ( group = = "copyToOutput" ?
2017-01-12 11:22:41 -08:00
Enumerable . Empty < string > ( ) :
2016-12-14 13:59:01 -08:00
ProjectFilesCollection . DefaultBuiltInExcludePatterns ) ;
if ( group = = "embed" )
{
defaultExcludePatterns . Add ( "@(EmbeddedResource)" ) ;
}
return defaultExcludePatterns ;
2016-10-12 16:01:22 -07:00
}
private static IEnumerable < string > GetDefaultIncludePatterns ( string group )
{
2017-01-12 11:22:41 -08:00
return Enumerable . Empty < string > ( ) ;
2016-10-12 16:01:22 -07:00
}
private static void VerifyContentMetadata ( ProjectItemElement item )
{
if ( item . ItemType = = "Content" )
{
item . Metadata . Count ( m = > m . Name = = "CopyToOutputDirectory" ) . Should ( ) . Be ( 1 ) ;
}
}
private void WriteExtraFiles ( string directory )
{
Directory . CreateDirectory ( Path . Combine ( directory , "root" ) ) ;
Directory . CreateDirectory ( Path . Combine ( directory , "src" ) ) ;
File . WriteAllText ( Path . Combine ( directory , "root" , "file1.txt" ) , "content" ) ;
File . WriteAllText ( Path . Combine ( directory , "root" , "file2.txt" ) , "content" ) ;
File . WriteAllText ( Path . Combine ( directory , "root" , "file3.txt" ) , "content" ) ;
File . WriteAllText ( Path . Combine ( directory , "src" , "file1.cs" ) , "content" ) ;
File . WriteAllText ( Path . Combine ( directory , "src" , "file2.cs" ) , "content" ) ;
File . WriteAllText ( Path . Combine ( directory , "src" , "file3.cs" ) , "content" ) ;
File . WriteAllText ( Path . Combine ( directory , "rootfile.cs" ) , "content" ) ;
}
2017-01-31 18:30:37 -10:00
private void WriteCompileResourceFiles ( string directory )
{
Directory . CreateDirectory ( Path . Combine ( directory , "compiler" ) ) ;
Directory . CreateDirectory ( Path . Combine ( directory , "compiler" , "resources" ) ) ;
File . WriteAllText ( Path . Combine ( directory , "compiler" , "resources" , "file.cs" ) , "content" ) ;
}
2016-08-22 12:24:10 -07:00
private ProjectRootElement RunBuildOptionsRuleOnPj ( string s , string testDirectory = null )
{
testDirectory = testDirectory ? ? Temp . CreateDirectory ( ) . Path ;
return TemporaryProjectFileRuleRunner . RunRules ( new IMigrationRule [ ]
{
new MigrateBuildOptionsRule ( )
} , s , testDirectory ) ;
}
}
}