dotnet-installer/test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigratePublishOptions.cs
Piotr Puszkiewicz a07ebe2e7b Merge remote-tracking branch 'refs/remotes/dotnet/master'
Conflicts:
	TestAssets/DesktopTestProjects/AppWithProjTool2Fx/App.csproj
	TestAssets/NonRestoredTestProjects/AppThrowingException/App/AppThrowingException.csproj
	TestAssets/NonRestoredTestProjects/AppThrowingException/AppDependingOnOtherAsTool/AppDependingOnOtherAsTool.csproj
	TestAssets/NonRestoredTestProjects/DotnetAddP2PProjects/EmptyItemGroup/EmptyItemGroup.csproj
	TestAssets/NonRestoredTestProjects/DotnetAddP2PProjects/Lib/Lib.csproj
	TestAssets/NonRestoredTestProjects/DotnetAddP2PProjects/MoreThanOne/a.csproj
	TestAssets/NonRestoredTestProjects/DotnetAddP2PProjects/MoreThanOne/b.csproj
	TestAssets/NonRestoredTestProjects/DotnetAddP2PProjects/Net452AndNetCoreApp10Lib/Net452AndNetCoreApp10Lib.csproj
	TestAssets/NonRestoredTestProjects/DotnetAddP2PProjects/Net45Lib/Net45Lib.csproj
	TestAssets/NonRestoredTestProjects/DotnetAddP2PProjects/ValidRef/ValidRef.csproj
	TestAssets/NonRestoredTestProjects/DotnetAddP2PProjects/WithDoubledRef/WithDoubledRef.csproj
	TestAssets/NonRestoredTestProjects/DotnetAddP2PProjects/WithExistingRefCondOnItem/WithExistingRefCondOnItem.csproj
	TestAssets/NonRestoredTestProjects/DotnetAddP2PProjects/WithExistingRefCondWhitespaces/WithExistingRefCondWhitespaces.csproj
	TestAssets/NonRestoredTestProjects/DotnetAddP2PProjects/WithRefCondNonUniform/WithRefCondNonUniform.csproj
	TestAssets/NonRestoredTestProjects/DotnetAddP2PProjects/WithRefNoCondNonUniform/WithRefNoCondNonUniform.csproj
	TestAssets/NonRestoredTestProjects/ToolWithRandomPackageName/AppWithDepOnTool/AppWithDepOnTool.csproj
	TestAssets/TestPackages/dotnet-dependency-context-test/dotnet-dependency-context-test.csproj
	TestAssets/TestPackages/dotnet-dependency-tool-invoker/dotnet-dependency-tool-invoker.csproj
	TestAssets/TestPackages/dotnet-portable/dotnet-portable.csproj
	TestAssets/TestProjects/AppWithDepOnToolWithOutputName/AppWithDepOnToolWithOutputName.csproj
	TestAssets/TestProjects/AppWithDirectDepWithOutputName/AppWithDirectDepWithOutputName.csproj
	TestAssets/TestProjects/AppWithMultipleFxAndTools/MSBuildAppWithMultipleFrameworksAndTools.csproj
	TestAssets/TestProjects/AppWithToolDependency/AppWithToolDependency.csproj
	TestAssets/TestProjects/DependencyContextFromTool/DependencyContextFromTool.csproj
	TestAssets/TestProjects/MSBuildAppWithMultipleFrameworks/MSBuildAppWithMultipleFrameworks.csproj
	TestAssets/TestProjects/MSBuildAppWithMultipleFrameworksAndTools/MSBuildAppWithMultipleFrameworksAndTools.csproj
	TestAssets/TestProjects/TestAppSimple/TestAppSimple.csproj
	TestAssets/TestProjects/TestAppWithProjDepTool/TestAppWithProjDepTool.csproj
	TestAssets/TestProjects/VSTestDesktopAndNetCore/VSTestDesktopAndNetCore.csproj
	TestAssets/TestProjects/VSTestDotNetCore/VSTestDotNetCore.csproj
	TestAssets/TestProjects/VSTestXunitDesktopAndNetCore/VSTestXunitDesktopAndNetCore.csproj
	TestAssets/TestProjects/VSTestXunitDotNetCore/VSTestXunitDotNetCore.csproj
	build_projects/Microsoft.DotNet.Cli.Build.Framework/Microsoft.DotNet.Cli.Build.Framework.csproj
	build_projects/dotnet-cli-build/dotnet-cli-build.csproj
	build_projects/shared-build-targets-utils/shared-build-targets-utils.csproj
	test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigrateConfigurations.cs
	test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigrateTFMs.cs
2017-03-02 23:49:01 -08:00

353 lines
16 KiB
C#

// 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;
using System.IO;
using System.Linq;
using Microsoft.Build.Construction;
using Microsoft.DotNet.Internal.ProjectModel;
using Microsoft.DotNet.Internal.ProjectModel.Files;
using Microsoft.DotNet.TestFramework;
using Microsoft.DotNet.Tools.Test.Utilities;
using NuGet.Frameworks;
using Xunit;
using FluentAssertions;
using Microsoft.DotNet.ProjectJsonMigration.Rules;
namespace Microsoft.DotNet.ProjectJsonMigration.Tests
{
public class GivenThatIWantToMigratePublishOptions : TestBase
{
[Fact]
private void MigratingPublishOptionsForConsoleAppIncludeExcludePopulatesContentItemWithInclude()
{
var testDirectory = Temp.CreateDirectory().Path;
WriteFilesInProjectDirectory(testDirectory);
var mockProj = RunPublishOptionsRuleOnPj(@"
{
""publishOptions"": {
""include"": [""root"", ""src"", ""rootfile.cs""],
""exclude"": [""src"", ""rootfile.cs""],
""includeFiles"": [""src/file1.cs"", ""src/file2.cs""],
""excludeFiles"": [""src/file2.cs""]
}
}",
testDirectory: testDirectory);
mockProj.Items.Count(i => i.ItemType.Equals("None", StringComparison.Ordinal)).Should().Be(4);
foreach (var item in mockProj.Items.Where(i => i.ItemType.Equals("None", StringComparison.Ordinal)))
{
item.Metadata.Count(m => m.Name == "CopyToPublishDirectory").Should().Be(1);
if (item.Update.Contains(@"src\file1.cs"))
{
item.Update.Should().Be(@"src\file1.cs");
item.Exclude.Should().BeEmpty();
item.Metadata.Count(m =>
m.Name == "CopyToPublishDirectory" && m.Value == "PreserveNewest").Should().Be(1);
}
else if (item.Update.Contains(@"src\file2.cs"))
{
item.Update.Should().Be(@"src\file2.cs");
item.Exclude.Should().BeEmpty();
item.Metadata.Count(m =>
m.Name == "CopyToPublishDirectory" && m.Value == "Never").Should().Be(1);
}
else if (item.Update.Contains(@"root\**\*"))
{
item.Update.Should().Be(@"root\**\*");
item.Exclude.Should().BeEmpty();
item.Metadata.Count(m =>
m.Name == "CopyToPublishDirectory" && m.Value == "PreserveNewest").Should().Be(1);
}
else
{
item.Update.Should().Be(@"src\**\*;rootfile.cs");
item.Exclude.Should().BeEmpty();
item.Metadata.Count(m =>
m.Name == "CopyToPublishDirectory" && m.Value == "Never").Should().Be(1);
}
}
}
[Fact]
private void MigratingPublishOptionsForWebAppIncludeExcludePopulatesContentItemWithUpdate()
{
var testDirectory = Temp.CreateDirectory().Path;
WriteFilesInProjectDirectory(testDirectory);
var mockProj = RunPublishOptionsRuleOnPj(@"
{
""publishOptions"": {
""include"": [""root"", ""src"", ""rootfile.cs""],
""exclude"": [""src"", ""rootfile.cs""],
""includeFiles"": [""src/file1.cs"", ""src/file2.cs""],
""excludeFiles"": [""src/file2.cs""]
},
""buildOptions"": {
""emitEntryPoint"": true
},
""dependencies"": {
""Microsoft.AspNetCore.Mvc"" : {
""version"": ""1.0.0""
}
},
""frameworks"": {
""netcoreapp1.0"": {}
}
}",
testDirectory: testDirectory);
mockProj.Items.Count(i => i.ItemType.Equals("None", StringComparison.Ordinal)).Should().Be(4);
foreach (var item in mockProj.Items.Where(i => i.ItemType.Equals("None", StringComparison.Ordinal)))
{
item.Metadata.Count(m => m.Name == "CopyToPublishDirectory").Should().Be(1);
if (item.Update.Contains(@"src\file1.cs"))
{
item.Update.Should().Be(@"src\file1.cs");
item.Exclude.Should().BeEmpty();
item.Metadata.Count(m =>
m.Name == "CopyToPublishDirectory" && m.Value == "PreserveNewest").Should().Be(1);
}
else if (item.Update.Contains(@"src\file2.cs"))
{
item.Update.Should().Be(@"src\file2.cs");
item.Exclude.Should().BeEmpty();
item.Metadata.Count(m =>
m.Name == "CopyToPublishDirectory" && m.Value == "Never").Should().Be(1);
}
else if (item.Update.Contains(@"root\**\*"))
{
item.Update.Should().Be(@"root\**\*");
item.Exclude.Should().BeEmpty();
item.Metadata.Count(m =>
m.Name == "CopyToPublishDirectory" && m.Value == "PreserveNewest").Should().Be(1);
}
else
{
item.Update.Should().Be(@"src\**\*;rootfile.cs");
item.Exclude.Should().BeEmpty();
item.Metadata.Count(m =>
m.Name == "CopyToPublishDirectory" && m.Value == "Never").Should().Be(1);
}
}
}
[Fact]
private void MigratingConsoleAppWithPublishOptionsAndBuildOptionsCopyToOutputMergesContentItemsWithInclude()
{
var testDirectory = Temp.CreateDirectory().Path;
WriteFilesInProjectDirectory(testDirectory);
var mockProj = RunPublishAndBuildOptionsRuleOnPj(@"
{
""buildOptions"": {
""copyToOutput"": {
""include"": [""src"", ""rootfile.cs""],
""exclude"": [""src"", ""rootfile.cs""],
""includeFiles"": [""src/file1.cs"", ""src/file2.cs""],
""excludeFiles"": [""src/file2.cs""]
}
},
""publishOptions"": {
""include"": [""root"", ""src"", ""rootfile.cs""],
""exclude"": [""src"", ""rootfile.cs""],
""includeFiles"": [""src/file1.cs"", ""src/file2.cs""],
""excludeFiles"": [""src/file3.cs""]
}
}",
testDirectory: testDirectory);
mockProj.Items.Count(i => i.ItemType.Equals("None", StringComparison.Ordinal)).Should().Be(5);
foreach (var item in mockProj.Items.Where(i => i.ItemType.Equals("None", StringComparison.Ordinal)))
{
if (item.Update.Contains(@"root\**\*"))
{
item.Update.Should().Be(@"root\**\*");
item.Metadata.Count(m =>
m.Name == "CopyToPublishDirectory" && m.Value == "PreserveNewest").Should().Be(1);
}
else if (item.Update.Contains(@"src\file1.cs"))
{
item.Update.Should().Be(@"src\file1.cs");
item.Metadata.Count(m =>
m.Name == "CopyToOutputDirectory" && m.Value == "PreserveNewest").Should().Be(1);
item.Metadata.Count(m =>
m.Name == "CopyToPublishDirectory" && m.Value == "PreserveNewest").Should().Be(1);
}
else if (item.Update.Contains(@"src\file2.cs"))
{
item.Update.Should().Be(@"src\file2.cs");
item.Metadata.Count(m =>
m.Name == "CopyToOutputDirectory" && m.Value == "Never").Should().Be(1);
item.Metadata.Count(m =>
m.Name == "CopyToPublishDirectory" && m.Value == "PreserveNewest").Should().Be(1);
}
else if (item.Update.Contains(@"src\file3.cs"))
{
item.Update.Should().Be(@"src\file3.cs");
item.Metadata.Count(m =>
m.Name == "CopyToPublishDirectory" && m.Value == "Never").Should().Be(1);
}
else
{
item.Update.Should()
.Be(@"src\**\*;rootfile.cs");
item.Metadata.Count(m =>
m.Name == "CopyToOutputDirectory" && m.Value == "Never").Should().Be(1);
item.Metadata.Count(m =>
m.Name == "CopyToPublishDirectory" && m.Value == "Never").Should().Be(1);
}
}
}
[Fact]
private void MigratingWebAppWithPublishOptionsAndBuildOptionsCopyToOutputMergesContentItemsWithUpdate()
{
var testDirectory = Temp.CreateDirectory().Path;
WriteFilesInProjectDirectory(testDirectory);
var mockProj = RunPublishAndBuildOptionsRuleOnPj(@"
{
""buildOptions"": {
""copyToOutput"": {
""include"": [""src"", ""rootfile.cs""],
""exclude"": [""src"", ""rootfile.cs""],
""includeFiles"": [""src/file1.cs"", ""src/file2.cs""],
""excludeFiles"": [""src/file2.cs""]
},
""emitEntryPoint"": true
},
""publishOptions"": {
""include"": [""root"", ""src"", ""rootfile.cs""],
""exclude"": [""src"", ""rootfile.cs""],
""includeFiles"": [""src/file1.cs"", ""src/file2.cs""],
""excludeFiles"": [""src/file3.cs""]
},
""dependencies"": {
""Microsoft.AspNetCore.Mvc"" : {
""version"": ""1.0.0""
}
},
""frameworks"": {
""netcoreapp1.0"": {}
}
}",
testDirectory: testDirectory);
mockProj.Items.Count(i => i.ItemType.Equals("None", StringComparison.Ordinal)).Should().Be(5);
// From ProjectReader #L725 (Both are empty)
var defaultIncludePatterns = Enumerable.Empty<string>();
var defaultExcludePatterns = Enumerable.Empty<string>();
foreach (var item in mockProj.Items.Where(i => i.ItemType.Equals("None", StringComparison.Ordinal)))
{
var metadata = string.Join(",", item.Metadata.Select(m => m.Name));
if (item.Update.Contains(@"root\**\*"))
{
item.Update.Should().Be(@"root\**\*");
item.Exclude.Should().BeEmpty();
item.Metadata.Count(m =>
m.Name == "CopyToPublishDirectory" && m.Value == "PreserveNewest").Should().Be(1);
}
else if (item.Update.Contains(@"src\file1.cs"))
{
item.Update.Should().Be(@"src\file1.cs");
item.Exclude.Should().BeEmpty();
item.Metadata.Count(m =>
m.Name == "CopyToOutputDirectory" && m.Value == "PreserveNewest").Should().Be(1);
item.Metadata.Count(m =>
m.Name == "CopyToPublishDirectory" && m.Value == "PreserveNewest").Should().Be(1);
}
else if (item.Update.Contains(@"src\file2.cs"))
{
item.Update.Should().Be(@"src\file2.cs");
item.Exclude.Should().BeEmpty();
item.Metadata.Count(m =>
m.Name == "CopyToOutputDirectory" && m.Value == "Never").Should().Be(1);
item.Metadata.Count(m =>
m.Name == "CopyToPublishDirectory" && m.Value == "PreserveNewest").Should().Be(1);
}
else if (item.Update.Contains(@"src\file3.cs"))
{
item.Update.Should().Be(@"src\file3.cs");
item.Exclude.Should().BeEmpty();
item.Metadata.Count(m =>
m.Name == "CopyToPublishDirectory" && m.Value == "Never").Should().Be(1);
}
else
{
item.Update.Should().Be(@"src\**\*;rootfile.cs");
item.Exclude.Should().BeEmpty();
item.Metadata.Count(m =>
m.Name == "CopyToOutputDirectory" && m.Value == "Never").Should().Be(1);
item.Metadata.Count(m =>
m.Name == "CopyToPublishDirectory" && m.Value == "Never").Should().Be(1);
}
}
}
[Fact]
public void ExcludedPatternsAreNotEmittedOnNoneWhenBuildingAWebProject()
{
var mockProj = RunPublishOptionsRuleOnPj(@"
{
""buildOptions"": {
""emitEntryPoint"": true
},
""publishOptions"": {
""include"": [""wwwroot"", ""**/*.cshtml"", ""appsettings.json"", ""web.config""],
},
""dependencies"": {
""Microsoft.AspNetCore.Mvc"" : {
""version"": ""1.0.0""
}
},
""frameworks"": {
""netcoreapp1.0"": {}
}
}");
mockProj.Items.Count(i => i.ItemType.Equals("None", StringComparison.Ordinal)).Should().Be(0);
}
private void WriteFilesInProjectDirectory(string testDirectory)
{
Directory.CreateDirectory(Path.Combine(testDirectory, "root"));
Directory.CreateDirectory(Path.Combine(testDirectory, "src"));
File.WriteAllText(Path.Combine(testDirectory, "root", "file1.txt"), "content");
File.WriteAllText(Path.Combine(testDirectory, "root", "file2.txt"), "content");
File.WriteAllText(Path.Combine(testDirectory, "root", "file3.txt"), "content");
File.WriteAllText(Path.Combine(testDirectory, "src", "file1.cs"), "content");
File.WriteAllText(Path.Combine(testDirectory, "src", "file2.cs"), "content");
File.WriteAllText(Path.Combine(testDirectory, "src", "file3.cs"), "content");
File.WriteAllText(Path.Combine(testDirectory, "rootfile.cs"), "content");
}
private ProjectRootElement RunPublishOptionsRuleOnPj(string s, string testDirectory = null)
{
testDirectory = testDirectory ?? Temp.CreateDirectory().Path;
return TemporaryProjectFileRuleRunner.RunRules(new IMigrationRule[]
{
new MigratePublishOptionsRule()
}, s, testDirectory);
}
private ProjectRootElement RunPublishAndBuildOptionsRuleOnPj(string s, string testDirectory = null)
{
testDirectory = testDirectory ?? Temp.CreateDirectory().Path;
return TemporaryProjectFileRuleRunner.RunRules(new IMigrationRule[]
{
new MigrateBuildOptionsRule(),
new MigratePublishOptionsRule()
}, s, testDirectory);
}
}
}