Skip to content

Commit

Permalink
Merge pull request #817 from JoeRobich/fix-argument-names
Browse files Browse the repository at this point in the history
Fix Run argument names
  • Loading branch information
JoeRobich committed Sep 18, 2020
2 parents f0a480f + bc79b22 commit 57eafbb
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 48 deletions.
1 change: 1 addition & 0 deletions .vscode/launch.json
Expand Up @@ -51,6 +51,7 @@
"program": "${workspaceFolder}/artifacts/bin/dotnet-format/Debug/netcoreapp2.1/dotnet-format.dll",
"args": [
"format.sln",
"--fix-whitespace",
"--fix-style",
"warn",
"-v",
Expand Down
18 changes: 18 additions & 0 deletions src/FormatCommand.cs
Expand Up @@ -4,16 +4,34 @@
using System.CommandLine;
using System.CommandLine.Parsing;
using System.Linq;
using System.Threading.Tasks;

namespace Microsoft.CodeAnalysis.Tools
{
internal static class FormatCommand
{
// This delegate should be kept in Sync with the FormatCommand options and arguement names
// so that values bind correctly.
internal delegate Task<int> Handler(
string? workspace,
bool folder,
bool fixWhitespace,
string? fixStyle,
string? fixAnalyzers,
string? verbosity,
bool check,
string[] include,
string[] exclude,
string? report,
bool includeGenerated,
IConsole console);

internal static string[] VerbosityLevels => new[] { "q", "quiet", "m", "minimal", "n", "normal", "d", "detailed", "diag", "diagnostic" };
internal static string[] SeverityLevels => new[] { FixSeverity.Info, FixSeverity.Warn, FixSeverity.Error };

internal static RootCommand CreateCommandLineOptions()
{
// Sync changes to option and argument names with the FormatCommant.Handler above.
var rootCommand = new RootCommand
{
new Argument<string?>("workspace")
Expand Down
14 changes: 7 additions & 7 deletions src/Program.cs
Expand Up @@ -29,7 +29,7 @@ internal class Program
private static async Task<int> Main(string[] args)
{
var rootCommand = FormatCommand.CreateCommandLineOptions();
rootCommand.Handler = CommandHandler.Create(typeof(Program).GetMethod(nameof(Run))!);
rootCommand.Handler = CommandHandler.Create(new FormatCommand.Handler(Run));

// Parse the incoming args so we can give warnings when deprecated options are used.
s_parseResult = rootCommand.Parse(args);
Expand All @@ -40,9 +40,9 @@ private static async Task<int> Main(string[] args)
public static async Task<int> Run(
string? workspace,
bool folder,
bool whitespace,
string? style,
string? analyzers,
bool fixWhitespace,
string? fixStyle,
string? fixAnalyzers,
string? verbosity,
bool check,
string[] include,
Expand Down Expand Up @@ -143,7 +143,7 @@ private static async Task<int> Main(string[] args)
fixType |= FixCategory.Analyzers;
}

if (fixType == FixCategory.None || whitespace)
if (fixType == FixCategory.None || fixWhitespace)
{
fixType |= FixCategory.Whitespace;
}
Expand All @@ -155,8 +155,8 @@ private static async Task<int> Main(string[] args)
workspaceType,
logLevel,
fixType,
codeStyleSeverity: GetSeverity(style ?? FixSeverity.Error),
analyzerSeverity: GetSeverity(analyzers ?? FixSeverity.Error),
codeStyleSeverity: GetSeverity(fixStyle ?? FixSeverity.Error),
analyzerSeverity: GetSeverity(fixAnalyzers ?? FixSeverity.Error),
saveFormattedFiles: !check,
changesAreErrors: check,
fileMatcher,
Expand Down
109 changes: 68 additions & 41 deletions tests/ProgramTests.cs
@@ -1,26 +1,17 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.CommandLine.Parsing;
using System.Threading.Tasks;
using Xunit;

namespace Microsoft.CodeAnalysis.Tools.Tests
{
public class ProgramTests
{
// Should be kept in sync with Program.Run
private delegate void TestCommandHandlerDelegate(
string workspace,
bool folder,
string verbosity,
bool check,
string[] include,
string[] exclude,
string report,
bool includeGenerated);

[Fact]
public void ExitCodeIsOneWithCheckAndAnyFilesFormatted()
{
Expand Down Expand Up @@ -125,36 +116,6 @@ public void CommandLine_ProjectArgument_WithOption_BeforeArgument()
Assert.Equal("detailed", result.ValueForOption("verbosity"));
}

[Fact]
public void CommandLine_ProjectArgument_GetsPassedToHandler()
{
// Arrange
var sut = FormatCommand.CreateCommandLineOptions();
var handlerWasCalled = false;
sut.Handler = CommandHandler.Create(new TestCommandHandlerDelegate(TestCommandHandler));

void TestCommandHandler(
string workspace,
bool folder,
string verbosity,
bool check,
string[] include,
string[] exclude,
string report,
bool includeGenerated)
{
handlerWasCalled = true;
Assert.Equal("workspaceValue", workspace);
Assert.Equal("detailed", verbosity);
};

// Act
var result = sut.Invoke(new[] { "--verbosity", "detailed", "workspace" });

// Assert
Assert.True(handlerWasCalled);
}

[Fact]
public void CommandLine_ProjectArgument_FailsIfSpecifiedTwice()
{
Expand Down Expand Up @@ -206,5 +167,71 @@ public void CommandLine_AnalyzerOptions_CanSpecifyBothWithDefaults()
// Assert
Assert.Equal(0, result.Errors.Count);
}

[Fact]
// If this test fails that means FormatCommand options have changed, ensure the FormatCommand.Handler has been updated to match.
public async Task CommandLine_AllArguments_Bind()
{
// Arrange
var uniqueExitCode = 143;

var sut = FormatCommand.CreateCommandLineOptions();
sut.Handler = CommandHandler.Create(new FormatCommand.Handler(TestRun));

Task<int> TestRun(
string workspace,
bool folder,
bool fixWhitespace,
string fixStyle,
string fixAnalyzers,
string verbosity,
bool check,
string[] include,
string[] exclude,
string report,
bool includeGenerated,
IConsole console = null)
{
Assert.Equal("./src", workspace);
Assert.False(folder);
Assert.True(fixWhitespace);
Assert.Equal("warn", fixStyle);
Assert.Equal("info", fixAnalyzers);
Assert.Equal("diag", verbosity);
Assert.True(check);
Assert.Equal(new[] { "*.cs" }, include);
Assert.Equal(new[] { "*.vb" }, exclude);
Assert.Equal("report.json", report);
Assert.True(includeGenerated);

return Task.FromResult(uniqueExitCode);
};

var args = @"
./src
--fix-whitespace
--fix-style
warn
--fix-analyzers
info
--verbosity
diag
--check
--include
*.cs
--exclude
*.vb
--report
report.json
--include-generated".Split('\n', StringSplitOptions.RemoveEmptyEntries);

// Act
var parseResult = sut.Parse(args);
var result = await sut.InvokeAsync(args);

// Assert
Assert.Equal(0, parseResult.Errors.Count);
Assert.Equal(uniqueExitCode, result);
}
}
}

0 comments on commit 57eafbb

Please sign in to comment.