-
-
Notifications
You must be signed in to change notification settings - Fork 438
Add test case for multiline string #610
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
Add test case for multiline string #610
Conversation
|
This test case doesn't actually expose the error you're talking about. It does expose a formatting bug: But it does not show the error you're seeing. |
|
It's weird, I could reproduce the error from CI but not always. It seems to be related to file ending (CRLF) Sometime having one line of description give the expected error, sometimes not, can't understand why or what is happening |
weaverryan
left a comment
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.
Hey @jon-ht!
You went the extra mile and fixed the bug too! Congrats on navigating my horribly confusing code in YamlSourceManipulator :)
I've left some minor comments. It's mostly that we need to do our best to keep this complex class as understandable as possible.
Thanks!
|
|
||
| $data = Yaml::parse($source); | ||
| // Multiline string ends with an \n | ||
| $data = Yaml::parse(trim($source, "\n")); |
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 actually trim this below already. So it looks like we just need to trim it earlier. I'm thinking the whole inside of the foreach would be this:
list($source, $changeCode, $expected) = explode('===', $file->getContents());
$source = rtrim($source, "\n");
$expected = ltrim($expected, "\n");
$data = Yaml::parse($source);
eval($changeCode);
yield $file->getFilename() => [
'source' => $source,
'newData' => $data,
'expectedSource' => $expected,
];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 catch, I missed this !
| description: | | ||
| Multiline string | ||
| Second line | ||
| Third line No newline at end of file |
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.
beautiful test cases
src/Util/YamlSourceManipulator.php
Outdated
| $patternValue = $quotedValue; | ||
|
|
||
| // Iterates until we find a new line char or we reach end of file | ||
| if ($this->findPositionOfMultilineCharInLine($offset)) { |
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.
| if ($this->findPositionOfMultilineCharInLine($offset)) { | |
| if ($this->findPositionOfMultilineCharInLine($offset) !== null) { |
That will help clarify that we're looking for IF there is a multiline char here, we're not (in this case) actually caring about the exact position returned.
src/Util/YamlSourceManipulator.php
Outdated
| && rtrim($currentVal, "\n") !== $currentVal | ||
| && rtrim($newVal, "\n") === $newVal | ||
| ) { | ||
| $newVal .= "\n"; |
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.
Could you add a $this->log() line here that explains what you're doing? There's a little-known feature where, in YamlSourceManipulatorTest.php, you can uncomment a setLogger() line to see all these logs entries. And it helps document what each part does, since it's SO complex. For instance, in this case, I'm not entirely sure what edge-case this is fixing.
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.
Well, it seems not required. I think I went crazy and wanted to add/remove \n everywhere 😄.
I will remove completely
src/Util/YamlSourceManipulator.php
Outdated
| $newYamlValue = $this->convertToYaml($value); | ||
| if (!\is_array($originalVal) && \is_array($value)) { | ||
| $isMultilineValue = null !== $this->findPositionOfMultilineCharInLine($this->currentPosition); | ||
| $originalValEndsWithNewLine = $isMultilineValue ? rtrim($originalVal, "\n") !== $originalVal : false; |
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 think the rtrim($originalVal, "\n") !== $originalVal is confusing me. What about this instead?
str_ends_with($originalValue, "\n")
That's a PHP 8 function, but if you require "symfony/polyfill-php80": "^1.15", then we can use it.
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.
Are you OK to require symfony/polyfill-php80 then ? I'll made apprioriate changes if so (there are some other lines like https://github.com/symfony/maker-bundle/pull/610/files#diff-a07d297f64d0bf4a1e9c902b787ecf20R219-R223)
|
|
||
| // In case of multiline, $value is converted as plain string like "Foo\nBar" | ||
| // We need to keep it "as is" | ||
| $newYamlValue = $isMultilineValue ? rtrim($value, "\n") : $this->convertToYaml($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.
Why do we need to rtrim() here? I can see that if I remove it the tests fail, but I can't figure out why it's needed.
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.
One thing I noticed is, in this test case multiline_string_before_property.test, $value ends with an \n
This will cause indentMultilineYamlArray to add an extra line that's not wanted.
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.
Ah, I understand. I think the implementation is what's confusing me. All multiline arrays must end in a line break, by their very nature. If anything, in this spot, (just thinking about the logical flow of the code) I would expect you to be guaranteeing that the $newYamlValue does end in \n, instead of stripping it. After all, if we made sure that $newYamlValue ended in 1 line break... then that should work perfectly with YAML, which also wants the line break :). I'm wondering if we're trimming and adding \n in so many places that we're doing and undoing extra work.
There is the edge-case of not needing the "\n" if the multiline string is at the end of the file. That should be handled (somewhere) with an if statement specifically for it. Something like:
// maybe down before the `$newContents = substr(...)` line
if (false === $this->findNextLineBreak($newPosition) && $isMultilineValue) {
$newYamlValue = trim($newYamlValue, "\n");
}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 won't lie to you, I also think there is a lot of trimming to handle multiline string...
Actually, it seems that re-adding original \n (after $newYamlValue = "\n".$this->indentMultilineYamlArray($newYamlValue);) breaks test case because, as mentionned above, YamlDumper will add an extra line
Maybe this bug is related to Yaml component and not YamlSourceManipulator
|
Thanks for taking this on - I knew that I didn't cover this case when I built |
|
I was able to remove some useless trimming after a second pass, maybe this is less confusing now. |
|
Thanks for solving this SUPER ugly shortcoming @jon-ht! |


Fix #608