MCPcopy Create free account
hub / github.com/cli/cli / TestCmdLog

Function TestCmdLog

script/ghmock/log_test.go:41–186  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

39}
40
41func TestCmdLog(t *testing.T) {
42 first := loggedRequest{Run: "run-1", Binary: "./old", Seq: 1, Method: "GET", Name: "GET /user", Status: 200, Matched: true, Response: 1, Responses: 1}
43 query := loggedRequest{
44 Run: "run-2", Binary: "./new", Seq: 1, Method: "POST", Name: "graphql Viewer",
45 GraphQL: &graphQLRequest{Kind: "query", Operation: "Viewer", Query: "query Viewer {\n viewer { login }\n}", Variables: json.RawMessage(`{"a": 1}`)},
46 Status: 200, Matched: true, Response: 1, Responses: 1,
47 }
48 write := loggedRequest{Run: "run-2", Binary: "./new", Seq: 2, Method: "POST", Name: "POST /repos/o/r/issues", Body: `{"title": "Bug"}`, Status: 404}
49 raw := []byte{0, 255}
50
51 tests := []struct {
52 name string
53 entries []loggedRequest
54 runs []runRecord
55 args []string
56 want string
57 large bool
58 }{
59 {
60 name: "the latest run can have no requests",
61 entries: []loggedRequest{first},
62 runs: []runRecord{{ID: "run-1"}, {ID: "run-2"}},
63 want: "No requests logged.\n",
64 },
65 {
66 name: "history remains available after a request-free run",
67 entries: []loggedRequest{first},
68 runs: []runRecord{{ID: "run-1"}, {ID: "run-2"}},
69 args: []string{"--all"},
70 want: "=== run run-1, gh ./old\n1. GET /user -> 200, response 1 of 1\n",
71 },
72 {
73 name: "raw response larger than the old JSONL scanner limit",
74 args: []string{"--full"},
75 large: true,
76 },
77 {
78 name: "raw bytes are retained in JSON",
79 entries: []loggedRequest{{Seq: 1, Method: "POST", Name: "POST /binary", RawBody: &raw, ResponseRawBody: &raw, Status: 200}},
80 args: []string{"--json"},
81 want: `{"run":"","binary":"","seq":1,"method":"POST","host":"","name":"POST /binary","raw_body":"AP8=","status":200,"matched":false,"response_raw_body":"AP8="}` + "\n",
82 },
83 {
84 name: "raw bytes are summarized safely in full logs",
85 entries: []loggedRequest{{Seq: 1, Method: "POST", Name: "POST /binary", RawBody: &raw, ResponseRawBody: &raw, Status: 200}},
86 args: []string{"--full"},
87 want: fmt.Sprintf("1. POST /binary raw body 2 bytes, sha256 %[1]x -> 200, no response stored\n raw request body: 2 bytes, sha256 %[1]x (bytes in --json)\n raw response body: 2 bytes, sha256 %[1]x (bytes in --json)\n", sha256.Sum256(raw)),
88 },
89 {
90 name: "nothing logged",
91 want: "No requests logged.\n",
92 },
93 {
94 name: "only the last run by default",
95 entries: []loggedRequest{first, query, write},
96 want: `=== run run-2, gh ./new
971. graphql Viewer {"a":1} -> 200, response 1 of 1
982. POST /repos/o/r/issues body {"title":"Bug"} -> 404, no response stored

Callers

nothing calls this directly

Calls 7

withProblemsFunction · 0.85
cmdLogFunction · 0.85
appendLogMethod · 0.80
appendRunMethod · 0.80
EqualMethod · 0.80
RunMethod · 0.65
StringMethod · 0.65

Tested by

no test coverage detected