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

428 lines
16 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-10-04 23:48:14 +00:00
using System.Runtime.Loader;
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;
CleanBinObj(projectDirectory);
var outputComparisonData = BuildProjectJsonMigrateBuildMSBuild(projectDirectory, projectName);
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-10-04 23:48:14 +00:00
public void It_migrates_signed_apps(string projectName)
{
var projectDirectory = TestAssetsManager.CreateTestInstance("TestAppWithSigning", callingMethod: "i").WithLockFiles().Path;
CleanBinObj(projectDirectory);
var outputComparisonData = BuildProjectJsonMigrateBuildMSBuild(projectDirectory, projectName);
var outputsIdentical =
outputComparisonData.ProjectJsonBuildOutputs.SetEquals(outputComparisonData.MSBuildBuildOutputs);
if (!outputsIdentical)
{
OutputDiagnostics(outputComparisonData);
}
outputsIdentical.Should().BeTrue();
VerifyAllMSBuildOutputsRunnable(projectDirectory);
VerifyAllMSBuildOutputsAreSigned(projectDirectory);
}
2016-08-23 20:50:05 +00:00
[Fact]
public void It_migrates_dotnet_new_console_with_identical_outputs()
{
2016-10-04 03:10:09 +00:00
var projectDirectory = Path.Combine(AppContext.BaseDirectory, "newconsoletest");
2016-10-04 17:13:01 +00:00
if (Directory.Exists(projectDirectory))
{
Directory.Delete(projectDirectory, true);
}
2016-10-04 03:10:09 +00:00
Directory.CreateDirectory(projectDirectory);
2016-08-23 20:50:05 +00:00
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);
}
2016-10-04 03:10:09 +00:00
[Fact(Skip="https://github.com/dotnet/cli/issues/4299")]
2016-08-23 20:50:05 +00:00
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, projectName);
2016-09-15 22:54:10 +00:00
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;
var outputComparisonData = BuildProjectJsonMigrateBuildMSBuild(projectDirectory, Path.GetFileNameWithoutExtension(projectName));
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
}
[Theory]
[InlineData("ProjectA", "ProjectA,ProjectB,ProjectC,ProjectD,ProjectE")]
[InlineData("ProjectB", "ProjectB,ProjectC,ProjectD,ProjectE")]
[InlineData("ProjectC", "ProjectC,ProjectD,ProjectE")]
[InlineData("ProjectD", "ProjectD")]
[InlineData("ProjectE", "ProjectE")]
public void It_migrates_root_project_and_references(string projectName, string expectedProjects)
{
var projectDirectory =
TestAssetsManager.CreateTestInstance("TestAppDependencyGraph", callingMethod: $"{projectName}.RefsTest").Path;
FixUpProjectJsons(projectDirectory);
MigrateProject(Path.Combine(projectDirectory, projectName));
string[] migratedProjects = expectedProjects.Split(new char[] { ',' });
VerifyMigration(migratedProjects, projectDirectory);
}
[Theory]
[InlineData("ProjectA")]
[InlineData("ProjectB")]
[InlineData("ProjectC")]
[InlineData("ProjectD")]
[InlineData("ProjectE")]
public void It_migrates_root_project_and_skips_references(string projectName)
{
var projectDirectory =
TestAssetsManager.CreateTestInstance("TestAppDependencyGraph", callingMethod: $"{projectName}.SkipRefsTest").Path;
FixUpProjectJsons(projectDirectory);
MigrateCommand.Run(new [] { Path.Combine(projectDirectory, projectName), "--skip-project-references" }).Should().Be(0);
VerifyMigration(Enumerable.Repeat(projectName, 1), projectDirectory);
}
2016-09-26 22:30:51 +00:00
[Theory]
[InlineData(true)]
[InlineData(false)]
public void It_migrates_all_projects_in_given_directory(bool skipRefs)
{
2016-09-26 22:30:51 +00:00
var projectDirectory = TestAssetsManager.CreateTestInstance("TestAppDependencyGraph", callingMethod: $"MigrateDirectory.SkipRefs.{skipRefs}").Path;
FixUpProjectJsons(projectDirectory);
2016-09-26 22:30:51 +00:00
if (skipRefs)
{
MigrateCommand.Run(new [] { projectDirectory, "--skip-project-references" }).Should().Be(0);
}
else
{
MigrateCommand.Run(new [] { projectDirectory }).Should().Be(0);
}
2016-09-26 22:30:51 +00:00
string[] migratedProjects = new string[] { "ProjectA", "ProjectB", "ProjectC", "ProjectD", "ProjectE", "ProjectF", "ProjectG" };
VerifyMigration(migratedProjects, projectDirectory);
}
[Fact]
public void It_migrates_given_project_json()
{
var projectDirectory = TestAssetsManager.CreateTestInstance("TestAppDependencyGraph").Path;
FixUpProjectJsons(projectDirectory);
var project = Path.Combine(projectDirectory, "ProjectA", "project.json");
MigrateCommand.Run(new [] { project }).Should().Be(0);
string[] migratedProjects = new string[] { "ProjectA", "ProjectB", "ProjectC", "ProjectD", "ProjectE" };
VerifyMigration(migratedProjects, projectDirectory);
}
private void FixUpProjectJsons(string projectDirectory)
{
var pjs = Directory.EnumerateFiles(projectDirectory, "project.json.1", SearchOption.AllDirectories);
foreach(var pj in pjs)
{
var newPj = pj.Replace("project.json.1", "project.json");
File.Move(pj, newPj);
}
}
private void VerifyMigration(IEnumerable<string> expectedProjects, string rootDir)
{
var migratedProjects = Directory.EnumerateFiles(rootDir, "*.csproj", SearchOption.AllDirectories)
.Select(s => Path.GetFileNameWithoutExtension(s));
migratedProjects.Should().BeEquivalentTo(expectedProjects);
}
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
var outputComparisonData =
2016-10-04 03:10:09 +00:00
BuildProjectJsonMigrateBuildMSBuild(projectDirectory);
2016-08-23 20:50:05 +00:00
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-10-04 23:48:14 +00:00
private void VerifyAllMSBuildOutputsAreSigned(string projectDirectory)
{
var dllFileName = Path.GetFileName(projectDirectory) + ".dll";
var runnableDlls = Directory.EnumerateFiles(Path.Combine(projectDirectory, "bin"), dllFileName,
SearchOption.AllDirectories);
foreach (var dll in runnableDlls)
{
var assemblyName = AssemblyLoadContext.GetAssemblyName(dll);
var token = assemblyName.GetPublicKeyToken();
token.Should().NotBeNullOrEmpty();
}
}
2016-10-04 03:10:09 +00:00
private MigratedBuildComparisonData BuildProjectJsonMigrateBuildMSBuild(string projectDirectory, string projectName=null)
2016-08-23 20:50:05 +00:00
{
BuildProjectJson(projectDirectory);
var projectJsonBuildOutputs = new HashSet<string>(CollectBuildOutputs(projectDirectory));
CleanBinObj(projectDirectory);
2016-09-22 04:23:50 +00:00
// Remove lock file for migration
File.Delete(Path.Combine(projectDirectory, "project.lock.json"));
2016-08-23 20:50:05 +00:00
MigrateProject(projectDirectory);
2016-09-27 04:40:11 +00:00
DeleteXproj(projectDirectory);
2016-10-04 03:10:09 +00:00
Restore3(projectDirectory, projectName);
BuildMSBuild(projectDirectory, projectName);
2016-08-23 20:50:05 +00:00
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)
{
if(Directory.Exists(dir))
{
Directory.Delete(dir, true);
}
2016-08-22 19:24:10 +00:00
}
}
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 [] { projectDirectory });
2016-08-23 20:50:05 +00:00
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-10-04 03:10:09 +00:00
private void Restore3(string projectDirectory, string projectName=null)
{
2016-10-04 03:10:09 +00:00
var command = new Restore3Command()
.WithWorkingDirectory(projectDirectory);
if (projectName != null)
{
command.Execute($"{projectName}.csproj")
.Should()
.Pass();
2016-10-04 03:10:09 +00:00
}
else
{
command.Execute()
.Should()
.Pass();
}
}
private string BuildMSBuild(string projectDirectory, string projectName, string configuration="Debug")
2016-08-22 19:24:10 +00:00
{
2016-10-04 03:10:09 +00:00
if (projectName != null)
{
projectName = projectName + ".csproj";
}
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-10-04 03:10:09 +00:00
.ExecuteWithCapturedOutput($"{projectName} /p:Configuration={configuration}");
2016-08-23 20:50:05 +00:00
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
}
}