Skip to content

Commit 19fa190

Browse files
committed
test(cli): cover malformed diff baseline inputs
1 parent 0f3b0b6 commit 19fa190

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package cli
2+
3+
import (
4+
"maps"
5+
"slices"
6+
"strings"
7+
"testing"
8+
)
9+
10+
// These fixtures are corrupt files, not incomplete sets of valid results:
11+
// parsing must fail without returning the names read before the bad line.
12+
var malformedBaselines = []struct{ name, content string }{
13+
{"truncated JSON array", `[{"subdomain":`},
14+
{"unterminated CSV quote", "subdomain,type,value\n\"www.example.com,A,192.0.2.1\n"},
15+
// encoding/csv enforces the header's field count. Keep that rejection
16+
// explicit instead of treating a short row as a names-only result.
17+
{"short CSV row", "subdomain,type,value\nwww.example.com\n"},
18+
{"bad middle JSONL line", "{\"subdomain\":\"first.example.com\"}\n{\"subdomain\":}\n{\"subdomain\":\"last.example.com\"}\n"},
19+
}
20+
21+
func TestParseBaselineMalformedInput(t *testing.T) {
22+
for _, tc := range malformedBaselines {
23+
t.Run(tc.name, func(t *testing.T) {
24+
b, err := parseBaseline([]byte(tc.content))
25+
if err == nil {
26+
t.Fatal("malformed baseline was accepted")
27+
}
28+
if len(b.names) != 0 || len(b.records) != 0 || b.hasRecords {
29+
t.Fatalf("malformed baseline returned partial results: %+v", b)
30+
}
31+
})
32+
}
33+
}
34+
35+
func TestParseBaselineTextChangesAndBOM(t *testing.T) {
36+
for _, tc := range []struct {
37+
name, content string
38+
want []string
39+
hasRecords bool
40+
}{
41+
// A text baseline is a list of names, not an instruction to replay
42+
// changes: even the removed-name line remains part of that list.
43+
{"diff text", "+ new.example.com\n- old.example.com\n~ cdn.example.com A=192.0.2.1 (was A=192.0.2.2)\n", []string{"cdn.example.com", "new.example.com", "old.example.com"}, false},
44+
{"BOM JSON array", "\uFEFF" + `[{"subdomain":"WWW.example.com."}]`, []string{"www.example.com"}, true},
45+
} {
46+
t.Run(tc.name, func(t *testing.T) {
47+
b, err := parseBaseline([]byte(tc.content))
48+
if err != nil {
49+
t.Fatal(err)
50+
}
51+
if got := slices.Sorted(maps.Keys(b.names)); !slices.Equal(got, tc.want) {
52+
t.Errorf("names %v, want %v", got, tc.want)
53+
}
54+
if b.hasRecords != tc.hasRecords || len(b.records) != 0 {
55+
t.Errorf("hasRecords %v, records %v; want %v and no records", b.hasRecords, b.records, tc.hasRecords)
56+
}
57+
})
58+
}
59+
}
60+
61+
func TestE2EDiffRejectsMalformedBaselines(t *testing.T) {
62+
valid := writeFile(t, "valid.txt", "www.example.com\n")
63+
for _, tc := range malformedBaselines {
64+
t.Run(tc.name, func(t *testing.T) {
65+
bad := writeFile(t, "bad.txt", tc.content)
66+
for _, files := range [][]string{{bad, valid}, {valid, bad}} {
67+
code, stdout, stderr := runCLIStreams(t, append([]string{"diff"}, files...)...)
68+
if code != exitFailure || stdout != "" || !strings.Contains(stderr, "Error: reading "+bad+":") {
69+
t.Errorf("diff %v: exit %d, stdout %q, stderr %q; want failure naming the bad file and no output", files, code, stdout, stderr)
70+
}
71+
}
72+
})
73+
}
74+
}

0 commit comments

Comments
 (0)