Skip to content

[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

Merged
merged 2 commits into from
Dec 22, 2016

Conversation

chalasr
Copy link
Member

@chalasr chalasr commented Dec 17, 2016

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

$io = new SymfonyStyle($input, $output);
$io->ask('Do you want to use Foo\\Bar <comment>or</comment> Foo\\Baz\\?', 'Foo\\Bar');

Before output
before

After output
after

@ogizanagi
Copy link
Contributor

👍

Status: Reviewed

@@ -101,6 +103,15 @@ public function testAskEscapeLabel()
$this->assertOutputContains('Do you want a \?', $output);
Copy link
Contributor

@ogizanagi ogizanagi Dec 17, 2016

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 😕

Copy link
Member Author

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?

Copy link
Contributor

@ogizanagi ogizanagi Dec 17, 2016

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>'));
    }

Copy link
Member Author

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

@chalasr chalasr force-pushed the style/format-question branch from 06b0ddd to 9d46712 Compare December 17, 2016 13:05
@sstok
Copy link
Contributor

sstok commented Dec 17, 2016

So to be clear, if you have a value with backslashes like a namespace you need to replace these before usage?

@ogizanagi
Copy link
Contributor

ogizanagi commented Dec 17, 2016

@sstok: You'll at least need to escape the last character if it's a backslash using OutputFormatter::escape(), or replace it by <<. If backslashes are not at the end (which means right before a </info> tag for instance here), it should not be an issue.

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;
});
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good to know, reverted

@ogizanagi
Copy link
Contributor

ogizanagi commented Dec 17, 2016

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.

@chalasr chalasr force-pushed the style/format-question branch from cba6f86 to fd23354 Compare December 18, 2016 11:33
@chalasr
Copy link
Member Author

chalasr commented Dec 18, 2016

@ogizanagi It makes good sense to me, thank you for the patch, applied.

*
* @return string Escaped text
*/
public static function escapeTrailingBackslash($text)
Copy link
Member

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?

Copy link
Contributor

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.

Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@chalasr chalasr force-pushed the style/format-question branch from fd23354 to e189183 Compare December 19, 2016 10:07
@chalasr
Copy link
Member Author

chalasr commented Dec 22, 2016

This is ready to go

@fabpot
Copy link
Member

fabpot commented Dec 22, 2016

Thank you @chalasr.

@fabpot fabpot merged commit e189183 into symfony:2.7 Dec 22, 2016
fabpot added a commit that referenced this pull request Dec 22, 2016
…) (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
![before](http://image.prntscr.com/image/af3806f866654deda2dec79b50d1ffa2.png)

After output
![after](http://image.prntscr.com/image/59c031d9e02949cebeae7a4734c7043f.png)

Commits
-------

e189183 [Console] SymfonyStyle: Escape trailing backslashes in user texts
9d46712 [Console] Fix question formatting using SymfonyStyle::ask()
@chalasr chalasr deleted the style/format-question branch December 22, 2016 12:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants