From 32cd0b12a4b1fc7b15a45d3cdde5681cb18b57e9 Mon Sep 17 00:00:00 2001 From: Matthias Diester Date: Fri, 17 Sep 2021 21:13:27 +0200 Subject: [PATCH] Fix block style strip multi-line issue As reported in https://github.com/homeport/dyff/issues/180, when using text that spans over multiple lines using the strip option (`|-`), the output was not created correctly. Simplify multi-line output code to properly place a newline at every line with the last line being an exception. --- output_yaml.go | 11 +++++------ output_yaml_test.go | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/output_yaml.go b/output_yaml.go index 3aa7ea9..c142f12 100644 --- a/output_yaml.go +++ b/output_yaml.go @@ -292,13 +292,12 @@ func (p *OutputProcessor) neatYAMLofNode(prefix string, skipIndentOnFirstLine bo colorName = "multiLineTextColor" fmt.Fprint(p.out, p.colorize("|", colorName), "\n") for i, line := range lines { - if i == len(lines)-1 { - continue - } - - fmt.Fprint(p.out, prefix, p.colorize(line, colorName)) + fmt.Fprint(p.out, + prefix, + p.colorize(line, colorName), + ) - if i < len(lines)-2 { + if i != len(lines)-1 { fmt.Fprint(p.out, "\n") } } diff --git a/output_yaml_test.go b/output_yaml_test.go index c62fe24..1e07101 100644 --- a/output_yaml_test.go +++ b/output_yaml_test.go @@ -194,7 +194,7 @@ list: # at list definition - two # two # before multiline -multiline: | +multiline: |- This is a multi line te @@ -333,6 +333,37 @@ yaml: Expect(err).ToNot(HaveOccurred()) Expect(output).To(BeEquivalentTo(expected)) }) + + It("should create YAML output of multi-line text", func() { + example := []byte(`--- +data: + repos.yaml: |- + repos: + - apply_requirements: + - approved + - mergeable + id: /.*/ + test: /.*/ +`) + + expected := `--- +data: + repos.yaml: | + repos: + - apply_requirements: + - approved + - mergeable + id: /.*/ + test: /.*/ +` + + var node yamlv3.Node + Expect(yamlv3.Unmarshal(example, &node)).ToNot(HaveOccurred()) + + output, err := ToYAMLString(node) + Expect(err).ToNot(HaveOccurred()) + Expect(output).To(BeEquivalentTo(expected)) + }) }) Context("create YAML output for type struct", func() {