Skip to content

Commit

Permalink
Detect mismatch between cli and msbuild versions
Browse files Browse the repository at this point in the history
closes #490
  • Loading branch information
belav committed Dec 6, 2021
1 parent 1a3076c commit 9ddd2ca
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CSharpier.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>0.12.0</Version>
<Version>0.11.2</Version>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryUrl>https://github.com/belav/csharpier</RepositoryUrl>
<RepositoryType>git</RepositoryType>
Expand Down
15 changes: 14 additions & 1 deletion Src/CSharpier.Cli/CommandLineFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Diagnostics;
using System.IO.Abstractions;
using System.Linq;
using System.Reflection;
using System.Xml.Linq;
using System.Xml.XPath;
using CSharpier.Utilities;
using Microsoft.Extensions.Logging;

Expand Down Expand Up @@ -94,6 +96,17 @@ await FormatPhysicalFile(
}
else if (fileSystem.Directory.Exists(directoryOrFile))
{
if (
HasMismatchedCliAndMsBuildVersions.Check(
directoryOrFile,
fileSystem,
logger
)
)
{
return 1;
}

var tasks = fileSystem.Directory
.EnumerateFiles(directoryOrFile, "*.cs", SearchOption.AllDirectories)
.Select(FormatFile)
Expand Down
55 changes: 55 additions & 0 deletions Src/CSharpier.Cli/HasMismatchedCliAndMsBuildVersions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.IO.Abstractions;
using System.Xml.Linq;
using System.Xml.XPath;
using Microsoft.Extensions.Logging;

namespace CSharpier.Cli;

public static class HasMismatchedCliAndMsBuildVersions
{
public static bool Check(string directory, IFileSystem fileSystem, ILogger logger)
{
var csProjPaths = fileSystem.Directory
.EnumerateFiles(directory, "*.csproj", SearchOption.AllDirectories)
.ToArray();

var versionOfDotnetTool = typeof(CommandLineFormatter).Assembly.GetName().Version!.ToString(
3
);

foreach (var pathToCsProj in csProjPaths)
{
// this could potentially use the Microsoft.CodeAnalysis.Project class, but that was
// proving difficult to use
var csProjXElement = XElement.Load(fileSystem.File.OpenRead(pathToCsProj));
var csharpierMsBuildElement = csProjXElement
.XPathSelectElements("//PackageReference[@Include='CSharpier.MsBuild']")
.FirstOrDefault();
if (csharpierMsBuildElement == null)
{
continue;
}

var versionOfMsBuildPackage = csharpierMsBuildElement.Attribute("Version")?.Value;
if (versionOfMsBuildPackage == null)
{
logger.LogError(
$"The csproj at {pathToCsProj} uses an unknown version of CSharpier.MsBuild"
+ $" which is a mismatch with version {versionOfDotnetTool}"
);
return true;
}

if (versionOfDotnetTool != versionOfMsBuildPackage)
{
logger.LogError(
$"The csproj at {pathToCsProj} uses version {versionOfMsBuildPackage} of CSharpier.MsBuild"
+ $" which is a mismatch with version {versionOfDotnetTool}"
);
return true;
}
}

return false;
}
}
86 changes: 86 additions & 0 deletions Src/CSharpier.Tests/CommandLineFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,91 @@ public void Format_Writes_File_With_Directory_Path()
this.GetFileContent(unformattedFilePath).Should().Be(FormattedClassContent);
}

[TestCase("0.9.0", false)]
[TestCase("9999.0.0", false)]
[TestCase("current", true)]
public void Works_With_MSBuild_Version_Checking(string version, bool shouldPass)
{
var currentVersion = typeof(CommandLineFormatter).Assembly.GetName().Version!.ToString(3);

var versionToTest = version == "current" ? currentVersion : version;

WhenAFileExists(
"Test.csproj",
$@"<Project Sdk=""Microsoft.NET.Sdk"">
<ItemGroup>
<PackageReference Include=""CSharpier.MsBuild"" Version=""{versionToTest}"" />
</ItemGroup>
</Project>
"
);

var result = this.Format();

if (shouldPass)
{
result.ExitCode.Should().Be(0);
result.ErrorLines.Should().BeEmpty();
}
else
{
result.ExitCode.Should().Be(1);
result.ErrorLines
.First()
.Should()
.EndWith(
$@"Test.csproj uses version {version} of CSharpier.MsBuild which is a mismatch with version {currentVersion}"
);
}
}

[Test]
public void Works_With_MSBuild_Version_Checking_When_No_Version_Specified()
{
var currentVersion = typeof(CommandLineFormatter).Assembly.GetName().Version!.ToString(3);

WhenAFileExists(
"Test.csproj",
$@"<Project Sdk=""Microsoft.NET.Sdk"">
<ItemGroup>
<PackageReference Include=""CSharpier.MsBuild"" />
</ItemGroup>
</Project>
"
);

var result = this.Format();

result.ExitCode.Should().Be(1);
result.ErrorLines
.First()
.Should()
.EndWith(
$"Test.csproj uses an unknown version of CSharpier.MsBuild which is a mismatch with version {currentVersion}"
);
}

[Test]
public void Works_With_MSBuild_Version_Checking_When_No_Version_Included()
{
var currentVersion = typeof(CommandLineFormatter).Assembly.GetName().Version!.ToString(3);

WhenAFileExists(
"Test.csproj",
$@"<Project Sdk=""Microsoft.NET.Sdk"">
<ItemGroup>
<PackageReference Include=""SomeOtherLibrary"" />
</ItemGroup>
</Project>
"
);

var result = this.Format();

result.ExitCode.Should().Be(0);
result.ErrorLines.Should().BeEmpty();
}

[Test]
public void Format_Writes_File_With_File_Path()
{
Expand Down Expand Up @@ -357,6 +442,7 @@ public void Empty_Config_Files_Should_Log_Warning(string configFileName)

result.Lines
.First()
.Replace("\\", "/")
.Should()
.Be($"Warning The configuration file at {configPath} was empty.");
}
Expand Down

0 comments on commit 9ddd2ca

Please sign in to comment.