Skip to content

Commit 8e8c6c7

Browse files
authored
Backport #2362 (#2375)
Backports the file-based testing to release/6.0.2xx
1 parent 8e455f0 commit 8e8c6c7

File tree

13 files changed

+318
-140
lines changed

13 files changed

+318
-140
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
using System.Threading.Tasks;
3+
using Xunit;
4+
5+
namespace ILLink.RoslynAnalyzer.Tests.Interop.InternalCalls
6+
{
7+
public sealed class ComTests : LinkerTestBase
8+
{
9+
protected override string TestSuiteName => "Interop/InternalCalls/Com";
10+
11+
[Fact]
12+
public Task DefaultConstructorOfParameterIsRemoved ()
13+
{
14+
return RunTest ();
15+
}
16+
17+
[Fact]
18+
public Task DefaultConstructorOfReturnTypeIsRemoved ()
19+
{
20+
return RunTest ();
21+
}
22+
23+
[Fact]
24+
public Task FieldsOfParameterAreRemoved ()
25+
{
26+
return RunTest ();
27+
}
28+
29+
[Fact]
30+
public Task FieldsOfReturnTypeAreRemoved ()
31+
{
32+
return RunTest ();
33+
}
34+
35+
[Fact]
36+
public Task FieldsOfThisAreRemoved ()
37+
{
38+
return RunTest ();
39+
}
40+
}
41+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
using System.Threading.Tasks;
3+
using Xunit;
4+
5+
namespace ILLink.RoslynAnalyzer.Tests.Interop
6+
{
7+
public sealed class InternalCallsTests : LinkerTestBase
8+
{
9+
protected override string TestSuiteName => "Interop/InternalCalls";
10+
11+
[Fact]
12+
public Task UnusedDefaultConstructorIsRemoved ()
13+
{
14+
return RunTest ();
15+
}
16+
17+
[Fact]
18+
public Task UnusedFieldsOfTypesAreNotRemoved ()
19+
{
20+
return RunTest ();
21+
}
22+
23+
[Fact]
24+
public Task UnusedFieldsOfTypesWhenHasThisAreNotRemoved ()
25+
{
26+
return RunTest ();
27+
}
28+
29+
[Fact]
30+
public Task DefaultConstructorOfReturnTypeIsNotRemoved ()
31+
{
32+
return RunTest ();
33+
}
34+
35+
[Fact]
36+
public Task UnusedDefaultConstructorOfTypePassedByRefIsNotRemoved ()
37+
{
38+
return RunTest ();
39+
}
40+
41+
[Fact]
42+
public Task UnusedFieldsOfTypesPassedByRefAreNotRemoved ()
43+
{
44+
return RunTest ();
45+
}
46+
}
47+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
using System.Threading.Tasks;
3+
using Xunit;
4+
5+
namespace ILLink.RoslynAnalyzer.Tests.Interop.PInvoke
6+
{
7+
public sealed class ComTests : LinkerTestBase
8+
{
9+
protected override string TestSuiteName => "Interop/PInvoke/Com";
10+
11+
[Fact]
12+
public Task DefaultConstructorOfParameterIsRemoved ()
13+
{
14+
return RunTest ();
15+
}
16+
17+
[Fact]
18+
public Task DefaultConstructorOfReturnTypeIsRemoved ()
19+
{
20+
return RunTest ();
21+
}
22+
23+
[Fact]
24+
public Task FieldsOfParameterAreRemoved ()
25+
{
26+
return RunTest ();
27+
}
28+
29+
[Fact]
30+
public Task FieldsOfReturnTypeAreRemoved ()
31+
{
32+
return RunTest ();
33+
}
34+
35+
[Fact]
36+
public Task FieldsOfThisAreRemoved ()
37+
{
38+
return RunTest ();
39+
}
40+
}
41+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
using System.Threading.Tasks;
3+
using Xunit;
4+
5+
namespace ILLink.RoslynAnalyzer.Tests.Interop.PInvoke
6+
{
7+
public class IndividualTests : LinkerTestBase
8+
{
9+
protected override string TestSuiteName => "Interop/PInvoke/Individual";
10+
11+
[Fact]
12+
public Task CanOutputPInvokes ()
13+
{
14+
return RunTest ();
15+
}
16+
}
17+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
using System.Threading.Tasks;
3+
using Xunit;
4+
5+
namespace ILLink.RoslynAnalyzer.Tests.Interop
6+
{
7+
public sealed class PInvokeTests : LinkerTestBase
8+
{
9+
protected override string TestSuiteName => "Interop/PInvoke";
10+
11+
12+
[Fact]
13+
public Task UnusedDefaultConstructorIsRemoved ()
14+
{
15+
return RunTest ();
16+
}
17+
18+
[Fact]
19+
public Task UnusedFieldsOfTypesPassedByRefAreNotRemoved ()
20+
{
21+
return RunTest ();
22+
}
23+
24+
[Fact]
25+
public Task DefaultConstructorOfReturnTypeIsNotRemoved ()
26+
{
27+
return RunTest ();
28+
}
29+
30+
[Fact]
31+
public Task UnusedDefaultConstructorOfTypePassedByRefIsNotRemoved ()
32+
{
33+
return RunTest ();
34+
}
35+
36+
[Fact]
37+
public Task UnusedFieldsOfTypesAreNotRemoved ()
38+
{
39+
return RunTest ();
40+
}
41+
42+
[Fact]
43+
public Task UnusedPInvoke ()
44+
{
45+
return RunTest ();
46+
}
47+
}
48+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
using System.Threading.Tasks;
3+
using Xunit;
4+
5+
namespace ILLink.RoslynAnalyzer.Tests.Interop.PInvoke
6+
{
7+
public sealed class WarningsTests : LinkerTestBase
8+
{
9+
protected override string TestSuiteName => "Interop/PInvoke/Warnings";
10+
11+
[Fact]
12+
public Task ComPInvokeWarning ()
13+
{
14+
return RunTest ();
15+
}
16+
}
17+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
using System.Runtime.CompilerServices;
3+
using System.Threading.Tasks;
4+
5+
namespace ILLink.RoslynAnalyzer.Tests
6+
{
7+
public abstract class LinkerTestBase : TestCaseUtils
8+
{
9+
protected abstract string TestSuiteName { get; }
10+
11+
private static readonly (string, string)[] MSBuildProperties = UseMSBuildProperties (
12+
MSBuildPropertyOptionNames.EnableTrimAnalyzer,
13+
MSBuildPropertyOptionNames.EnableSingleFileAnalyzer);
14+
15+
protected Task RunTest ([CallerMemberName] string testName = "")
16+
{
17+
return RunTestFile (TestSuiteName, testName, MSBuildProperties);
18+
}
19+
}
20+
}

test/ILLink.RoslynAnalyzer.Tests/LinkerTestCases.cs

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
using System.Threading.Tasks;
3+
using Xunit;
4+
5+
namespace ILLink.RoslynAnalyzer.Tests
6+
{
7+
public sealed class RequiresCapabilityTests : LinkerTestBase
8+
{
9+
protected override string TestSuiteName => "RequiresCapability";
10+
11+
[Fact]
12+
public Task RequiresCapability ()
13+
{
14+
return RunTest (nameof (RequiresCapability));
15+
}
16+
17+
[Fact]
18+
public Task RequiresCapabilityFromCopiedAssembly ()
19+
{
20+
return RunTest (nameof (RequiresCapabilityFromCopiedAssembly));
21+
}
22+
23+
[Fact]
24+
public Task RequiresCapabilityReflectionAnalysisEnabled ()
25+
{
26+
return RunTest (nameof (RequiresCapabilityReflectionAnalysisEnabled));
27+
}
28+
29+
[Fact]
30+
public Task RequiresInCompilerGeneratedCode ()
31+
{
32+
return RunTest (nameof (RequiresInCompilerGeneratedCode));
33+
}
34+
35+
[Fact]
36+
public Task RequiresOnAttributeCtor ()
37+
{
38+
return RunTest (nameof (RequiresOnAttributeCtor));
39+
}
40+
}
41+
}

test/ILLink.RoslynAnalyzer.Tests/TestCase.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,34 +28,28 @@ public TestCase (MemberDeclarationSyntax memberSyntax, IEnumerable<AttributeSynt
2828
Attributes = attributes;
2929
}
3030

31-
public void Run (params (string, string)[] MSBuildProperties)
31+
public void Run ((CompilationWithAnalyzers, SemanticModel) compAndModel)
3232
{
33-
var testSyntaxTree = MemberSyntax.SyntaxTree.GetRoot ().SyntaxTree;
33+
var testSyntaxTree = MemberSyntax.SyntaxTree;
3434
var testDependenciesSource = GetTestDependencies (testSyntaxTree)
3535
.Select (testDependency => CSharpSyntaxTree.ParseText (File.ReadAllText (testDependency)));
3636

37-
var test = new TestChecker (
38-
MemberSyntax,
39-
TestCaseCompilation.CreateCompilation (
40-
testSyntaxTree,
41-
MSBuildProperties,
42-
additionalSources: testDependenciesSource).Result);
43-
37+
var test = new TestChecker (MemberSyntax, compAndModel);
4438
test.ValidateAttributes (Attributes);
4539
}
4640

47-
private static IEnumerable<string> GetTestDependencies (SyntaxTree testSyntaxTree)
41+
public static IEnumerable<string> GetTestDependencies (SyntaxTree testSyntaxTree)
4842
{
49-
TestCaseUtils.GetDirectoryPaths (out var rootSourceDir, out _);
43+
LinkerTestBase.GetDirectoryPaths (out var rootSourceDir, out _);
5044
foreach (var attribute in testSyntaxTree.GetRoot ().DescendantNodes ().OfType<AttributeSyntax> ()) {
5145
if (attribute.Name.ToString () != "SetupCompileBefore")
5246
continue;
5347

5448
var testNamespace = testSyntaxTree.GetRoot ().DescendantNodes ().OfType<NamespaceDeclarationSyntax> ().Single ().Name.ToString ();
5549
var testSuiteName = testNamespace.Substring (testNamespace.LastIndexOf ('.') + 1);
56-
var args = TestCaseUtils.GetAttributeArguments (attribute);
50+
var args = LinkerTestBase.GetAttributeArguments (attribute);
5751
foreach (var sourceFile in ((ImplicitArrayCreationExpressionSyntax) args["#1"]).DescendantNodes ().OfType<LiteralExpressionSyntax> ())
58-
yield return Path.Combine (rootSourceDir, testSuiteName, TestCaseUtils.GetStringFromExpression (sourceFile));
52+
yield return Path.Combine (rootSourceDir, testSuiteName, LinkerTestBase.GetStringFromExpression (sourceFile));
5953
}
6054
}
6155
}

0 commit comments

Comments
 (0)