-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathcli_test.go
More file actions
103 lines (83 loc) · 2.77 KB
/
Copy pathcli_test.go
File metadata and controls
103 lines (83 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// OpenRDAP
// Copyright 2017 Tom Harwood
// MIT License, see the LICENSE file.
package rdap
import (
"bytes"
"flag"
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"github.com/openrdap/rdap/test"
)
// Run with `go test -run TestRunCLI -update ./...` to regenerate the golden
// files after an intentional output change.
var updateGolden = flag.Bool("update", false, "update CLI golden files")
// fixtureServer serves the RDAP test fixtures over HTTP so the CLI can be
// driven end-to-end via --server without touching the network.
func fixtureServer(t *testing.T) *httptest.Server {
t.Helper()
mux := http.NewServeMux()
mux.HandleFunc("/domain/example.cz", func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/rdap+json")
_, _ = w.Write(test.LoadFile("rdap/rdap.nic.cz/domain-example.cz.json"))
})
srv := httptest.NewServer(mux)
t.Cleanup(srv.Close)
return srv
}
// TestRunCLIGolden drives RunCLI end-to-end for a range of commands and output
// modes, comparing exit code, stdout, and stderr against committed golden files.
// Network-backed cases point --server at an in-process fixture server and use an
// in-memory cache (--cache-dir "") so nothing touches the real filesystem.
func TestRunCLIGolden(t *testing.T) {
srv := fixtureServer(t)
withServer := func(args ...string) []string {
return append(args, "--cache-dir", "", "--server", srv.URL)
}
cases := []struct {
name string
args []string
}{
{"domain-text", withServer("example.cz")},
{"domain-json", withServer("--json", "example.cz")},
{"domain-whois", withServer("--whois", "example.cz")},
{"domain-raw", withServer("--raw", "example.cz")},
{"version", []string{"--version"}},
{"no-args", []string{}},
{"unknown-type", []string{"--type", "bogus", "example.cz"}},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
var stdout, stderr bytes.Buffer
code := RunCLI(tc.args, &stdout, &stderr, CLIOptions{})
got := fmt.Sprintf("exit: %d\n--- stdout ---\n%s\n--- stderr ---\n%s",
code, stdout.String(), stderr.String())
assertGolden(t, tc.name, got)
})
}
}
func assertGolden(t *testing.T, name, got string) {
t.Helper()
goldenPath := filepath.Join("testdata", "cli", name+".golden")
if *updateGolden {
if err := os.MkdirAll(filepath.Dir(goldenPath), 0o750); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(goldenPath, []byte(got), 0o600); err != nil {
t.Fatal(err)
}
return
}
want, err := os.ReadFile(goldenPath)
if err != nil {
t.Fatalf("read golden (run `go test -run TestRunCLI -update` to create): %s", err)
}
if got != string(want) {
t.Errorf("output mismatch for %s (run `go test -run TestRunCLI -update` to regenerate)\n--- got ---\n%s\n--- want ---\n%s",
name, got, want)
}
}