Skip to content

Commit

Permalink
Restore sorting empty target framework last
Browse files Browse the repository at this point in the history
Commit 8e85a15 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 8e85a15 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](stryker-mutator/stryker-net#2811) but I think restoring the previous sorting behaviour is a good idea anyway.
  • Loading branch information
0xced committed Jan 6, 2024
1 parent 79a8a15 commit e29caf2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
18 changes: 17 additions & 1 deletion src/Buildalyzer/AnalyzerResults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal void Add(IEnumerable<IAnalyzerResult> results, bool overallSuccess)

public IAnalyzerResult this[string targetFramework] => _results[targetFramework];

public IEnumerable<string> TargetFrameworks => _results.Keys.OrderBy(e => e, StringComparer.OrdinalIgnoreCase);
public IEnumerable<string> TargetFrameworks => _results.Keys.OrderBy(e => e, TargetFrameworkComparer.Instance);

public IEnumerable<IAnalyzerResult> Results => TargetFrameworks.Select(e => _results[e]);

Expand All @@ -38,5 +38,21 @@ internal void Add(IEnumerable<IAnalyzerResult> results, bool overallSuccess)
public IEnumerator<IAnalyzerResult> GetEnumerator() => Results.GetEnumerator();

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

private class TargetFrameworkComparer : IComparer<string>
{
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)
};
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
{
Expand Down

0 comments on commit e29caf2

Please sign in to comment.