зеркало из
1
0
Форкнуть 0

Merge pull request #62 from jonsequitur/LegalFilePathsOnly-rule

add LegalFilePathsOnly arguments rule
This commit is contained in:
Jon Sequeira 2017-04-11 11:31:17 -07:00 коммит произвёл GitHub
Родитель 094f31e729 5ad3e34ce3
Коммит bfce4616c3
2 изменённых файлов: 56 добавлений и 0 удалений

Просмотреть файл

@ -99,6 +99,39 @@ namespace Microsoft.DotNet.Cli.CommandLine.Tests
.Contain("Options '--one' and '--two' cannot be used together."); .Contain("Options '--one' and '--two' cannot be used together.");
} }
[Fact]
public void LegalFilePathsOnly_rejects_arguments_containing_invalid_path_characters()
{
var command = Command("the-command", "",
ZeroOrMoreArguments()
.LegalFilePathsOnly());
var invalidCharacters = $"|{Path.GetInvalidPathChars().First()}|";
output.WriteLine(string.Join("\n", Path.GetInvalidPathChars()));
var result = command.Parse($"the-command {invalidCharacters}");
result.UnmatchedTokens
.Should()
.BeEquivalentTo(invalidCharacters);
}
[Fact]
public void LegalFilePathsOnly_accepts_arguments_containing_valid_path_characters()
{
var command = Command("the-command", "",
ZeroOrMoreArguments()
.LegalFilePathsOnly());
var validPathName = Directory.GetCurrentDirectory();
var validNonExistingFileName = Path.Combine(validPathName, Guid.NewGuid().ToString());
var result = command.Parse($"the-command {validPathName} {validNonExistingFileName}");
result.Errors.Should().BeEmpty();
}
[Fact] [Fact]
public void An_argument_can_be_invalid_based_on_file_existence() public void An_argument_can_be_invalid_based_on_file_existence()
{ {

Просмотреть файл

@ -86,6 +86,29 @@ namespace Microsoft.DotNet.Cli.CommandLine
return null; return null;
})); }));
public static ArgumentsRule LegalFilePathsOnly(
this ArgumentsRule rule) =>
rule.And(new ArgumentsRule(o =>
{
foreach (var arg in o.Arguments)
{
try
{
var fileInfo = new FileInfo(arg);
}
catch (NotSupportedException ex)
{
return ex.Message;
}
catch (ArgumentException ex)
{
return ex.Message;
}
}
return null;
}));
public static ArgumentsRule WithSuggestionsFrom( public static ArgumentsRule WithSuggestionsFrom(
params string[] values) => params string[] values) =>
new ArgumentsRule( new ArgumentsRule(