Skip to content

Commit

Permalink
feat: Restore support for aliases via Buildalyzer (#3132)
Browse files Browse the repository at this point in the history
Co-authored-by: Rouke Broersma <[email protected]>
  • Loading branch information
dupdob and rouke-broersma authored Dec 20, 2024
1 parent 035fce3 commit 70e676d
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
extern alias TheLib;

using System.Linq;

namespace TargetProject.Constructs;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
extern alias TheLog;

using System.Diagnostics;
using TheLog::Serilog.Configuration;

namespace TargetProject.StrykerFeatures;
public class UseAssert
Expand All @@ -7,6 +10,8 @@ public class UseAssert

public void IncrementCounter()
{
// this is only to check for alias support
var test = new BatchingOptions();
_counter++;
Debug.Assert(_counter > 0, "Counter should be greater than 0");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@
project reference path (it also ensures the import above was properly
evaluated).
-->
<ProjectReference Include="..\$(LibraryDir)\Library.csproj" />
<ProjectReference Include="..\$(LibraryDir)\Library.csproj">
<Aliases>TheLib</Aliases>
</ProjectReference>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>$(AssemblyName).Tests</_Parameter1>
</AssemblyAttribute>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.11.0" />
<PackageReference Include="Serilog" Version="4.1.0">
<Aliases>TheLog</Aliases>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ public int Subtract(int first, int second)
public void CompilingProcessTests_ShouldSupportPackageReferenceAliases()
{
var syntaxTree = CSharpSyntaxTree.ParseText(@"
extern alias TestAlias;
using TestAlias::System;
extern alias TheAlias;
using TheAlias::System;
namespace ExampleProject
{
Expand All @@ -88,6 +88,10 @@ public int Subtract(int first, int second)
}
}
}");
var alias = new Dictionary<string, ImmutableArray<string>>();
var immutableArray = ImmutableArray.Create("TheAlias");
alias[typeof(object).Assembly.Location]=immutableArray;

var input = new MutationTestInput()
{
SourceProjectInfo = new SourceProjectInfo
Expand All @@ -101,21 +105,8 @@ public int Subtract(int first, int second)
{ "TargetFileName", "TargetFileName.dll" },
},
// add a reference to system so the example code can compile
references: new[] { typeof(object).Assembly.Location },
packageReferences: new ReadOnlyDictionary<string, IReadOnlyDictionary<string, string>>(
new Dictionary<string, IReadOnlyDictionary<string, string>>
{
{
typeof(object).Assembly.GetName().Name,
new ReadOnlyDictionary<string, string>(
new Dictionary<string, string>
{
{ "Aliases", "TestAlias" }
}
)
}
}
)
references: [typeof(object).Assembly.Location],
aliases: alias.ToImmutableDictionary()
).Object
}
};
Expand Down
6 changes: 5 additions & 1 deletion src/Stryker.Core/Stryker.Core.UnitTest/TestHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using Buildalyzer;
using Moq;
Expand All @@ -15,7 +16,8 @@ public static Mock<IAnalyzerResult> SetupProjectAnalyzerResult(Dictionary<string
IReadOnlyDictionary<string, IReadOnlyDictionary<string, string>> packageReferences = null,
string[] references = null,
string[] preprocessorSymbols = null,
string[] analyzers = null
string[] analyzers = null,
ImmutableDictionary<string, ImmutableArray<string>> aliases = null
)
{
var analyzerResultMock = new Mock<IAnalyzerResult>();
Expand Down Expand Up @@ -67,7 +69,9 @@ public static Mock<IAnalyzerResult> SetupProjectAnalyzerResult(Dictionary<string
{
analyzerResultMock.Setup(x => x.AnalyzerReferences).Returns(analyzers);
}
aliases ??= ImmutableDictionary<string, ImmutableArray<string>>.Empty;
analyzerResultMock.Setup(x => x.Items).Returns(new Dictionary<string, IProjectItem[]>());
analyzerResultMock.Setup(x => x.ReferenceAliases).Returns(aliases);

return analyzerResultMock;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,28 +107,14 @@ public static IEnumerable<MetadataReference> LoadReferences(this IAnalyzerResult
{
foreach (var reference in analyzerResult.References)
{
var referenceFileNameWithoutExtension = Path.GetFileNameWithoutExtension(reference);
string packageWithAlias = null;

if (analyzerResult.PackageReferences is not null)
if (!analyzerResult.ReferenceAliases.TryGetValue(reference, out var aliases))
{
// Check for any matching package reference with an alias using LINQ
packageWithAlias = analyzerResult.PackageReferences
.Where(pr => pr.Key == referenceFileNameWithoutExtension && pr.Value.ContainsKey("Aliases"))
.Select(pr => pr.Value["Aliases"])
.FirstOrDefault();
}

if (packageWithAlias is not null)
{
// Return the reference with the alias
yield return MetadataReference.CreateFromFile(reference).WithAliases(new[] { packageWithAlias });
}
else
{
// If no alias is found, return the reference without aliases
yield return MetadataReference.CreateFromFile(reference);
aliases = [];
}

// If no alias is found, return the reference without aliases
yield return MetadataReference.CreateFromFile(reference).WithAliases(aliases);
}
}

Expand Down

0 comments on commit 70e676d

Please sign in to comment.