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

Validate that the project or solution argument is not a mistyped option #1388

Closed
wants to merge 5 commits into from
Closed
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
33 changes: 33 additions & 0 deletions src/Commands/FormatCommandCommon.cs
Expand Up @@ -99,6 +99,8 @@ static FormatCommandCommon()
VerbosityOption.AcceptOnlyFromAmong(VerbosityLevels);
BinarylogOption.AcceptLegalFilePathsOnly();
ReportOption.AcceptLegalFilePathsOnly();
SlnOrProjectArgument.AcceptLegalFilePathsOnly();
SlnOrProjectArgument.PathIsNotOptionLike();
}

internal static async Task<int> FormatAsync(FormatOptions formatOptions, ILogger<Program> logger, CancellationToken cancellationToken)
Expand Down Expand Up @@ -152,6 +154,37 @@ public static CliArgument<string> DefaultToCurrentDirectory(this CliArgument<str
return arg;
}

public static CliArgument<T> PathIsNotOptionLike<T>(this CliArgument<T> arg)
{
// Hyphens are valid path characters so options that exist in other subcommands or don't
// exist at all will be considered valid values for the ProjectOrSolution argument.
// This validator ensures that we show the user an "unrecognized command" message when
// they mistype or misuse options.

arg.Validators.Add(symbol =>
{
// Look at each token. In practice this should be a single token since the
// ProjectOrSolution argument accepts at most one value.
foreach (var token in symbol.Tokens)
{
// If the path looks like an option and doesn't look like a relative path, ensure
// a folder exists with the same name.
if (token.Value.StartsWith('-') &&
!token.Value.Contains('\\') &&
!token.Value.Contains('/'))
{
var path = Path.GetFullPath(token.Value, Environment.CurrentDirectory);
if (!Directory.Exists(path))
{
symbol.AddError(string.Format(Resources.Unrecognized_command_or_argument_0, token.Value));
}
}
}
});

return arg;
}

public static ILogger<Program> SetupLogging(this IConsole console, LogLevel minimalLogLevel, LogLevel minimalErrorLevel)
{
var loggerFactory = new LoggerFactory()
Expand Down
3 changes: 3 additions & 0 deletions src/Resources.resx
Expand Up @@ -360,6 +360,9 @@
<data name="Run_whitespace_formatting" xml:space="preserve">
<value>Run whitespace formatting.</value>
</data>
<data name="Unrecognized_command_or_argument_0" xml:space="preserve">
<value>Unrecognized command or argument '{0}'</value>
</data>
<data name="A_space_separated_list_of_diagnostic_ids_to_ignore_when_fixing_code_style_or_3rd_party_issues" xml:space="preserve">
<value>A space separated list of diagnostic ids to ignore when fixing code style or 3rd party issues.</value>
</data>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.cs.xlf
Expand Up @@ -387,6 +387,11 @@
<target state="translated">Nepovedlo se uspořádat importy pro {0}. Dokument je příliš složitý.</target>
<note />
</trans-unit>
<trans-unit id="Unrecognized_command_or_argument_0">
<source>Unrecognized command or argument '{0}'</source>
<target state="new">Unrecognized command or argument '{0}'</target>
<note />
</trans-unit>
<trans-unit id="Using_msbuildexe_located_in_0">
<source>Using MSBuild.exe located in '{0}'.</source>
<target state="translated">Používá se MSBuild.exe umístěný v {0}.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.de.xlf
Expand Up @@ -387,6 +387,11 @@
<target state="translated">Importe für "{0}" können nicht organisiert werden. Das Dokument ist zu komplex.</target>
<note />
</trans-unit>
<trans-unit id="Unrecognized_command_or_argument_0">
<source>Unrecognized command or argument '{0}'</source>
<target state="new">Unrecognized command or argument '{0}'</target>
<note />
</trans-unit>
<trans-unit id="Using_msbuildexe_located_in_0">
<source>Using MSBuild.exe located in '{0}'.</source>
<target state="translated">MSBuild.exe in "{0}" wird verwendet.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.es.xlf
Expand Up @@ -387,6 +387,11 @@
<target state="translated">No se pueden organizar las importaciones para "{0}". El documento es demasiado complejo.</target>
<note />
</trans-unit>
<trans-unit id="Unrecognized_command_or_argument_0">
<source>Unrecognized command or argument '{0}'</source>
<target state="new">Unrecognized command or argument '{0}'</target>
<note />
</trans-unit>
<trans-unit id="Using_msbuildexe_located_in_0">
<source>Using MSBuild.exe located in '{0}'.</source>
<target state="translated">Se está usando el archivo MSBuild.exe ubicado en "{0}".</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.fr.xlf
Expand Up @@ -387,6 +387,11 @@
<target state="translated">Impossible d'organiser les importations pour '{0}'. Le document est trop complexe.</target>
<note />
</trans-unit>
<trans-unit id="Unrecognized_command_or_argument_0">
<source>Unrecognized command or argument '{0}'</source>
<target state="new">Unrecognized command or argument '{0}'</target>
<note />
</trans-unit>
<trans-unit id="Using_msbuildexe_located_in_0">
<source>Using MSBuild.exe located in '{0}'.</source>
<target state="translated">Utilisation de MSBuild.exe dans '{0}'.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.it.xlf
Expand Up @@ -387,6 +387,11 @@
<target state="translated">Non è possibile organizzare le importazioni per '{0}'. Il documento è troppo complesso.</target>
<note />
</trans-unit>
<trans-unit id="Unrecognized_command_or_argument_0">
<source>Unrecognized command or argument '{0}'</source>
<target state="new">Unrecognized command or argument '{0}'</target>
<note />
</trans-unit>
<trans-unit id="Using_msbuildexe_located_in_0">
<source>Using MSBuild.exe located in '{0}'.</source>
<target state="translated">Verrà usata la versione di MSBuild.exe disponibile in '{0}'.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.ja.xlf
Expand Up @@ -387,6 +387,11 @@
<target state="translated">'{0}' のインポートを整理できません。ドキュメントが複雑すぎます。</target>
<note />
</trans-unit>
<trans-unit id="Unrecognized_command_or_argument_0">
<source>Unrecognized command or argument '{0}'</source>
<target state="new">Unrecognized command or argument '{0}'</target>
<note />
</trans-unit>
<trans-unit id="Using_msbuildexe_located_in_0">
<source>Using MSBuild.exe located in '{0}'.</source>
<target state="translated">'{0}' にある MSBuild.exe を使用しています。</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.ko.xlf
Expand Up @@ -387,6 +387,11 @@
<target state="translated">'{0}'에 대한 가져오기를 구성할 수 없습니다. 문서가 너무 복잡합니다.</target>
<note />
</trans-unit>
<trans-unit id="Unrecognized_command_or_argument_0">
<source>Unrecognized command or argument '{0}'</source>
<target state="new">Unrecognized command or argument '{0}'</target>
<note />
</trans-unit>
<trans-unit id="Using_msbuildexe_located_in_0">
<source>Using MSBuild.exe located in '{0}'.</source>
<target state="translated">'{0}'에 있는 MSBuild.exe를 사용합니다.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.pl.xlf
Expand Up @@ -387,6 +387,11 @@
<target state="translated">Nie można zorganizować importów dla elementu „{0}”. Dokument jest zbyt złożony.</target>
<note />
</trans-unit>
<trans-unit id="Unrecognized_command_or_argument_0">
<source>Unrecognized command or argument '{0}'</source>
<target state="new">Unrecognized command or argument '{0}'</target>
<note />
</trans-unit>
<trans-unit id="Using_msbuildexe_located_in_0">
<source>Using MSBuild.exe located in '{0}'.</source>
<target state="translated">Używanie pliku MSBuild.exe znajdującego się w lokalizacji „{0}”.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.pt-BR.xlf
Expand Up @@ -387,6 +387,11 @@
<target state="translated">Não é possível organizar importações para '{0}'. O documento é muito complexo.</target>
<note />
</trans-unit>
<trans-unit id="Unrecognized_command_or_argument_0">
<source>Unrecognized command or argument '{0}'</source>
<target state="new">Unrecognized command or argument '{0}'</target>
<note />
</trans-unit>
<trans-unit id="Using_msbuildexe_located_in_0">
<source>Using MSBuild.exe located in '{0}'.</source>
<target state="translated">Usando MSBuild.exe localizado em '{0}'.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.ru.xlf
Expand Up @@ -387,6 +387,11 @@
<target state="translated">Не удается организовать импорты для "{0}". Слишком сложный документ.</target>
<note />
</trans-unit>
<trans-unit id="Unrecognized_command_or_argument_0">
<source>Unrecognized command or argument '{0}'</source>
<target state="new">Unrecognized command or argument '{0}'</target>
<note />
</trans-unit>
<trans-unit id="Using_msbuildexe_located_in_0">
<source>Using MSBuild.exe located in '{0}'.</source>
<target state="translated">Используется файл MSBuild.exe, расположенный в каталоге "{0}".</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.tr.xlf
Expand Up @@ -387,6 +387,11 @@
<target state="translated">'{0}' için içeri aktarmalar düzenlenemiyor. Belge çok karmaşık.</target>
<note />
</trans-unit>
<trans-unit id="Unrecognized_command_or_argument_0">
<source>Unrecognized command or argument '{0}'</source>
<target state="new">Unrecognized command or argument '{0}'</target>
<note />
</trans-unit>
<trans-unit id="Using_msbuildexe_located_in_0">
<source>Using MSBuild.exe located in '{0}'.</source>
<target state="translated">'{0}' içinde bulunan MSBuild.exe kullanılıyor.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.zh-Hans.xlf
Expand Up @@ -387,6 +387,11 @@
<target state="translated">无法整理“{0}”的导入项。文档太复杂。</target>
<note />
</trans-unit>
<trans-unit id="Unrecognized_command_or_argument_0">
<source>Unrecognized command or argument '{0}'</source>
<target state="new">Unrecognized command or argument '{0}'</target>
<note />
</trans-unit>
<trans-unit id="Using_msbuildexe_located_in_0">
<source>Using MSBuild.exe located in '{0}'.</source>
<target state="translated">使用“{0}”中的 MSBuild.exe。</target>
Expand Down
5 changes: 5 additions & 0 deletions src/xlf/Resources.zh-Hant.xlf
Expand Up @@ -387,6 +387,11 @@
<target state="translated">無法組織 '{0}' 的匯入。文件太複雜。</target>
<note />
</trans-unit>
<trans-unit id="Unrecognized_command_or_argument_0">
<source>Unrecognized command or argument '{0}'</source>
<target state="new">Unrecognized command or argument '{0}'</target>
<note />
</trans-unit>
<trans-unit id="Using_msbuildexe_located_in_0">
<source>Using MSBuild.exe located in '{0}'.</source>
<target state="translated">正在使用位於 '{0}' 的 MSBuild.exe。</target>
Expand Down
17 changes: 2 additions & 15 deletions tests/ProgramTests.cs
Expand Up @@ -127,26 +127,13 @@ public void CommandLine_ProjectArgument_FailsIfSpecifiedTwice()
}

[Fact]
public void CommandLine_FolderValidation_FailsIfFixAnalyzersSpecified()
public void CommandLine_ProjectOrSolutionArgumentValidation_FailsIfInvalidOptionIsSpecified()
{
// Arrange
var sut = RootFormatCommand.GetCommand();

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

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

[Fact]
public void CommandLine_FolderValidation_FailsIfFixStyleSpecified()
{
// Arrange
var sut = RootFormatCommand.GetCommand();

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

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