Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 109 additions & 1 deletion src/System.CommandLine.Tests/ParsingValidationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<FileInfo>("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<FileInfo>("--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()
{
Expand Down Expand Up @@ -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<IEnumerable<FileInfo>>("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()
{
Expand All @@ -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<IEnumerable<FileInfo>>("--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()
{
Expand Down Expand Up @@ -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<FileSystemInfo>("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<FileSystemInfo>("--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()
{
Expand Down
2 changes: 1 addition & 1 deletion src/System.CommandLine/ArgumentValidation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ private static void FileOrDirectoryExists<T>(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));
}
Expand Down