Skip to content

Commit c5bd2f5

Browse files
committed
Handle analyzers from targeting packs in tests
1 parent 2314d55 commit c5bd2f5

File tree

1 file changed

+51
-26
lines changed

1 file changed

+51
-26
lines changed

src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToUseAnalyzers.cs

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -52,29 +52,35 @@ public void It_resolves_analyzers_correctly(string language, string testAssetNam
5252
"Analyzer",
5353
GetValuesCommand.ValueType.Item);
5454

55-
command.Execute().Should().Pass();
55+
command
56+
.WithWorkingDirectory(asset.Path)
57+
.Execute("/bl")
58+
.Should().Pass();
5659

5760
var analyzers = command.GetValues();
5861

5962
switch (language)
6063
{
6164
case "C#":
62-
analyzers.Select(x => RelativeNuGetPath(x)).Where(x => x != null).Should().BeEquivalentTo(
63-
"Microsoft.NET.Sdk/targets/../analyzers/Microsoft.CodeAnalysis.CSharp.NetAnalyzers.dll",
64-
"Microsoft.NET.Sdk/targets/../analyzers/Microsoft.CodeAnalysis.NetAnalyzers.dll",
65-
"microsoft.codequality.analyzers/2.6.0/analyzers/dotnet/cs/Microsoft.CodeQuality.Analyzers.dll",
66-
"microsoft.codequality.analyzers/2.6.0/analyzers/dotnet/cs/Microsoft.CodeQuality.CSharp.Analyzers.dll",
67-
"microsoft.dependencyvalidation.analyzers/0.9.0/analyzers/dotnet/Microsoft.DependencyValidation.Analyzers.dll"
65+
analyzers.Select(x => GetPackageAndPath(x)).Should().BeEquivalentTo(
66+
("Microsoft.NET.Sdk", (string) null, "analyzers/Microsoft.CodeAnalysis.CSharp.NetAnalyzers.dll"),
67+
("Microsoft.NET.Sdk", (string)null, "analyzers/Microsoft.CodeAnalysis.NetAnalyzers.dll"),
68+
("microsoft.netcore.app.ref", (string)null, "analyzers/dotnet/cs/System.Text.Json.SourceGeneration.dll"),
69+
("microsoft.netcore.app.ref", (string)null, "analyzers/dotnet/cs/System.Text.RegularExpressions.Generator.dll"),
70+
("microsoft.codequality.analyzers", "2.6.0", "analyzers/dotnet/cs/Microsoft.CodeQuality.Analyzers.dll"),
71+
("microsoft.codequality.analyzers", "2.6.0", "analyzers/dotnet/cs/Microsoft.CodeQuality.CSharp.Analyzers.dll"),
72+
("microsoft.dependencyvalidation.analyzers", "0.9.0", "analyzers/dotnet/Microsoft.DependencyValidation.Analyzers.dll")
73+
6874
);
6975
break;
7076

7177
case "VB":
72-
analyzers.Select(x => RelativeNuGetPath(x)).Should().BeEquivalentTo(
73-
"Microsoft.NET.Sdk/targets/../analyzers/Microsoft.CodeAnalysis.VisualBasic.NetAnalyzers.dll",
74-
"Microsoft.NET.Sdk/targets/../analyzers/Microsoft.CodeAnalysis.NetAnalyzers.dll",
75-
"microsoft.codequality.analyzers/2.6.0/analyzers/dotnet/vb/Microsoft.CodeQuality.Analyzers.dll",
76-
"microsoft.codequality.analyzers/2.6.0/analyzers/dotnet/vb/Microsoft.CodeQuality.VisualBasic.Analyzers.dll",
77-
"microsoft.dependencyvalidation.analyzers/0.9.0/analyzers/dotnet/Microsoft.DependencyValidation.Analyzers.dll"
78+
analyzers.Select(x => GetPackageAndPath(x)).Should().BeEquivalentTo(
79+
("Microsoft.NET.Sdk", (string)null, "analyzers/Microsoft.CodeAnalysis.VisualBasic.NetAnalyzers.dll"),
80+
("Microsoft.NET.Sdk", (string)null, "analyzers/Microsoft.CodeAnalysis.NetAnalyzers.dll"),
81+
("microsoft.codequality.analyzers", "2.6.0", "analyzers/dotnet/vb/Microsoft.CodeQuality.Analyzers.dll"),
82+
("microsoft.codequality.analyzers", "2.6.0", "analyzers/dotnet/vb/Microsoft.CodeQuality.VisualBasic.Analyzers.dll"),
83+
("microsoft.dependencyvalidation.analyzers", "0.9.0", "analyzers/dotnet/Microsoft.DependencyValidation.Analyzers.dll")
7884
);
7985
break;
8086

@@ -113,7 +119,7 @@ public void It_resolves_multitargeted_analyzers()
113119

114120
var testAsset = _testAssetsManager.CreateTestProject(testProject);
115121

116-
List<string> GetAnalyzersForTargetFramework(string targetFramework)
122+
List<(string package, string version, string path)> GetAnalyzersForTargetFramework(string targetFramework)
117123
{
118124
var getValuesCommand = new GetValuesCommand(testAsset,
119125
valueName: "Analyzer",
@@ -124,35 +130,54 @@ List<string> GetAnalyzersForTargetFramework(string targetFramework)
124130

125131
getValuesCommand.Execute("-p:TargetFramework=" + targetFramework).Should().Pass();
126132

127-
return getValuesCommand.GetValues().Select(x => RelativeNuGetPath(x,false)).ToList();
133+
return getValuesCommand.GetValues().Select(x => GetPackageAndPath(x)).ToList();
128134
}
129135

130-
GetAnalyzersForTargetFramework("net6.0").Should().BeEquivalentTo("system.text.json/6.0.0-preview.4.21253.7/analyzers/dotnet/cs/System.Text.Json.SourceGeneration.dll");
136+
GetAnalyzersForTargetFramework("net6.0").Should().BeEquivalentTo(("system.text.json", "6.0.0-preview.4.21253.7", "analyzers/dotnet/cs/System.Text.Json.SourceGeneration.dll"));
131137
GetAnalyzersForTargetFramework("net472").Should().BeEmpty();
132138
}
133139

134140
static readonly List<string> nugetRoots = new List<string>()
135141
{
136142
TestContext.Current.NuGetCachePath,
137143
Path.Combine(FileConstants.UserProfileFolder, ".dotnet", "NuGetFallbackFolder"),
138-
TestContext.Current.ToolsetUnderTest.SdksPath
144+
Path.Combine(TestContext.Current.ToolsetUnderTest.DotNetRoot, "packs")
139145
};
140146

141-
static string RelativeNuGetPath(string absoluteNuGetPath, bool excludeSourceGeneration = true)
147+
static (string package, string version, string path) GetPackageAndPath(string absolutePath)
142148
{
149+
absolutePath = Path.GetFullPath(absolutePath);
150+
151+
if (absolutePath.StartsWith(TestContext.Current.ToolsetUnderTest.SdksPath))
152+
{
153+
string path = absolutePath.Substring(TestContext.Current.ToolsetUnderTest.SdksPath.Length + 1)
154+
.Replace(Path.DirectorySeparatorChar, '/');
155+
var components = path.Split(new char[] { '/' }, 2);
156+
string sdkName = components[0];
157+
string pathInSdk = components[1];
158+
return (sdkName, null, pathInSdk);
159+
}
160+
143161
foreach (var nugetRoot in nugetRoots)
144162
{
145-
if (excludeSourceGeneration && absoluteNuGetPath.EndsWith("System.Text.Json.SourceGeneration.dll"))
146-
{
147-
return null;
148-
}
149-
if (absoluteNuGetPath.StartsWith(nugetRoot + Path.DirectorySeparatorChar))
163+
if (absolutePath.StartsWith(nugetRoot + Path.DirectorySeparatorChar))
150164
{
151-
return absoluteNuGetPath.Substring(nugetRoot.Length + 1)
152-
.Replace(Path.DirectorySeparatorChar, '/');
165+
string path = absolutePath.Substring(nugetRoot.Length + 1)
166+
.Replace(Path.DirectorySeparatorChar, '/');
167+
var components = path.Split(new char[] { '/' }, 3);
168+
var packageName = components[0];
169+
var packageVersion = components[1];
170+
var pathInPackage = components[2];
171+
// Don't check package version for analyzers included in targeting pack, as the version changes during development
172+
if (packageName.Equals("microsoft.netcore.app.ref", StringComparison.Ordinal))
173+
{
174+
packageVersion = null;
175+
}
176+
return (packageName, packageVersion, pathInPackage);
153177
}
154178
}
155-
throw new InvalidDataException("Expected path to be under a NuGet root: " + absoluteNuGetPath);
179+
180+
throw new InvalidDataException("Expected path to be under a known root: " + absolutePath);
156181
}
157182
}
158183
}

0 commit comments

Comments
 (0)