From e29caf26a2e473510058a8e343c3845f6d9c77f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ce=CC=81dric=20Luthi?= Date: Sat, 6 Jan 2024 15:04:03 +0100 Subject: [PATCH] Restore sorting empty target framework last Commit 8e85a15e324a65f9da5033f3d8a2320c76dda378 dropped the NuGet.Frameworks dependency and as a result sorting of the target frameworks behaviour changed. Previously, the `NuGetFrameworkSorter` would sort the empty target framework last. Since 8e85a15e324a65f9da5033f3d8a2320c76dda378 the empty target framework is sorted first. This commit restores the old behaviour of sorting the empty target framework last. Note: Stryker.NET was inadvertently depending on this behaviour, so v6.0.1 broke Stryker.NET. I have [fixed the issue in Styker.NET](https://github.com/stryker-mutator/stryker-net/pull/2811) but I think restoring the previous sorting behaviour is a good idea anyway. --- src/Buildalyzer/AnalyzerResults.cs | 18 +++++++++++++++++- .../Integration/SimpleProjectsFixture.cs | 2 +- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Buildalyzer/AnalyzerResults.cs b/src/Buildalyzer/AnalyzerResults.cs index e48ecf98..c361db23 100644 --- a/src/Buildalyzer/AnalyzerResults.cs +++ b/src/Buildalyzer/AnalyzerResults.cs @@ -25,7 +25,7 @@ internal void Add(IEnumerable results, bool overallSuccess) public IAnalyzerResult this[string targetFramework] => _results[targetFramework]; - public IEnumerable TargetFrameworks => _results.Keys.OrderBy(e => e, StringComparer.OrdinalIgnoreCase); + public IEnumerable TargetFrameworks => _results.Keys.OrderBy(e => e, TargetFrameworkComparer.Instance); public IEnumerable Results => TargetFrameworks.Select(e => _results[e]); @@ -38,5 +38,21 @@ internal void Add(IEnumerable results, bool overallSuccess) public IEnumerator GetEnumerator() => Results.GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + + private class TargetFrameworkComparer : IComparer + { + public static TargetFrameworkComparer Instance { get; } = new TargetFrameworkComparer(); + + public int Compare(string x, string y) + { + return (string.IsNullOrEmpty(x), string.IsNullOrEmpty(y)) switch + { + (true, true) => 0, + (true, false) => +1, + (false, true) => -1, + _ => StringComparer.OrdinalIgnoreCase.Compare(x, y) + }; + } + } } } \ No newline at end of file diff --git a/tests/Buildalyzer.Tests/Integration/SimpleProjectsFixture.cs b/tests/Buildalyzer.Tests/Integration/SimpleProjectsFixture.cs index c2fb91a5..801798af 100644 --- a/tests/Buildalyzer.Tests/Integration/SimpleProjectsFixture.cs +++ b/tests/Buildalyzer.Tests/Integration/SimpleProjectsFixture.cs @@ -244,7 +244,7 @@ public void MultiTargetingBuildAllTargetFrameworksGetsSourceFiles() // Then // Multi-targeting projects product an extra result with an empty target framework that holds some MSBuild properties (I.e. the "outer" build) results.Count.ShouldBe(3); - results.TargetFrameworks.ShouldBe(new[] { string.Empty, "net462", "netstandard2.0" }, ignoreOrder: false, log.ToString()); + results.TargetFrameworks.ShouldBe(new[] { "net462", "netstandard2.0", string.Empty }, ignoreOrder: false, log.ToString()); results[string.Empty].SourceFiles.ShouldBeEmpty(); new[] {