Skip to content

Update to Microsoft.CodeAnalysis.Testing #2562

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 19, 2019

Conversation

sharwell
Copy link
Contributor

@sharwell sharwell commented Feb 14, 2019

Fixes #2480

@codecov
Copy link

codecov bot commented Feb 15, 2019

Codecov Report

❗ No coverage uploaded for pull request base (master@b2587fa). Click here to learn what that means.
The diff coverage is 94.28%.

@@            Coverage Diff            @@
##             master    #2562   +/-   ##
=========================================
  Coverage          ?   71.45%           
=========================================
  Files             ?      800           
  Lines             ?   141566           
  Branches          ?    16103           
=========================================
  Hits              ?   101155           
  Misses            ?    35967           
  Partials          ?     4444
Flag Coverage Δ
#Debug 71.45% <94.28%> (?)
#production 67.73% <ø> (?)
#test 85.73% <94.28%> (?)

@@ -42,6 +42,7 @@
<!-- Test-only Dependencies -->
<PropertyGroup>
<BenchmarkDotNetVersion>0.11.3</BenchmarkDotNetVersion>
<MicrosoftCodeAnalysisTestingVersion>1.0.0-beta1-63812-02</MicrosoftCodeAnalysisTestingVersion>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know anything regarding analyzers. Is it normal to use beta package? Is where are stable version available?

Copy link
Contributor Author

@sharwell sharwell Feb 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a dependency we control. We're still stabilizing the API for the testing package (and determining whether breaking changes are allowed in updates after stable) so this is what's currently available. The package performs much more rigorous testing than previous harnesses, which will show up in some of the comments. Several other analyzers internally (dotnet/roslyn-analyzers, Microsoft/vs-threading, Microsoft/VSSDK-Analyzers) and externally (DotNetAnalyzers/StyleCopAnalyzers) are already using this.

{
private readonly Lazy<string> SourceAttribute = TestUtils.LazySource("BestFriendAttribute.cs");
private readonly Lazy<string> SourceDeclaration = TestUtils.LazySource("BestFriendOnPublicDeclaration.cs");

[Fact]
public void BestFriendOnPublicDeclaration()
public async Task BestFriendOnPublicDeclaration()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 I kept the test names the same, which means you'll keep your pass/fail history on ADO. If you prefer the Async suffix convention even in test code, I'm happy to add it.

diagMsg.CreateDiagnosticResult(basis + 32, 35, "Check", "\"Less fine: \" + env.GetType().Name"),
diagName.CreateDiagnosticResult(basis + 34, 17, "CheckUserArg", "name", "\"p\""),
diagDecode.CreateDiagnosticResult(basis + 39, 41, "CheckDecode", "\"This message is suspicious\""),
new DiagnosticResult("CS0051", DiagnosticSeverity.Error).WithLocation(14, 16).WithMessage("Inconsistent accessibility: parameter type 'IHostEnvironment' is less accessible than method 'TypeName.TypeName(IHostEnvironment, float, int)'"),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 It was not clear to me if this was a desired error in the test code. It could indicate that the test input has become stale relative to some API and needs to be updated.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very interesting. Absolutely not intended, these may have crept in as the input source files changed. Anyway it should be fixed, though perhaps not part of this PR, since this is about using the proper test harness, and we can hardly expect you to fix problems with our faulty tests. Anyway, I have filed a separate issue #2588 to capture this work. My comment would be the same for all these "compilation error" additions you needed to add, absolutely not the intent of the class. But thank you for finding this, this is definitely something we must do!

VerifyCS.Diagnostic(ContractsCheckAnalyzer.NameofDiagnostic.Rule).WithLocation(basis + 34, 17).WithArguments("CheckUserArg", "name", "\"p\""),
VerifyCS.Diagnostic(ContractsCheckAnalyzer.DecodeMessageWithLoadContextDiagnostic.Rule).WithLocation(basis + 39, 41).WithArguments("CheckDecode", "\"This message is suspicious\""),
new DiagnosticResult("CS0117", DiagnosticSeverity.Error).WithLocation("Test1.cs", 220, 70).WithMessage("'MessageSensitivity' does not contain a definition for 'UserData'"),
new DiagnosticResult("CS0117", DiagnosticSeverity.Error).WithLocation("Test1.cs", 231, 70).WithMessage("'MessageSensitivity' does not contain a definition for 'Schema'"),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 It's not clear these two errors were intended to be part of the test input. If the test input needs to be updated please let me know.

@@ -23,22 +27,19 @@ class TypeName
private static int _muck = 4;
private readonly float _blorg = 3.0f;
private string _fooBacking;
public string Foo { get; set => _fooBacking = value; }
public string Foo { get => _fooBacking; set => _fooBacking = value; }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 I went ahead and fixed this compilation error in a test input. It seemed unintentional.

{
TestCode = TestSource,
FixedCode = FixedTestSource,
NumberOfFixAllIterations = 2,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 This means the application of a Fix All did not actually fix all instances at once. The expected FixedTestSource was reached, but it took two Fix All iterations to reach. The test harness forces you to call out these cases because they indicate places where a code fix works but could have improved usability.

VerifyCSharpDiagnostic(Source, expected);
var test = new VerifyCS.Test { TestCode = Source };
test.ExpectedDiagnostics.AddRange(expected);
test.Exclusions &= ~AnalysisExclusions.GeneratedCode;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 This means the analyzer reports diagnostics even in code marked as generated (most commonly due to // <auto-generated/> in the file header, but also handles certain designer files, etc.). If this was not intentional, we should update the analyzer to indicate these files should be excluded from diagnostic reporting.

Some other analyzers in this test suite require this as well. Please review all of them to ensure exclusions are applied/not applied where you want.

diagTp.CreateDiagnosticResult(41, 24, "T"),
diag.CreateDiagnosticResult(55, 26, p("text")),
VerifyCS.Diagnostic(TypeIsSchemaShapeAnalyzer.ShapeParameterDiagnostic.Rule).WithLocation(18, 24).WithArguments("T"),
new DiagnosticResult("CS8205", DiagnosticSeverity.Error).WithLocation(21, 52).WithMessage("Attributes are not allowed on local function parameters or type parameters"),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 This seems like a bug in the test input, but I wasn't sure if you wanted me to go ahead and fix it.

diagPg.CreateDiagnosticResult(38, 13, "Class9", "F2"),
diagPc.CreateDiagnosticResult(39, 13, "Class10"),
diagPc.CreateDiagnosticResult(40, 13, "Class11"),
new DiagnosticResult("CS0246", DiagnosticSeverity.Error).WithLocation(44, 71).WithMessage("The type or namespace name 'MissingClass' could not be found (are you missing a using directive or an assembly reference?)"),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Based on the name, it seems this could be intentional. If it's not, let me know.

@sharwell
Copy link
Contributor Author

I'll fix the build error in the morning. Looks like dependency version on CI is slightly different from local but shouldn't be bad to fix.

Copy link
Contributor

@TomFinley TomFinley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @sharwell for lending your expertise!! I'm quite glad to have an expert in this area improving this part of our infrastructure.

@sharwell
Copy link
Contributor Author

@TomFinley no problem! I helped write this test library following experiences with a few other analyzers. It tests a lot of different aspects, and by default it tries to test many scenarios relevant to the most common analyzer designs (things like verifying that generated code is excluded by default). I'm sure questions will come up as you continue to use it, and I'm happy to help figure things out and/or make improvements to the library so it's easier to work with.

Copy link
Contributor

@shauheen shauheen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@TomFinley TomFinley merged commit 369a9b6 into dotnet:master Feb 19, 2019
@sharwell sharwell deleted the use-testing-library branch February 19, 2019 07:19
@ghost ghost locked as resolved and limited conversation to collaborators Mar 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants