Skip to content
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

Added CodeFormatter tests for --fix-style #751

Merged
merged 2 commits into from Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions perf/FormattedFiles.cs
Expand Up @@ -37,7 +37,7 @@ public void FilesFormattedFolder()
fixCodeStyle: false,
codeStyleSeverity: DiagnosticSeverity.Error,
fixAnalyzers: false,
analyerSeverity: DiagnosticSeverity.Error,
analyzerSeverity: DiagnosticSeverity.Error,
saveFormattedFiles: false,
changesAreErrors: false,
AllFileMatcher,
Expand All @@ -57,7 +57,7 @@ public void FilesFormattedProject()
fixCodeStyle: false,
codeStyleSeverity: DiagnosticSeverity.Error,
fixAnalyzers: false,
analyerSeverity: DiagnosticSeverity.Error,
analyzerSeverity: DiagnosticSeverity.Error,
saveFormattedFiles: false,
changesAreErrors: false,
AllFileMatcher,
Expand All @@ -77,7 +77,7 @@ public void FilesFormattedSolution()
fixCodeStyle: false,
codeStyleSeverity: DiagnosticSeverity.Error,
fixAnalyzers: false,
analyerSeverity: DiagnosticSeverity.Error,
analyzerSeverity: DiagnosticSeverity.Error,
saveFormattedFiles: false,
changesAreErrors: false,
AllFileMatcher,
Expand Down
6 changes: 3 additions & 3 deletions perf/NoFilesFormatted.cs
Expand Up @@ -37,7 +37,7 @@ public void NoFilesFormattedFolder()
fixCodeStyle: false,
codeStyleSeverity: DiagnosticSeverity.Error,
fixAnalyzers: false,
analyerSeverity: DiagnosticSeverity.Error,
analyzerSeverity: DiagnosticSeverity.Error,
saveFormattedFiles: false,
changesAreErrors: false,
AllFileMatcher,
Expand All @@ -57,7 +57,7 @@ public void NoFilesFormattedProject()
fixCodeStyle: false,
codeStyleSeverity: DiagnosticSeverity.Error,
fixAnalyzers: false,
analyerSeverity: DiagnosticSeverity.Error,
analyzerSeverity: DiagnosticSeverity.Error,
saveFormattedFiles: false,
changesAreErrors: false,
AllFileMatcher,
Expand All @@ -77,7 +77,7 @@ public void NoFilesFormattedSolution()
fixCodeStyle: false,
codeStyleSeverity: DiagnosticSeverity.Error,
fixAnalyzers: false,
analyerSeverity: DiagnosticSeverity.Error,
analyzerSeverity: DiagnosticSeverity.Error,
saveFormattedFiles: false,
changesAreErrors: false,
AllFileMatcher,
Expand Down
4 changes: 2 additions & 2 deletions perf/RealWorldSolution.cs
Expand Up @@ -39,7 +39,7 @@ public void FilesFormattedSolution()
fixCodeStyle: false,
codeStyleSeverity: DiagnosticSeverity.Error,
fixAnalyzers: false,
analyerSeverity: DiagnosticSeverity.Error,
analyzerSeverity: DiagnosticSeverity.Error,
saveFormattedFiles: false,
changesAreErrors: false,
AllFileMatcher,
Expand All @@ -59,7 +59,7 @@ public void FilesFormattedFolder()
fixCodeStyle: false,
codeStyleSeverity: DiagnosticSeverity.Error,
fixAnalyzers: false,
analyerSeverity: DiagnosticSeverity.Error,
analyzerSeverity: DiagnosticSeverity.Error,
saveFormattedFiles: false,
changesAreErrors: false,
AllFileMatcher,
Expand Down
3 changes: 0 additions & 3 deletions src/CodeFormatter.cs
Expand Up @@ -170,9 +170,6 @@ private static async Task<Workspace> OpenFolderWorkspaceAsync(string workspacePa
// rather than a separate one. Any tasks isolated in AppDomains or tasks that create
// AppDomains will likely not work due to https://github.com/Microsoft/MSBuildLocator/issues/16.
{ "AlwaysCompileMarkupFilesInSeparateDomain", bool.FalseString },
// This flag is used at restore time to avoid imports from packages changing the inputs to restore,
// without this it is possible to get different results between the first and second restore.
{ "ExcludeRestorePackageImports", bool.TrueString },
Comment on lines -173 to -175
Copy link
Member Author

@JoeRobich JoeRobich Aug 10, 2020

Choose a reason for hiding this comment

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

This property was stopping NetStandard projects from resolving framework references from the project.assets.json file.

Even with this property removed, a full restore must be run on the project prior to formatting in order to produce this project.assets.json file, otherwise missing framework references will be reported in compiler diagnostics.

};

var workspace = MSBuildWorkspace.Create(properties);
Expand Down
8 changes: 4 additions & 4 deletions src/FormatOptions.cs
Expand Up @@ -27,7 +27,7 @@ internal class FormatOptions
bool fixCodeStyle,
DiagnosticSeverity codeStyleSeverity,
bool fixAnalyzers,
DiagnosticSeverity analyerSeverity,
DiagnosticSeverity analyzerSeverity,
bool saveFormattedFiles,
bool changesAreErrors,
Matcher fileMatcher,
Expand All @@ -40,7 +40,7 @@ internal class FormatOptions
FixCodeStyle = fixCodeStyle;
CodeStyleSeverity = codeStyleSeverity;
FixAnalyzers = fixAnalyzers;
AnalyzerSeverity = analyerSeverity;
AnalyzerSeverity = analyzerSeverity;
SaveFormattedFiles = saveFormattedFiles;
ChangesAreErrors = changesAreErrors;
FileMatcher = fileMatcher;
Expand All @@ -55,7 +55,7 @@ internal class FormatOptions
out bool fixCodeStyle,
out DiagnosticSeverity codeStyleSeverity,
out bool fixAnalyzers,
out DiagnosticSeverity analyerSeverity,
out DiagnosticSeverity analyzerSeverity,
out bool saveFormattedFiles,
out bool changesAreErrors,
out Matcher fileMatcher,
Expand All @@ -68,7 +68,7 @@ internal class FormatOptions
fixCodeStyle = FixCodeStyle;
codeStyleSeverity = CodeStyleSeverity;
fixAnalyzers = FixAnalyzers;
analyerSeverity = AnalyzerSeverity;
analyzerSeverity = AnalyzerSeverity;
saveFormattedFiles = SaveFormattedFiles;
changesAreErrors = ChangesAreErrors;
fileMatcher = FileMatcher;
Expand Down
2 changes: 1 addition & 1 deletion src/Program.cs
Expand Up @@ -134,7 +134,7 @@ private static async Task<int> Main(string[] args)
fixCodeStyle: s_parseResult.WasOptionUsed("--fix-style", "-fs"),
codeStyleSeverity: GetSeverity(fixStyle ?? FixSeverity.Error),
fixAnalyzers: s_parseResult.WasOptionUsed("--fix-analyzers", "-fa"),
analyerSeverity: GetSeverity(fixAnalyzers ?? FixSeverity.Error),
analyzerSeverity: GetSeverity(fixAnalyzers ?? FixSeverity.Error),
saveFormattedFiles: !check,
changesAreErrors: check,
fileMatcher,
Expand Down
89 changes: 68 additions & 21 deletions tests/CodeFormatterTests.cs
Expand Up @@ -29,6 +29,9 @@ public class CodeFormatterTests : IClassFixture<MSBuildFixture>, IClassFixture<T
private const string FSharpProjectPath = "for_code_formatter/fsharp_project/";
private const string FSharpProjectFilePath = FSharpProjectPath + "fsharp_project.fsproj";

private const string CodeStyleSolutionPath = "for_code_formatter/codestyle_solution/";
private const string CodeStyleSolutionFilePath = CodeStyleSolutionPath + "codestyle_solution.sln";

private static IEnumerable<string> EmptyFilesList => Array.Empty<string>();

private Regex FindFormattingLogLine => new Regex(@"((.*)\(\d+,\d+\): (.*))\r|((.*)\(\d+,\d+\): (.*))");
Expand All @@ -41,6 +44,10 @@ public CodeFormatterTests(ITestOutputHelper output, MSBuildFixture msBuildFixtur

testProjectsPathFixture.SetCurrentDirectory();
msBuildFixture.RegisterInstance(_output);

// For NetStandard projects to resolve framework references, the project.assets.json files must
// be constructed by a NuGet restore.
ProcessRunner.CreateProcess("dotnet", $"restore \"{CodeStyleSolutionFilePath}\"").Result.GetAwaiter().GetResult();
}

[Fact]
Expand Down Expand Up @@ -320,19 +327,14 @@ public async Task IgnoreFileWhenListedInExcludeList()
{
var include = new[] { UnformattedProgramFilePath };

var log = await TestFormatWorkspaceAsync(
await TestFormatWorkspaceAsync(
UnformattedSolutionFilePath,
include: include,
exclude: include,
includeGenerated: false,
expectedExitCode: 0,
expectedFilesFormatted: 0,
expectedFileCount: 5);

var pattern = string.Format(Resources.Formatted_code_file_0, @"(.*)");
var match = new Regex(pattern, RegexOptions.Multiline).Match(log);

Assert.False(match.Success, log);
}

[Fact]
Expand All @@ -341,19 +343,14 @@ public async Task IgnoreFileWhenContainingFolderListedInExcludeList()
var include = new[] { UnformattedProgramFilePath };
var exclude = new[] { UnformattedProjectPath };

var log = await TestFormatWorkspaceAsync(
await TestFormatWorkspaceAsync(
UnformattedSolutionFilePath,
include: include,
exclude: exclude,
includeGenerated: false,
expectedExitCode: 0,
expectedFilesFormatted: 0,
expectedFileCount: 5);

var pattern = string.Format(Resources.Formatted_code_file_0, @"(.*)");
var match = new Regex(pattern, RegexOptions.Multiline).Match(log);

Assert.False(match.Success, log);
}

[Fact]
Expand All @@ -362,22 +359,72 @@ public async Task IgnoreAllFileWhenExcludingAllFiles()
var include = new[] { UnformattedProgramFilePath };
var exclude = new[] { "**/*.*" };

var log = await TestFormatWorkspaceAsync(
await TestFormatWorkspaceAsync(
UnformattedSolutionFilePath,
include: include,
exclude: exclude,
includeGenerated: false,
expectedExitCode: 0,
expectedFilesFormatted: 0,
expectedFileCount: 5);
}

var pattern = string.Format(Resources.Formatted_code_file_0, @"(.*)");
var match = new Regex(pattern, RegexOptions.Multiline).Match(log);
[Fact]
public async Task NoFilesFormattedInCodeStyleSolution_WhenNotFixingCodeStyle()
{
await TestFormatWorkspaceAsync(
CodeStyleSolutionFilePath,
include: EmptyFilesList,
exclude: EmptyFilesList,
includeGenerated: false,
expectedExitCode: 0,
expectedFilesFormatted: 0,
expectedFileCount: 6,
fixCodeStyle: false);
}

Assert.False(match.Success, log);
[Fact]
public async Task NoFilesFormattedInCodeStyleSolution_WhenFixingCodeStyleErrors()
{
await TestFormatWorkspaceAsync(
CodeStyleSolutionFilePath,
include: EmptyFilesList,
exclude: EmptyFilesList,
includeGenerated: false,
expectedExitCode: 0,
expectedFilesFormatted: 0,
expectedFileCount: 6,
fixCodeStyle: true,
codeStyleSeverity: DiagnosticSeverity.Error);
}

[Fact]
public async Task FilesFormattedInCodeStyleSolution_WhenFixingCodeStyleWarnings()
{
await TestFormatWorkspaceAsync(
CodeStyleSolutionFilePath,
include: EmptyFilesList,
exclude: EmptyFilesList,
includeGenerated: false,
expectedExitCode: 0,
expectedFilesFormatted: 2,
expectedFileCount: 6,
fixCodeStyle: true,
codeStyleSeverity: DiagnosticSeverity.Warning);
}

public async Task<string> TestFormatWorkspaceAsync(string workspaceFilePath, IEnumerable<string> include, IEnumerable<string> exclude, bool includeGenerated, int expectedExitCode, int expectedFilesFormatted, int expectedFileCount)
public async Task<string> TestFormatWorkspaceAsync(
string workspaceFilePath,
IEnumerable<string> include,
IEnumerable<string> exclude,
bool includeGenerated,
int expectedExitCode,
int expectedFilesFormatted,
int expectedFileCount,
bool fixCodeStyle = false,
DiagnosticSeverity codeStyleSeverity = DiagnosticSeverity.Error,
bool fixAnalyzers = false,
DiagnosticSeverity analyzerSeverity = DiagnosticSeverity.Error)
{
var workspacePath = Path.GetFullPath(workspaceFilePath);

Expand All @@ -399,10 +446,10 @@ public async Task<string> TestFormatWorkspaceAsync(string workspaceFilePath, IEn
workspacePath,
workspaceType,
LogLevel.Trace,
fixCodeStyle: false,
codeStyleSeverity: DiagnosticSeverity.Error,
fixAnalyzers: false,
analyerSeverity: DiagnosticSeverity.Error,
fixCodeStyle,
codeStyleSeverity,
fixAnalyzers,
analyzerSeverity,
saveFormattedFiles: false,
changesAreErrors: false,
fileMatcher,
Expand Down
2 changes: 1 addition & 1 deletion tests/Formatters/AbstractFormatterTests.cs
Expand Up @@ -119,7 +119,7 @@ private protected async Task<SourceText> TestAsync(string testCode, string expec
fixCodeStyle: false,
codeStyleSeverity: DiagnosticSeverity.Error,
fixAnalyzers: false,
analyerSeverity: DiagnosticSeverity.Error,
analyzerSeverity: DiagnosticSeverity.Error,
saveFormattedFiles: false,
changesAreErrors: false,
fileMatcher,
Expand Down
2 changes: 1 addition & 1 deletion tests/Formatters/FormattedFilesTests.cs
Expand Up @@ -61,7 +61,7 @@ private async Task<List<FormattedFile>> TestFormattedFiles(string testCode)
fixCodeStyle: false,
codeStyleSeverity: DiagnosticSeverity.Error,
fixAnalyzers: false,
analyerSeverity: DiagnosticSeverity.Error,
analyzerSeverity: DiagnosticSeverity.Error,
saveFormattedFiles: false,
changesAreErrors: false,
fileMatcher,
Expand Down
3 changes: 3 additions & 0 deletions tests/dotnet-format.UnitTests.csproj
Expand Up @@ -6,6 +6,9 @@
<IsPackable>false</IsPackable>
<RootNamespace>Microsoft.CodeAnalysis.Tools.Tests</RootNamespace>

<!-- Copy nuget assemblies to build directory so that Microsoft.CodeAnalysis.Features.* can be located when running tests. -->
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>

<!-- Always run on the latest runtime installed. -->
<RollForward>LatestMajor</RollForward>
</PropertyGroup>
Expand Down