From 5fa9592d5e6b9b5c315e33a530861572dc78ae1b Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Wed, 30 Sep 2020 09:20:36 +0200 Subject: [PATCH] do not dump extra trailing newlines for multiline blocks --- src/Symfony/Component/Yaml/Dumper.php | 12 ++++-- .../Component/Yaml/Tests/DumperTest.php | 39 ++++++++++++------- .../multiple_lines_as_literal_block.yml | 2 +- ...eral_block_leading_space_in_first_line.yml | 4 -- 4 files changed, 34 insertions(+), 23 deletions(-) delete mode 100644 src/Symfony/Component/Yaml/Tests/Fixtures/multiple_lines_as_literal_block_leading_space_in_first_line.yml diff --git a/src/Symfony/Component/Yaml/Dumper.php b/src/Symfony/Component/Yaml/Dumper.php index 515a361297..5dec10945c 100644 --- a/src/Symfony/Component/Yaml/Dumper.php +++ b/src/Symfony/Component/Yaml/Dumper.php @@ -64,14 +64,18 @@ class Dumper $dumpAsMap = Inline::isHash($input); foreach ($input as $key => $value) { + if ('' !== $output && "\n" !== $output[-1]) { + $output .= "\n"; + } + if ($inline >= 1 && Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value) && false !== strpos($value, "\n") && false === strpos($value, "\r")) { // If the first line starts with a space character, the spec requires a blockIndicationIndicator // http://www.yaml.org/spec/1.2/spec.html#id2793979 $blockIndentationIndicator = (' ' === substr($value, 0, 1)) ? (string) $this->indentation : ''; - $output .= sprintf("%s%s%s |%s\n", $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '', $blockIndentationIndicator); + $output .= sprintf('%s%s%s |%s-', $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '', $blockIndentationIndicator); foreach (explode("\n", $value) as $row) { - $output .= sprintf("%s%s%s\n", $prefix, str_repeat(' ', $this->indentation), $row); + $output .= sprintf("\n%s%s%s", $prefix, str_repeat(' ', $this->indentation), $row); } continue; @@ -84,10 +88,10 @@ class Dumper // If the first line starts with a space character, the spec requires a blockIndicationIndicator // http://www.yaml.org/spec/1.2/spec.html#id2793979 $blockIndentationIndicator = (' ' === substr($value->getValue(), 0, 1)) ? (string) $this->indentation : ''; - $output .= sprintf(" |%s\n", $blockIndentationIndicator); + $output .= sprintf(' |%s', $blockIndentationIndicator); foreach (explode("\n", $value->getValue()) as $row) { - $output .= sprintf("%s%s%s\n", $prefix, str_repeat(' ', $this->indentation), $row); + $output .= sprintf("\n%s%s%s", $prefix, str_repeat(' ', $this->indentation), $row); } continue; diff --git a/src/Symfony/Component/Yaml/Tests/DumperTest.php b/src/Symfony/Component/Yaml/Tests/DumperTest.php index cc2b1e26fe..ef449a4dfd 100644 --- a/src/Symfony/Component/Yaml/Tests/DumperTest.php +++ b/src/Symfony/Component/Yaml/Tests/DumperTest.php @@ -493,19 +493,16 @@ YAML; $data = [ 'foo' => new TaggedValue('bar', "foo\nline with trailing spaces:\n \nbar\ninteger like line:\n123456789\nempty line:\n\nbaz"), ]; - $expected = <<assertSame($expected, $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK)); } @@ -545,7 +542,9 @@ YAML; ], ]; - $this->assertSame(file_get_contents(__DIR__.'/Fixtures/multiple_lines_as_literal_block_leading_space_in_first_line.yml'), $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK)); + $expected = "data:\n multi_line: |4-\n the first line has leading spaces\n The second line does not."; + + $this->assertSame($expected, $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK)); } public function testCarriageReturnFollowedByNewlineIsMaintainedWhenDumpingAsMultiLineLiteralBlock() @@ -568,6 +567,18 @@ YAML; ], 4, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK)); } + public function testNoTrailingNewlineWhenDumpingAsMultiLineLiteralBlock() + { + $data = [ + "a\nb", + "c\nd", + ]; + $yaml = $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK); + + $this->assertSame("- |-\n a\n b\n- |-\n c\n d", $yaml); + $this->assertSame($data, Yaml::parse($yaml)); + } + public function testZeroIndentationThrowsException() { $this->expectException('InvalidArgumentException'); diff --git a/src/Symfony/Component/Yaml/Tests/Fixtures/multiple_lines_as_literal_block.yml b/src/Symfony/Component/Yaml/Tests/Fixtures/multiple_lines_as_literal_block.yml index 9d72f09be8..8c43bd4754 100644 --- a/src/Symfony/Component/Yaml/Tests/Fixtures/multiple_lines_as_literal_block.yml +++ b/src/Symfony/Component/Yaml/Tests/Fixtures/multiple_lines_as_literal_block.yml @@ -1,6 +1,6 @@ data: single_line: 'foo bar baz' - multi_line: | + multi_line: |- foo line with trailing spaces: diff --git a/src/Symfony/Component/Yaml/Tests/Fixtures/multiple_lines_as_literal_block_leading_space_in_first_line.yml b/src/Symfony/Component/Yaml/Tests/Fixtures/multiple_lines_as_literal_block_leading_space_in_first_line.yml deleted file mode 100644 index 3f2dedd10e..0000000000 --- a/src/Symfony/Component/Yaml/Tests/Fixtures/multiple_lines_as_literal_block_leading_space_in_first_line.yml +++ /dev/null @@ -1,4 +0,0 @@ -data: - multi_line: |4 - the first line has leading spaces - The second line does not.