dotnet-installer/test/dotnet-migrate.Tests/GivenThatIWantToMigrateTestApps.cs

260 lines
9.6 KiB
C#
Raw Normal View History

2016-08-22 19:24:10 +00:00
using Microsoft.Build.Construction;
using Microsoft.DotNet.ProjectJsonMigration;
using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.Tools.Test.Utilities;
using NuGet.Frameworks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
using FluentAssertions;
using System.IO;
2016-08-23 20:50:05 +00:00
using System.Runtime.InteropServices;
2016-08-22 19:24:10 +00:00
using Microsoft.DotNet.Tools.Common;
using Microsoft.DotNet.Cli;
2016-08-23 20:50:05 +00:00
using Microsoft.DotNet.Tools.Migrate;
2016-08-22 19:24:10 +00:00
using Build3Command = Microsoft.DotNet.Tools.Test.Utilities.Build3Command;
2016-09-19 20:25:40 +00:00
using BuildCommand = Microsoft.DotNet.Tools.Test.Utilities.BuildCommand;
2016-08-22 19:24:10 +00:00
namespace Microsoft.DotNet.Migration.Tests
{
public class GivenThatIWantToMigrateTestApps : TestBase
{
[Theory]
// TODO: Standalone apps [InlineData("TestAppSimple", false)]
// https://github.com/dotnet/sdk/issues/73 [InlineData("TestAppWithLibrary/TestApp", false)]
2016-08-23 20:50:05 +00:00
[InlineData("TestAppWithRuntimeOptions")]
2016-09-08 21:40:46 +00:00
[InlineData("TestAppWithContents")]
2016-08-23 20:50:05 +00:00
public void It_migrates_apps(string projectName)
2016-08-22 19:24:10 +00:00
{
var projectDirectory = TestAssetsManager.CreateTestInstance(projectName, callingMethod: "i").WithLockFiles().Path;
2016-08-23 20:50:05 +00:00
var outputComparisonData = BuildProjectJsonMigrateBuildMSBuild(projectDirectory);
2016-08-22 19:24:10 +00:00
2016-08-23 20:50:05 +00:00
var outputsIdentical =
outputComparisonData.ProjectJsonBuildOutputs.SetEquals(outputComparisonData.MSBuildBuildOutputs);
2016-08-22 19:24:10 +00:00
if (!outputsIdentical)
{
2016-08-23 20:50:05 +00:00
OutputDiagnostics(outputComparisonData);
}
outputsIdentical.Should().BeTrue();
VerifyAllMSBuildOutputsRunnable(projectDirectory);
}
2016-08-22 19:24:10 +00:00
2016-08-23 20:50:05 +00:00
[Fact]
public void It_migrates_dotnet_new_console_with_identical_outputs()
{
var projectDirectory = Temp.CreateDirectory().Path;
var outputComparisonData = GetDotnetNewComparisonData(projectDirectory, "console");
2016-08-22 19:24:10 +00:00
2016-08-23 20:50:05 +00:00
var outputsIdentical =
outputComparisonData.ProjectJsonBuildOutputs.SetEquals(outputComparisonData.MSBuildBuildOutputs);
if (!outputsIdentical)
{
OutputDiagnostics(outputComparisonData);
2016-08-22 19:24:10 +00:00
}
outputsIdentical.Should().BeTrue();
2016-08-23 20:50:05 +00:00
VerifyAllMSBuildOutputsRunnable(projectDirectory);
}
[Fact]
public void It_migrates_dotnet_new_web_with_outputs_containing_project_json_outputs()
{
var projectDirectory = Temp.CreateDirectory().Path;
var outputComparisonData = GetDotnetNewComparisonData(projectDirectory, "web");
2016-08-22 19:24:10 +00:00
var outputsIdentical =
outputComparisonData.ProjectJsonBuildOutputs.SetEquals(outputComparisonData.MSBuildBuildOutputs);
if (!outputsIdentical)
2016-08-22 19:24:10 +00:00
{
2016-08-23 20:50:05 +00:00
OutputDiagnostics(outputComparisonData);
2016-08-22 19:24:10 +00:00
}
outputsIdentical.Should().BeTrue();
2016-08-22 19:24:10 +00:00
}
2016-09-15 22:54:10 +00:00
[Theory]
// TODO: Enable this when X-Targeting is in
// [InlineData("TestLibraryWithMultipleFrameworks")]
2016-09-15 23:30:39 +00:00
public void It_migrates_projects_with_multiple_TFMs(string projectName)
2016-09-15 22:54:10 +00:00
{
var projectDirectory =
TestAssetsManager.CreateTestInstance(projectName, callingMethod: "i").WithLockFiles().Path;
var outputComparisonData = BuildProjectJsonMigrateBuildMSBuild(projectDirectory);
var outputsIdentical =
outputComparisonData.ProjectJsonBuildOutputs.SetEquals(outputComparisonData.MSBuildBuildOutputs);
if (!outputsIdentical)
{
OutputDiagnostics(outputComparisonData);
}
outputsIdentical.Should().BeTrue();
}
2016-08-22 19:24:10 +00:00
[Theory]
[InlineData("TestAppWithLibrary/TestLibrary")]
[InlineData("TestLibraryWithAnalyzer")]
[InlineData("TestLibraryWithConfiguration")]
public void It_migrates_a_library(string projectName)
{
var projectDirectory =
TestAssetsManager.CreateTestInstance(projectName, callingMethod: "i").WithLockFiles().Path;
2016-08-23 20:50:05 +00:00
var outputComparisonData = BuildProjectJsonMigrateBuildMSBuild(projectDirectory);
2016-08-22 19:24:10 +00:00
var outputsIdentical =
outputComparisonData.ProjectJsonBuildOutputs.SetEquals(outputComparisonData.MSBuildBuildOutputs);
2016-08-22 19:24:10 +00:00
if (!outputsIdentical)
2016-08-22 19:24:10 +00:00
{
2016-08-23 20:50:05 +00:00
OutputDiagnostics(outputComparisonData);
2016-08-22 19:24:10 +00:00
}
outputsIdentical.Should().BeTrue();
2016-08-22 19:24:10 +00:00
}
2016-08-23 20:50:05 +00:00
private MigratedBuildComparisonData GetDotnetNewComparisonData(string projectDirectory, string dotnetNewType)
2016-08-22 19:24:10 +00:00
{
2016-08-23 20:50:05 +00:00
DotnetNew(projectDirectory, dotnetNewType);
File.Copy("NuGet.tempaspnetpatch.config", Path.Combine(projectDirectory, "NuGet.Config"));
2016-08-23 20:50:05 +00:00
Restore(projectDirectory);
2016-08-22 19:24:10 +00:00
2016-08-23 20:50:05 +00:00
var outputComparisonData = BuildProjectJsonMigrateBuildMSBuild(projectDirectory);
return outputComparisonData;
2016-08-22 19:24:10 +00:00
}
2016-08-23 20:50:05 +00:00
private void VerifyAllMSBuildOutputsRunnable(string projectDirectory)
2016-08-22 19:24:10 +00:00
{
var dllFileName = Path.GetFileName(projectDirectory) + ".dll";
var runnableDlls = Directory.EnumerateFiles(Path.Combine(projectDirectory, "bin"), dllFileName,
SearchOption.AllDirectories);
foreach (var dll in runnableDlls)
{
new TestCommand("dotnet").ExecuteWithCapturedOutput(dll).Should().Pass();
}
}
2016-08-23 20:50:05 +00:00
private MigratedBuildComparisonData BuildProjectJsonMigrateBuildMSBuild(string projectDirectory)
{
BuildProjectJson(projectDirectory);
var projectJsonBuildOutputs = new HashSet<string>(CollectBuildOutputs(projectDirectory));
CleanBinObj(projectDirectory);
MigrateProject(projectDirectory);
Restore(projectDirectory);
BuildMSBuild(projectDirectory);
var msbuildBuildOutputs = new HashSet<string>(CollectBuildOutputs(projectDirectory));
return new MigratedBuildComparisonData(projectJsonBuildOutputs, msbuildBuildOutputs);
}
2016-08-22 19:24:10 +00:00
private IEnumerable<string> CollectBuildOutputs(string projectDirectory)
{
var fullBinPath = Path.GetFullPath(Path.Combine(projectDirectory, "bin"));
return Directory.EnumerateFiles(fullBinPath, "*", SearchOption.AllDirectories)
.Select(p => Path.GetFullPath(p).Substring(fullBinPath.Length));
}
private void CleanBinObj(string projectDirectory)
{
var dirs = new string[] { Path.Combine(projectDirectory, "bin"), Path.Combine(projectDirectory, "obj") };
foreach (var dir in dirs)
{
Directory.Delete(dir, true);
}
}
private void BuildProjectJson(string projectDirectory)
{
var projectFile = Path.Combine(projectDirectory, "project.json");
var result = new BuildCommand(projectPath: projectFile)
.ExecuteWithCapturedOutput();
result.Should().Pass();
}
private void MigrateProject(string projectDirectory)
{
2016-08-23 20:50:05 +00:00
var result =
MigrateCommand.Run(new [] { "-p", projectDirectory });
result.Should().Be(0);
}
private void DotnetNew(string projectDirectory, string dotnetNewType)
{
new NewCommand().WithWorkingDirectory(projectDirectory)
.ExecuteWithCapturedOutput($"-t {dotnetNewType}")
.Should()
.Pass();
2016-08-22 19:24:10 +00:00
}
private void Restore(string projectDirectory)
{
new TestCommand("dotnet")
.WithWorkingDirectory(projectDirectory)
.Execute("restore")
.Should()
.Pass();
}
2016-08-23 20:50:05 +00:00
private string BuildMSBuild(string projectDirectory, string configuration="Debug")
2016-08-22 19:24:10 +00:00
{
2016-09-08 21:40:46 +00:00
DeleteXproj(projectDirectory);
2016-08-22 19:24:10 +00:00
var result = new Build3Command()
.WithWorkingDirectory(projectDirectory)
2016-08-23 20:50:05 +00:00
.ExecuteWithCapturedOutput($"/p:Configuration={configuration}");
2016-08-22 19:24:10 +00:00
result
.Should()
.Pass();
return result.StdOut;
}
2016-08-23 20:50:05 +00:00
2016-09-08 21:40:46 +00:00
private void DeleteXproj(string projectDirectory)
{
var xprojFiles = Directory.EnumerateFiles(projectDirectory, "*.xproj");
foreach (var xprojFile in xprojFiles)
{
File.Delete(xprojFile);
}
}
2016-08-23 20:50:05 +00:00
private void OutputDiagnostics(MigratedBuildComparisonData comparisonData)
{
OutputDiagnostics(comparisonData.MSBuildBuildOutputs, comparisonData.ProjectJsonBuildOutputs);
}
private void OutputDiagnostics(HashSet<string> msbuildBuildOutputs, HashSet<string> projectJsonBuildOutputs)
{
Console.WriteLine("Project.json Outputs:");
Console.WriteLine(string.Join("\n", projectJsonBuildOutputs));
Console.WriteLine("");
Console.WriteLine("MSBuild Outputs:");
Console.WriteLine(string.Join("\n", msbuildBuildOutputs));
}
private class MigratedBuildComparisonData
{
public HashSet<string> ProjectJsonBuildOutputs { get; }
public HashSet<string> MSBuildBuildOutputs { get; }
public MigratedBuildComparisonData(HashSet<string> projectJsonBuildOutputs,
HashSet<string> msBuildBuildOutputs)
{
ProjectJsonBuildOutputs = projectJsonBuildOutputs;
MSBuildBuildOutputs = msBuildBuildOutputs;
}
}
2016-08-22 19:24:10 +00:00
}
}