Skip to content

Commit 5d2da07

Browse files
committed
internal/cuetxtar: Append \n to generated txtar file content
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 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1210652 TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Daniel Martí <[email protected]> Unity-Result: CUE porcuepine <[email protected]>
1 parent 153fc8a commit 5d2da07

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

Diff for: encoding/jsonschema/decode_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func TestDecode(t *testing.T) {
126126
expr, err := jsonschema.Extract(v, cfg)
127127
if err != nil {
128128
got := "ERROR:\n" + errors.Details(err, nil)
129-
w.Write([]byte(strings.TrimSpace(got) + "\n"))
129+
w.Write([]byte(got))
130130
return
131131
}
132132
if expr == nil {

Diff for: encoding/openapi/openapi_test.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,14 @@ func TestGenerateOpenAPI(t *testing.T) {
124124
}
125125
}
126126

127-
var out = &bytes.Buffer{}
128-
err = json.Indent(out, b, "", " ")
127+
var out bytes.Buffer
128+
err = json.Indent(&out, b, "", " ")
129129
if err != nil {
130130
t.Fatal(err)
131131
}
132132

133133
w := t.Writer("out.json")
134-
jsonStr := out.String()
135-
jsonStr = strings.TrimSpace(jsonStr) + "\n"
136-
_, err = w.Write([]byte(jsonStr))
134+
_, err = w.Write(out.Bytes())
137135
if err != nil {
138136
t.Fatal(err)
139137
}

Diff for: internal/cuetxtar/txtar.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,20 @@ type file struct {
148148
diff bool // true if this contains a diff between fallback and main
149149
}
150150

151+
// bytes returns the bytes in the file's buffer, and ensures that the
152+
// slice finishes with a newline (\n). txtar archives cannot contain
153+
// files without a final newline. Consequently, when comparing
154+
// proposed/generated file content with content from an archive's
155+
// file, we must ensure that the proposed content also finishes with a
156+
// newline.
157+
func (f *file) bytes() []byte {
158+
bs := f.buf.Bytes()
159+
if l := len(bs); l > 0 && bs[l-1] != '\n' {
160+
bs = append(bs, '\n')
161+
}
162+
return bs
163+
}
164+
151165
// HasTag reports whether the tag with the given key is defined
152166
// for the current test. A tag x is defined by a line in the comment
153167
// section of the txtar file like:
@@ -505,7 +519,7 @@ func (x *TxTarTest) run(t *testing.T, m *cuetdtest.M, f func(tc *Test)) {
505519
}
506520
fallback := a.Files[j].Data
507521

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

557571
for _, sub := range tc.outFiles {
558-
result := sub.buf.Bytes()
572+
result := sub.bytes()
559573

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

0 commit comments

Comments
 (0)