Skip to content

Commit cc946f1

Browse files
authored
test(cli): improve TestServer/SpammyLogs line count (#16814)
1 parent 77479cd commit cc946f1

File tree

2 files changed

+27
-36
lines changed

2 files changed

+27
-36
lines changed

cli/server_test.go

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"runtime"
2626
"strconv"
2727
"strings"
28-
"sync"
2928
"sync/atomic"
3029
"testing"
3130
"time"
@@ -253,10 +252,8 @@ func TestServer(t *testing.T) {
253252
"--access-url", "http://localhost:3000/",
254253
"--cache-dir", t.TempDir(),
255254
)
256-
stdoutRW := syncReaderWriter{}
257-
stderrRW := syncReaderWriter{}
258-
inv.Stdout = io.MultiWriter(os.Stdout, &stdoutRW)
259-
inv.Stderr = io.MultiWriter(os.Stderr, &stderrRW)
255+
pty := ptytest.New(t).Attach(inv)
256+
require.NoError(t, pty.Resize(20, 80))
260257
clitest.Start(t, inv)
261258

262259
// Wait for startup
@@ -270,8 +267,9 @@ func TestServer(t *testing.T) {
270267
// normally shown to the user, so we'll ignore them.
271268
ignoreLines := []string{
272269
"isn't externally reachable",
273-
"install.sh will be unavailable",
270+
"open install.sh: file does not exist",
274271
"telemetry disabled, unable to notify of security issues",
272+
"installed terraform version newer than expected",
275273
}
276274

277275
countLines := func(fullOutput string) int {
@@ -282,9 +280,11 @@ func TestServer(t *testing.T) {
282280
for _, line := range linesByNewline {
283281
for _, ignoreLine := range ignoreLines {
284282
if strings.Contains(line, ignoreLine) {
283+
t.Logf("Ignoring: %q", line)
285284
continue lineLoop
286285
}
287286
}
287+
t.Logf("Counting: %q", line)
288288
if line == "" {
289289
// Empty lines take up one line.
290290
countByWidth++
@@ -295,17 +295,10 @@ func TestServer(t *testing.T) {
295295
return countByWidth
296296
}
297297

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)
298+
out := pty.ReadAll()
299+
numLines := countLines(string(out))
300+
t.Logf("numLines: %d", numLines)
301+
require.Less(t, numLines, 12, "expected less than 12 lines of output (terminal width 80), got %d", numLines)
309302
})
310303

311304
t.Run("OAuth2GitHubDefaultProvider", func(t *testing.T) {
@@ -2355,22 +2348,3 @@ func mockTelemetryServer(t *testing.T) (*url.URL, chan *telemetry.Deployment, ch
23552348

23562349
return serverURL, deployment, snapshot
23572350
}
2358-
2359-
// syncWriter provides a thread-safe io.ReadWriter implementation
2360-
type syncReaderWriter struct {
2361-
buf bytes.Buffer
2362-
mu sync.Mutex
2363-
}
2364-
2365-
func (w *syncReaderWriter) Write(p []byte) (n int, err error) {
2366-
w.mu.Lock()
2367-
defer w.mu.Unlock()
2368-
return w.buf.Write(p)
2369-
}
2370-
2371-
func (w *syncReaderWriter) Read(p []byte) (n int, err error) {
2372-
w.mu.Lock()
2373-
defer w.mu.Unlock()
2374-
2375-
return w.buf.Read(p)
2376-
}

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy