-
Notifications
You must be signed in to change notification settings - Fork 172
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
Add unit test for 3rd party fixer formatting #896
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
eec63a8
Add a lock around loading a MSBuild workspace
JoeRobich cf045dc
Extract out a nuget restore helper
JoeRobich bc9060a
Add a project to store analyzer package references
JoeRobich b78dcd9
Add ability to add AnalyzerReferences during test formatting
JoeRobich 1fb7f29
Add a 3rd party analyzer test class
JoeRobich e3d2ce1
Enhance other tests to log the formatter output on failure
JoeRobich a5387a9
Fix file encoding
JoeRobich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,13 +6,19 @@ | |
using Microsoft.CodeAnalysis.Tools.Formatters; | ||
using Microsoft.CodeAnalysis.Tools.Tests.Formatters; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Microsoft.CodeAnalysis.Tools.Tests.Analyzers | ||
{ | ||
public class CodeStyleAnalyzerFormatterTests : CSharpFormatterTests | ||
{ | ||
private protected override ICodeFormatter Formatter => AnalyzerFormatter.CodeStyleFormatter; | ||
|
||
public CodeStyleAnalyzerFormatterTests(ITestOutputHelper output) | ||
{ | ||
TestOutputHelper = output; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On failure of any tests in the class, the formatter log will be written to the test output. |
||
} | ||
|
||
[Fact] | ||
public async Task TestUseVarCodeStyle_AppliesWhenNotUsingVar() | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.Tools.Analyzers; | ||
using Microsoft.CodeAnalysis.Tools.Formatters; | ||
using Microsoft.CodeAnalysis.Tools.Tests.Formatters; | ||
using Microsoft.CodeAnalysis.Tools.Tests.Utilities; | ||
using Microsoft.CodeAnalysis.Tools.Workspaces; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Microsoft.CodeAnalysis.Tools.Tests.Analyzers | ||
{ | ||
public class ThirdPartyAnalyzerFormatterTests : CSharpFormatterTests, IAsyncLifetime | ||
{ | ||
private static readonly string s_analyzerProjectFilePath = Path.Combine("for_analyzer_formatter", "analyzer_project", "analyzer_project.csproj"); | ||
|
||
private protected override ICodeFormatter Formatter => AnalyzerFormatter.ThirdPartyFormatter; | ||
|
||
private Project _analyzerReferencesProject; | ||
|
||
public ThirdPartyAnalyzerFormatterTests(ITestOutputHelper output) | ||
{ | ||
TestOutputHelper = output; | ||
} | ||
|
||
public async Task InitializeAsync() | ||
{ | ||
var logger = new TestLogger(); | ||
|
||
try | ||
{ | ||
// Restore the Analyzer packages that have been added to `for_analyzer_formatter/analyzer_project/analyzer_project.csproj` | ||
var exitCode = await NuGetHelper.PerformRestore(s_analyzerProjectFilePath, TestOutputHelper); | ||
Assert.Equal(0, exitCode); | ||
|
||
// Load the analyzer_project into a MSBuildWorkspace. | ||
var workspacePath = Path.Combine(TestProjectsPathHelper.GetProjectsDirectory(), s_analyzerProjectFilePath); | ||
|
||
MSBuildRegistrar.RegisterInstance(logger); | ||
var analyzerWorkspace = await MSBuildWorkspaceLoader.LoadAsync(workspacePath, WorkspaceType.Project, createBinaryLog: false, logWorkspaceWarnings: true, logger, CancellationToken.None); | ||
|
||
// From this project we can get valid AnalyzerReferences to add to our test project. | ||
_analyzerReferencesProject = analyzerWorkspace.CurrentSolution.Projects.Single(); | ||
} | ||
catch | ||
{ | ||
TestOutputHelper.WriteLine(logger.GetLog()); | ||
throw; | ||
} | ||
} | ||
|
||
public Task DisposeAsync() | ||
{ | ||
_analyzerReferencesProject = null; | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
private IEnumerable<AnalyzerReference> GetAnalyzerReferences(string prefix) | ||
=> _analyzerReferencesProject.AnalyzerReferences.Where(reference => reference.Display.StartsWith(prefix)); | ||
|
||
[Fact] | ||
public async Task TestStyleCopBlankLineFixer_RemovesUnnecessaryBlankLines() | ||
{ | ||
var analyzerReferences = GetAnalyzerReferences("StyleCop"); | ||
|
||
var testCode = @" | ||
class C | ||
{ | ||
|
||
void M() | ||
|
||
{ | ||
|
||
object obj = new object(); | ||
|
||
|
||
int count = 5; | ||
|
||
} | ||
|
||
} | ||
"; | ||
|
||
var expectedCode = @" | ||
class C | ||
{ | ||
void M() | ||
{ | ||
object obj = new object(); | ||
|
||
int count = 5; | ||
} | ||
} | ||
"; | ||
|
||
var editorConfig = new Dictionary<string, string>() | ||
{ | ||
// Turn off all diagnostics analyzers | ||
["dotnet_analyzer_diagnostic.severity"] = "none", | ||
|
||
// Two or more consecutive blank lines: Remove down to one blank line. SA1507 | ||
["dotnet_diagnostic.SA1507.severity"] = "error", | ||
|
||
// Blank line immediately before or after a { line: remove it. SA1505, SA1509 | ||
["dotnet_diagnostic.SA1505.severity"] = "error", | ||
["dotnet_diagnostic.SA1509.severity"] = "error", | ||
|
||
// Blank line immediately before a } line: remove it. SA1508 | ||
["dotnet_diagnostic.SA1508.severity"] = "error", | ||
}; | ||
|
||
await AssertCodeChangedAsync(testCode, expectedCode, editorConfig, fixCategory: FixCategory.Analyzers, analyzerReferences: analyzerReferences); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is needed because tests run in parallel but msbuild won't
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still don't quite understand this. Why can't each test create their own workspace?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the CodeFormatterTests is creating MSBuildWorkspaces and now we are creating a MSBuildWorkspace here, there is an InvalidOperationException "Cannot start a new build while a build is in progress", since they are running parallel.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we create a
WorspaceFact
that controls this intead of changing product code?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Took a stab at this in #899