-
Notifications
You must be signed in to change notification settings - Fork 382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue 2279 #2282
base: main
Are you sure you want to change the base?
Issue 2279 #2282
Conversation
@dotnet-policy-service agree |
This syntax is not recognised by GitHub. Please see Linking a pull request to an issue |
var parseResult = command.Parse(commandLine); | ||
|
||
parseResult.GetCompletionContext() | ||
.Should() | ||
.BeOfType<TextCompletionContext>() | ||
.Which | ||
.CommandLineText | ||
.Should() | ||
.Be(commandLine); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this new test demonstrate the bug, i.e. fail if the bug is not fixed? The call stack of the exception in the issue included ParseResult.GetCompletions, but this test does not seem to call that function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ive checked and surprisingly - no it does not.
when i manually run the following code manually ( with or without a debugger ) it throws an error before I apply my fix ( only if the argument has a = symbol separating the key value ) and after I apply my fix it works. - IE exactly what it should do.
` CliRootCommand _outerCommand = new CliRootCommand
{
new CliCommand("inner")
{
new CliOption("--optionOne"),
new CliOption("--optionTwo")
}
};
var result = _outerCommand.Parse("ou\\=ter inner --optionOne argument1 --optionTwo=argument2");
result.GetCompletions();
`
Within my test I have almost exactly the same code - the only differences are _outerCommand is now named command and the command line string is now defined in a variable before the command is called.
neither of these issues should have changed it.
However when i reverted my fix the test still worked.
I tried adding a check to detect the 'InvalidOperationException' and it made no difference
I tried the following test code and its not failing without my fix being applied ( note i added the manual "throw new InvalidOperationException("hello");" to prove that my test is correctly detecting errors, I also ran it without this line )
` public void CommandLineText_is_parsed_when_option_is_in_name_equals_sign_value_format()
{
Assert.Throws<InvalidOperationException>(methodThatThrows);
void methodThatThrows()
{
CliRootCommand command = new CliRootCommand
{
new CliCommand("inner")
{
new CliOption<string>("--optionOne"),
new CliOption<string>("--optionTwo")
}
};
var commandLine = "outer inner --optionOne argument1 --optionTwo=argument2";
var parseResult = command.Parse(commandLine);
throw new InvalidOperationException("hello");
}
}`
I dont know what to do here or why my functionally identical code is behaving differently when it runs in test.
Advice would be appreciated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps the argument of GetCompletions makes a difference. In #2279 (comment),
(Note: I found the easiest way to actually reproduce this was by running this with the arguments of: [suggest:30] "CompletionCrashRepro --test=asd" rather than actually doing tab completion.)
which would presumably call GetCompletions(30). Your code above calls result.GetCompletions()
instead.
The length of CompletionCrashRepro --test=asd
is 31 characters. I think 30 then means the insertion point would have been between --test=as
and d
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also
i tried adding 'System.Environment.Exit(2);' to 'GetWordToComplete' in 'CompletionContext.cs' and running the test and non of them fail which shows that none of the tests run this piece of code.
They do exit when running the code manually.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest you run the test in a debugger and trace why it doesn't go to GetWordToComplete.
It might be related to how the CliRootCommand constructor uses CliRootCommand.ExecutableName as the name of the command. CliRootCommand derives that from Environment.GetCommandLineArgs()[0]
, which would differ between the test runner process and your experimental application.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test fixed - its working properly now.
please finish reviwing
Tests are fixed, please review again |
My apologies for barging into the conversion like that, but please note that this is not a proper fix, as it is introducing another bug. Without the suggested PR, a CLI like To reproduce, it is enough to change the CLI string the test is using from
to
It is important to note that the occurrence of this issue is conditional on the option/argument value containing A proper fix would ideally also address the related issue that the GetCompletions() method seems to not like the last option/argument value in a CLI string having white-spaces when the CLI parameters have been passed to the Parse method as a single string only. Regardless of whether this PR is applied or not, |
I did think of that the = character is a special character which has to be escaped if used on the CLI https://mywiki.wooledge.org/BashGuide/SpecialCharacters I did test this during development but did not include a test for this |
Incorrect in the sense that this is not generally true. Whether |
Even if |
the "var commandLine = "inner --optionOne -=Yay=-";" issue has been fixed |
@KalleOlaviNiemitalo can you guys take another look at this please |
Assert.True(parseResult.Tokens[1].Value == "--optionOne"); | ||
Assert.True(parseResult.Tokens[2].Value == "argument1"); | ||
Assert.True(parseResult.Tokens[3].Value == "--optionTwo"); | ||
Assert.True(parseResult.Tokens[4].Value == "argument2"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We generally avoid the use of Assert.IsTrue
in this codebase as it provides very little useful feedback when a test fails. Also, when there are multiple assertions, only the first failure is seen in the output, without feedback about whether the other assertions pass or fail.
I'd suggest changing the assertions to the following, which addresses both concerns:
parseResult.Tokens.Should().BeEquivalentSequenceTo("--optionOne", "argument1", "--optionTwo", "argument2");
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried
parseResult.Tokens.Should().BeEquivalentSequenceTo("--optionOne", "argument1", "--optionTwo", "argument2");
and i get this error
Expected item[0] to be System.String, but found System.CommandLine.Parsing.CliToken
I also tried this
parseResult.Tokens.Should().BeEquivalentSequenceTo( new CommandLine.Parsing.CliToken("inner", CommandLine.Parsing.CliTokenType.Command, null), new CommandLine.Parsing.CliToken("--optionOne", CommandLine.Parsing.CliTokenType.Command, null), new CommandLine.Parsing.CliToken("argument1", CommandLine.Parsing.CliTokenType.Command, null), new CommandLine.Parsing.CliToken("--optionTwo", CommandLine.Parsing.CliTokenType.Command, null), new CommandLine.Parsing.CliToken("argument2", CommandLine.Parsing.CliTokenType.Command, null) );
and got the following error
Expected object to be inner, but found inner.
Expected object to be --optionOne, but found --optionOne.
Expected object to be argument1, but found argument1.
Expected object to be --optionTwo, but found --optionTwo.
Expected object to be argument2, but found argument2.
Any ideas how to make this work?
@@ -33,6 +36,128 @@ public void CommandLineText_preserves_command_line_prior_to_splitting_when_compl | |||
.Be(commandLine); | |||
} | |||
|
|||
[Fact] | |||
public void CommandLineText_is_parsed_when_option_other_than_last_is_in_name_equals_sign_value_format() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test name is a little hard to understand, and CommandLinetext_is_parsed
isn't very specific about the expectation. I think the expectation is that the tokens don't include the delimiters? (Also, that it doesn't throw, but that typically goes without saying.)
A more sentence-style test name is encouraged, e.g. something like When_equals_sign_is_used_as_delimiter_then_it_is_not_included_in_tokens
.
Some of these tests seem to differ only by the arguments being parsed, so combining them into a [Theory]
might give you fewer tests to name.
Fix issue #2279
The test covers both the --option value and --option=value formats