|
1 | 1 | package cli |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "strings" |
4 | 7 | "testing" |
5 | 8 | ) |
6 | 9 |
|
| 10 | +// TestDiffSubcommand: `subenum diff` compares two saved files offline (#310): |
| 11 | +// added, missing and (with -records) changed names, exit 4 on differences. |
| 12 | +func TestDiffSubcommand(t *testing.T) { |
| 13 | + prev := writeFile(t, "prev.jsonl", strings.Join([]string{ |
| 14 | + `{"subdomain":"www.example.com","records":[{"type":"A","value":"192.0.2.1"}]}`, |
| 15 | + `{"subdomain":"old.example.com","records":[{"type":"A","value":"192.0.2.2"}]}`, |
| 16 | + `{"subdomain":"cdn.example.com","records":[{"type":"CNAME","value":"a.cdn.test"},{"type":"A","value":"192.0.2.3"}]}`, |
| 17 | + `{"type":"summary","verdict":"complete"}`, |
| 18 | + }, "\n")+"\n") |
| 19 | + cur := writeFile(t, "cur.csv", "subdomain,type,value\n"+ |
| 20 | + "WWW.example.com.,A,192.0.2.9\n"+ |
| 21 | + "new.example.com,A,192.0.2.4\n"+ |
| 22 | + "cdn.example.com,CNAME,b.cdn.test\n") |
| 23 | + run := func(args ...string) (int, string, string) { |
| 24 | + var stdout, stderr bytes.Buffer |
| 25 | + code := runWith(runArgs{args: append([]string{"diff"}, args...), stdin: strings.NewReader(""), stdout: &stdout, stderr: &stderr}) |
| 26 | + return code, stdout.String(), stderr.String() |
| 27 | + } |
| 28 | + |
| 29 | + code, out, errOut := run(prev, cur) |
| 30 | + if want := "+ new.example.com\n- old.example.com\n"; code != exitChanges || out != want { |
| 31 | + t.Fatalf("names only: code %d, stdout %q (stderr %q); want %d, %q", code, out, errOut, exitChanges, want) |
| 32 | + } |
| 33 | + |
| 34 | + code, out, _ = run("-records", prev, cur) |
| 35 | + if want := "+ new.example.com A=192.0.2.4\n~ cdn.example.com CNAME=b.cdn.test (was CNAME=a.cdn.test A=192.0.2.3)\n~ www.example.com A=192.0.2.9 (was A=192.0.2.1)\n- old.example.com A=192.0.2.2\n"; code != exitChanges || out != want { |
| 36 | + t.Fatalf("-records: code %d, stdout %q; want %q", code, out, want) |
| 37 | + } |
| 38 | + |
| 39 | + // -type CNAME leaves www's moved address out of the comparison. |
| 40 | + code, out, _ = run("-records", "-type", "CNAME", "-format", "jsonl", prev, cur) |
| 41 | + changes := map[string]string{} |
| 42 | + for _, line := range strings.Split(strings.TrimSpace(out), "\n") { |
| 43 | + var r struct{ Subdomain, Change string } |
| 44 | + if err := json.Unmarshal([]byte(line), &r); err != nil { |
| 45 | + t.Fatalf("jsonl line %q: %v", line, err) |
| 46 | + } |
| 47 | + changes[r.Subdomain] = r.Change |
| 48 | + } |
| 49 | + want := map[string]string{"new.example.com": "added", "old.example.com": "missing", "cdn.example.com": "changed"} |
| 50 | + if code != exitChanges || len(changes) != len(want) { |
| 51 | + t.Fatalf("-type CNAME jsonl: code %d, changes %v; want %v", code, changes, want) |
| 52 | + } |
| 53 | + for name, c := range want { |
| 54 | + if changes[name] != c { |
| 55 | + t.Errorf("-type CNAME jsonl: %s is %q, want %q", name, changes[name], c) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + // The same file on both sides: no output, exit 0. |
| 60 | + if code, out, _ := run("-records", prev, prev); code != exitOK || out != "" { |
| 61 | + t.Errorf("identical files: code %d, stdout %q; want 0 and nothing", code, out) |
| 62 | + } |
| 63 | + |
| 64 | + // -records with a text file falls back to names, and says so. |
| 65 | + text := writeFile(t, "cur.txt", "www.example.com\ncdn.example.com\nold.example.com\n") |
| 66 | + if code, out, errOut := run("-records", prev, text); code != exitOK || out != "" || !strings.Contains(errOut, "comparing names only") { |
| 67 | + t.Errorf("text file: code %d, stdout %q, stderr %q", code, out, errOut) |
| 68 | + } |
| 69 | + |
| 70 | + for _, args := range [][]string{{prev}, {prev, cur, cur}, {"-format", "xml", prev, cur}, {"-type", "SRV", prev, cur}} { |
| 71 | + if code, _, _ := run(args...); code != exitUsage { |
| 72 | + t.Errorf("diff %v: code %d, want %d", args, code, exitUsage) |
| 73 | + } |
| 74 | + } |
| 75 | + if code, _, _ := run(prev, writeFile(t, "dir", "")+"-does-not-exist"); code != exitFailure { |
| 76 | + t.Errorf("missing file: code %d, want %d", code, exitFailure) |
| 77 | + } |
| 78 | +} |
| 79 | + |
7 | 80 | // TestLoadBaselineRecords: structured formats carry records, text does not. |
8 | 81 | func TestLoadBaselineRecords(t *testing.T) { |
9 | 82 | for _, c := range []struct { |
|
0 commit comments