Skip to content

Commit

Permalink
Merge pull request #5065 from Youssef1313/use-AnalyzerConfigFiles
Browse files Browse the repository at this point in the history
Use AnalyzerConfigFiles instead of AnalyzerConfigDocument
  • Loading branch information
mavasani authored May 21, 2021
2 parents fcf0fc8 + ffe1ce5 commit 69fccc3
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 150 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Testing;
using Microsoft.CodeAnalysis.Text;
using Test.Utilities;
using Xunit;
using VerifyCS = Test.Utilities.CSharpCodeFixVerifier<
Expand Down Expand Up @@ -870,7 +868,7 @@ public class C
[*]
{editorConfigText}
") }
") },
}
}.RunAsync();

Expand Down Expand Up @@ -926,14 +924,14 @@ public class C
{{
}}
}}"
}
},
AnalyzerConfigFiles = { ("/.editorconfig", $"[*]{Environment.NewLine}{editorConfigText}") },
},
SolutionTransforms = { WithAnalyzerConfigDocument }
};

if (expectDiagnostic)
{
csTest.ExpectedDiagnostics.Add(VerifyCS.Diagnostic().WithSpan(@"/Test0.cs", 4, 23, 4, 29).WithArguments("unused", "M"));
csTest.ExpectedDiagnostics.Add(VerifyCS.Diagnostic().WithSpan(4, 23, 4, 29).WithArguments("unused", "M"));
}

await csTest.RunAsync();
Expand All @@ -950,35 +948,16 @@ Public Class C
End Sub
End Class"
},
AnalyzerConfigFiles = { ("/.editorconfig", $"[*]{Environment.NewLine}{editorConfigText}") },
},
SolutionTransforms = { WithAnalyzerConfigDocument }
};

if (expectDiagnostic)
{
vbTest.ExpectedDiagnostics.Add(VerifyVB.Diagnostic().WithSpan(@"/Test0.vb", 3, 18, 3, 24).WithArguments("unused", "M"));
vbTest.ExpectedDiagnostics.Add(VerifyVB.Diagnostic().WithSpan(3, 18, 3, 24).WithArguments("unused", "M"));
}

await vbTest.RunAsync();
return;

Solution WithAnalyzerConfigDocument(Solution solution, ProjectId projectId)
{
var project = solution.GetProject(projectId)!;
var projectFilePath = project.Language == LanguageNames.CSharp ? @"/Test.csproj" : @"/Test.vbproj";
solution = solution.WithProjectFilePath(projectId, projectFilePath);

var documentId = project.DocumentIds.Single();
var documentExtension = project.Language == LanguageNames.CSharp ? "cs" : "vb";
solution = solution.WithDocumentFilePath(documentId, $@"/Test0.{documentExtension}");

return solution.GetProject(projectId)!
.AddAnalyzerConfigDocument(
".editorconfig",
SourceText.From($"[*.{documentExtension}]" + Environment.NewLine + editorConfigText),
filePath: @"/.editorconfig")
.Project.Solution;
}
}

[Theory]
Expand Down Expand Up @@ -1019,12 +998,11 @@ public class C
{editorConfigText}
") }
},
SolutionTransforms = { WithAnalyzerConfigDocument }
};

if (expectDiagnostic)
{
csTest.ExpectedDiagnostics.Add(VerifyCS.Diagnostic().WithSpan(@"/Test0.cs", 4, 23, 4, 29).WithArguments("unused", "M"));
csTest.ExpectedDiagnostics.Add(VerifyCS.Diagnostic().WithSpan(4, 23, 4, 29).WithArguments("unused", "M"));
}

await csTest.RunAsync();
Expand All @@ -1047,34 +1025,14 @@ End Class"
{editorConfigText}
") }
},
SolutionTransforms = { WithAnalyzerConfigDocument }
};

if (expectDiagnostic)
{
vbTest.ExpectedDiagnostics.Add(VerifyVB.Diagnostic().WithSpan(@"/Test0.vb", 3, 18, 3, 24).WithArguments("unused", "M"));
vbTest.ExpectedDiagnostics.Add(VerifyVB.Diagnostic().WithSpan(3, 18, 3, 24).WithArguments("unused", "M"));
}

await vbTest.RunAsync();
return;

Solution WithAnalyzerConfigDocument(Solution solution, ProjectId projectId)
{
var project = solution.GetProject(projectId)!;
var projectFilePath = project.Language == LanguageNames.CSharp ? @"/Test.csproj" : @"/Test.vbproj";
solution = solution.WithProjectFilePath(projectId, projectFilePath);

var documentId = project.DocumentIds.Single();
var documentExtension = project.Language == LanguageNames.CSharp ? "cs" : "vb";
solution = solution.WithDocumentFilePath(documentId, $@"/Test0.{documentExtension}");

return solution.GetProject(projectId)!
.AddAnalyzerConfigDocument(
".editorconfig",
SourceText.From($"[*.{documentExtension}]" + Environment.NewLine + editorConfigText),
filePath: @"/.editorconfig")
.Project.Solution;
}
}

[Fact, WorkItem(3106, "https://github.com/dotnet/roslyn-analyzers/issues/3106")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -860,8 +860,11 @@ protected void Application_End() { }

await new VerifyCS.Test()
{
TestCode = csSource,
AnalyzerConfigDocument = editorConfigText,
TestState =
{
Sources = { csSource },
AnalyzerConfigFiles = { ("/.editorconfig", $"[*]\r\n{editorConfigText}") },
}
}.RunAsync();

var vbSource = @"
Expand Down Expand Up @@ -911,8 +914,11 @@ End Class
";
await new VerifyVB.Test()
{
TestCode = vbSource,
AnalyzerConfigDocument = editorConfigText,
TestState =
{
Sources = { vbSource },
AnalyzerConfigFiles = { ("/.editorconfig", $"[*]\r\n{editorConfigText}") },
}
}.RunAsync();
}

Expand Down Expand Up @@ -1157,8 +1163,11 @@ public class Test
}";
await new VerifyCS.Test()
{
TestCode = csSource,
AnalyzerConfigDocument = editorConfigText,
TestState =
{
Sources = { csSource },
AnalyzerConfigFiles = { ("/.editorconfig", $"[*]\r\n{editorConfigText}") },
}
}.RunAsync();

var vbSource = @"
Expand All @@ -1181,8 +1190,11 @@ End Function
End Class";
await new VerifyVB.Test()
{
TestCode = vbSource,
AnalyzerConfigDocument = editorConfigText,
TestState =
{
Sources = { vbSource },
AnalyzerConfigFiles = { ("/.editorconfig", $"[*]\r\n{editorConfigText}") },
}
}.RunAsync();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -742,8 +742,11 @@ public void M2(C c)
}";
var csTest = new VerifyCS.Test()
{
TestCode = csCode,
AnalyzerConfigDocument = editorConfig
TestState =
{
Sources = { csCode },
AnalyzerConfigFiles = { ("/.editorconfig", $"[*]\r\n{editorConfig}") },
}
};

if (pointsToAnalysisKind == PointsToAnalysisKind.Complete)
Expand Down Expand Up @@ -789,8 +792,11 @@ End Sub

var vbTest = new VerifyVB.Test()
{
TestCode = vbCode,
AnalyzerConfigDocument = editorConfig
TestState =
{
Sources = { vbCode },
AnalyzerConfigFiles = { ("/.editorconfig", $"[*]\r\n{editorConfig}") },
}
};

if (pointsToAnalysisKind == PointsToAnalysisKind.Complete)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class C
{
public string M() => Assembly.GetExecutingAssembly().Location;
}";
string analyzerConfig = "";
string analyzerConfig = "[*]\r\n";
if (publish is not null)
{
analyzerConfig += $"build_property.{PublishSingleFile} = {publish}" + Environment.NewLine;
Expand All @@ -38,16 +38,19 @@ class C

var test = new VerifyCS.Test
{
TestCode = source,
AnalyzerConfigDocument = analyzerConfig
TestState =
{
Sources = { source },
AnalyzerConfigFiles = { ("/.editorconfig", analyzerConfig) },
}
};

DiagnosticResult[] diagnostics;
if (publish is true && includeContent is not true)
{
diagnostics = new DiagnosticResult[] {
// /0/Test0.cs(5,26): warning IL3000: 'System.Reflection.Assembly.Location' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'.
VerifyCS.Diagnostic(AvoidAssemblyLocationInSingleFile.IL3000).WithSpan(5, 26, 5, 66).WithArguments("System.Reflection.Assembly.Location"),
VerifyCS.Diagnostic(IL3000).WithSpan(5, 26, 5, 66).WithArguments("System.Reflection.Assembly.Location"),
};
}
else
Expand Down Expand Up @@ -77,7 +80,7 @@ public void M()
}";
return VerifyDiagnosticsAsync(src,
// /0/Test0.cs(8,13): warning IL3000: 'System.Reflection.Assembly.Location' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'.
VerifyCS.Diagnostic(AvoidAssemblyLocationInSingleFile.IL3000).WithSpan(8, 13, 8, 23).WithArguments("System.Reflection.Assembly.Location")
VerifyCS.Diagnostic(IL3000).WithSpan(8, 13, 8, 23).WithArguments("System.Reflection.Assembly.Location")
);
}

Expand All @@ -97,9 +100,9 @@ public void M()
}";
return VerifyDiagnosticsAsync(src,
// /0/Test0.cs(8,13): warning IL3001: Assemblies embedded in a single-file app cannot have additional files in the manifest.
VerifyCS.Diagnostic(AvoidAssemblyLocationInSingleFile.IL3001).WithSpan(8, 13, 8, 41).WithArguments("System.Reflection.Assembly.GetFile(string)"),
VerifyCS.Diagnostic(IL3001).WithSpan(8, 13, 8, 41).WithArguments("System.Reflection.Assembly.GetFile(string)"),
// /0/Test0.cs(9,13): warning IL3001: Assemblies embedded in a single-file app cannot have additional files in the manifest.
VerifyCS.Diagnostic(AvoidAssemblyLocationInSingleFile.IL3001).WithSpan(9, 13, 9, 25).WithArguments("System.Reflection.Assembly.GetFiles()")
VerifyCS.Diagnostic(IL3001).WithSpan(9, 13, 9, 25).WithArguments("System.Reflection.Assembly.GetFiles()")
);
}

Expand All @@ -119,9 +122,9 @@ public void M()
}";
return VerifyDiagnosticsAsync(src,
// /0/Test0.cs(8,13): warning IL3000: 'System.Reflection.AssemblyName.CodeBase' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'.
VerifyCS.Diagnostic(AvoidAssemblyLocationInSingleFile.IL3000).WithSpan(8, 13, 8, 23).WithArguments("System.Reflection.AssemblyName.CodeBase"),
VerifyCS.Diagnostic(IL3000).WithSpan(8, 13, 8, 23).WithArguments("System.Reflection.AssemblyName.CodeBase"),
// /0/Test0.cs(9,13): warning IL3000: 'System.Reflection.AssemblyName.EscapedCodeBase' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'.
VerifyCS.Diagnostic(AvoidAssemblyLocationInSingleFile.IL3000).WithSpan(9, 13, 9, 30).WithArguments("System.Reflection.AssemblyName.EscapedCodeBase")
VerifyCS.Diagnostic(IL3000).WithSpan(9, 13, 9, 30).WithArguments("System.Reflection.AssemblyName.EscapedCodeBase")
);
}

Expand All @@ -143,25 +146,28 @@ public void M()
}";
return VerifyDiagnosticsAsync(src,
// /0/Test0.cs(8,13): warning IL3000: 'System.Reflection.Assembly.Location' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'.
VerifyCS.Diagnostic(AvoidAssemblyLocationInSingleFile.IL3000).WithSpan(8, 13, 8, 23).WithArguments("System.Reflection.Assembly.Location"),
VerifyCS.Diagnostic(IL3000).WithSpan(8, 13, 8, 23).WithArguments("System.Reflection.Assembly.Location"),
// /0/Test0.cs(9,13): warning IL3001: Assemblies embedded in a single-file app cannot have additional files in the manifest.
VerifyCS.Diagnostic(AvoidAssemblyLocationInSingleFile.IL3001).WithSpan(9, 13, 9, 25).WithArguments("System.Reflection.Assembly.GetFiles()")
VerifyCS.Diagnostic(IL3001).WithSpan(9, 13, 9, 25).WithArguments("System.Reflection.Assembly.GetFiles()")
);
}

private Task VerifyDiagnosticsAsync(string source, params DiagnosticResult[] expected)
{
const string singleFilePublishConfig = @"
const string singleFilePublishConfig = @"is_global = true
build_property." + PublishSingleFile + " = true";

var test = new VerifyCS.Test
{
TestCode = source,
AnalyzerConfigDocument = singleFilePublishConfig
TestState =
{
Sources = { source },
AnalyzerConfigFiles = { ("/.editorconfig", singleFilePublishConfig) },
}
};

test.ExpectedDiagnostics.AddRange(expected);
return test.RunAsync();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1010,8 +1010,11 @@ class C
";
var csTest = new VerifyCS.Test()
{
TestCode = csCode,
AnalyzerConfigDocument = editorConfig
TestState =
{
Sources = { csCode },
AnalyzerConfigFiles = { ("/.editorconfig", $"[*]\r\n{editorConfig}") },
}
};

await csTest.RunAsync();
Expand Down Expand Up @@ -1045,8 +1048,11 @@ End Class
";
var vbTest = new VerifyVB.Test()
{
TestCode = vbCode,
AnalyzerConfigDocument = editorConfig
TestState =
{
Sources = { vbCode },
AnalyzerConfigFiles = { ("/.editorconfig", $"[*]\r\n{editorConfig}") },
}
};

await vbTest.RunAsync();
Expand Down Expand Up @@ -1094,8 +1100,11 @@ class C
";
var csTest = new VerifyCS.Test()
{
TestCode = csCode,
AnalyzerConfigDocument = editorConfig
TestState =
{
Sources = { csCode },
AnalyzerConfigFiles = { ("/.editorconfig", $"[*]\r\n{editorConfig}") },
}
};

await csTest.RunAsync();
Expand Down Expand Up @@ -1128,8 +1137,11 @@ End Class
";
var vbTest = new VerifyVB.Test()
{
TestCode = vbCode,
AnalyzerConfigDocument = editorConfig
TestState =
{
Sources = { vbCode },
AnalyzerConfigFiles = { ("/.editorconfig", $"[*]\r\n{editorConfig}") },
}
};

await vbTest.RunAsync();
Expand Down
Loading

0 comments on commit 69fccc3

Please sign in to comment.