test(cli): improve TestServer/SpammyLogs line count (#16814)

This commit is contained in:
Mathias Fredriksson
2025-03-05 17:04:35 +02:00
committed by GitHub
parent 77479cdd51
commit cc946f199d
2 changed files with 27 additions and 36 deletions

View File

@ -25,7 +25,6 @@ import (
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
"sync"
"sync/atomic" "sync/atomic"
"testing" "testing"
"time" "time"
@ -253,10 +252,8 @@ func TestServer(t *testing.T) {
"--access-url", "http://localhost:3000/", "--access-url", "http://localhost:3000/",
"--cache-dir", t.TempDir(), "--cache-dir", t.TempDir(),
) )
stdoutRW := syncReaderWriter{} pty := ptytest.New(t).Attach(inv)
stderrRW := syncReaderWriter{} require.NoError(t, pty.Resize(20, 80))
inv.Stdout = io.MultiWriter(os.Stdout, &stdoutRW)
inv.Stderr = io.MultiWriter(os.Stderr, &stderrRW)
clitest.Start(t, inv) clitest.Start(t, inv)
// Wait for startup // Wait for startup
@ -270,8 +267,9 @@ func TestServer(t *testing.T) {
// normally shown to the user, so we'll ignore them. // normally shown to the user, so we'll ignore them.
ignoreLines := []string{ ignoreLines := []string{
"isn't externally reachable", "isn't externally reachable",
"install.sh will be unavailable", "open install.sh: file does not exist",
"telemetry disabled, unable to notify of security issues", "telemetry disabled, unable to notify of security issues",
"installed terraform version newer than expected",
} }
countLines := func(fullOutput string) int { countLines := func(fullOutput string) int {
@ -282,9 +280,11 @@ func TestServer(t *testing.T) {
for _, line := range linesByNewline { for _, line := range linesByNewline {
for _, ignoreLine := range ignoreLines { for _, ignoreLine := range ignoreLines {
if strings.Contains(line, ignoreLine) { if strings.Contains(line, ignoreLine) {
t.Logf("Ignoring: %q", line)
continue lineLoop continue lineLoop
} }
} }
t.Logf("Counting: %q", line)
if line == "" { if line == "" {
// Empty lines take up one line. // Empty lines take up one line.
countByWidth++ countByWidth++
@ -295,17 +295,10 @@ func TestServer(t *testing.T) {
return countByWidth return countByWidth
} }
stdout, err := io.ReadAll(&stdoutRW) out := pty.ReadAll()
if err != nil { numLines := countLines(string(out))
t.Fatalf("failed to read stdout: %v", err) t.Logf("numLines: %d", numLines)
} require.Less(t, numLines, 12, "expected less than 12 lines of output (terminal width 80), got %d", numLines)
stderr, err := io.ReadAll(&stderrRW)
if err != nil {
t.Fatalf("failed to read stderr: %v", err)
}
numLines := countLines(string(stdout)) + countLines(string(stderr))
require.Less(t, numLines, 20)
}) })
t.Run("OAuth2GitHubDefaultProvider", func(t *testing.T) { t.Run("OAuth2GitHubDefaultProvider", func(t *testing.T) {
@ -2355,22 +2348,3 @@ func mockTelemetryServer(t *testing.T) (*url.URL, chan *telemetry.Deployment, ch
return serverURL, deployment, snapshot return serverURL, deployment, snapshot
} }
// syncWriter provides a thread-safe io.ReadWriter implementation
type syncReaderWriter struct {
buf bytes.Buffer
mu sync.Mutex
}
func (w *syncReaderWriter) Write(p []byte) (n int, err error) {
w.mu.Lock()
defer w.mu.Unlock()
return w.buf.Write(p)
}
func (w *syncReaderWriter) Read(p []byte) (n int, err error) {
w.mu.Lock()
defer w.mu.Unlock()
return w.buf.Read(p)
}

View File

@ -319,6 +319,11 @@ func (e *outExpecter) ReadLine(ctx context.Context) string {
return buffer.String() return buffer.String()
} }
func (e *outExpecter) ReadAll() []byte {
e.t.Helper()
return e.out.ReadAll()
}
func (e *outExpecter) doMatchWithDeadline(ctx context.Context, name string, fn func(*bufio.Reader) error) error { func (e *outExpecter) doMatchWithDeadline(ctx context.Context, name string, fn func(*bufio.Reader) error) error {
e.t.Helper() e.t.Helper()
@ -460,6 +465,18 @@ func newStdbuf() *stdbuf {
return &stdbuf{more: make(chan struct{}, 1)} return &stdbuf{more: make(chan struct{}, 1)}
} }
func (b *stdbuf) ReadAll() []byte {
b.mu.Lock()
defer b.mu.Unlock()
if b.err != nil {
return nil
}
p := append([]byte(nil), b.b...)
b.b = b.b[len(b.b):]
return p
}
func (b *stdbuf) Read(p []byte) (int, error) { func (b *stdbuf) Read(p []byte) (int, error) {
if b.r == nil { if b.r == nil {
return b.readOrWaitForMore(p) return b.readOrWaitForMore(p)