From 86bdc568d177a608634899021f375815a633af3b Mon Sep 17 00:00:00 2001 From: jonsequitur Date: Thu, 6 Apr 2017 12:42:14 -0400 Subject: [PATCH 1/2] add LegalFilePathsOnly arguments rule --- CommandLine.Tests/ParsingValidationTests.cs | 34 +++++++++++++++++++++ CommandLine/Accept.cs | 23 ++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/CommandLine.Tests/ParsingValidationTests.cs b/CommandLine.Tests/ParsingValidationTests.cs index 448a7bd..8bb2198 100644 --- a/CommandLine.Tests/ParsingValidationTests.cs +++ b/CommandLine.Tests/ParsingValidationTests.cs @@ -99,6 +99,40 @@ namespace Microsoft.DotNet.Cli.CommandLine.Tests .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 invalidPathName = "/t:SomeMsBuildArg"; + var invalidCharacters = $"|{Path.GetInvalidPathChars().First()}|"; + + output.WriteLine(string.Join("\n", Path.GetInvalidPathChars())); + + var result = command.Parse($"the-command {invalidPathName} {invalidCharacters}"); + + result.UnmatchedTokens + .Should() + .BeEquivalentTo(invalidPathName, 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] public void An_argument_can_be_invalid_based_on_file_existence() { diff --git a/CommandLine/Accept.cs b/CommandLine/Accept.cs index 503cd6d..8f6ae75 100644 --- a/CommandLine/Accept.cs +++ b/CommandLine/Accept.cs @@ -86,6 +86,29 @@ namespace Microsoft.DotNet.Cli.CommandLine 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( params string[] values) => new ArgumentsRule( From 5ad3e34ce3da81404f131b7b9d1352e502d875f6 Mon Sep 17 00:00:00 2001 From: jonsequitur Date: Thu, 6 Apr 2017 16:38:56 -0400 Subject: [PATCH 2/2] fix test for OS X --- CommandLine.Tests/ParsingValidationTests.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/CommandLine.Tests/ParsingValidationTests.cs b/CommandLine.Tests/ParsingValidationTests.cs index 8bb2198..fa6bb7d 100644 --- a/CommandLine.Tests/ParsingValidationTests.cs +++ b/CommandLine.Tests/ParsingValidationTests.cs @@ -106,16 +106,15 @@ namespace Microsoft.DotNet.Cli.CommandLine.Tests ZeroOrMoreArguments() .LegalFilePathsOnly()); - var invalidPathName = "/t:SomeMsBuildArg"; var invalidCharacters = $"|{Path.GetInvalidPathChars().First()}|"; output.WriteLine(string.Join("\n", Path.GetInvalidPathChars())); - var result = command.Parse($"the-command {invalidPathName} {invalidCharacters}"); + var result = command.Parse($"the-command {invalidCharacters}"); result.UnmatchedTokens .Should() - .BeEquivalentTo(invalidPathName, invalidCharacters); + .BeEquivalentTo(invalidCharacters); } [Fact]