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

1011 lines
36 KiB
C#
Raw Normal View History

// 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.TestFramework;
Create backup folder in the directory where 'dotnet migrate' is executed (#5306) * Create backup folder in the directory where 'dotnet migrate' is executed With this change, 'dotnet migrate' will create the backup folder in the workspace directory rather than the parent of the workspace directory. This solves two problems: 1. It makes it easier for the user where the backup is -- it's in the directory they targeted with 'dotnet migrate'. 2. It solves a problem of file oollisions with global.json files when migrating multiple projects. Consider the following directory structure: root | project1 | global.json | src | project1 project2 | global.json | src | project2 Prior to this change, running 'dotnet migrate' project1 and then running it again in project2 would have caused an exception to be thrown because the migration would try to produce a backup folder like so: root | backup | | | global.json | | | project1 | | | project2 | | project1 | src | project1 project2 | src | project2 Now, we produce the following structure, which has no collisions: root | project1 | backup | | | global.json | | | project1 | src | project1 | project2 | backup | | | global.json | | | project2 | src | project2 In addition, to help avoid further collisions, a number is appened to the backup folder's name if it already exists. So, if the user runs dotnet migrate again for some reason, they'll see backup_1, backup_2, etc. * Fix test helper * Fix foolish bug causing infinite loop * Fix up a couple more tests * Rework MigrationBackupPlan to process all projects at once * Fix up tests * Still fixing tests * Compute common root folder of projects to determine where backup folder should be placed * Fix typo * Fix test to not look in backup folder now that it's in a better location
2017-01-21 09:58:28 +00:00
using Microsoft.DotNet.Tools.Common;
2016-08-22 19:24:10 +00:00
using Microsoft.DotNet.Tools.Test.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;
using FluentAssertions;
using System.IO;
2016-08-23 20:50:05 +00:00
using Microsoft.DotNet.Tools.Migrate;
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-10-20 22:04:53 +00:00
using Newtonsoft.Json.Linq;
2016-08-22 19:24:10 +00:00
using MigrateCommand = Microsoft.DotNet.Tools.Migrate.MigrateCommand;
2016-08-22 19:24:10 +00:00
namespace Microsoft.DotNet.Migration.Tests
{
public class GivenThatIWantToMigrateTestApps : TestBase
{
[Theory]
2016-08-23 20:50:05 +00:00
[InlineData("TestAppWithRuntimeOptions")]
2016-09-08 21:40:46 +00:00
[InlineData("TestAppWithContents")]
[InlineData("AppWithAssemblyInfo")]
[InlineData("TestAppWithEmbeddedResources")]
public void ItMigratesApps(string projectName)
2016-08-22 19:24:10 +00:00
{
var projectDirectory = TestAssets
.GetProjectJson(projectName)
.CreateInstance(identifier: projectName)
.WithSourceFiles()
.WithRestoreFiles()
.WithEmptyGlobalJson()
.Root;
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);
}
2016-08-23 20:50:05 +00:00
outputsIdentical.Should().BeTrue();
2017-01-06 06:55:43 +00:00
VerifyAllMSBuildOutputsRunnable(projectDirectory);
var outputCsProj = projectDirectory.GetFile(projectName + ".csproj");
outputCsProj.ReadAllText()
.Should().EndWith("\n");
2017-01-06 06:55:43 +00:00
}
[WindowsOnlyTheory]
[InlineData("TestAppMultipleFrameworksNoRuntimes", null)]
2017-01-06 06:55:43 +00:00
[InlineData("TestAppWithMultipleFullFrameworksOnly", "net461")]
public void ItMigratesAppsWithFullFramework(string projectName, string framework)
{
var projectDirectory = TestAssets
.GetProjectJson(projectName)
.CreateInstance(identifier: projectName)
.WithSourceFiles()
.WithRestoreFiles()
.WithEmptyGlobalJson()
.Root;
2017-01-06 06:55:43 +00:00
CleanBinObj(projectDirectory);
MigrateProject(new [] { projectDirectory.FullName });
2017-01-06 06:55:43 +00:00
Restore(projectDirectory);
BuildMSBuild(projectDirectory, projectName, framework: framework);
2016-08-23 20:50:05 +00:00
}
2016-08-22 19:24:10 +00:00
[Fact]
public void ItMigratesSignedApps()
2016-10-04 23:48:14 +00:00
{
var projectDirectory = TestAssets
.GetProjectJson("TestAppWithSigning")
.CreateInstance()
.WithSourceFiles()
.WithRestoreFiles()
.WithEmptyGlobalJson()
.Root;
2016-10-04 23:48:14 +00:00
CleanBinObj(projectDirectory);
var outputComparisonData = BuildProjectJsonMigrateBuildMSBuild(projectDirectory, "TestAppWithSigning");
2016-10-04 23:48:14 +00:00
var outputsIdentical =
outputComparisonData.ProjectJsonBuildOutputs.SetEquals(outputComparisonData.MSBuildBuildOutputs);
2016-10-04 23:48:14 +00:00
if (!outputsIdentical)
{
OutputDiagnostics(outputComparisonData);
}
2016-10-04 23:48:14 +00:00
outputsIdentical.Should().BeTrue();
2016-10-04 23:48:14 +00:00
VerifyAllMSBuildOutputsRunnable(projectDirectory);
2016-10-04 23:48:14 +00:00
VerifyAllMSBuildOutputsAreSigned(projectDirectory);
}
2016-08-23 20:50:05 +00:00
[Fact]
public void ItMigratesDotnetNewConsoleWithIdenticalOutputs()
2016-08-23 20:50:05 +00:00
{
var projectDirectory = TestAssets
.GetProjectJson("ProjectJsonConsoleTemplate")
.CreateInstance()
.WithSourceFiles()
.WithEmptyGlobalJson()
.Root;
2016-10-04 03:10:09 +00:00
var outputComparisonData = GetComparisonData(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-23 20:50:05 +00:00
if (!outputsIdentical)
{
OutputDiagnostics(outputComparisonData);
2016-08-22 19:24:10 +00:00
}
2016-08-22 19:24:10 +00:00
outputsIdentical.Should().BeTrue();
2016-08-23 20:50:05 +00:00
VerifyAllMSBuildOutputsRunnable(projectDirectory);
}
[Fact]
public void ItMigratesOldDotnetNewWebWithoutToolsWithOutputsContainingProjectJsonOutputs()
2016-08-23 20:50:05 +00:00
{
var projectDirectory = TestAssets
.GetProjectJson("ProjectJsonWebTemplate")
.CreateInstance()
.WithSourceFiles()
.WithRestoreFiles()
.WithEmptyGlobalJson()
.Root;
var globalDirectory = projectDirectory.Parent;
2016-10-20 22:04:53 +00:00
WriteGlobalJson(globalDirectory);
2016-10-20 22:04:53 +00:00
var outputComparisonData = GetComparisonData(projectDirectory);
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
}
[Fact]
public void ItMigratesAndPublishesWebApp()
{
var projectName = "WebAppWithMissingFileInPublishOptions";
var projectDirectory = TestAssets
.GetProjectJson(projectName)
.CreateInstance()
.WithSourceFiles()
.WithEmptyGlobalJson()
.Root;
File.Copy("NuGet.tempaspnetpatch.config", projectDirectory.GetFile("NuGet.Config").FullName);
MigrateProject(new [] { projectDirectory.FullName });
Restore(projectDirectory);
PublishMSBuild(projectDirectory, projectName);
}
[Fact]
public void ItMigratesAPackageReferenceAsSuchEvenIfAFolderWithTheSameNameExistsInTheRepo()
{
var solutionDirectory = TestAssets
.GetProjectJson("AppWithPackageNamedAfterFolder")
.CreateInstance()
.WithSourceFiles()
.WithEmptyGlobalJson()
.Root;
var appProject = solutionDirectory
.GetDirectory("src", "App")
.GetFile("App.csproj");
MigrateProject(solutionDirectory.FullName);
var projectRootElement = ProjectRootElement.Open(appProject.FullName);
projectRootElement.Items.Where(
i => i.Include == "EntityFramework" && i.ItemType == "PackageReference")
.Should().HaveCount(2);
}
[Fact]
public void ItMigratesAProjectThatDependsOnAMigratedProjectWithTheSkipProjectReferenceFlag()
{
const string dependentProject = "ProjectA";
const string dependencyProject = "ProjectB";
var projectDirectory = TestAssets
.GetProjectJson("TestAppDependencyGraph")
.CreateInstance()
.WithSourceFiles()
.WithEmptyGlobalJson()
.Root;
MigrateProject(projectDirectory.GetDirectory(dependencyProject).FullName);
MigrateProject("--skip-project-references", projectDirectory.GetDirectory(dependentProject).FullName);
}
[Fact]
public void ItAddsMicrosoftNetWebSdkToTheSdkAttributeOfAWebApp()
{
var projectDirectory = TestAssets
.Get("ProjectJsonWebTemplate")
.CreateInstance()
.WithSourceFiles()
.WithRestoreFiles()
.WithEmptyGlobalJson()
.Root;
var globalDirectory = projectDirectory.Parent;
var projectJsonFile = projectDirectory.GetFile("project.json");
MigrateProject(new [] { projectDirectory.FullName });
var csProj = projectDirectory.GetFile($"{projectDirectory.Name}.csproj");
csProj.ReadAllText().Should().Contain(@"Sdk=""Microsoft.NET.Sdk.Web""");
}
2016-09-15 22:54:10 +00:00
[Theory]
[InlineData("TestLibraryWithTwoFrameworks")]
public void ItMigratesProjectsWithMultipleTFMs(string projectName)
2016-09-15 22:54:10 +00:00
{
var projectDirectory = TestAssets
.GetProjectJson(projectName)
.CreateInstance(identifier: projectName)
.WithSourceFiles()
.WithRestoreFiles()
.WithEmptyGlobalJson()
.Root;
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();
}
[WindowsOnlyFact]
public void ItMigratesLibraryWithMultipleTFMsAndFullFramework()
{
var projectName = "PJLibWithMultipleFrameworks";
var projectDirectory = TestAssets
.GetProjectJson(projectName)
.CreateInstance(identifier: projectName)
.WithSourceFiles()
.WithRestoreFiles()
.WithEmptyGlobalJson()
.Root;
var outputComparisonData = BuildProjectJsonMigrateBuildMSBuild(projectDirectory, projectName);
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("PJTestLibraryWithConfiguration")]
public void ItMigratesALibrary(string projectName)
2016-08-22 19:24:10 +00:00
{
var projectDirectory = TestAssets
.GetProjectJson(projectName)
.CreateInstance(identifier: projectName)
.WithSourceFiles()
.WithRestoreFiles()
.WithEmptyGlobalJson()
.Root;
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 ItMigratesRootProjectAndReferences(string projectName, string expectedProjects)
{
var projectDirectory = TestAssets
.GetProjectJson("TestAppDependencyGraph")
.CreateInstance(identifier: $"{projectName}.RefsTest")
.WithSourceFiles()
.WithEmptyGlobalJson()
.Root;
MigrateProject(new [] { projectDirectory.GetDirectory(projectName).FullName });
string[] migratedProjects = expectedProjects.Split(new char[] { ',' });
VerifyMigration(migratedProjects, projectDirectory);
}
[Theory]
[InlineData("ProjectA")]
[InlineData("ProjectB")]
[InlineData("ProjectC")]
[InlineData("ProjectD")]
[InlineData("ProjectE")]
public void ItMigratesRootProjectAndSkipsReferences(string projectName)
{
var projectDirectory = TestAssets
.GetProjectJson("TestAppDependencyGraph")
.CreateInstance($"{projectName}.SkipRefsTest")
.WithSourceFiles()
.WithEmptyGlobalJson()
.Root;
MigrateProject(new [] { projectDirectory.GetDirectory(projectName).FullName, "--skip-project-references" });
VerifyMigration(Enumerable.Repeat(projectName, 1), projectDirectory);
}
2016-09-26 22:30:51 +00:00
[Theory]
[InlineData(true)]
[InlineData(false)]
public void ItMigratesAllProjectsInGivenDirectory(bool skipRefs)
{
var projectDirectory = TestAssets
.GetProjectJson("TestAppDependencyGraph")
.CreateInstance(callingMethod: $"MigrateDirectory.SkipRefs.{skipRefs}")
.WithSourceFiles()
.WithEmptyGlobalJson()
.Root;
2016-09-26 22:30:51 +00:00
if (skipRefs)
{
MigrateProject(new [] { projectDirectory.FullName, "--skip-project-references" });
2016-09-26 22:30:51 +00:00
}
else
{
MigrateProject(new [] { projectDirectory.FullName });
2016-09-26 22:30:51 +00:00
}
string[] migratedProjects = new string[] { "ProjectA", "ProjectB", "ProjectC", "ProjectD", "ProjectE", "ProjectF", "ProjectG", "ProjectH", "ProjectI", "ProjectJ" };
VerifyMigration(migratedProjects, projectDirectory);
}
[Fact]
public void ItMigratesGivenProjectJson()
{
var projectDirectory = TestAssets
.GetProjectJson("TestAppDependencyGraph")
.CreateInstance()
.WithSourceFiles()
.WithEmptyGlobalJson()
.Root;
var project = projectDirectory
.GetDirectory("ProjectA")
.GetFile("project.json");
MigrateProject(new [] { project.FullName });
string[] migratedProjects = new string[] { "ProjectA", "ProjectB", "ProjectC", "ProjectD", "ProjectE" };
VerifyMigration(migratedProjects, projectDirectory);
}
[Fact]
// regression test for https://github.com/dotnet/cli/issues/4269
public void ItMigratesAndBuildsP2PReferences()
{
var assetsDir = TestAssets
.GetProjectJson("TestAppDependencyGraph")
.CreateInstance()
.WithSourceFiles()
.WithRestoreFiles()
.WithEmptyGlobalJson()
.Root;
var projectDirectory = assetsDir.GetDirectory("ProjectF");
var restoreDirectories = new DirectoryInfo[]
{
projectDirectory,
assetsDir.GetDirectory("ProjectG")
};
var outputComparisonData = BuildProjectJsonMigrateBuildMSBuild(projectDirectory, "ProjectF", new [] { projectDirectory.FullName }, restoreDirectories);
var outputsIdentical = outputComparisonData.ProjectJsonBuildOutputs
.SetEquals(outputComparisonData.MSBuildBuildOutputs);
if (!outputsIdentical)
{
OutputDiagnostics(outputComparisonData);
}
outputsIdentical.Should().BeTrue();
VerifyAllMSBuildOutputsRunnable(projectDirectory);
}
[Theory]
[InlineData("src", "H")]
[InlineData("src with spaces", "J")]
public void ItMigratesAndBuildsProjectsInGlobalJson(string path, string projectNameSuffix)
{
var assetsDir = TestAssets
.GetProjectJson("ProjectsWithGlobalJson")
.CreateInstance(identifier: projectNameSuffix)
.WithSourceFiles()
.WithRestoreFiles()
.WithEmptyGlobalJson()
.Root;
var projectName = $"Project{projectNameSuffix}";
var globalJson = assetsDir.GetFile("global.json");
var restoreDirectories = new DirectoryInfo[]
{
assetsDir.GetDirectory("src", "ProjectH"),
assetsDir.GetDirectory("src", "ProjectI"),
assetsDir.GetDirectory("src with spaces", "ProjectJ")
};
var projectDirectory = assetsDir.GetDirectory(path, projectName);
var outputComparisonData = BuildProjectJsonMigrateBuildMSBuild(projectDirectory,
projectName,
new [] { globalJson.FullName },
restoreDirectories);
var outputsIdentical = outputComparisonData.ProjectJsonBuildOutputs
.SetEquals(outputComparisonData.MSBuildBuildOutputs);
if (!outputsIdentical)
{
OutputDiagnostics(outputComparisonData);
}
outputsIdentical.Should().BeTrue();
VerifyAllMSBuildOutputsRunnable(projectDirectory);
}
2016-10-11 01:01:59 +00:00
[Theory]
[InlineData(true)]
[InlineData(false)]
public void MigrationOutputsErrorWhenNoProjectsFound(bool useGlobalJson)
2016-10-11 01:01:59 +00:00
{
var projectDirectory = TestAssets.CreateTestDirectory("Migration_outputs_error_when_no_projects_found");
string argstr = string.Empty;
string errorMessage = string.Empty;
2016-10-11 01:01:59 +00:00
if (useGlobalJson)
{
var globalJson = projectDirectory.GetFile("global.json");
using (StreamWriter sw = globalJson.CreateText())
{
sw.WriteLine("{");
sw.WriteLine("\"projects\": [ \".\" ]");
sw.WriteLine("}");
}
argstr = globalJson.FullName;
errorMessage = "Unable to find any projects in global.json";
2016-10-11 01:01:59 +00:00
}
else
{
argstr = projectDirectory.FullName;
errorMessage = $"No project.json file found in '{projectDirectory.FullName}'";
2016-10-11 01:01:59 +00:00
}
var result = new TestCommand("dotnet")
.WithWorkingDirectory(projectDirectory)
.ExecuteWithCapturedOutput($"migrate {argstr}");
// Expecting an error exit code.
result.ExitCode.Should().Be(1);
// Verify the error messages. Note that debug builds also show the call stack, so we search
// for the error strings that should be present (rather than an exact match).
result.StdErr
.Should().Contain(errorMessage)
.And.Contain("Migration failed.");
2016-10-11 01:01:59 +00:00
}
[Fact]
public void ItMigratesAndPublishesProjectsWithRuntimes()
{
var projectName = "PJTestAppSimple";
var projectDirectory = TestAssets
.GetProjectJson(projectName)
.CreateInstance()
.WithSourceFiles()
.WithRestoreFiles()
.WithEmptyGlobalJson()
.Root;
CleanBinObj(projectDirectory);
BuildProjectJsonMigrateBuildMSBuild(projectDirectory, projectName);
PublishMSBuild(projectDirectory, projectName, "win7-x64");
}
[WindowsOnlyTheory]
[InlineData("DesktopTestProjects", "AutoAddDesktopReferencesDuringMigrate", true)]
[InlineData("TestProjects", "PJTestAppSimple", false)]
public void ItAutoAddDesktopReferencesDuringMigrate(string testGroup, string projectName, bool isDesktopApp)
{
2016-10-31 23:22:10 +00:00
var runtime = DotnetLegacyRuntimeIdentifiers.InferLegacyRestoreRuntimeIdentifier();
var projectDirectory = TestAssets
.GetProjectJson(testGroup, projectName)
.CreateInstance()
.WithSourceFiles()
.WithRestoreFiles()
.WithEmptyGlobalJson()
.Root;
CleanBinObj(projectDirectory);
MigrateProject(new string[] { projectDirectory.FullName });
2016-10-31 23:22:10 +00:00
Restore(projectDirectory, runtime: runtime);
BuildMSBuild(projectDirectory, projectName, runtime:runtime);
VerifyAutoInjectedDesktopReferences(projectDirectory, projectName, isDesktopApp);
VerifyAllMSBuildOutputsRunnable(projectDirectory);
}
[Fact]
public void ItBuildsAMigratedAppWithAnIndirectDependency()
{
const string projectName = "ProjectA";
var solutionDirectory = TestAssets
.GetProjectJson("TestAppDependencyGraph")
.CreateInstance()
.WithSourceFiles()
.WithEmptyGlobalJson()
.Root;
var projectDirectory = solutionDirectory.GetDirectory(projectName);
MigrateProject(new string[] { projectDirectory.FullName });
Restore(projectDirectory);
BuildMSBuild(projectDirectory, projectName);
VerifyAllMSBuildOutputsRunnable(projectDirectory);
}
2016-10-29 00:27:07 +00:00
[Fact]
public void ItMigratesProjectWithOutputName()
2016-10-29 00:27:07 +00:00
{
var projectName = "AppWithOutputAssemblyName";
var expectedOutputName = "MyApp";
2016-10-29 00:27:07 +00:00
var projectDirectory = TestAssets
.GetProjectJson(projectName)
.CreateInstance()
.WithSourceFiles()
.WithRestoreFiles()
.WithEmptyGlobalJson()
.Root;
var expectedCsprojPath = projectDirectory.GetFile($"{projectName}.csproj");
if (expectedCsprojPath.Exists)
2016-10-29 00:27:07 +00:00
{
expectedCsprojPath.Delete();
2016-10-29 00:27:07 +00:00
}
CleanBinObj(projectDirectory);
MigrateProject(projectDirectory.FullName);
expectedCsprojPath.Refresh();
expectedCsprojPath.Should().Exist();
2016-10-29 00:27:07 +00:00
Restore(projectDirectory, projectName);
BuildMSBuild(projectDirectory, projectName);
projectDirectory
.GetDirectory("bin")
.EnumerateFiles($"{expectedOutputName}.pdb", SearchOption.AllDirectories)
.Count().Should().Be(1);
PackMSBuild(projectDirectory, projectName);
projectDirectory
.GetDirectory("bin")
.EnumerateFiles($"{projectName}.1.0.0.nupkg", SearchOption.AllDirectories)
2016-10-29 00:27:07 +00:00
.Count().Should().Be(1);
}
[Theory]
[InlineData("LibraryWithoutNetStandardLibRef")]
[InlineData("LibraryWithNetStandardLibRef")]
public void ItMigratesAndBuildsLibrary(string projectName)
{
var projectDirectory = TestAssets
.GetProjectJson(projectName)
.CreateInstance(identifier: projectName)
.WithSourceFiles()
.WithEmptyGlobalJson()
.Root;
MigrateProject(projectDirectory.FullName);
Restore(projectDirectory, projectName);
BuildMSBuild(projectDirectory, projectName);
}
2017-01-20 23:59:21 +00:00
[Fact]
public void ItMigratesAndBuildsAppWithExplicitInclude()
{
const string projectName = "TestAppWithExplicitInclude";
var projectDirectory = TestAssets.Get(projectName)
.CreateInstance()
.WithSourceFiles()
.Root;
MigrateProject(projectDirectory.FullName);
Restore(projectDirectory, projectName);
BuildMSBuild(projectDirectory, projectName);
}
2017-01-20 23:59:21 +00:00
[Fact]
public void ItMigratesAndBuildsAppWithExplicitIncludeGlob()
{
const string projectName = "TestAppWithExplicitIncludeGlob";
var projectDirectory = TestAssets.Get(projectName)
.CreateInstance()
.WithSourceFiles()
.Root;
MigrateProject(projectDirectory.FullName);
Restore(projectDirectory, projectName);
BuildMSBuild(projectDirectory, projectName);
}
private void VerifyAutoInjectedDesktopReferences(DirectoryInfo projectDirectory, string projectName, bool shouldBePresent)
{
if (projectName != null)
{
projectName = projectName + ".csproj";
}
var root = ProjectRootElement.Open(projectDirectory.GetFile(projectName).FullName);
var autoInjectedReferences = root
.Items
.Where(i => i.ItemType == "Reference"
&& (i.Include == "System" || i.Include == "Microsoft.CSharp"));
if (shouldBePresent)
{
autoInjectedReferences.Should().HaveCount(2);
}
else
{
autoInjectedReferences.Should().BeEmpty();
}
}
private void VerifyMigration(IEnumerable<string> expectedProjects, DirectoryInfo rootDir)
{
var backupDir = rootDir.GetDirectory("backup");
Create backup folder in the directory where 'dotnet migrate' is executed (#5306) * Create backup folder in the directory where 'dotnet migrate' is executed With this change, 'dotnet migrate' will create the backup folder in the workspace directory rather than the parent of the workspace directory. This solves two problems: 1. It makes it easier for the user where the backup is -- it's in the directory they targeted with 'dotnet migrate'. 2. It solves a problem of file oollisions with global.json files when migrating multiple projects. Consider the following directory structure: root | project1 | global.json | src | project1 project2 | global.json | src | project2 Prior to this change, running 'dotnet migrate' project1 and then running it again in project2 would have caused an exception to be thrown because the migration would try to produce a backup folder like so: root | backup | | | global.json | | | project1 | | | project2 | | project1 | src | project1 project2 | src | project2 Now, we produce the following structure, which has no collisions: root | project1 | backup | | | global.json | | | project1 | src | project1 | project2 | backup | | | global.json | | | project2 | src | project2 In addition, to help avoid further collisions, a number is appened to the backup folder's name if it already exists. So, if the user runs dotnet migrate again for some reason, they'll see backup_1, backup_2, etc. * Fix test helper * Fix foolish bug causing infinite loop * Fix up a couple more tests * Rework MigrationBackupPlan to process all projects at once * Fix up tests * Still fixing tests * Compute common root folder of projects to determine where backup folder should be placed * Fix typo * Fix test to not look in backup folder now that it's in a better location
2017-01-21 09:58:28 +00:00
var migratedProjects = rootDir.EnumerateFiles("*.csproj", SearchOption.AllDirectories)
.Where(s => !PathUtility.IsChildOfDirectory(backupDir.FullName, s.FullName))
.Where(s => Directory.EnumerateFiles(Path.GetDirectoryName(s.FullName), "*.csproj").Count() == 1)
.Where(s => Path.GetFileName(Path.GetDirectoryName(s.FullName)).Contains("Project"))
.Select(s => Path.GetFileName(Path.GetDirectoryName(s.FullName)));
migratedProjects.Should().BeEquivalentTo(expectedProjects);
}
private MigratedBuildComparisonData GetComparisonData(DirectoryInfo projectDirectory)
2016-08-22 19:24:10 +00:00
{
File.Copy("NuGet.tempaspnetpatch.config", projectDirectory.GetFile("NuGet.Config").FullName);
RestoreProjectJson(projectDirectory);
2016-08-22 19:24:10 +00:00
var outputComparisonData =
BuildProjectJsonMigrateBuildMSBuild(projectDirectory, Path.GetFileNameWithoutExtension(projectDirectory.FullName));
2016-08-23 20:50:05 +00:00
return outputComparisonData;
2016-08-22 19:24:10 +00:00
}
private void VerifyAllMSBuildOutputsRunnable(DirectoryInfo projectDirectory)
2016-08-22 19:24:10 +00:00
{
var dllFileName = Path.GetFileName(projectDirectory.FullName) + ".dll";
2016-08-22 19:24:10 +00:00
var runnableDlls = projectDirectory
.GetDirectory("bin")
.GetFiles(dllFileName, SearchOption.AllDirectories);
2016-08-22 19:24:10 +00:00
foreach (var dll in runnableDlls)
{
new TestCommand("dotnet").ExecuteWithCapturedOutput($"\"{dll.FullName}\"").Should().Pass();
2016-08-22 19:24:10 +00:00
}
}
private void VerifyAllMSBuildOutputsAreSigned(DirectoryInfo projectDirectory)
2016-10-04 23:48:14 +00:00
{
var dllFileName = Path.GetFileName(projectDirectory.FullName) + ".dll";
2016-10-04 23:48:14 +00:00
var runnableDlls = projectDirectory
.GetDirectory("bin")
.EnumerateFiles(dllFileName, SearchOption.AllDirectories);
2016-10-04 23:48:14 +00:00
foreach (var dll in runnableDlls)
{
var assemblyName = AssemblyLoadContext.GetAssemblyName(dll.FullName);
2016-10-04 23:48:14 +00:00
var token = assemblyName.GetPublicKeyToken();
2016-10-04 23:48:14 +00:00
token.Should().NotBeNullOrEmpty();
}
}
private MigratedBuildComparisonData BuildProjectJsonMigrateBuildMSBuild(DirectoryInfo projectDirectory,
string projectName)
{
return BuildProjectJsonMigrateBuildMSBuild(projectDirectory,
projectName,
new [] { projectDirectory.FullName },
new [] { projectDirectory });
}
private MigratedBuildComparisonData BuildProjectJsonMigrateBuildMSBuild(DirectoryInfo projectDirectory,
string projectName,
string[] migrateArgs,
DirectoryInfo[] restoreDirectories)
2016-08-23 20:50:05 +00:00
{
BuildProjectJson(projectDirectory);
var projectJsonBuildOutputs = new HashSet<string>(CollectBuildOutputs(projectDirectory.FullName));
2016-08-23 20:50:05 +00:00
CleanBinObj(projectDirectory);
2016-09-22 04:23:50 +00:00
// Remove lock file for migration
foreach(var dir in restoreDirectories)
{
dir.GetFile("project.lock.json").Delete();
}
2016-09-27 04:40:11 +00:00
MigrateProject(migrateArgs);
2016-09-27 04:40:11 +00:00
DeleteXproj(projectDirectory);
foreach(var dir in restoreDirectories)
{
Restore(dir);
}
2016-10-04 03:10:09 +00:00
BuildMSBuild(projectDirectory, projectName);
2016-08-23 20:50:05 +00:00
var msbuildBuildOutputs = new HashSet<string>(CollectBuildOutputs(projectDirectory.FullName));
2016-08-23 20:50:05 +00:00
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(DirectoryInfo projectDirectory)
{
var dirs = new DirectoryInfo[] { projectDirectory.GetDirectory("bin"), projectDirectory.GetDirectory("obj") };
2016-08-22 19:24:10 +00:00
foreach (var dir in dirs)
{
if(dir.Exists)
{
dir.Delete(true);
}
2016-08-22 19:24:10 +00:00
}
}
private void BuildProjectJson(DirectoryInfo projectDirectory)
2016-08-22 19:24:10 +00:00
{
Console.WriteLine(projectDirectory);
var projectFile = $"\"{projectDirectory.GetFile("project.json").FullName}\"";
var result = new BuildPJCommand()
.WithCapturedOutput()
.WithForwardingToConsole()
.Execute(projectFile);
2016-08-22 19:24:10 +00:00
result.Should().Pass();
}
2016-10-29 00:27:07 +00:00
private void MigrateProject(params string[] migrateArgs)
2016-08-22 19:24:10 +00:00
{
new TestCommand("dotnet")
.WithForwardingToConsole()
.Execute($"migrate {string.Join(" ", migrateArgs)}")
.Should()
.Pass();
2016-08-23 20:50:05 +00:00
}
private void RestoreProjectJson(DirectoryInfo projectDirectory)
2016-08-22 19:24:10 +00:00
{
var projectFile = $"\"{projectDirectory.GetFile("project.json").FullName}\"";
new RestoreProjectJsonCommand()
.Execute(projectFile)
.Should().Pass();
2016-08-22 19:24:10 +00:00
}
private void Restore(DirectoryInfo projectDirectory, string projectName=null, string runtime=null)
{
var command = new RestoreCommand()
2016-10-31 23:22:10 +00:00
.WithWorkingDirectory(projectDirectory)
.WithRuntime(runtime);
2016-10-04 03:10:09 +00:00
if (projectName != null)
{
if (!Path.HasExtension(projectName))
{
projectName += ".csproj";
}
command.Execute($"{projectName} /p:SkipInvalidConfigurations=true;_InvalidConfigurationWarning=false")
.Should().Pass();
2016-10-04 03:10:09 +00:00
}
else
{
2016-10-20 22:19:35 +00:00
command.Execute("/p:SkipInvalidConfigurations=true;_InvalidConfigurationWarning=false")
.Should().Pass();
}
}
2016-10-31 23:22:10 +00:00
private string BuildMSBuild(
DirectoryInfo projectDirectory,
2016-10-31 23:22:10 +00:00
string projectName,
string configuration="Debug",
2017-01-06 06:55:43 +00:00
string runtime=null,
string framework=null)
2016-08-22 19:24:10 +00:00
{
if (projectName != null && !Path.HasExtension(projectName))
2016-10-04 03:10:09 +00:00
{
projectName = projectName + ".csproj";
}
2016-09-08 21:40:46 +00:00
DeleteXproj(projectDirectory);
var result = new BuildCommand()
2016-08-22 19:24:10 +00:00
.WithWorkingDirectory(projectDirectory)
2016-10-31 23:22:10 +00:00
.WithRuntime(runtime)
2017-01-06 06:55:43 +00:00
.WithFramework(framework)
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();
2016-08-22 19:24:10 +00:00
return result.StdOut;
}
2016-08-23 20:50:05 +00:00
private string PublishMSBuild(
DirectoryInfo projectDirectory,
string projectName,
string runtime = null,
string configuration = "Debug")
{
if (projectName != null)
{
projectName = projectName + ".csproj";
}
DeleteXproj(projectDirectory);
var result = new PublishCommand()
.WithRuntime(runtime)
.WithWorkingDirectory(projectDirectory)
.ExecuteWithCapturedOutput($"{projectName} /p:Configuration={configuration}");
result.Should().Pass();
return result.StdOut;
}
private string PackMSBuild(DirectoryInfo projectDirectory, string projectName)
{
if (projectName != null && !Path.HasExtension(projectName))
{
projectName = projectName + ".csproj";
}
var result = new PackCommand()
.WithWorkingDirectory(projectDirectory)
.ExecuteWithCapturedOutput($"{projectName}");
result.Should().Pass();
return result.StdOut;
}
private void DeleteXproj(DirectoryInfo projectDirectory)
2016-09-08 21:40:46 +00:00
{
var xprojFiles = projectDirectory.EnumerateFiles("*.xproj");
2016-09-08 21:40:46 +00:00
foreach (var xprojFile in xprojFiles)
{
xprojFile.Delete();
2016-09-08 21:40:46 +00:00
}
}
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:");
2016-08-23 20:50:05 +00:00
Console.WriteLine(string.Join("\n", projectJsonBuildOutputs));
Console.WriteLine("");
Console.WriteLine("MSBuild Outputs:");
2016-08-23 20:50:05 +00:00
Console.WriteLine(string.Join("\n", msbuildBuildOutputs));
}
private class MigratedBuildComparisonData
{
public HashSet<string> ProjectJsonBuildOutputs { get; }
2016-08-23 20:50:05 +00:00
public HashSet<string> MSBuildBuildOutputs { get; }
public MigratedBuildComparisonData(HashSet<string> projectJsonBuildOutputs,
HashSet<string> msBuildBuildOutputs)
{
ProjectJsonBuildOutputs = projectJsonBuildOutputs;
2016-08-23 20:50:05 +00:00
MSBuildBuildOutputs = msBuildBuildOutputs;
}
}
2016-10-20 22:04:53 +00:00
private void WriteGlobalJson(DirectoryInfo globalDirectory)
2016-10-20 22:04:53 +00:00
{
var file = globalDirectory.GetFile("global.json");
File.WriteAllText(file.FullName, @"
2016-10-20 22:04:53 +00:00
{
""projects"": [ ]
}");
}
2016-08-22 19:24:10 +00:00
}
}