Skip to content

Make src/Analyzers tests pass successfully locally in Vs2019 #20274

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
Apr 8, 2020
Merged

Make src/Analyzers tests pass successfully locally in Vs2019 #20274

merged 1 commit into from
Apr 8, 2020

Conversation

tebeco
Copy link
Contributor

@tebeco tebeco commented Mar 28, 2020

Summary of the changes (Less than 80 chars)

Addresses #bugnumber (not created yet)

As i have no idea if it will break the CI or not, i did not created an issue yet

@Pilchie Pilchie added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Mar 28, 2020
@sharwell
Copy link
Contributor

It looks like this project is using an analyzer test framework called "Microsoft.AspNetCore.Analyzer.Testing", which so far I haven't been able to find. This is not the standard analyzer test framework, so the first step on testing is to remove this reference and instead use Microsoft.CodeAnalysis.Testing. I'm still investigating the steps necessary to do this for this repository.

@tebeco
Copy link
Contributor Author

tebeco commented Mar 28, 2020

AHAH, yeah It took me time to find it too
I think It was from extensions and probably now in dotnet/extensions

(As you know this repo is going to be moved to both dotnet/runtime and dotnet/aspnetcore

EDIT :
https://github.com/dotnet/extensions/tree/master/src/TestingUtils

@sharwell
Copy link
Contributor

I cannot figure out what this is testing:

[Fact]
public async Task DetectFeaturesAsync_FindsNoFeatures()
{
// Arrange
var compilation = await CreateCompilationAsync(nameof(StartupWithNoFeatures));
var symbols = new StartupSymbols(compilation);
var type = (INamedTypeSymbol)compilation.GetSymbolsWithName(nameof(StartupWithNoFeatures)).Single();
Assert.True(StartupFacts.IsStartupClass(symbols, type));
// Act
var features = await CompilationFeatureDetector.DetectFeaturesAsync(compilation);
// Assert
Assert.Empty(features);
}

@tebeco
Copy link
Contributor Author

tebeco commented Mar 28, 2020

This test seems to be analyzing StartupWithNoFeatures.cs
File is here

public class StartupWithNoFeatures
{
public void Configure(IApplicationBuilder app)
{
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapFallbackToFile("index.html");
});
}

The CompilationFeatureDetector seems to recreate AdhockWorkspace and add the specified StartupFoo.cs to it

     var compilation = await CreateCompilationAsync(nameof(StartupWithNoFeatures));   <=== recreate the workspace

This code ensure that it properly detect it as a Startup file/class

     var type = (INamedTypeSymbol)compilation.GetSymbolsWithName(nameof(StartupWithNoFeatures)).Single(); 
     Assert.True(StartupFacts.IsStartupClass(symbols, type)); 

And then each StartupFoo.cs has it own specificity (Feature ?)
this part is supposed to find features :
CompilationFeatureDetector.DetectFeaturesAsync(compilation)

and it's not supposed to find any :
Assert.Empty(features);

@tebeco
Copy link
Contributor Author

tebeco commented Mar 28, 2020

also the following line is VERY VERY confusion in the Arange part, because in fact it's only used to detect if it's an actual Startup class
var symbols = new StartupSymbols(compilation);

If you dig in CompilationFeatureDetector.DetectFeaturesAsync you will see that this method does the exact same thing ... but internally (no noise outside the API, there may be reason, no idea) :

public static async Task<IImmutableSet<string>> DetectFeaturesAsync(
Compilation compilation,
CancellationToken cancellationToken = default)
{
var symbols = new StartupSymbols(compilation);
if (!symbols.HasRequiredSymbols)
{
// Cannot find ASP.NET Core types.
return ImmutableHashSet<string>.Empty;
}

Also i'm not very sure about the Symbol thing, if i had to do a wild guess, i think it's to check that Symbols are "loaded/available" so that Roslyn can properly do it job when it's going to analyze the code later on.
I see it as a "Sanity check" (kinda like testing if(foo != null) before doing foo?.whatever

@sharwell

This comment has been minimized.

@sharwell
Copy link
Contributor

I submitted #20277 to update the analyzer tests to the new library. I verified that this pull request fixes the experience for the remaining tests.

@tebeco
Copy link
Contributor Author

tebeco commented Mar 28, 2020

I guessed that it was on purpose to make sure the code is actually seen as code and not as string
==> "it does build properly so the code is valid"

And later on the call to var compilation = await CreateCompilationAsync(nameof(StartupWithNoFeatures)); would automatically be able to resolve all dependencies since it does already build.

I guess this is now the job of CSharpVerifierHelper you added ?

@tebeco tebeco marked this pull request as ready for review March 28, 2020 21:40
@tebeco
Copy link
Contributor Author

tebeco commented Mar 28, 2020

(i changed the status of the PR to Ready for review)

Honestly i was not sure about the changes since it implied both AppContext and few issue about Helix (i have no idea yet the detail, but i often see files/dependencies issue around it so i hope i did not make any regression here)

@tebeco
Copy link
Contributor Author

tebeco commented Mar 30, 2020

@sharwell
Not sure if i should add or change anything else in that PR, i'm a bit lost now ;)

@sharwell
Copy link
Contributor

This pull request seems fine to me.

@mkArtakMSFT mkArtakMSFT merged commit f3963e7 into dotnet:master Apr 8, 2020
@tebeco
Copy link
Contributor Author

tebeco commented Apr 8, 2020

thx ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants