Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ast: append new line at the end of file #329

Merged
merged 2 commits into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion ast/ast.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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