Skip to content

Commit 369a9b6

Browse files
sharwellTomFinley
authored andcommitted
Update to Microsoft.CodeAnalysis.Testing (dotnet#2562)
Fixes dotnet#2480
1 parent d1a63ba commit 369a9b6

20 files changed

+455
-973
lines changed

Directory.Build.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
https://api.nuget.org/v3/index.json;
1919
https://dotnetfeed.blob.core.windows.net/dotnet-core/index.json;
2020
https://dotnet.myget.org/F/dotnet-core/api/v3/index.json;
21+
https://dotnet.myget.org/F/roslyn-analyzers/api/v3/index.json;
2122
</RestoreSources>
2223
</PropertyGroup>
2324

build/Dependencies.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
<!-- Test-only Dependencies -->
4343
<PropertyGroup>
4444
<BenchmarkDotNetVersion>0.11.3</BenchmarkDotNetVersion>
45+
<MicrosoftCodeAnalysisTestingVersion>1.0.0-beta1-63812-02</MicrosoftCodeAnalysisTestingVersion>
4546
<MicrosoftMLTestModelsPackageVersion>0.0.3-test</MicrosoftMLTestModelsPackageVersion>
4647
<MicrosoftMLTensorFlowTestModelsVersion>0.0.11-test</MicrosoftMLTensorFlowTestModelsVersion>
4748
<MicrosoftMLOnnxTestModelsVersion>0.0.4-test</MicrosoftMLOnnxTestModelsVersion>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Runtime.CompilerServices;
6+
7+
[assembly: InternalsVisibleTo("Microsoft.ML.CodeAnalyzer.Tests, PublicKey=002400000480000094000000060200000024000052534131000400000100010015c01ae1f50e8cc09ba9eac9147cf8fd9fce2cfe9f8dce4f7301c4132ca9fb50ce8cbf1df4dc18dd4d210e4345c744ecb3365ed327efdbc52603faa5e21daa11234c8c4a73e51f03bf192544581ebe107adee3a34928e39d04e524a9ce729d5090bfd7dad9d10c722c0def9ccc08ff0a03790e48bcd1f9b6c476063e1966a1c4")]

test/Microsoft.ML.CodeAnalyzer.Tests/Code/BestFriendOnPublicDeclarationTest.cs

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,61 +3,51 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6-
using System.Collections.Generic;
7-
using System.Collections.Immutable;
8-
using System.Linq;
9-
using System.Reflection;
10-
using Microsoft.CodeAnalysis;
11-
using Microsoft.CodeAnalysis.Diagnostics;
6+
using System.Threading.Tasks;
7+
using Microsoft.CodeAnalysis.Testing;
128
using Microsoft.ML.CodeAnalyzer.Tests.Helpers;
139
using Xunit;
10+
using VerifyCS = Microsoft.ML.CodeAnalyzer.Tests.Helpers.CSharpCodeFixVerifier<
11+
Microsoft.ML.InternalCodeAnalyzer.BestFriendOnPublicDeclarationsAnalyzer,
12+
Microsoft.CodeAnalysis.Testing.EmptyCodeFixProvider>;
1413

1514
namespace Microsoft.ML.InternalCodeAnalyzer.Tests
1615
{
17-
public sealed class BestFriendOnPublicDeclarationTest : DiagnosticVerifier<BestFriendOnPublicDeclarationsAnalyzer>
16+
public sealed class BestFriendOnPublicDeclarationTest
1817
{
1918
private readonly Lazy<string> SourceAttribute = TestUtils.LazySource("BestFriendAttribute.cs");
2019
private readonly Lazy<string> SourceDeclaration = TestUtils.LazySource("BestFriendOnPublicDeclaration.cs");
2120

2221
[Fact]
23-
public void BestFriendOnPublicDeclaration()
22+
public async Task BestFriendOnPublicDeclaration()
2423
{
25-
Solution solution = null;
26-
var projA = CreateProject("ProjectA", ref solution, SourceDeclaration.Value, SourceAttribute.Value);
27-
28-
var analyzer = new BestFriendOnPublicDeclarationsAnalyzer();
29-
30-
var refs = new List<MetadataReference> {
31-
RefFromType<object>(), RefFromType<Attribute>(),
32-
MetadataReference.CreateFromFile(Assembly.Load("netstandard, Version=2.0.0.0").Location),
33-
MetadataReference.CreateFromFile(Assembly.Load("System.Runtime, Version=0.0.0.0").Location)
24+
var expected = new DiagnosticResult[] {
25+
VerifyCS.Diagnostic().WithLocation(8, 6).WithArguments("PublicClass"),
26+
VerifyCS.Diagnostic().WithLocation(11, 10).WithArguments("PublicField"),
27+
VerifyCS.Diagnostic().WithLocation(14, 10).WithArguments("PublicProperty"),
28+
VerifyCS.Diagnostic().WithLocation(20, 10).WithArguments("PublicMethod"),
29+
VerifyCS.Diagnostic().WithLocation(26, 10).WithArguments("PublicDelegate"),
30+
VerifyCS.Diagnostic().WithLocation(29, 10).WithArguments("PublicClass"),
31+
VerifyCS.Diagnostic().WithLocation(35, 6).WithArguments("PublicStruct"),
32+
VerifyCS.Diagnostic().WithLocation(40, 6).WithArguments("PublicEnum"),
33+
VerifyCS.Diagnostic().WithLocation(47, 6).WithArguments("PublicInterface"),
34+
VerifyCS.Diagnostic().WithLocation(102, 10).WithArguments("PublicMethod"),
3435
};
3536

36-
var comp = projA.GetCompilationAsync().Result.WithReferences(refs.ToArray());
37-
var compilationWithAnalyzers = comp.WithAnalyzers(ImmutableArray.Create((DiagnosticAnalyzer)analyzer));
38-
var allDiags = compilationWithAnalyzers.GetAnalyzerDiagnosticsAsync().Result;
39-
40-
var projectTrees = new HashSet<SyntaxTree>(projA.Documents.Select(r => r.GetSyntaxTreeAsync().Result));
41-
var diags = allDiags
42-
.Where(d => d.Location == Location.None || d.Location.IsInMetadata || projectTrees.Contains(d.Location.SourceTree))
43-
.OrderBy(d => d.Location.SourceSpan.Start).ToArray();
44-
45-
var diag = analyzer.SupportedDiagnostics[0];
46-
var expected = new DiagnosticResult[] {
47-
diag.CreateDiagnosticResult(8, 6, "PublicClass"),
48-
diag.CreateDiagnosticResult(11, 10, "PublicField"),
49-
diag.CreateDiagnosticResult(14, 10, "PublicProperty"),
50-
diag.CreateDiagnosticResult(20, 10, "PublicMethod"),
51-
diag.CreateDiagnosticResult(26, 10, "PublicDelegate"),
52-
diag.CreateDiagnosticResult(29, 10, "PublicClass"),
53-
diag.CreateDiagnosticResult(35, 6, "PublicStruct"),
54-
diag.CreateDiagnosticResult(40, 6, "PublicEnum"),
55-
diag.CreateDiagnosticResult(47, 6, "PublicInterface"),
56-
diag.CreateDiagnosticResult(102, 10, "PublicMethod")
37+
var test = new VerifyCS.Test
38+
{
39+
TestState =
40+
{
41+
Sources =
42+
{
43+
SourceDeclaration.Value,
44+
("BestFriendAttribute.cs", SourceAttribute.Value),
45+
},
46+
},
5747
};
5848

59-
VerifyDiagnosticResults(diags, analyzer, expected);
49+
test.ExpectedDiagnostics.AddRange(expected);
50+
await test.RunAsync();
6051
}
6152
}
6253
}
63-

test/Microsoft.ML.CodeAnalyzer.Tests/Code/BestFriendTest.cs

Lines changed: 54 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6-
using System.Collections.Generic;
7-
using System.Collections.Immutable;
8-
using System.IO;
96
using System.Linq;
10-
using System.Reflection;
7+
using System.Threading.Tasks;
118
using Microsoft.CodeAnalysis;
129
using Microsoft.CodeAnalysis.CSharp;
13-
using Microsoft.CodeAnalysis.Diagnostics;
10+
using Microsoft.CodeAnalysis.Testing;
1411
using Microsoft.ML.CodeAnalyzer.Tests.Helpers;
1512
using Xunit;
13+
using VerifyCS = Microsoft.ML.CodeAnalyzer.Tests.Helpers.CSharpCodeFixVerifier<
14+
Microsoft.ML.InternalCodeAnalyzer.BestFriendAnalyzer,
15+
Microsoft.CodeAnalysis.Testing.EmptyCodeFixProvider>;
1616

1717
namespace Microsoft.ML.InternalCodeAnalyzer.Tests
1818
{
19-
public sealed class BestFriendTest : DiagnosticVerifier<BestFriendAnalyzer>
19+
public sealed class BestFriendTest
2020
{
2121
// We do things in this somewhat odd way rather than just referencing the Core assembly directly,
2222
// because we certainly want the best friend attribute itself to be internal, but the assembly
@@ -29,70 +29,67 @@ public sealed class BestFriendTest : DiagnosticVerifier<BestFriendAnalyzer>
2929
private readonly Lazy<string> SourceUser = TestUtils.LazySource("BestFriendUser.cs");
3030

3131
[Fact]
32-
public void BestFriend()
32+
public async Task BestFriend()
3333
{
3434
// The setup to this one is a bit more involved than many of the analyzer tests,
3535
// because in this case we have to actually set up *two* assemblies, where the
3636
// first considers the second a friend. But, setting up this dependency structure
3737
// so that things actually compile to the point where the analyzer can actually do
3838
// its work is rather involved.
39-
Solution solution = null;
40-
var projA = CreateProject("ProjectA", ref solution, SourceDeclaration.Value);
41-
var projB = CreateProject("ProjectB", ref solution, SourceUser.Value);
42-
solution = solution.AddProjectReference(projB.Id, new ProjectReference(projA.Id));
4339

44-
var analyzer = new BestFriendAnalyzer();
40+
var expected = new DiagnosticResult[] {
41+
VerifyCS.Diagnostic().WithLocation(10, 31).WithArguments("A"),
42+
VerifyCS.Diagnostic().WithLocation(11, 31).WithArguments("A"),
43+
VerifyCS.Diagnostic().WithLocation(11, 33).WithArguments("My"),
44+
VerifyCS.Diagnostic().WithLocation(14, 33).WithArguments("Awhile"),
45+
VerifyCS.Diagnostic().WithLocation(15, 33).WithArguments("And"),
46+
VerifyCS.Diagnostic().WithLocation(18, 13).WithArguments("A"),
47+
VerifyCS.Diagnostic().WithLocation(18, 25).WithArguments("A"),
48+
new DiagnosticResult("CS0122", DiagnosticSeverity.Error).WithLocation(23, 21).WithMessage("'D.D(float)' is inaccessible due to its protection level"),
49+
VerifyCS.Diagnostic().WithLocation(25, 13).WithArguments("IA"),
50+
VerifyCS.Diagnostic().WithLocation(25, 23).WithArguments("IA"),
51+
VerifyCS.Diagnostic().WithLocation(32, 38).WithArguments(".ctor"),
52+
VerifyCS.Diagnostic().WithLocation(38, 38).WithArguments(".ctor"),
53+
};
4554

46-
MetadataReference peRef;
47-
var refs = new List<MetadataReference> {
48-
RefFromType<object>(), RefFromType<Attribute>(),
49-
MetadataReference.CreateFromFile(Assembly.Load("netstandard, Version=2.0.0.0").Location),
50-
MetadataReference.CreateFromFile(Assembly.Load("System.Runtime, Version=0.0.0.0").Location)
51-
};
52-
using (var ms = new MemoryStream())
55+
VerifyCS.Test test = null;
56+
test = new VerifyCS.Test
5357
{
54-
// We also test whether private protected can be accessed, so we need C# 7.2 at least.
55-
var parseOpts = new CSharpParseOptions(LanguageVersion.CSharp7_3);
56-
var tree = CSharpSyntaxTree.ParseText(SourceDeclaration.Value, parseOpts);
57-
var treeAttr = CSharpSyntaxTree.ParseText(SourceAttribute.Value, parseOpts);
58-
59-
var compOpts = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);
60-
var innerComp = CSharpCompilation.Create(projA.Name, new[] { tree, treeAttr }, refs, compOpts);
61-
62-
var emitResult = innerComp.Emit(ms);
63-
Assert.True(emitResult.Success, $"Compilation of {projA.Name} did not work. Diagnostics: {string.Join(" || ", emitResult.Diagnostics)}");
58+
LanguageVersion = LanguageVersion.CSharp7_2,
59+
TestState =
60+
{
61+
Sources = { SourceUser.Value },
62+
AdditionalReferences = { MetadataReference.CreateFromFile(typeof(Console).Assembly.Location) },
63+
},
64+
SolutionTransforms =
65+
{
66+
(solution, projectId) =>
67+
{
68+
var projectA = solution.AddProject("ProjectA", "ProjectA", LanguageNames.CSharp);
69+
projectA = projectA.AddDocument("BestFriendAttribute.cs", SourceAttribute.Value).Project;
70+
projectA = projectA.AddDocument("BestFriendDeclaration.cs", SourceDeclaration.Value).Project;
71+
projectA = projectA.WithParseOptions(((CSharpParseOptions)projectA.ParseOptions).WithLanguageVersion(LanguageVersion.CSharp7_2));
72+
projectA = projectA.WithCompilationOptions(projectA.CompilationOptions.WithOutputKind(OutputKind.DynamicallyLinkedLibrary));
73+
projectA = projectA.WithMetadataReferences(solution.GetProject(projectId).MetadataReferences.Concat(test.TestState.AdditionalReferences));
74+
solution = projectA.Solution;
6475

65-
var peImage = ms.ToArray().ToImmutableArray();
66-
peRef = MetadataReference.CreateFromImage(peImage);
67-
}
76+
solution = solution.AddProjectReference(projectId, new ProjectReference(projectA.Id));
77+
solution = solution.WithProjectAssemblyName(projectId, "ProjectB");
6878

69-
refs.Add(peRef);
70-
var comp = projB.GetCompilationAsync().Result
71-
.WithReferences(refs.ToArray());
72-
var compilationWithAnalyzers = comp.WithAnalyzers(ImmutableArray.Create((DiagnosticAnalyzer)analyzer));
73-
var allDiags = compilationWithAnalyzers.GetAnalyzerDiagnosticsAsync().Result;
74-
75-
var projectTrees = new HashSet<SyntaxTree>(projB.Documents.Select(r => r.GetSyntaxTreeAsync().Result));
76-
var diags = allDiags
77-
.Where(d => d.Location == Location.None || d.Location.IsInMetadata || projectTrees.Contains(d.Location.SourceTree))
78-
.OrderBy(d => d.Location.SourceSpan.Start).ToArray();
79-
80-
var diag = analyzer.SupportedDiagnostics[0];
81-
var expected = new DiagnosticResult[] {
82-
diag.CreateDiagnosticResult(10, 31, "A"),
83-
diag.CreateDiagnosticResult(11, 31, "A"),
84-
diag.CreateDiagnosticResult(11, 33, "My"),
85-
diag.CreateDiagnosticResult(14, 33, "Awhile"),
86-
diag.CreateDiagnosticResult(15, 33, "And"),
87-
diag.CreateDiagnosticResult(18, 13, "A"),
88-
diag.CreateDiagnosticResult(18, 25, "A"),
89-
diag.CreateDiagnosticResult(25, 13, "IA"),
90-
diag.CreateDiagnosticResult(25, 23, "IA"),
91-
diag.CreateDiagnosticResult(32, 38, ".ctor"),
92-
diag.CreateDiagnosticResult(38, 38, ".ctor"),
79+
return solution;
80+
},
81+
},
9382
};
9483

95-
VerifyDiagnosticResults(diags, analyzer, expected);
84+
// Remove these assemblies, or an additional definition of BestFriendAttribute could exist and the
85+
// compilation will not be able to locate a single definition for use in the analyzer.
86+
test.TestState.AdditionalReferences.Remove(AdditionalMetadataReferences.MLNetCoreReference);
87+
test.TestState.AdditionalReferences.Remove(AdditionalMetadataReferences.MLNetDataReference);
88+
test.TestState.AdditionalReferences.Remove(AdditionalMetadataReferences.MLNetStaticPipeReference);
89+
90+
test.Exclusions &= ~AnalysisExclusions.GeneratedCode;
91+
test.ExpectedDiagnostics.AddRange(expected);
92+
await test.RunAsync();
9693
}
9794
}
9895
}

0 commit comments

Comments
 (0)