Merge pull request #6530 from mikkelbu/gracefully-report-invalid-projects

Gracefully report invalid project when added via sln-add
This commit is contained in:
Livar 2017-05-08 11:39:17 -07:00 committed by GitHub
commit 760bdf8f75
3 changed files with 44 additions and 1 deletions

View file

@ -101,6 +101,7 @@ namespace Microsoft.DotNet.Tools
public const string MoreThanOneProjectInDirectory = "Found more than one project in `{0}`. Please specify which one to use.";
public const string FoundInvalidProject = "Found a project `{0}` but it is invalid.";
public const string InvalidProject = "Invalid project `{0}`.";
public const string InvalidProjectWithExceptionMessage = "Invalid project `{0}`. {1}";
/// Solution
public const string CouldNotFindSolutionIn = "Specified solution file {0} does not exist, or there is no solution file in the directory.";

View file

@ -3,6 +3,7 @@
using Microsoft.Build.Construction;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Exceptions;
using Microsoft.Build.Execution;
using Microsoft.DotNet.Cli.Sln.Internal;
using Microsoft.DotNet.Cli.Utils;
@ -36,7 +37,19 @@ namespace Microsoft.DotNet.Tools.Common
}
else
{
var projectInstance = new ProjectInstance(fullProjectPath);
ProjectInstance projectInstance = null;
try
{
projectInstance = new ProjectInstance(fullProjectPath);
}
catch (InvalidProjectFileException e)
{
Reporter.Error.WriteLine(string.Format(
CommonLocalizableStrings.InvalidProjectWithExceptionMessage,
fullProjectPath,
e.Message));
return;
}
var slnProject = new SlnProject
{

View file

@ -480,6 +480,35 @@ EndGlobal
cmd.StdErr.Should().BeEmpty();
}
[Theory]
[InlineData("TestAppWithSlnAndCsprojFiles")]
[InlineData("TestAppWithSlnAndCsprojProjectGuidFiles")]
[InlineData("TestAppWithEmptySln")]
public void WhenInvalidProjectIsPassedItDoesNotGetAdded(string testAsset)
{
var projectDirectory = TestAssets
.Get(testAsset)
.CreateInstance()
.WithSourceFiles()
.Root
.FullName;
var projectToAdd = "Lib/Library.cs";
var projectPath = Path.Combine("Lib", "Library.cs");
var slnFile = SlnFile.Read(Path.Combine(projectDirectory, "App.sln"));
var expectedNumberOfProjects = slnFile.Projects.Count();
var cmd = new DotnetCommand()
.WithWorkingDirectory(projectDirectory)
.ExecuteWithCapturedOutput($"sln App.sln add {projectToAdd}");
cmd.Should().Pass();
cmd.StdOut.Should().BeEmpty();
cmd.StdErr.Should().Match("Invalid project `*`. The project file could not be loaded.*");
slnFile = SlnFile.Read(Path.Combine(projectDirectory, "App.sln"));
slnFile.Projects.Count().Should().Be(expectedNumberOfProjects);
}
[Theory]
[InlineData("TestAppWithSlnAndCsprojFiles")]
[InlineData("TestAppWithSlnAndCsprojProjectGuidFiles")]