Skip to content

Commit

Permalink
ast: append new line at the end of file (#329)
Browse files Browse the repository at this point in the history
  • Loading branch information
kurochan committed Dec 8, 2022
1 parent 49dc308 commit dc3fed8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
6 changes: 5 additions & 1 deletion ast/ast.go
Expand Up @@ -566,7 +566,11 @@ func (f *File) String() string {
for _, doc := range f.Docs {
docs = append(docs, doc.String())
}
return strings.Join(docs, "\n")
if len(docs) > 0 {
return strings.Join(docs, "\n") + "\n"
} else {
return ""
}
}

// DocumentNode type of Document
Expand Down
6 changes: 3 additions & 3 deletions parser/parser_test.go
Expand Up @@ -595,7 +595,7 @@ i: 'j'
for _, doc := range f.Docs {
ast.Walk(&v, doc.Body)
}
expect := fmt.Sprintf("\n%+v\n", f)
expect := fmt.Sprintf("\n%+v", f)
if test.expect != expect {
tokens.Dump()
t.Fatalf("unexpected output: [%s] != [%s]", test.expect, expect)
Expand All @@ -614,7 +614,7 @@ func TestNewLineChar(t *testing.T) {
if err != nil {
t.Fatalf("%+v", err)
}
actual := fmt.Sprintf("%v\n", ast)
actual := fmt.Sprintf("%v", ast)
expect := `a: "a"
b: 1
`
Expand Down Expand Up @@ -800,7 +800,7 @@ foo: > # comment
if err != nil {
t.Fatalf("%+v", err)
}
got := "\n" + f.String() + "\n"
got := "\n" + f.String()
if test.yaml != got {
t.Fatalf("expected:%s\ngot:%s", test.yaml, got)
}
Expand Down
12 changes: 6 additions & 6 deletions path_test.go
Expand Up @@ -343,7 +343,7 @@ a:
if err := path.MergeFromReader(file, strings.NewReader(test.src)); err != nil {
t.Fatalf("%+v", err)
}
actual := "\n" + file.String() + "\n"
actual := "\n" + file.String()
if test.expected != actual {
t.Fatalf("expected: %q. but got %q", test.expected, actual)
}
Expand All @@ -360,7 +360,7 @@ a:
if err := path.MergeFromFile(file, src); err != nil {
t.Fatalf("%+v", err)
}
actual := "\n" + file.String() + "\n"
actual := "\n" + file.String()
if test.expected != actual {
t.Fatalf("expected: %q. but got %q", test.expected, actual)
}
Expand All @@ -380,7 +380,7 @@ a:
if err := path.MergeFromNode(file, src.Docs[0]); err != nil {
t.Fatalf("%+v", err)
}
actual := "\n" + file.String() + "\n"
actual := "\n" + file.String()
if test.expected != actual {
t.Fatalf("expected: %q. but got %q", test.expected, actual)
}
Expand Down Expand Up @@ -525,7 +525,7 @@ building:
if err := path.ReplaceWithReader(file, strings.NewReader(test.src)); err != nil {
t.Fatalf("%+v", err)
}
actual := "\n" + file.String() + "\n"
actual := "\n" + file.String()
if test.expected != actual {
t.Fatalf("expected: %q. but got %q", test.expected, actual)
}
Expand All @@ -542,7 +542,7 @@ building:
if err := path.ReplaceWithFile(file, src); err != nil {
t.Fatalf("%+v", err)
}
actual := "\n" + file.String() + "\n"
actual := "\n" + file.String()
if test.expected != actual {
t.Fatalf("expected: %q. but got %q", test.expected, actual)
}
Expand All @@ -562,7 +562,7 @@ building:
if err := path.ReplaceWithNode(file, src.Docs[0]); err != nil {
t.Fatalf("%+v", err)
}
actual := "\n" + file.String() + "\n"
actual := "\n" + file.String()
if test.expected != actual {
t.Fatalf("expected: %q. but got %q", test.expected, actual)
}
Expand Down

0 comments on commit dc3fed8

Please sign in to comment.