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 dd90344
Show file tree
Hide file tree
Showing 3 changed files with 122 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
57 changes: 56 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,11 @@ await FormatPhysicalFile(
}
else if (fileSystem.Directory.Exists(directoryOrFile))
{
if (!ValidateMsBuildVersion(directoryOrFile, fileSystem, logger))
{
return 1;
}

var tasks = fileSystem.Directory
.EnumerateFiles(directoryOrFile, "*.cs", SearchOption.AllDirectories)
.Select(FormatFile)
Expand Down Expand Up @@ -144,6 +151,54 @@ await FormatPhysicalFile(
}
}

private static bool ValidateMsBuildVersion(
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)
{
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 false;
}

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

return true;
}

private static async Task FormatPhysicalFile(
string filePath,
string directoryOrFile,
Expand Down
65 changes: 65 additions & 0 deletions Src/CSharpier.Tests/CommandLineFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,70 @@ 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 Format_Writes_File_With_File_Path()
{
Expand Down Expand Up @@ -357,6 +421,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 dd90344

Please sign in to comment.