diff --git a/src/System.CommandLine.Tests/ParsingValidationTests.cs b/src/System.CommandLine.Tests/ParsingValidationTests.cs index a038013d20..9052765911 100644 --- a/src/System.CommandLine.Tests/ParsingValidationTests.cs +++ b/src/System.CommandLine.Tests/ParsingValidationTests.cs @@ -869,6 +869,44 @@ public void An_option_argument_can_be_invalid_based_on_file_existence() e.Message == $"File does not exist: '{path}'."); } + [Fact] + public void A_command_argument_with_FileInfo_is_invalid_when_the_path_is_an_existing_directory() + { + var command = new Command("move") + { + new Argument("to").AcceptExistingOnly() + }; + + var path = ExistingDirectory(); + var result = command.Parse($@"move ""{path}"""); + + result.Errors + .Should() + .HaveCount(1) + .And + .Contain(e => ((ArgumentResult)e.SymbolResult).Argument.Name == "to" && + e.Message == $"File does not exist: '{path}'."); + } + + [Fact] + public void An_option_argument_with_FileInfo_is_invalid_when_the_path_is_an_existing_directory() + { + var command = new Command("move") + { + new Option("--to").AcceptExistingOnly() + }; + + var path = ExistingDirectory(); + var result = command.Parse($@"move --to ""{path}"""); + + result.Errors + .Should() + .HaveCount(1) + .And + .Contain(e => ((OptionResult)e.SymbolResult).Option.Name == "--to" && + e.Message == $"File does not exist: '{path}'."); + } + [Fact] public void A_command_argument_can_be_invalid_based_on_directory_existence() { @@ -963,7 +1001,26 @@ public void A_command_argument_with_multiple_files_can_be_invalid_based_on_file_ .Contain(e => ((ArgumentResult)e.SymbolResult).Argument.Name == "to" && e.Message == $"File does not exist: '{path}'."); } - + + [Fact] + public void A_command_argument_with_multiple_files_is_invalid_when_the_path_is_an_existing_directory() + { + var command = new Command("move") + { + new Argument>("to").AcceptExistingOnly() + }; + + var path = ExistingDirectory(); + var result = command.Parse($@"move ""{path}"""); + + result.Errors + .Should() + .HaveCount(1) + .And + .Contain(e => ((ArgumentResult)e.SymbolResult).Argument.Name == "to" && + e.Message == $"File does not exist: '{path}'."); + } + [Fact] public void An_option_argument_with_multiple_files_can_be_invalid_based_on_file_existence() { @@ -983,6 +1040,25 @@ public void An_option_argument_with_multiple_files_can_be_invalid_based_on_file_ e.Message == $"File does not exist: '{path}'."); } + [Fact] + public void An_option_argument_with_multiple_files_is_invalid_when_the_path_is_an_existing_directory() + { + var command = new Command("move") + { + new Option>("--to").AcceptExistingOnly() + }; + + var path = ExistingDirectory(); + var result = command.Parse($@"move --to ""{path}"""); + + result.Errors + .Should() + .HaveCount(1) + .And + .Contain(e => ((OptionResult)e.SymbolResult).Option.Name == "--to" && + e.Message == $"File does not exist: '{path}'."); + } + [Fact] public void A_command_argument_with_multiple_directories_can_be_invalid_based_on_directory_existence() { @@ -1127,6 +1203,38 @@ public void Option_argument_does_not_return_errors_when_file_exists() result.Errors.Should().BeEmpty(); } + [Theory] + [InlineData(true)] + [InlineData(false)] + public void Command_argument_does_not_return_errors_when_file_or_directory_exists_for_FileSystemInfo(bool useFile) + { + var command = new Command("move") + { + new Argument("arg").AcceptExistingOnly() + }; + + var path = useFile ? ExistingFile() : ExistingDirectory(); + var result = command.Parse($@"move ""{path}"""); + + result.Errors.Should().BeEmpty(); + } + + [Theory] + [InlineData(true)] + [InlineData(false)] + public void Option_argument_does_not_return_errors_when_file_or_directory_exists_for_FileSystemInfo(bool useFile) + { + var command = new Command("move") + { + new Option("--to").AcceptExistingOnly() + }; + + var path = useFile ? ExistingFile() : ExistingDirectory(); + var result = command.Parse($@"move --to ""{path}"""); + + result.Errors.Should().BeEmpty(); + } + [Fact] public void Command_argument_does_not_return_errors_when_Directory_exists() { diff --git a/src/System.CommandLine/ArgumentValidation.cs b/src/System.CommandLine/ArgumentValidation.cs index 42b996387f..11be097030 100644 --- a/src/System.CommandLine/ArgumentValidation.cs +++ b/src/System.CommandLine/ArgumentValidation.cs @@ -199,7 +199,7 @@ private static void FileOrDirectoryExists(ArgumentResult result) { result.AddError(LocalizationResources.DirectoryDoesNotExist(token.Value)); } - else if (checkFile && !Directory.Exists(token.Value) && !File.Exists(token.Value)) + else if (checkFile && !File.Exists(token.Value)) { result.AddError(LocalizationResources.FileDoesNotExist(token.Value)); }