Skip to content

Commit ca2067b

Browse files
committed
Configure reference assemblies
1 parent f4a000b commit ca2067b

File tree

3 files changed

+39
-18
lines changed

3 files changed

+39
-18
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test/HelperTests/IndentationHelperTests.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,9 @@ namespace StyleCop.Analyzers.Test.HelperTests
99
using Microsoft.CodeAnalysis;
1010
using Microsoft.CodeAnalysis.CSharp;
1111
using Microsoft.CodeAnalysis.Formatting;
12-
using Microsoft.CodeAnalysis.Testing;
13-
using Microsoft.CodeAnalysis.Testing.Verifiers;
1412
using Microsoft.CodeAnalysis.Text;
1513
using StyleCop.Analyzers.Helpers;
1614
using StyleCop.Analyzers.Settings.ObjectModel;
17-
using StyleCop.Analyzers.Test.Helpers;
1815
using StyleCop.Analyzers.Test.Verifiers;
1916
using Xunit;
2017

@@ -114,7 +111,7 @@ public class IndentationHelperTests
114111
public async Task VerifyGetIndentationStepsAsync(string indentationString, int expectedIndentationSteps, int indentationSize, int tabSize)
115112
{
116113
var testSource = $"{indentationString}public class TestClass {{}}";
117-
var document = CreateTestDocument(testSource, indentationSize, false, tabSize);
114+
var document = await CreateTestDocumentAsync(testSource, indentationSize, false, tabSize, CancellationToken.None).ConfigureAwait(false);
118115
StyleCopSettings settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, CancellationToken.None);
119116

120117
var syntaxRoot = await document.GetSyntaxRootAsync().ConfigureAwait(false);
@@ -132,7 +129,7 @@ public async Task VerifyGetIndentationStepsAsync(string indentationString, int e
132129
public async Task VerifyGetIndentationStepsForTokenNotAtStartOfLineAsync()
133130
{
134131
var testSource = " public class TestClass {}";
135-
var document = CreateTestDocument(testSource);
132+
var document = await CreateTestDocumentAsync(testSource, cancellationToken: CancellationToken.None).ConfigureAwait(false);
136133
StyleCopSettings settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, CancellationToken.None);
137134

138135
var syntaxRoot = await document.GetSyntaxRootAsync().ConfigureAwait(false);
@@ -141,7 +138,7 @@ public async Task VerifyGetIndentationStepsForTokenNotAtStartOfLineAsync()
141138
Assert.Equal(0, IndentationHelper.GetIndentationSteps(settings.Indentation, secondToken));
142139
}
143140

144-
private static Document CreateTestDocument(string source, int indentationSize = 4, bool useTabs = false, int tabSize = 4)
141+
private static async Task<Document> CreateTestDocumentAsync(string source, int indentationSize = 4, bool useTabs = false, int tabSize = 4, CancellationToken cancellationToken = default)
145142
{
146143
var workspace = GenericAnalyzerTest.CreateWorkspace();
147144
workspace.Options = workspace.Options
@@ -152,15 +149,12 @@ private static Document CreateTestDocument(string source, int indentationSize =
152149
var projectId = ProjectId.CreateNewId();
153150
var documentId = DocumentId.CreateNewId(projectId);
154151
var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true);
152+
var references = await GenericAnalyzerTest.ReferenceAssemblies.ResolveAsync(LanguageNames.CSharp, cancellationToken).ConfigureAwait(false);
155153

156154
var solution = workspace.CurrentSolution
157155
.AddProject(projectId, TestProjectName, TestProjectName, LanguageNames.CSharp)
158156
.WithProjectCompilationOptions(projectId, compilationOptions)
159-
.AddMetadataReference(projectId, MetadataReferences.CorlibReference)
160-
.AddMetadataReference(projectId, MetadataReferences.SystemReference)
161-
.AddMetadataReference(projectId, MetadataReferences.SystemCoreReference)
162-
.AddMetadataReference(projectId, GenericAnalyzerTest.CSharpSymbolsReference)
163-
.AddMetadataReference(projectId, MetadataReferences.CodeAnalysisReference)
157+
.AddMetadataReferences(projectId, references)
164158
.AddDocument(documentId, TestFilename, SourceText.From(source));
165159

166160
StyleCopSettings defaultSettings = new StyleCopSettings();

StyleCop.Analyzers/StyleCop.Analyzers.Test/Verifiers/GenericAnalyzerTest.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,35 @@
44
namespace StyleCop.Analyzers.Test.Verifiers
55
{
66
using System;
7+
using System.Collections.Immutable;
78
using System.Threading;
89
using System.Threading.Tasks;
910
using Microsoft.CodeAnalysis;
10-
using Microsoft.CodeAnalysis.CSharp;
1111
using Microsoft.CodeAnalysis.Host.Mef;
12+
using Microsoft.CodeAnalysis.Testing;
1213
using Microsoft.VisualStudio.Composition;
1314

1415
internal static class GenericAnalyzerTest
1516
{
16-
internal static readonly MetadataReference CSharpSymbolsReference = MetadataReference.CreateFromFile(typeof(CSharpCompilation).Assembly.Location);
17+
internal static readonly ReferenceAssemblies ReferenceAssemblies;
1718

1819
private static readonly Lazy<IExportProviderFactory> ExportProviderFactory;
1920

2021
static GenericAnalyzerTest()
2122
{
23+
string codeAnalysisTestVersion =
24+
typeof(Compilation).Assembly.GetName().Version.Major switch
25+
{
26+
1 => "1.2.1",
27+
2 => "2.8.2",
28+
3 => "3.3.1",
29+
_ => throw new InvalidOperationException("Unknown version."),
30+
};
31+
32+
ReferenceAssemblies = ReferenceAssemblies.Default.AddPackages(ImmutableArray.Create(
33+
new PackageIdentity("Microsoft.CodeAnalysis.CSharp", codeAnalysisTestVersion),
34+
new PackageIdentity("System.ValueTuple", "4.5.0")));
35+
2236
ExportProviderFactory = new Lazy<IExportProviderFactory>(
2337
() =>
2438
{

StyleCop.Analyzers/StyleCop.Analyzers.Test/Verifiers/StyleCopCodeFixVerifier`2.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ namespace StyleCop.Analyzers.Test.Verifiers
55
{
66
using System;
77
using System.Collections.Generic;
8+
using System.IO;
9+
using System.Linq;
810
using System.Reflection;
911
using System.Threading;
1012
using System.Threading.Tasks;
@@ -97,14 +99,14 @@ public CSharpTest()
9799

98100
public CSharpTest(LanguageVersion? languageVersion)
99101
{
102+
this.ReferenceAssemblies = GenericAnalyzerTest.ReferenceAssemblies;
103+
100104
this.OptionsTransforms.Add(options =>
101105
options
102106
.WithChangedOption(FormattingOptions.IndentationSize, this.Language, this.IndentationSize)
103107
.WithChangedOption(FormattingOptions.TabSize, this.Language, this.TabSize)
104108
.WithChangedOption(FormattingOptions.UseTabs, this.Language, this.UseTabs));
105109

106-
this.TestState.AdditionalReferences.Add(GenericAnalyzerTest.CSharpSymbolsReference);
107-
this.TestState.AdditionalReferences.Add(Netstandard20Reference);
108110
this.TestState.AdditionalFilesFactories.Add(GenerateSettingsFile);
109111
this.CodeActionValidationMode = CodeActionValidationMode.None;
110112

@@ -117,6 +119,20 @@ public CSharpTest(LanguageVersion? languageVersion)
117119
});
118120
}
119121

122+
this.SolutionTransforms.Add((solution, projectId) =>
123+
{
124+
var corlib = solution.GetProject(projectId).MetadataReferences.OfType<PortableExecutableReference>()
125+
.Single(reference => Path.GetFileName(reference.FilePath) == "mscorlib.dll");
126+
var system = solution.GetProject(projectId).MetadataReferences.OfType<PortableExecutableReference>()
127+
.Single(reference => Path.GetFileName(reference.FilePath) == "System.dll");
128+
129+
return solution
130+
.RemoveMetadataReference(projectId, corlib)
131+
.RemoveMetadataReference(projectId, system)
132+
.AddMetadataReference(projectId, corlib.WithAliases(new[] { "global", "corlib" }))
133+
.AddMetadataReference(projectId, system.WithAliases(new[] { "global", "system" }));
134+
});
135+
120136
return;
121137

122138
// Local function
@@ -222,9 +238,6 @@ public CSharpTest(LanguageVersion? languageVersion)
222238
/// </value>
223239
public List<string> ExplicitlyEnabledDiagnostics { get; } = new List<string>();
224240

225-
private static Assembly Netstandard20Reference
226-
=> Assembly.Load("netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51");
227-
228241
protected override CompilationOptions CreateCompilationOptions()
229242
{
230243
var compilationOptions = base.CreateCompilationOptions();

0 commit comments

Comments
 (0)