Skip to content

Commit

Permalink
internal/cuetxtar: Append \n to generated txtar file content
Browse files Browse the repository at this point in the history
Txtar archives cannot model files that lack a trailing newline.
We have many tests that generate content and we then compare that
content to files within txtar archives. Often, the generated content
does not contain a trailing newline, which poses a problem when it must
be compared to content from a txtar archive.

We've typically worked around this by using `fmt.Fprintln` to write out
the generated content, which adds the trailing newline. But this is
fragile and you have to remember to use the println variant. In a couple
of places we've done more elaborate trimming of content.

Given this limitation of txtar archives, it is preferable to always
ensure the generated file content (assuming there is some) ends with a
newline, and to achieve this within the txtar test framework itself. As
implemented here, this does not require any changes to our existing
tests: the numerous sites where fmt.Fprintln is in use continue to work,
but they are no longer necessary - using the plain buf.Write works just
fine now.

This has been tested with CUE_UPDATE=1 too, to ensure that that doesn't
cause any churn.

Signed-off-by: Matthew Sackman <[email protected]>
Change-Id: I55a941c8b99198117af04e23f0c005df3dd09b4d
Dispatch-Trailer: {"type":"trybot","CL":1210652,"patchset":1,"ref":"refs/changes/52/1210652/1","targetBranch":"master"}
  • Loading branch information
cuematthew authored and cueckoo committed Mar 6, 2025
1 parent 153fc8a commit a77be9c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion encoding/jsonschema/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func TestDecode(t *testing.T) {
expr, err := jsonschema.Extract(v, cfg)
if err != nil {
got := "ERROR:\n" + errors.Details(err, nil)
w.Write([]byte(strings.TrimSpace(got) + "\n"))
w.Write([]byte(got))
return
}
if expr == nil {
Expand Down
8 changes: 3 additions & 5 deletions encoding/openapi/openapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,14 @@ func TestGenerateOpenAPI(t *testing.T) {
}
}

var out = &bytes.Buffer{}
err = json.Indent(out, b, "", " ")
var out bytes.Buffer
err = json.Indent(&out, b, "", " ")
if err != nil {
t.Fatal(err)
}

w := t.Writer("out.json")
jsonStr := out.String()
jsonStr = strings.TrimSpace(jsonStr) + "\n"
_, err = w.Write([]byte(jsonStr))
_, err = w.Write(out.Bytes())
if err != nil {
t.Fatal(err)
}
Expand Down
18 changes: 16 additions & 2 deletions internal/cuetxtar/txtar.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,20 @@ type file struct {
diff bool // true if this contains a diff between fallback and main
}

// bytes returns the bytes in the file's buffer, and ensures that the
// slice finishes with a newline (\n). txtar archives cannot contain
// files without a final newline. Consequently, when comparing
// proposed/generated file content with content from an archive's
// file, we must ensure that the proposed content also finishes with a
// newline.
func (f *file) bytes() []byte {
bs := f.buf.Bytes()
if l := len(bs); l > 0 && bs[l-1] != '\n' {
bs = append(bs, '\n')
}
return bs
}

// HasTag reports whether the tag with the given key is defined
// for the current test. A tag x is defined by a line in the comment
// section of the txtar file like:
Expand Down Expand Up @@ -505,7 +519,7 @@ func (x *TxTarTest) run(t *testing.T, m *cuetdtest.M, f func(tc *Test)) {
}
fallback := a.Files[j].Data

result := sub.buf.Bytes()
result := sub.bytes()
if len(result) == 0 || len(fallback) == 0 {
continue
}
Expand Down Expand Up @@ -555,7 +569,7 @@ func (x *TxTarTest) run(t *testing.T, m *cuetdtest.M, f func(tc *Test)) {
files := make([]txtar.File, 0, len(a.Files))

for _, sub := range tc.outFiles {
result := sub.buf.Bytes()
result := sub.bytes()

files = append(files, txtar.File{Name: sub.name})
gold := &files[len(files)-1]
Expand Down

0 comments on commit a77be9c

Please sign in to comment.