Skip to content

Commit

Permalink
add tests and do not allow --folder to be specified with --binaryLog
Browse files Browse the repository at this point in the history
  • Loading branch information
jmarolf committed Mar 12, 2021
1 parent 81cf278 commit 97a7cf6
Show file tree
Hide file tree
Showing 17 changed files with 141 additions and 5 deletions.
14 changes: 11 additions & 3 deletions src/FormatCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System;
using System.CommandLine;
using System.CommandLine.Parsing;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

Expand Down Expand Up @@ -79,14 +78,14 @@ internal static RootCommand CreateCommandLineOptions()
},
new Option(new[] { "--binarylog" }, Resources.Log_all_project_or_solution_load_information_to_a_binary_log_file)
{
Argument = new Argument<string?>(
() => Path.Combine(Environment.CurrentDirectory, "formatDiagnosticLog.binlog") ) { Name = "binary-log-path" }.LegalFilePathsOnly()
Argument = new Argument<string?>(() => null) { Name = "binary-log-path", Arity = ArgumentArity.ZeroOrOne }.LegalFilePathsOnly()
},
};

rootCommand.Description = "dotnet-format";
rootCommand.AddValidator(EnsureFolderNotSpecifiedWhenFixingStyle);
rootCommand.AddValidator(EnsureFolderNotSpecifiedWhenFixingAnalyzers);
rootCommand.AddValidator(EnsureFolderNotSpecifiedWhenLoggingBinlog);

return rootCommand;
}
Expand All @@ -109,6 +108,15 @@ internal static RootCommand CreateCommandLineOptions()
: null;
}

internal static string? EnsureFolderNotSpecifiedWhenLoggingBinlog(CommandResult symbolResult)
{
var folder = symbolResult.ValueForOption<bool>("--folder");
var binarylog = symbolResult.OptionResult("--binarylog");
return folder && binarylog is not null && !binarylog.IsImplicit
? Resources.Cannot_specify_the_folder_option_when_writing_a_binary_log
: null;
}

internal static bool WasOptionUsed(this ParseResult result, params string[] aliases)
{
return result.Tokens
Expand Down
23 changes: 21 additions & 2 deletions src/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
// 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.Immutable;
Expand Down Expand Up @@ -183,9 +183,28 @@ private static async Task<int> Main(string[] args)
formatOptions,
logger,
cancellationTokenSource.Token,
binaryLogPath: s_parseResult.WasOptionUsed("--binarylog") ? binarylog : null).ConfigureAwait(false);
binaryLogPath: GetBinaryLogPath(s_parseResult, binarylog)).ConfigureAwait(false);

return GetExitCode(formatResult, check);

static string? GetBinaryLogPath(ParseResult parseResult, string? binarylog)
{
if (parseResult.WasOptionUsed("--binarylog"))
{
if (binarylog is null)
{
return Path.Combine(Directory.GetCurrentDirectory(), "format.binlog");
}
else if (Path.GetExtension(binarylog)?.Equals(".binlog") == false)
{
return Path.ChangeExtension(binarylog, ".binlog");
}

return binarylog;
}

return null;
}
}
catch (FileNotFoundException fex)
{
Expand Down
3 changes: 3 additions & 0 deletions src/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -324,4 +324,7 @@
<data name="Log_all_project_or_solution_load_information_to_a_binary_log_file" xml:space="preserve">
<value>Log all project or solution load information to a binary log file.</value>
</data>
<data name="Cannot_specify_the_folder_option_when_writing_a_binary_log" xml:space="preserve">
<value>Cannot specify the '--folder' option when writing a binary log.</value>
</data>
</root>
5 changes: 5 additions & 0 deletions src/xlf/Resources.cs.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
<target state="new">Cannot specify the '--folder' option when running analyzers.</target>
<note />
</trans-unit>
<trans-unit id="Cannot_specify_the_folder_option_when_writing_a_binary_log">
<source>Cannot specify the '--folder' option when writing a binary log.</source>
<target state="new">Cannot specify the '--folder' option when writing a binary log.</target>
<note />
</trans-unit>
<trans-unit id="Code_Style">
<source>Code Style</source>
<target state="translated">Styl kódu</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.de.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
<target state="new">Cannot specify the '--folder' option when running analyzers.</target>
<note />
</trans-unit>
<trans-unit id="Cannot_specify_the_folder_option_when_writing_a_binary_log">
<source>Cannot specify the '--folder' option when writing a binary log.</source>
<target state="new">Cannot specify the '--folder' option when writing a binary log.</target>
<note />
</trans-unit>
<trans-unit id="Code_Style">
<source>Code Style</source>
<target state="translated">Codeformat</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.es.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
<target state="new">Cannot specify the '--folder' option when running analyzers.</target>
<note />
</trans-unit>
<trans-unit id="Cannot_specify_the_folder_option_when_writing_a_binary_log">
<source>Cannot specify the '--folder' option when writing a binary log.</source>
<target state="new">Cannot specify the '--folder' option when writing a binary log.</target>
<note />
</trans-unit>
<trans-unit id="Code_Style">
<source>Code Style</source>
<target state="translated">Estilo de código</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.fr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
<target state="new">Cannot specify the '--folder' option when running analyzers.</target>
<note />
</trans-unit>
<trans-unit id="Cannot_specify_the_folder_option_when_writing_a_binary_log">
<source>Cannot specify the '--folder' option when writing a binary log.</source>
<target state="new">Cannot specify the '--folder' option when writing a binary log.</target>
<note />
</trans-unit>
<trans-unit id="Code_Style">
<source>Code Style</source>
<target state="translated">Style de code</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.it.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
<target state="new">Cannot specify the '--folder' option when running analyzers.</target>
<note />
</trans-unit>
<trans-unit id="Cannot_specify_the_folder_option_when_writing_a_binary_log">
<source>Cannot specify the '--folder' option when writing a binary log.</source>
<target state="new">Cannot specify the '--folder' option when writing a binary log.</target>
<note />
</trans-unit>
<trans-unit id="Code_Style">
<source>Code Style</source>
<target state="translated">Stile codice</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.ja.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
<target state="new">Cannot specify the '--folder' option when running analyzers.</target>
<note />
</trans-unit>
<trans-unit id="Cannot_specify_the_folder_option_when_writing_a_binary_log">
<source>Cannot specify the '--folder' option when writing a binary log.</source>
<target state="new">Cannot specify the '--folder' option when writing a binary log.</target>
<note />
</trans-unit>
<trans-unit id="Code_Style">
<source>Code Style</source>
<target state="translated">コード スタイル</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.ko.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
<target state="new">Cannot specify the '--folder' option when running analyzers.</target>
<note />
</trans-unit>
<trans-unit id="Cannot_specify_the_folder_option_when_writing_a_binary_log">
<source>Cannot specify the '--folder' option when writing a binary log.</source>
<target state="new">Cannot specify the '--folder' option when writing a binary log.</target>
<note />
</trans-unit>
<trans-unit id="Code_Style">
<source>Code Style</source>
<target state="translated">코드 스타일</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.pl.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
<target state="new">Cannot specify the '--folder' option when running analyzers.</target>
<note />
</trans-unit>
<trans-unit id="Cannot_specify_the_folder_option_when_writing_a_binary_log">
<source>Cannot specify the '--folder' option when writing a binary log.</source>
<target state="new">Cannot specify the '--folder' option when writing a binary log.</target>
<note />
</trans-unit>
<trans-unit id="Code_Style">
<source>Code Style</source>
<target state="translated">Styl kodu</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.pt-BR.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
<target state="new">Cannot specify the '--folder' option when running analyzers.</target>
<note />
</trans-unit>
<trans-unit id="Cannot_specify_the_folder_option_when_writing_a_binary_log">
<source>Cannot specify the '--folder' option when writing a binary log.</source>
<target state="new">Cannot specify the '--folder' option when writing a binary log.</target>
<note />
</trans-unit>
<trans-unit id="Code_Style">
<source>Code Style</source>
<target state="translated">Estilo do Código</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.ru.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
<target state="new">Cannot specify the '--folder' option when running analyzers.</target>
<note />
</trans-unit>
<trans-unit id="Cannot_specify_the_folder_option_when_writing_a_binary_log">
<source>Cannot specify the '--folder' option when writing a binary log.</source>
<target state="new">Cannot specify the '--folder' option when writing a binary log.</target>
<note />
</trans-unit>
<trans-unit id="Code_Style">
<source>Code Style</source>
<target state="translated">Стиль кода</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.tr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
<target state="new">Cannot specify the '--folder' option when running analyzers.</target>
<note />
</trans-unit>
<trans-unit id="Cannot_specify_the_folder_option_when_writing_a_binary_log">
<source>Cannot specify the '--folder' option when writing a binary log.</source>
<target state="new">Cannot specify the '--folder' option when writing a binary log.</target>
<note />
</trans-unit>
<trans-unit id="Code_Style">
<source>Code Style</source>
<target state="translated">Kod Stili</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.zh-Hans.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
<target state="new">Cannot specify the '--folder' option when running analyzers.</target>
<note />
</trans-unit>
<trans-unit id="Cannot_specify_the_folder_option_when_writing_a_binary_log">
<source>Cannot specify the '--folder' option when writing a binary log.</source>
<target state="new">Cannot specify the '--folder' option when writing a binary log.</target>
<note />
</trans-unit>
<trans-unit id="Code_Style">
<source>Code Style</source>
<target state="translated">代码样式</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.zh-Hant.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
<target state="new">Cannot specify the '--folder' option when running analyzers.</target>
<note />
</trans-unit>
<trans-unit id="Cannot_specify_the_folder_option_when_writing_a_binary_log">
<source>Cannot specify the '--folder' option when writing a binary log.</source>
<target state="new">Cannot specify the '--folder' option when writing a binary log.</target>
<note />
</trans-unit>
<trans-unit id="Code_Style">
<source>Code Style</source>
<target state="translated">程式碼樣式</target>
Expand Down
41 changes: 41 additions & 0 deletions tests/ProgramTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,5 +239,46 @@ public async Task CommandLine_AllArguments_Bind()
Assert.Equal(0, parseResult.Errors.Count);
Assert.Equal(uniqueExitCode, result);
}

[Fact]
public void CommandLine_BinaryLog_DoesNotFailIfPathNotSpecified()
{
// Arrange
var sut = FormatCommand.CreateCommandLineOptions();

// Act
var result = sut.Parse(new[] { "--binarylog" });

// Assert
Assert.Equal(0, result.Errors.Count);
Assert.True(result.WasOptionUsed("--binarylog"));
}

[Fact]
public void CommandLine_BinaryLog_DoesNotFailIfPathIsSpecified()
{
// Arrange
var sut = FormatCommand.CreateCommandLineOptions();

// Act
var result = sut.Parse(new[] { "--binarylog", "log" });

// Assert
Assert.Equal(0, result.Errors.Count);
Assert.True(result.WasOptionUsed("--binarylog"));
}

[Fact]
public void CommandLine_BinaryLog_FailsIfFolderIsSpecified()
{
// Arrange
var sut = FormatCommand.CreateCommandLineOptions();

// Act
var result = sut.Parse(new[] { "--folder", "--binarylog" });

// Assert
Assert.Equal(1, result.Errors.Count);
}
}
}

0 comments on commit 97a7cf6

Please sign in to comment.