Skip to content

Commit

Permalink
Merge pull request #10 from stuart-warren/doc-sep
Browse files Browse the repository at this point in the history
fix: remove extra doc separator
  • Loading branch information
stuart-warren authored Oct 11, 2019
2 parents 148b722 + 261e9fa commit 7210c5a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
7 changes: 4 additions & 3 deletions format.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ const indent = 2
func Format(r io.Reader) ([]byte, error) {
dec := yaml.NewDecoder(r)
out := bytes.NewBuffer(nil)
enc := yaml.NewEncoder(out)
enc.SetIndent(indent)
defer enc.Close()
for {
enc := yaml.NewEncoder(out)
enc.SetIndent(indent)
defer enc.Close()
var doc yaml.Node
err := dec.Decode(&doc)
if err == io.EOF {
Expand All @@ -33,6 +33,7 @@ func Format(r io.Reader) ([]byte, error) {
if err := enc.Encode(sortYAML(&doc)); err != nil {
return nil, fmt.Errorf("failed encoding: %s", err)
}
enc.Close()
}
return out.Bytes(), nil
}
51 changes: 49 additions & 2 deletions format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ k:
c: i
`
exp := []byte(expected)
out, err := yamlfmt.Format(bytes.NewBuffer([]byte(in)))
out, err := yamlfmt.Format(bytes.NewReader([]byte(in)))
if err != nil {
t.Fatalf("Unexpected error: %s\n", err)
}
Expand All @@ -50,7 +50,54 @@ bar:
foo: baz # comment
`
exp := []byte(expected)
out, err := yamlfmt.Format(bytes.NewBuffer([]byte(in)))
out, err := yamlfmt.Format(bytes.NewReader([]byte(in)))
if err != nil {
t.Fatalf("Unexpected error: %s\n", err)
}
if !bytes.Equal(out, exp) {
t.Fatalf("Got:\n%s\nexpected:\n%s\n", out, exp)
}
t.Logf("got:\n%v\n", out)
t.Logf("expected:\n%v\n", exp)
}

func TestYamlMultiSort(t *testing.T) {
var in = `---
k:
c: l
i:
i: k
v:
# comment
c: i
---
k:
c: l
i:
i: k
v:
# comment
c: i
`
var expected = `---
k:
c: l
i:
i: k
v:
# comment
c: i
---
k:
c: l
i:
i: k
v:
# comment
c: i
`
exp := []byte(expected)
out, err := yamlfmt.Format(bytes.NewReader([]byte(in)))
if err != nil {
t.Fatalf("Unexpected error: %s\n", err)
}
Expand Down

0 comments on commit 7210c5a

Please sign in to comment.