From bada7c00f47a7aa6a5d24deda77bf11bd515fd68 Mon Sep 17 00:00:00 2001 From: Matthias Diester Date: Fri, 19 Feb 2021 22:34:57 +0100 Subject: [PATCH] Fix multi document YAML restructure issue Fixes #133 Add explicit delimiter to separate different documents in a YAML file. --- assets/issues/issue-133/input.yml | 13 +++++++++++++ internal/cmd/cmds_test.go | 20 +++++++++++++++++++- internal/cmd/common.go | 1 + 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/assets/issues/issue-133/input.yml b/assets/issues/issue-133/input.yml index e69de29..90053d5 100644 --- a/assets/issues/issue-133/input.yml +++ b/assets/issues/issue-133/input.yml @@ -0,0 +1,13 @@ +--- +bar: foo +foo: bar +name: one + +--- +Foo: Bar +name: two +Bar: Foo + +--- +foobar: foobar +name: three diff --git a/internal/cmd/cmds_test.go b/internal/cmd/cmds_test.go index 0e61545..89e83e4 100644 --- a/internal/cmd/cmds_test.go +++ b/internal/cmd/cmds_test.go @@ -72,6 +72,23 @@ list: - name: one aaa: bbb +`)) + }) + + It("should write a YAML file with multiple documents to STDOUT using restructure feature", func() { + out, err := dyff("yaml", "--plain", "--restructure", assets("issues", "issue-133", "input.yml")) + Expect(err).ToNot(HaveOccurred()) + Expect(out).To(BeEquivalentTo(`--- +name: one +bar: foo +foo: bar +--- +name: two +Foo: Bar +Bar: Foo +--- +name: three +foobar: foobar `)) }) }) @@ -91,7 +108,8 @@ list: data, err := ioutil.ReadFile(filename) Expect(err).To(BeNil()) - Expect(string(data)).To(BeEquivalentTo(`list: + Expect(string(data)).To(BeEquivalentTo(`--- +list: - name: one aaa: bbb `)) diff --git a/internal/cmd/common.go b/internal/cmd/common.go index 489330d..af943e7 100644 --- a/internal/cmd/common.go +++ b/internal/cmd/common.go @@ -130,6 +130,7 @@ func (w *OutputWriter) write(writer io.Writer, filename string) error { fmt.Fprintf(writer, "%s\n", output) case w.PlainMode && w.OutputStyle == "yaml": + fmt.Fprintln(writer, "---") encoder := yamlv3.NewEncoder(writer) encoder.SetIndent(2) encoder.Encode(document)