Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make AnalyzerResults deterministically ordered #198

Merged
merged 2 commits into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/Buildalyzer/AnalyzerResults.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;

namespace Buildalyzer
{
Expand All @@ -23,17 +24,17 @@ internal void Add(IEnumerable<IAnalyzerResult> results, bool overallSuccess)

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

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

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

public int Count => _results.Count;

public bool ContainsTargetFramework(string targetFramework) => _results.ContainsKey(targetFramework);

public bool TryGetTargetFramework(string targetFramework, out IAnalyzerResult result) => _results.TryGetValue(targetFramework, out result);

public IEnumerator<IAnalyzerResult> GetEnumerator() => _results.Values.GetEnumerator();
public IEnumerator<IAnalyzerResult> GetEnumerator() => Results.GetEnumerator();

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
Expand Down
3 changes: 2 additions & 1 deletion src/Buildalyzer/Buildalyzer.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp3.1</TargetFrameworks>
<Description>A little utility to perform design-time builds of .NET projects without having to think too hard about it. Should work with any project type on any .NET runtime.</Description>
Expand All @@ -8,6 +8,7 @@
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.1.1" />
<PackageReference Include="MsBuildPipeLogger.Server" Version="1.1.3" />
<PackageReference Include="NuGet.Frameworks" Version="6.0.0" />
<PackageReference Include="System.Reflection.TypeExtensions" Version="4.7.0" />
<PackageReference Include="Microsoft.Build" Version="16.9.0" />
<PackageReference Include="Microsoft.Build.Framework" Version="16.9.0" />
Expand Down
23 changes: 23 additions & 0 deletions src/Buildalyzer/TargetFrameworkComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Collections.Generic;
using NuGet.Frameworks;

namespace Buildalyzer
{
internal class TargetFrameworkComparer : IComparer<string>
{
public static readonly TargetFrameworkComparer Instance = new TargetFrameworkComparer();

private static readonly NuGetFrameworkSorter Sorter = new NuGetFrameworkSorter();

private TargetFrameworkComparer()
{
}

public int Compare(string x, string y)
{
NuGetFramework xFramework = NuGetFramework.ParseFolder(x);
NuGetFramework yFramework = NuGetFramework.ParseFolder(y);
return Sorter.Compare(xFramework, yFramework);
}
}
}
27 changes: 4 additions & 23 deletions tests/Buildalyzer.Tests/Integration/SimpleProjectsFixture.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
Expand Down Expand Up @@ -125,10 +125,7 @@ public void GetsSourceFiles(
sourceFiles.ShouldNotBeNull(log.ToString());
new[]
{
#if Is_Windows
// Linux and Mac builds appear to omit the AssemblyAttributes.cs file
"AssemblyAttributes",
#endif
analyzer.ProjectFile.OutputType?.Equals("exe", StringComparison.OrdinalIgnoreCase) ?? false ? "Program" : "Class1",
"AssemblyInfo"
}.ShouldBeSubsetOf(sourceFiles.Select(x => Path.GetFileName(x).Split('.').TakeLast(2).First()), log.ToString());
Expand Down Expand Up @@ -188,10 +185,7 @@ public void GetsSourceFilesFromBinaryLog(
sourceFiles.ShouldNotBeNull(log.ToString());
new[]
{
#if Is_Windows
// Linux and Mac builds appear to omit the AssemblyAttributes.cs file
"AssemblyAttributes",
#endif
analyzer.ProjectFile.OutputType?.Equals("exe", StringComparison.OrdinalIgnoreCase) ?? false ? "Program" : "Class1",
"AssemblyInfo"
}.ShouldBeSubsetOf(sourceFiles.Select(x => Path.GetFileName(x).Split('.').TakeLast(2).First()), log.ToString());
Expand All @@ -205,8 +199,8 @@ public void GetsSourceFilesFromBinaryLog(
}
}

#if Is_Windows
[Test]
[Platform("win")]
public void WpfControlLibraryGetsSourceFiles()
{
// Given
Expand Down Expand Up @@ -242,18 +236,16 @@ 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[] { "net462", "netstandard2.0", string.Empty }, true, log.ToString());
results.TargetFrameworks.ShouldBe(new[] { "net462", "netstandard2.0", string.Empty }, ignoreOrder: false, log.ToString());
results[string.Empty].SourceFiles.ShouldBeEmpty();
new[]
{
// Linux and Mac builds appear to omit the AssemblyAttributes.cs file
"AssemblyAttributes",
"Class1",
"AssemblyInfo"
}.ShouldBeSubsetOf(results["net462"].SourceFiles.Select(x => Path.GetFileName(x).Split('.').TakeLast(2).First()), log.ToString());
new[]
{
// Linux and Mac builds appear to omit the AssemblyAttributes.cs file
"AssemblyAttributes",
"Class2",
"AssemblyInfo"
Expand Down Expand Up @@ -285,13 +277,11 @@ public void MultiTargetingBuildFrameworkTargetFrameworkGetsSourceFiles()
sourceFiles.ShouldNotBeNull(log.ToString());
new[]
{
// Linux and Mac builds appear to omit the AssemblyAttributes.cs file
"AssemblyAttributes",
"Class1",
"AssemblyInfo"
}.ShouldBeSubsetOf(sourceFiles.Select(x => Path.GetFileName(x).Split('.').TakeLast(2).First()), log.ToString());
}
#endif

[Test]
public void MultiTargetingBuildCoreTargetFrameworkGetsSourceFiles()
Expand All @@ -308,11 +298,8 @@ public void MultiTargetingBuildCoreTargetFrameworkGetsSourceFiles()
sourceFiles.ShouldNotBeNull(log.ToString());
new[]
{
#if Is_Windows
// Linux and Mac builds appear to omit the AssemblyAttributes.cs file
"AssemblyAttributes",
"AssemblyInfo",
#endif
"Class2"
}.ShouldBeSubsetOf(sourceFiles.Select(x => Path.GetFileName(x).Split('.').TakeLast(2).First()), log.ToString());
}
Expand Down Expand Up @@ -385,8 +372,8 @@ public void SdkProjectWithDefineContstantsGetsPreprocessorSymbols()
#endif
}

#if Is_Windows
[Test]
[Platform("win")]
public void LegacyFrameworkProjectWithPackageReferenceGetsReferences()
{
// Given
Expand Down Expand Up @@ -431,7 +418,6 @@ public void LegacyFrameworkProjectWithProjectReferenceGetsReferences()
references.ShouldContain(x => x.EndsWith("LegacyFrameworkProject.csproj"), log.ToString());
references.ShouldContain(x => x.EndsWith("LegacyFrameworkProjectWithPackageReference.csproj"), log.ToString());
}
#endif

[Test]
public void GetsProjectsInSolution()
Expand Down Expand Up @@ -501,7 +487,6 @@ public void GetsProjectGuidFromSolution([ValueSource(nameof(Preferences))] Envir
results.First().ProjectGuid.ToString().ShouldBe("016713d9-b665-4272-9980-148801a9b88f");
}

#if Is_Windows
[Test]
public void GetsProjectGuidFromProject([ValueSource(nameof(Preferences))] EnvironmentPreference preference)
{
Expand All @@ -524,7 +509,6 @@ public void GetsProjectGuidFromProject([ValueSource(nameof(Preferences))] Enviro
// so this may need to be updated periodically
results.First().ProjectGuid.ToString().ShouldBe("1ff50b40-c27b-5cea-b265-29c5436a8a7b");
}
#endif

[Test]
public void BuildsProjectWithoutLogger([ValueSource(nameof(Preferences))] EnvironmentPreference preference)
Expand Down Expand Up @@ -635,10 +619,7 @@ public void GetsSourceFilesFromBinLogFile(string path, int expectedVersion)
sourceFiles.ShouldNotBeNull(log.ToString());
new[]
{
#if Is_Windows
// Linux and Mac builds appear to omit the AssemblyAttributes.cs file
"AssemblyAttributes",
#endif
"Class1",
"AssemblyInfo"
}.ShouldBeSubsetOf(sourceFiles.Select(x => Path.GetFileName(x).Split('.').TakeLast(2).First()), log.ToString());
Expand Down