2016-09-22 16:14: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.
using Microsoft.Build.Construction ;
using Microsoft.DotNet.Tools.Test.Utilities ;
2016-10-20 14:35:45 -07:00
using System.IO ;
2016-09-22 16:14:27 -07:00
using System.Linq ;
using Xunit ;
using FluentAssertions ;
using Microsoft.DotNet.ProjectJsonMigration.Rules ;
using System ;
namespace Microsoft.DotNet.ProjectJsonMigration.Tests
{
public class GivenThatIWantToMigratePackOptions : TestBase
{
[Fact]
2017-02-23 20:36:02 -08:00
public void ItDoesNotMigrateSummary ( )
2016-09-22 16:14:27 -07:00
{
var mockProj = RunPackOptionsRuleOnPj ( @ "
{
"" packOptions "" : {
"" summary "" : "" Some not important summary ""
2017-03-02 20:35:20 -08:00
}
2016-09-22 16:14:27 -07:00
} ");
2017-03-02 20:35:20 -08:00
EmitsOnlyAlwaysEmittedPackOptionsProperties ( mockProj ) ;
2016-09-22 16:14:27 -07:00
}
[Fact]
2017-02-23 20:36:02 -08:00
public void ItDoesNotMigrateOwner ( )
2016-09-22 16:14:27 -07:00
{
var mockProj = RunPackOptionsRuleOnPj ( @ "
{
"" packOptions "" : {
"" owner "" : "" Some not important owner ""
2017-03-02 20:35:20 -08:00
}
2016-09-22 16:14:27 -07:00
} ");
EmitsOnlyAlwaysEmittedPackOptionsProperties ( mockProj ) ;
}
[Fact]
2017-02-23 20:36:02 -08:00
public void MigratingEmptyTagsDoesNotPopulatePackageTags ( )
2016-09-22 16:14:27 -07:00
{
var mockProj = RunPackOptionsRuleOnPj ( @ "
{
"" packOptions "" : {
"" tags "" : [ ]
2017-03-02 20:35:20 -08:00
}
2016-09-22 16:14:27 -07:00
} ");
mockProj . Properties . Count ( p = > p . Name = = "PackageTags" ) . Should ( ) . Be ( 0 ) ;
}
[Fact]
2017-02-23 20:36:02 -08:00
public void MigratingTagsPopulatesPackageTagsSemicolonDelimited ( )
2016-09-22 16:14:27 -07:00
{
var mockProj = RunPackOptionsRuleOnPj ( @ "
{
"" packOptions "" : {
"" tags "" : [ "" hyperscale "" , "" cats "" ]
2017-03-02 20:35:20 -08:00
}
2016-09-22 16:14:27 -07:00
} ");
mockProj . Properties . Count ( p = > p . Name = = "PackageTags" ) . Should ( ) . Be ( 1 ) ;
mockProj . Properties . First ( p = > p . Name = = "PackageTags" ) . Value . Should ( ) . Be ( "hyperscale;cats" ) ;
}
[Fact]
2017-02-23 20:36:02 -08:00
public void MigratingReleaseNotesPopulatesPackageReleaseNotes ( )
2016-09-22 16:14:27 -07:00
{
var mockProj = RunPackOptionsRuleOnPj ( @ "
{
"" packOptions "" : {
"" releaseNotes "" : "" Some release notes value . ""
2017-03-02 20:35:20 -08:00
}
2016-09-22 16:14:27 -07:00
} ");
mockProj . Properties . Count ( p = > p . Name = = "PackageReleaseNotes" ) . Should ( ) . Be ( 1 ) ;
mockProj . Properties . First ( p = > p . Name = = "PackageReleaseNotes" ) . Value . Should ( )
. Be ( "Some release notes value." ) ;
}
[Fact]
2017-02-23 20:36:02 -08:00
public void MigratingIconUrlPopulatesPackageIconUrl ( )
2016-09-22 16:14:27 -07:00
{
var mockProj = RunPackOptionsRuleOnPj ( @ "
{
"" packOptions "" : {
"" iconUrl "" : "" http : //www.mylibrary.gov/favicon.ico""
2017-03-02 20:35:20 -08:00
}
2016-09-22 16:14:27 -07:00
} ");
mockProj . Properties . Count ( p = > p . Name = = "PackageIconUrl" ) . Should ( ) . Be ( 1 ) ;
mockProj . Properties . First ( p = > p . Name = = "PackageIconUrl" ) . Value . Should ( )
. Be ( "http://www.mylibrary.gov/favicon.ico" ) ;
}
[Fact]
2017-02-23 20:36:02 -08:00
public void MigratingProjectUrlPopulatesPackageProjectUrl ( )
2016-09-22 16:14:27 -07:00
{
var mockProj = RunPackOptionsRuleOnPj ( @ "
{
"" packOptions "" : {
"" projectUrl "" : "" http : //www.url.to.library.com""
2017-03-02 20:35:20 -08:00
}
2016-09-22 16:14:27 -07:00
} ");
mockProj . Properties . Count ( p = > p . Name = = "PackageProjectUrl" ) . Should ( ) . Be ( 1 ) ;
mockProj . Properties . First ( p = > p . Name = = "PackageProjectUrl" ) . Value . Should ( )
. Be ( "http://www.url.to.library.com" ) ;
}
[Fact]
2017-02-23 20:36:02 -08:00
public void MigratingLicenseUrlPopulatesPackageLicenseUrl ( )
2016-09-22 16:14:27 -07:00
{
var mockProj = RunPackOptionsRuleOnPj ( @ "
{
"" packOptions "" : {
"" licenseUrl "" : "" http : //www.url.to.library.com/licence""
2017-03-02 20:35:20 -08:00
}
2016-09-22 16:14:27 -07:00
} ");
mockProj . Properties . Count ( p = > p . Name = = "PackageLicenseUrl" ) . Should ( ) . Be ( 1 ) ;
mockProj . Properties . First ( p = > p . Name = = "PackageLicenseUrl" ) . Value . Should ( )
. Be ( "http://www.url.to.library.com/licence" ) ;
}
[Fact]
2017-02-23 20:36:02 -08:00
public void MigratingRequireLicenseAcceptancePopulatesPackageRequireLicenseAcceptance ( )
2016-09-22 16:14:27 -07:00
{
var mockProj = RunPackOptionsRuleOnPj ( @ "
{
"" packOptions "" : {
"" requireLicenseAcceptance "" : "" true ""
2017-03-02 20:35:20 -08:00
}
2016-09-22 16:14:27 -07:00
} ");
mockProj . Properties . Count ( p = > p . Name = = "PackageRequireLicenseAcceptance" ) . Should ( ) . Be ( 1 ) ;
mockProj . Properties . First ( p = > p . Name = = "PackageRequireLicenseAcceptance" ) . Value . Should ( ) . Be ( "true" ) ;
}
[Fact]
2017-02-23 20:36:02 -08:00
public void MigratingRequireLicenseAcceptancePopulatesPackageRequireLicenseAcceptanceEvenIfItsValueIsFalse ( )
2016-09-22 16:14:27 -07:00
{
var mockProj = RunPackOptionsRuleOnPj ( @ "
{
"" packOptions "" : {
"" requireLicenseAcceptance "" : "" false ""
2017-03-02 20:35:20 -08:00
}
2016-09-22 16:14:27 -07:00
} ");
mockProj . Properties . Count ( p = > p . Name = = "PackageRequireLicenseAcceptance" ) . Should ( ) . Be ( 1 ) ;
mockProj . Properties . First ( p = > p . Name = = "PackageRequireLicenseAcceptance" ) . Value . Should ( ) . Be ( "false" ) ;
}
[Fact]
2017-02-23 20:36:02 -08:00
public void MigratingRepositoryTypePopulatesRepositoryType ( )
2016-09-22 16:14:27 -07:00
{
var mockProj = RunPackOptionsRuleOnPj ( @ "
{
"" packOptions "" : {
"" repository "" : {
"" type "" : "" git ""
}
2017-03-02 20:35:20 -08:00
}
2016-09-22 16:14:27 -07:00
} ");
mockProj . Properties . Count ( p = > p . Name = = "RepositoryType" ) . Should ( ) . Be ( 1 ) ;
mockProj . Properties . First ( p = > p . Name = = "RepositoryType" ) . Value . Should ( ) . Be ( "git" ) ;
}
[Fact]
2017-02-23 20:36:02 -08:00
public void MigratingRepositoryUrlPopulatesRepositoryUrl ( )
2016-09-22 16:14:27 -07:00
{
var mockProj = RunPackOptionsRuleOnPj ( @ "
{
"" packOptions "" : {
"" repository "" : {
"" url "" : "" http : //github.com/dotnet/cli""
}
}
} ");
mockProj . Properties . Count ( p = > p . Name = = "RepositoryUrl" ) . Should ( ) . Be ( 1 ) ;
mockProj . Properties . First ( p = > p . Name = = "RepositoryUrl" ) . Value . Should ( ) . Be ( "http://github.com/dotnet/cli" ) ;
}
[Fact]
2017-02-23 20:36:02 -08:00
public void MigratingFilesWithoutMappingsPopulatesContentWithSamePathAsIncludeAndPackTrue ( )
2016-09-22 16:14:27 -07:00
{
2016-10-20 11:24:58 -07:00
var mockProj = RunPackOptionsRuleOnPj ( @ "
{
"" packOptions "" : {
"" files "" : {
"" include "" : [ "" path / to / some / file . cs "" , "" path / to / some / other / file . cs "" ]
}
}
} ");
var contentItems = mockProj . Items
2017-02-22 22:06:45 -08:00
. Where ( item = > item . ItemType . Equals ( "None" , StringComparison . Ordinal ) )
2016-10-20 14:35:45 -07:00
. Where ( item = > item . GetMetadataWithName ( "Pack" ) . Value = = "true" ) ;
2016-10-20 11:24:58 -07:00
contentItems . Count ( ) . Should ( ) . Be ( 1 ) ;
2017-02-22 23:16:54 -08:00
contentItems . First ( ) . Update . Should ( ) . Be ( @"path\to\some\file.cs;path\to\some\other\file.cs" ) ;
2016-10-20 11:24:58 -07:00
}
[Fact]
2017-02-23 20:36:02 -08:00
public void MigratingFilesWithExcludePopulatesNoneWithPackFalseForTheExcludedFiles ( )
{
var mockProj = RunPackOptionsRuleOnPj ( @ "
{
"" packOptions "" : {
"" files "" : {
"" include "" : [ "" path / to / some / file . cs "" , "" path / to / some / other / file . cs "" ] ,
"" exclude "" : [ "" path / to / file / to / exclude . cs "" ]
}
}
} ");
2017-02-23 22:55:35 -08:00
foreach ( var item in mockProj . Items . Where ( i = > i . ItemType . Equals ( "None" , StringComparison . Ordinal ) ) )
{
Console . WriteLine ( $"Update: {item.Update}, Include: {item.Include}, Remove: {item.Remove}" ) ;
foreach ( var meta in item . Metadata )
{
Console . WriteLine ( $"\tMetadata: Name: {meta.Name}, Value: {meta.Value}" ) ;
}
foreach ( var condition in item . ConditionChain ( ) )
{
Console . WriteLine ( $"\tCondition: {condition}" ) ;
}
}
2017-02-23 20:36:02 -08:00
var contentItemsToInclude = mockProj . Items
. Where ( item = > item . ItemType . Equals ( "None" , StringComparison . Ordinal ) )
. Where ( item = > item . GetMetadataWithName ( "Pack" ) . Value = = "true" ) ;
contentItemsToInclude . Count ( ) . Should ( ) . Be ( 1 ) ;
contentItemsToInclude . First ( ) . Update . Should ( ) . Be ( @"path\to\some\file.cs;path\to\some\other\file.cs" ) ;
var contentItemsToExclude = mockProj . Items
. Where ( item = > item . ItemType . Equals ( "None" , StringComparison . Ordinal ) )
. Where ( item = > item . GetMetadataWithName ( "Pack" ) . Value = = "false" ) ;
2017-02-23 22:55:35 -08:00
contentItemsToExclude . Count ( ) . Should ( ) . Be ( 1 ) ;
contentItemsToExclude . First ( ) . Update . Should ( ) . Be ( @"path\to\file\to\exclude.cs" ) ;
2017-02-23 20:36:02 -08:00
}
[Fact]
public void MigratingFilesWithMappingsPopulatesContentPackagePathMetadata ( )
2016-10-20 11:24:58 -07:00
{
var mockProj = RunPackOptionsRuleOnPj ( @ "
2016-09-22 16:14:27 -07:00
{
"" packOptions "" : {
"" files "" : {
2016-10-20 11:24:58 -07:00
"" include "" : [ "" path / to / some / file . cs "" ] ,
"" mappings "" : {
"" some / other / path / file . cs "" : "" path / to / some / file . cs ""
}
2016-09-22 16:14:27 -07:00
}
}
} ");
2016-10-20 11:24:58 -07:00
var contentItems = mockProj . Items
2017-02-22 22:06:45 -08:00
. Where ( item = > item . ItemType . Equals ( "None" , StringComparison . Ordinal ) )
2016-10-20 11:24:58 -07:00
. Where ( item = >
2016-10-20 14:35:45 -07:00
item . GetMetadataWithName ( "Pack" ) . Value = = "true" & &
2016-10-20 11:24:58 -07:00
item . GetMetadataWithName ( "PackagePath" ) ! = null ) ;
contentItems . Count ( ) . Should ( ) . Be ( 1 ) ;
2017-02-22 23:16:54 -08:00
contentItems . First ( ) . Update . Should ( ) . Be ( @"path\to\some\file.cs" ) ;
2016-10-20 14:35:45 -07:00
contentItems . First ( ) . GetMetadataWithName ( "PackagePath" ) . Value . Should ( ) . Be (
Path . Combine ( "some" , "other" , "path" ) ) ;
2016-09-22 16:14:27 -07:00
}
2016-10-20 11:56:30 -07:00
[Fact]
2017-02-23 20:36:02 -08:00
public void MigratingFilesWithMappingsToRootPopulatesContentPackagePathMetadataButLeavesItEmpty ( )
2016-10-20 11:56:30 -07:00
{
var mockProj = RunPackOptionsRuleOnPj ( @ "
{
"" packOptions "" : {
"" files "" : {
"" include "" : [ "" path / to / some / file . cs "" ] ,
"" mappings "" : {
"" . file . cs "" : "" path / to / some / file . cs ""
}
}
}
} ");
var contentItems = mockProj . Items
2017-02-22 22:06:45 -08:00
. Where ( item = > item . ItemType . Equals ( "None" , StringComparison . Ordinal ) )
2016-10-20 11:56:30 -07:00
. Where ( item = >
2016-10-20 14:35:45 -07:00
item . GetMetadataWithName ( "Pack" ) . Value = = "true" & &
2016-10-20 11:56:30 -07:00
item . GetMetadataWithName ( "PackagePath" ) ! = null ) ;
contentItems . Count ( ) . Should ( ) . Be ( 1 ) ;
2017-02-22 23:16:54 -08:00
contentItems . First ( ) . Update . Should ( ) . Be ( @"path\to\some\file.cs" ) ;
2016-10-20 11:56:30 -07:00
contentItems . First ( ) . GetMetadataWithName ( "PackagePath" ) . Value . Should ( ) . BeEmpty ( ) ;
}
2016-10-20 11:24:58 -07:00
2016-10-25 15:26:04 -07:00
[Fact]
2017-02-23 20:36:02 -08:00
public void MigratingSameFileWithMultipleMappingsStringJoinsTheMappingsInPackagePath ( )
2016-10-25 15:26:04 -07:00
{
var mockProj = RunPackOptionsRuleOnPj ( @ "
{
"" packOptions "" : {
"" files "" : {
"" include "" : [ "" path / to / some / file . cs "" ] ,
"" mappings "" : {
"" other / path / file . cs "" : "" path / to / some / file . cs "" ,
"" different / path / file1 . cs "" : "" path / to / some / file . cs ""
}
}
}
} ");
var expectedPackagePath = string . Join (
2017-03-02 20:35:20 -08:00
";" ,
new [ ] {
2016-10-25 15:26:04 -07:00
Path . Combine ( "different" , "path" ) ,
Path . Combine ( "other" , "path" )
} ) ;
var contentItems = mockProj . Items
2017-02-22 22:06:45 -08:00
. Where ( item = > item . ItemType . Equals ( "None" , StringComparison . Ordinal ) )
2016-10-25 15:26:04 -07:00
. Where ( item = >
item . GetMetadataWithName ( "Pack" ) . Value = = "true" & &
item . GetMetadataWithName ( "PackagePath" ) ! = null ) ;
contentItems . Count ( ) . Should ( ) . Be ( 1 ) ;
2017-02-22 23:16:54 -08:00
contentItems . First ( ) . Update . Should ( ) . Be ( @"path\to\some\file.cs" ) ;
2016-10-25 15:26:04 -07:00
contentItems . First ( ) . GetMetadataWithName ( "PackagePath" ) . Value . Should ( ) . Be ( expectedPackagePath ) ;
}
2016-09-22 16:14:27 -07:00
private ProjectRootElement RunPackOptionsRuleOnPj ( string packOptions , string testDirectory = null )
{
testDirectory = testDirectory ? ? Temp . CreateDirectory ( ) . Path ;
return TemporaryProjectFileRuleRunner . RunRules ( new IMigrationRule [ ]
{
new MigratePackOptionsRule ( )
} , packOptions , testDirectory ) ;
}
private void EmitsOnlyAlwaysEmittedPackOptionsProperties ( ProjectRootElement project )
{
project . Properties . Count ( ) . Should ( ) . Be ( 1 ) ;
project . Properties . All ( p = > p . Name = = "PackageRequireLicenseAcceptance" ) . Should ( ) . BeTrue ( ) ;
2017-03-02 20:35:20 -08:00
}
2016-09-22 16:14:27 -07:00
}
2017-03-02 20:35:20 -08:00
}