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