Skip to content

Commit

Permalink
Present error instead of throwing exception for empty argument to Fil…
Browse files Browse the repository at this point in the history
…eSystemInfo type (#1704)

* Error instead of exception for FileSystemInfo null paths

* Revert autoformat
  • Loading branch information
therealchjones authored Apr 19, 2022
1 parent 70d72f0 commit 3fef055
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/System.CommandLine.Tests/Binding/TypeConversionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ public void Command_argument_of_FileInfo_returns_null_when_argument_is_not_provi
.BeNull();
}

[Fact]
public void Argument_of_FileInfo_that_is_empty_results_in_an_informative_error()
{
var option = new Option<FileInfo>("--file");
var result = option.Parse(new string[] { "--file", "" });

result.Errors
.Should()
.ContainSingle()
.Which
.Message
.Should()
.Contain("Cannot parse argument '' for option '--file'");
}

[Fact]
public void Argument_of_array_of_FileInfo_can_be_called_without_custom_conversion_logic()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ internal static partial class ArgumentConverter

[typeof(DirectoryInfo)] = (string path, out object? value) =>
{
if (String.IsNullOrEmpty(path))
{
value = default;
return false;
}
value = new DirectoryInfo(path);
return true;
},
Expand All @@ -80,12 +85,22 @@ internal static partial class ArgumentConverter

[typeof(FileInfo)] = (string path, out object? value) =>
{
if (String.IsNullOrEmpty(path))
{
value = default;
return false;
}
value = new FileInfo(path);
return true;
},

[typeof(FileSystemInfo)] = (string path, out object? value) =>
{
if (String.IsNullOrEmpty(path))
{
value = default;
return false;
}
if (Directory.Exists(path))
{
value = new DirectoryInfo(path);
Expand Down

0 comments on commit 3fef055

Please sign in to comment.