Skip to content
This repository was archived by the owner on Sep 25, 2026. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 14 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
2 changes: 1 addition & 1 deletion external/cecil
Submodule cecil updated from a27b1f to e069cd
34 changes: 22 additions & 12 deletions test/ILLink.RoslynAnalyzer.Tests/LinkerTestCases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Xunit;

Expand All @@ -13,20 +16,27 @@ namespace ILLink.RoslynAnalyzer.Tests
/// </summary>
public class LinkerTestCases : TestCaseUtils
{
[Theory]
[MemberData (nameof (TestCaseUtils.GetTestData), parameters: nameof (RequiresCapability))]
public void RequiresCapability (MethodDeclarationSyntax m, List<AttributeSyntax> attrs)
private static Lazy<IEnumerable<MetadataReference>> _additionalReferences = new Lazy<IEnumerable<MetadataReference>> (LoadDependencies);

private static IEnumerable<MetadataReference> LoadDependencies ()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not put this in LinkerTestCases.cs? It feels like GetReferenceFilesByDirName should only be used there and have the dependencies resolved before compilation (ideally through looking at the linker attributes that we use to add assemblies to the compilation).

{
switch (m.Identifier.ValueText) {
// There is a discrepancy between the way linker and the analyzer represent the location of the error,
// linker will point to the method caller and the analyzer will point to a line of code.
// The TestTypeIsBeforeFieldInit scenario is supported by the analyzer, just the diagnostic message is different
// We verify the analyzer generating the right diagnostic in RequiresUnreferencedCodeAnalyzerTests.cs
case "TestTypeIsBeforeFieldInit":
return;
List<MetadataReference> additionalReferences = new List<MetadataReference> ();
var s_refFiles = GetReferenceFilesByDirName ();
foreach (var refFile in s_refFiles["Dependencies"]) {
if (refFile.Contains ("RequiresCapability"))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels somewhat fragile looking into the string of a filename here.

additionalReferences.Add (TestCaseUtils.GetCompilation (File.ReadAllText (refFile)).Result.EmitToImageReference ());
}

RunTest<RequiresUnreferencedCodeAnalyzer> (m, attrs, UseMSBuildProperties (MSBuildPropertyOptionNames.EnableTrimAnalyzer));
return additionalReferences;
}

[Theory]
[MemberData (nameof (TestCaseUtils.GetTestData), parameters: "RequiresCapability")]
public void RequiresUnreferencedCodeCapability (SyntaxNode m, List<AttributeSyntax> attrs) =>
RunTest<RequiresUnreferencedCodeAnalyzer> (m, attrs, _additionalReferences.Value, UseMSBuildProperties (MSBuildPropertyOptionNames.EnableTrimAnalyzer));

[Theory]
[MemberData (nameof (TestCaseUtils.GetTestData), parameters: "RequiresCapability")]
public void RequiresAssemblyFilesCapability (SyntaxNode m, List<AttributeSyntax> attrs) =>
RunTest<RequiresAssemblyFilesAnalyzer> (m, attrs, _additionalReferences.Value, UseMSBuildProperties (MSBuildPropertyOptionNames.EnableSingleFileAnalyzer));
}
}
Loading