Merge pull request #5106 from jonsequitur/make-migration-backup-folder-peer-of-project

make migration backup folder a sibling of the project root
This commit is contained in:
Jon Sequeira 2016-12-21 12:12:11 -08:00 committed by GitHub
commit 3b0a2f5d20
11 changed files with 323 additions and 114 deletions

View file

@ -0,0 +1,94 @@
// 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 FluentAssertions;
using Microsoft.DotNet.Internal.ProjectModel.Utilities;
using Microsoft.DotNet.ProjectJsonMigration;
using System;
using System.IO;
using System.Linq;
using Xunit;
namespace Microsoft.DotNet.ProjectJsonMigration.Tests
{
public partial class MigrationBackupPlanTests
{
[Fact]
public void TheRootBackupDirectoryIsASiblingOfTheRootProject()
{
var dir = new DirectoryInfo(Path.Combine("src", "some-proj"));
System.Console.WriteLine(dir.FullName);
WhenMigrating(
projectDirectory: dir.FullName,
workspaceDirectory: Path.Combine("src", "RootProject"))
.RootBackupDirectory
.FullName
.Should()
.Be(new DirectoryInfo(Path.Combine("src", "backup")).FullName.EnsureTrailingSlash());
}
[Fact]
public void TheRootProjectsBackupDirectoryIsASubfolderOfTheRootBackupDirectory()
{
WhenMigrating(
projectDirectory: Path.Combine("src", "RootProject"),
workspaceDirectory: Path.Combine("src", "RootProject"))
.ProjectBackupDirectory
.FullName
.Should()
.Be(new DirectoryInfo(Path.Combine("src", "backup", "RootProject")).FullName.EnsureTrailingSlash());
}
[Fact]
public void ADependentProjectsMigrationBackupDirectoryIsASubfolderOfTheRootBackupDirectory()
{
WhenMigrating(
projectDirectory: Path.Combine("src", "Dependency"),
workspaceDirectory: Path.Combine("src", "RootProject"))
.ProjectBackupDirectory
.FullName
.Should()
.Be(new DirectoryInfo(Path.Combine("src", "backup", "Dependency")).FullName.EnsureTrailingSlash());
}
[Fact]
public void FilesToBackUpAreIdentifiedInTheTheRootProjectDirectory()
{
var root = new DirectoryInfo(Path.Combine("src", "RootProject"));
WhenMigrating(
projectDirectory: root.FullName,
workspaceDirectory: root.FullName)
.FilesToMove
.Should()
.Contain(_ => _.FullName == Path.Combine(root.FullName, "project.json"));
}
[Fact]
public void FilesToBackUpAreIdentifiedInTheTheDependencyProjectDirectory()
{
var root = new DirectoryInfo(Path.Combine("src", "RootProject"));
var dependency = new DirectoryInfo(Path.Combine("src", "RootProject"));
WhenMigrating(
projectDirectory: dependency.FullName,
workspaceDirectory: root.FullName)
.FilesToMove
.Should()
.Contain(_ => _.FullName == Path.Combine(dependency.FullName, "project.json"));
}
private MigrationBackupPlan WhenMigrating(
string projectDirectory,
string workspaceDirectory) =>
new MigrationBackupPlan(
new DirectoryInfo(projectDirectory),
new DirectoryInfo(workspaceDirectory),
dir => new[] { new FileInfo(Path.Combine(dir.FullName, "project.json")) });
}
}

View file

@ -48,7 +48,7 @@ namespace Microsoft.DotNet.Tools.Test.Utilities
return subject;
}
public static TCommand WithForwardingToConsole<TCommand>(this TCommand subject, Action<string> writeLine) where TCommand : TestCommand
public static TCommand WithForwardingToConsole<TCommand>(this TCommand subject) where TCommand : TestCommand
{
subject.WithOutputDataReceivedHandler(s => Console.Out.WriteLine(s));

View file

@ -12,18 +12,47 @@ namespace Microsoft.DotNet.Migration.Tests
public class GivenThatAnAppWasMigrated : TestBase
{
[Theory]
[InlineData("PJTestAppSimple")]
[InlineData("TestAppWithLibrary")]
public void When_migration_succeeds_Then_project_json_artifacts_get_moved_to_backup(string testProjectName)
public void WhenProjectMigrationSucceedsThenProjectJsonArtifactsGetMovedToBackup(string testProjectName)
{
var testRoot = TestAssetsManager
.CreateTestInstance(testProjectName, identifier: testProjectName)
.CreateTestInstance(testProjectName)
.Path;
var backupRoot = Path.Combine(testRoot, "backup");
var testRootParent = new DirectoryInfo(testRoot).Parent.FullName;
var backupRoot = Path.Combine(testRootParent, "backup");
var migratableArtifacts = GetProjectJsonArtifacts(testRoot);
new MigrateCommand()
.WithWorkingDirectory(testRoot)
.Execute()
.Should().Pass();
var backupArtifacts = GetProjectJsonArtifacts(backupRoot);
backupArtifacts.Should().Equal(migratableArtifacts, "Because all of and only these artifacts should have been moved");
new DirectoryInfo(testRoot).Should().NotHaveFiles(backupArtifacts.Keys);
new DirectoryInfo(backupRoot).Should().HaveTextFiles(backupArtifacts);
}
[Theory]
[InlineData("PJTestAppSimple")]
public void WhenFolderMigrationSucceedsThenProjectJsonArtifactsGetMovedToBackup(string testProjectName)
{
var testRoot = TestAssetsManager
.CreateTestInstance(testProjectName)
.Path;
var testRootParent = new DirectoryInfo(testRoot).Parent.FullName;
var backupRoot = Path.Combine(testRootParent, "backup", testProjectName);
var migratableArtifacts = GetProjectJsonArtifacts(testRoot);
new MigrateCommand()
.WithWorkingDirectory(testRoot)
.Execute()
@ -40,7 +69,7 @@ namespace Microsoft.DotNet.Migration.Tests
[Theory]
[InlineData("TestAppWithLibraryAndMissingP2P")]
public void When_migration_fails_Then_project_json_artifacts_do_not_get_moved_to_backup(string testProjectName)
public void WhenMigrationFailsThenProjectJsonArtifactsDoNotGetMovedToBackup(string testProjectName)
{
var testRoot = new TestAssetsManager(Path.Combine(RepoRoot, "TestAssets", "NonRestoredTestProjects"))
.CreateTestInstance(testProjectName, identifier: testProjectName)
@ -54,7 +83,7 @@ namespace Microsoft.DotNet.Migration.Tests
.WithWorkingDirectory(testRoot)
.Execute()
.Should().Fail();
new DirectoryInfo(backupRoot).Should().NotExist("Because migration failed and therefore no backup is needed.");
new DirectoryInfo(testRoot).Should().HaveTextFiles(migratableArtifacts, "Because migration failed so nothing was moved to backup.");
@ -62,7 +91,7 @@ namespace Microsoft.DotNet.Migration.Tests
[Theory]
[InlineData("PJTestAppSimple")]
public void When_skipbackup_specified_Then_project_json_artifacts_do_not_get_moved_to_backup(string testProjectName)
public void WhenSkipbackupSpecifiedThenProjectJsonArtifactsDoNotGetMovedToBackup(string testProjectName)
{
var testRoot = TestAssetsManager.CreateTestInstance(testProjectName, identifier: testProjectName).Path;
@ -74,7 +103,7 @@ namespace Microsoft.DotNet.Migration.Tests
.WithWorkingDirectory(testRoot)
.Execute("--skip-backup")
.Should().Pass();
new DirectoryInfo(backupRoot).Should().NotExist("Because --skip-backup was specified.");
new DirectoryInfo(testRoot).Should().HaveTextFiles(migratableArtifacts, "Because --skip-backup was specified.");
@ -83,14 +112,14 @@ namespace Microsoft.DotNet.Migration.Tests
private Dictionary<string, string> GetProjectJsonArtifacts(string rootPath)
{
var catalog = new Dictionary<string, string>();
var patterns = new [] { "global.json", "project.json", "project.lock.json", "*.xproj", "*.xproj.user" };
var patterns = new[] { "global.json", "project.json", "project.lock.json", "*.xproj", "*.xproj.user" };
foreach (var pattern in patterns)
{
AddArtifactsToCatalog(catalog, rootPath, pattern);
}
return catalog;
}
@ -104,7 +133,8 @@ namespace Microsoft.DotNet.Migration.Tests
foreach (var file in files)
{
catalog.Add(PathUtility.GetRelativePath(basePath, file.FullName), File.ReadAllText(file.FullName));
var key = PathUtility.GetRelativePath(basePath, file.FullName);
catalog.Add(key, File.ReadAllText(file.FullName));
}
}
}

View file

@ -213,7 +213,9 @@ namespace Microsoft.DotNet.Migration.Tests
public void ItMigratesRootProjectAndReferences(string projectName, string expectedProjects)
{
var projectDirectory =
TestAssetsManager.CreateTestInstance("TestAppDependencyGraph", callingMethod: $"{projectName}.RefsTest").Path;
TestAssetsManager.CreateTestInstance(
"TestAppDependencyGraph",
identifier: $"{projectName}.RefsTest").Path;
MigrateProject(new [] { Path.Combine(projectDirectory, projectName) });
@ -231,7 +233,7 @@ namespace Microsoft.DotNet.Migration.Tests
public void ItMigratesRootProjectAndSkipsReferences(string projectName)
{
var projectDirectory =
TestAssetsManager.CreateTestInstance("TestAppDependencyGraph", callingMethod: $"{projectName}.SkipRefsTest").Path;
TestAssetsManager.CreateTestInstance("TestAppDependencyGraph", identifier: $"{projectName}.SkipRefsTest").Path;
MigrateProject(new [] { Path.Combine(projectDirectory, projectName), "--skip-project-references" });
@ -424,7 +426,7 @@ namespace Microsoft.DotNet.Migration.Tests
{
const string projectName = "ProjectA";
var solutionDirectory =
TestAssetsManager.CreateTestInstance("TestAppDependencyGraph", callingMethod: "p").Path;
TestAssetsManager.CreateTestInstance("TestAppDependencyGraph").Path;
var projectDirectory = Path.Combine(solutionDirectory, projectName);
MigrateProject(new string[] { projectDirectory });
@ -474,8 +476,9 @@ namespace Microsoft.DotNet.Migration.Tests
[InlineData("LibraryWithNetStandardLibRef")]
public void ItMigratesAndBuildsLibrary(string projectName)
{
var projectDirectory = TestAssetsManager.CreateTestInstance(projectName,
callingMethod: $"{nameof(ItMigratesAndBuildsLibrary)}-projectName").Path;
var projectDirectory = TestAssetsManager.CreateTestInstance(
projectName,
identifier: $"{projectName}").Path;
MigrateProject(projectDirectory);
Restore(projectDirectory, projectName);
@ -654,6 +657,7 @@ namespace Microsoft.DotNet.Migration.Tests
var result = new BuildPJCommand()
.WithCapturedOutput()
.WithForwardingToConsole()
.Execute(projectFile);
result.Should().Pass();
@ -661,10 +665,11 @@ namespace Microsoft.DotNet.Migration.Tests
private void MigrateProject(params string[] migrateArgs)
{
var result =
MigrateCommand.Run(migrateArgs);
result.Should().Be(0);
new TestCommand("dotnet")
.WithForwardingToConsole()
.Execute($"migrate {string.Join(" ", migrateArgs)}")
.Should()
.Pass();
}
private void RestoreProjectJson(string projectDirectory)