From bb1bceb8bb396a7a731799bf6850c6461c0f6b99 Mon Sep 17 00:00:00 2001 From: Livar Cunha Date: Thu, 8 Dec 2016 19:53:35 -0800 Subject: [PATCH] When we had a project.json without frameworks, we were failing to show the appropriate error message because when we were generating the error message itself we tried to access the project context (which requires a framework in the PJ). This fix makes the ProjectContext optional to add the migration error. --- .../project.json | 8 +++++ .../MigrationRuleInputs.cs | 2 +- .../ProjectMigrator.cs | 4 +-- .../GivenAProjectMigrator.cs | 32 +++++++++++++++++-- 4 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 TestAssets/TestProjects/TestLibraryWithProjectFileWithoutFrameworks/project.json diff --git a/TestAssets/TestProjects/TestLibraryWithProjectFileWithoutFrameworks/project.json b/TestAssets/TestProjects/TestLibraryWithProjectFileWithoutFrameworks/project.json new file mode 100644 index 000000000..9e6a2436c --- /dev/null +++ b/TestAssets/TestProjects/TestLibraryWithProjectFileWithoutFrameworks/project.json @@ -0,0 +1,8 @@ +{ + "dependencies": { + "Microsoft.VisualStudio.Web.CodeGeneration.Tools" : { + "version": "1.0.0-preview2-final", + "type": "build" + } + } +} \ No newline at end of file diff --git a/src/Microsoft.DotNet.ProjectJsonMigration/MigrationRuleInputs.cs b/src/Microsoft.DotNet.ProjectJsonMigration/MigrationRuleInputs.cs index f08c06c88..5d69da274 100644 --- a/src/Microsoft.DotNet.ProjectJsonMigration/MigrationRuleInputs.cs +++ b/src/Microsoft.DotNet.ProjectJsonMigration/MigrationRuleInputs.cs @@ -24,7 +24,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration { get { - return ProjectContexts.First(); + return ProjectContexts.FirstOrDefault(); } } diff --git a/src/Microsoft.DotNet.ProjectJsonMigration/ProjectMigrator.cs b/src/Microsoft.DotNet.ProjectJsonMigration/ProjectMigrator.cs index f6e4c46ad..155748556 100644 --- a/src/Microsoft.DotNet.ProjectJsonMigration/ProjectMigrator.cs +++ b/src/Microsoft.DotNet.ProjectJsonMigration/ProjectMigrator.cs @@ -55,7 +55,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration { new ProjectMigrationReport( rootSettings.ProjectDirectory, - rootInputs?.DefaultProjectContext.GetProjectName(), + rootInputs?.DefaultProjectContext?.GetProjectName(), new List {e.Error}, null) }); @@ -193,7 +193,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration { if (!projectContexts.Any()) { - MigrationErrorCodes.MIGRATE1013($"No projects found in {projectDirectory}").Throw(); + MigrationErrorCodes.MIGRATE1013($"The project.json specifies no target frameworks in {projectDirectory}").Throw(); } var defaultProjectContext = projectContexts.First(); diff --git a/test/Microsoft.DotNet.ProjectJsonMigration.Tests/GivenAProjectMigrator.cs b/test/Microsoft.DotNet.ProjectJsonMigration.Tests/GivenAProjectMigrator.cs index b6d321f77..a542088ec 100644 --- a/test/Microsoft.DotNet.ProjectJsonMigration.Tests/GivenAProjectMigrator.cs +++ b/test/Microsoft.DotNet.ProjectJsonMigration.Tests/GivenAProjectMigrator.cs @@ -16,7 +16,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests public class GivenAProjectMigrator : TestBase { [Fact] - public void It_copies_ProjectDirectory_contents_to_OutputDirectory_when_the_directories_are_different() + public void ItCopiesProjectDirectoryContentsToOutputDirectoryWhenTheDirectoriesAreDifferent() { var testProjectDirectory = TestAssetsManager .CreateTestInstance("PJTestAppSimple", callingMethod: "z") @@ -39,7 +39,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests } [Fact] - public void It_has_error_when_migrating_a_deprecated_projectJson() + public void ItHasErrorWhenMigratingADeprecatedProjectJson() { var testProjectDirectory = TestAssetsManager.CreateTestInstance("TestLibraryWithDeprecatedProjectFile", callingMethod: "z") @@ -60,7 +60,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests } [Fact] - public void It_has_error_when_migrating_a_non_csharp_app() + public void ItHasErrorWhenMigratingANonCsharpApp() { var testProjectDirectory = TestAssetsManager.CreateTestInstance("FSharpTestProjects/TestApp", callingMethod: "z") @@ -77,6 +77,32 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests errorMessage.Should().Contain("MIGRATE20013::Non-Csharp App: Cannot migrate project"); } + [Fact] + public void ItHasErrorWhenMigratingAProjectJsonWithoutAFrameworks() + { + var testAppName = "TestLibraryWithProjectFileWithoutFrameworks"; + var testInstance = TestAssets.Get(testAppName) + .CreateInstance(testAppName) + .WithSourceFiles(); + + var testProjectDirectory = testInstance.Root.FullName; + + var mockProj = ProjectRootElement.Create(); + var testSettings = MigrationSettings.CreateMigrationSettingsTestHook( + testProjectDirectory, + testProjectDirectory, + mockProj); + + var projectMigrator = new ProjectMigrator(new FakeEmptyMigrationRule()); + var report = projectMigrator.Migrate(testSettings); + + var projectReport = report.ProjectMigrationReports.First(); + + var errorMessage = projectReport.Errors.First().GetFormattedErrorMessage(); + errorMessage.Should().Contain("MIGRATE1013::No Project:"); + errorMessage.Should().Contain($"The project.json specifies no target frameworks in {testProjectDirectory}"); + } + private IEnumerable EnumerateFilesWithRelativePath(string testProjectDirectory) { return