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

Function TestFollow

pkg/cmd/agent-task/shared/log_test.go:16–97  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

14)
15
16func TestFollow(t *testing.T) {
17 tests := []struct {
18 name string
19 log string
20 wantStdoutFile string
21 wantStderrFile string
22 }{
23 {
24 name: "sample log 1",
25 log: "testdata/log-1-input.txt",
26 wantStdoutFile: "testdata/log-1-want.txt",
27 },
28 {
29 name: "sample log 2",
30 log: "testdata/log-2-input.txt",
31 wantStdoutFile: "testdata/log-2-want.txt",
32 },
33 {
34 name: "sample log 3 (tolerant parse failures)",
35 log: "testdata/log-3-synthetic-failures-input.txt",
36 wantStdoutFile: "testdata/log-3-synthetic-failures-want.txt",
37 wantStderrFile: "testdata/log-3-synthetic-failures-want-stderr.txt",
38 },
39 }
40
41 for _, tt := range tests {
42 t.Run(tt.name, func(t *testing.T) {
43 raw, err := os.ReadFile(tt.log)
44 require.NoError(t, err)
45
46 // Normalize CRLF to LF to make the tests OS-agnostic.
47 raw = []byte(strings.ReplaceAll(string(raw), "\r\n", "\n"))
48
49 lines := slices.DeleteFunc(strings.Split(string(raw), "\n"), func(line string) bool {
50 return line == ""
51 })
52
53 var hits int
54 fetcher := func() ([]byte, error) {
55 hits++
56 if hits > len(lines) {
57 require.FailNow(t, "too many API calls")
58 }
59 return []byte(strings.Join(lines[0:hits], "\n\n")), nil
60 }
61
62 ios, _, stdout, stderr := iostreams.Test()
63
64 err = NewLogRenderer().Follow(fetcher, stdout, ios)
65 require.NoError(t, err)
66
67 // Handy note for updating the testdata files when they change:
68 // ext := filepath.Ext(tt.log)
69 // stripped := strings.TrimSuffix(tt.log, ext)
70 // stripped = strings.TrimSuffix(stripped, "-input")
71 // os.WriteFile(stripped+"-want"+ext, stdout.Bytes(), 0644)
72 // if tt.wantStderrFile != "" {
73 // os.WriteFile(stripped+"-want-stderr"+ext, stderr.Bytes(), 0644)

Callers

nothing calls this directly

Calls 8

TestFunction · 0.92
NewLogRendererFunction · 0.85
JoinMethod · 0.80
EqualMethod · 0.80
EmptyMethod · 0.80
RunMethod · 0.65
FollowMethod · 0.65
StringMethod · 0.65

Tested by

no test coverage detected