-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Console] Fix question formatting using SymfonyStyle::ask() #20970
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
Conversation
b52d81f
to
da5d94c
Compare
da5d94c
to
06b0ddd
Compare
👍 Status: Reviewed |
@@ -101,6 +103,15 @@ public function testAskEscapeLabel() | |||
$this->assertOutputContains('Do you want a \?', $output); |
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 does not longer look relevant, and anyway I think it didn't covered well the initial issue 😕
Changing it for the following:
public function testAskEscapeLabel()
{
$helper = new SymfonyQuestionHelper();
$helper->setInputStream($this->getInputStream('sure'));
$helper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), new Question('Add a \\'));
$this->assertOutputContains('Add a \\', $output);
}
will result into the following failure:
1) Symfony\Component\Console\Tests\Helper\SymfonyQuestionHelperTest::testAskEscapeLabel
Failed asserting that ' Add a </info>:
> ' contains "Add a \".
Looks edgy to have a simple backslash at the end, but it actually was the initial issue. It seems there is no need to escape anything else 😕
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.
The initial issue was about having a backslash as default value which has been fixed, not really about a question ending with a \
, which is indeed a very edge case to me. I think this is a good consensus, don't you?
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.
As I said, your fix is legitimate, but there is still another issue elsewhere.
That said, the test mentioned does not actually cover anything I think and should be removed ^^'
Edit: This issue cannot be solved in the output formatter directly, because a \
before a tag already means escaping the tag and is the expected behavior.
If you want to use a trailing slash somewhere, an know it'll be decorated, simply escape it, or use <<
:
public function testDecoratedTrailingBackslash()
{
$formatter = new OutputFormatter(true);
- $this->assertSame("\033[32mstring ending with \\\e[39m", $formatter->format('<info>string ending with \</info>'));
+ $this->assertSame("\033[32mstring ending with \\\e[39m", $formatter->format('<info>string ending with <<</info>'));
}
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.
Thanks for looking at it. The useless test has been removed
06b0ddd
to
9d46712
Compare
So to be clear, if you have a value with backslashes like a namespace you need to replace these before usage? |
@sstok: You'll at least need to escape the last character if it's a backslash using Maybe we can do this check before wrapping any user text into tags, and replace it automatically, but that probably means searching and updating a lot more places. |
$question->setValidator(function ($value) { return $value; }); | ||
$question->setValidator(function ($value) { | ||
return $value; | ||
}); |
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.
should be reverted. The new PHP-CS-Fixer is not configured the right way, sorry about that.
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.
good to know, reverted
After all, I think there is only a few places that would require this special check, so it might be worth it and can be fixed within this very same PR IMHO. @chalasr You can apply 2.7...ogizanagi:fix/2.7/console/trailing_backslashes if you think it makes sense, or I'll submit another PR once this one is merged. |
cba6f86
to
fd23354
Compare
@ogizanagi It makes good sense to me, thank you for the patch, applied. |
* | ||
* @return string Escaped text | ||
*/ | ||
public static function escapeTrailingBackslash($text) |
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.
Shouldn't the method be marked as internal?
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.
Not sure. I guess it can be used by anyone creating it's own helpers in userland.
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 agree that it should be marked as internal.
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.
done
fd23354
to
e189183
Compare
This is ready to go |
Thank you @chalasr. |
…) (chalasr, ogizanagi) This PR was merged into the 2.7 branch. Discussion ---------- [Console] Fix question formatting using SymfonyStyle::ask() | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #20964 | License | MIT | Doc PR | n/a Given ```php $io = new SymfonyStyle($input, $output); $io->ask('Do you want to use Foo\\Bar <comment>or</comment> Foo\\Baz\\?', 'Foo\\Bar'); ``` Before output  After output  Commits ------- e189183 [Console] SymfonyStyle: Escape trailing backslashes in user texts 9d46712 [Console] Fix question formatting using SymfonyStyle::ask()
Given
Before output

After output
