Skip to content

Commit

Permalink
Fix multi document YAML restructure issue
Browse files Browse the repository at this point in the history
Fixes #133

Add explicit delimiter to separate different documents in a YAML file.
  • Loading branch information
HeavyWombat committed Feb 19, 2021
1 parent c2f1e58 commit bada7c0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
13 changes: 13 additions & 0 deletions assets/issues/issue-133/input.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
bar: foo
foo: bar
name: one

---
Foo: Bar
name: two
Bar: Foo

---
foobar: foobar
name: three
20 changes: 19 additions & 1 deletion internal/cmd/cmds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
`))
})
})
Expand All @@ -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
`))
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit bada7c0

Please sign in to comment.