Skip to content

Commit

Permalink
Fixing bug with empty csproj causing csharpier to fail (#671)
Browse files Browse the repository at this point in the history
closes #665
  • Loading branch information
belav authored May 23, 2022
1 parent 36c8b86 commit c3d384b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
12 changes: 12 additions & 0 deletions Src/CSharpier.Cli.Tests/CliTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,18 @@ public async Task Should_Not_Fail_On_Empty_File()
result.ExitCode.Should().Be(0);
}

[Test]
public async Task Should_Not_Fail_On_Bad_Csproj()
{
await this.WriteFileAsync("Empty.csproj", "");

var result = await new CsharpierProcess().WithArguments(".").ExecuteAsync();

result.ErrorOutput.Should().BeEmpty();
result.ExitCode.Should().Be(0);
result.Output.Should().StartWith("Warning The csproj at ");
}

private async Task WriteFileAsync(string path, string content)
{
var fileInfo = new FileInfo(Path.Combine(testFileDirectory, path));
Expand Down
15 changes: 14 additions & 1 deletion Src/CSharpier.Cli/HasMismatchedCliAndMsBuildVersions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,20 @@ public static bool Check(string directory, IFileSystem fileSystem, ILogger logge
{
// this could potentially use the Microsoft.CodeAnalysis.Project class, but that was
// proving difficult to use
var csProjXElement = XElement.Load(fileSystem.File.OpenRead(pathToCsProj));
XElement csProjXElement;
try
{
csProjXElement = XElement.Load(fileSystem.File.OpenRead(pathToCsProj));
}
catch (Exception ex)
{
logger.LogWarning(
$"The csproj at {pathToCsProj} failed to load with the following exception {ex.Message}"
);

continue;
}

var csharpierMsBuildElement = csProjXElement
.XPathSelectElements("//PackageReference[@Include='CSharpier.MsBuild']")
.FirstOrDefault();
Expand Down

0 comments on commit c3d384b

Please sign in to comment.