Skip to content

Commit dd1b097

Browse files
committed
test(cli): improve TestServer/SpammyLogs line count
1 parent 0913594 commit dd1b097

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

cli/server_test.go

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,8 @@ func TestServer(t *testing.T) {
253253
"--access-url", "http://localhost:3000/",
254254
"--cache-dir", t.TempDir(),
255255
)
256-
stdoutRW := syncReaderWriter{}
257-
stderrRW := syncReaderWriter{}
258-
inv.Stdout = io.MultiWriter(os.Stdout, &stdoutRW)
259-
inv.Stderr = io.MultiWriter(os.Stderr, &stderrRW)
256+
pty := ptytest.New(t).Attach(inv)
257+
require.NoError(t, pty.Resize(80, 80))
260258
clitest.Start(t, inv)
261259

262260
// Wait for startup
@@ -270,8 +268,9 @@ func TestServer(t *testing.T) {
270268
// normally shown to the user, so we'll ignore them.
271269
ignoreLines := []string{
272270
"isn't externally reachable",
273-
"install.sh will be unavailable",
271+
"open install.sh: file does not exist",
274272
"telemetry disabled, unable to notify of security issues",
273+
"installed terraform version newer than expected",
275274
}
276275

277276
countLines := func(fullOutput string) int {
@@ -282,9 +281,11 @@ func TestServer(t *testing.T) {
282281
for _, line := range linesByNewline {
283282
for _, ignoreLine := range ignoreLines {
284283
if strings.Contains(line, ignoreLine) {
284+
t.Logf("Ignoring: %q", line)
285285
continue lineLoop
286286
}
287287
}
288+
t.Logf("Counting: %q", line)
288289
if line == "" {
289290
// Empty lines take up one line.
290291
countByWidth++
@@ -295,17 +296,10 @@ func TestServer(t *testing.T) {
295296
return countByWidth
296297
}
297298

298-
stdout, err := io.ReadAll(&stdoutRW)
299-
if err != nil {
300-
t.Fatalf("failed to read stdout: %v", err)
301-
}
302-
stderr, err := io.ReadAll(&stderrRW)
303-
if err != nil {
304-
t.Fatalf("failed to read stderr: %v", err)
305-
}
306-
307-
numLines := countLines(string(stdout)) + countLines(string(stderr))
308-
require.Less(t, numLines, 20)
299+
out := pty.ReadAll()
300+
numLines := countLines(string(out))
301+
t.Logf("numLines: %d", numLines)
302+
require.Less(t, numLines, 12, "expected less than 12 lines of output (terminal width 80), got %d", numLines)
309303
})
310304

311305
t.Run("OAuth2GitHubDefaultProvider", func(t *testing.T) {

pty/ptytest/ptytest.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,11 @@ func (e *outExpecter) ReadLine(ctx context.Context) string {
319319
return buffer.String()
320320
}
321321

322+
func (e *outExpecter) ReadAll() []byte {
323+
e.t.Helper()
324+
return e.out.ReadAll()
325+
}
326+
322327
func (e *outExpecter) doMatchWithDeadline(ctx context.Context, name string, fn func(*bufio.Reader) error) error {
323328
e.t.Helper()
324329

@@ -460,6 +465,18 @@ func newStdbuf() *stdbuf {
460465
return &stdbuf{more: make(chan struct{}, 1)}
461466
}
462467

468+
func (b *stdbuf) ReadAll() []byte {
469+
b.mu.Lock()
470+
defer b.mu.Unlock()
471+
472+
if b.err != nil {
473+
return nil
474+
}
475+
p := append([]byte(nil), b.b...)
476+
b.b = b.b[len(b.b):]
477+
return p
478+
}
479+
463480
func (b *stdbuf) Read(p []byte) (int, error) {
464481
if b.r == nil {
465482
return b.readOrWaitForMore(p)

0 commit comments

Comments
 (0)