Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
test(cli): improve TestServer/SpammyLogs line count
  • Loading branch information
mafredri committed Mar 5, 2025
commit dd1b09772b2730d7c4f75ff8ab85818326769d4d
26 changes: 10 additions & 16 deletions cli/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,8 @@
"--access-url", "http://localhost:3000/",
"--cache-dir", t.TempDir(),
)
stdoutRW := syncReaderWriter{}
stderrRW := syncReaderWriter{}
inv.Stdout = io.MultiWriter(os.Stdout, &stdoutRW)
inv.Stderr = io.MultiWriter(os.Stderr, &stderrRW)
pty := ptytest.New(t).Attach(inv)
require.NoError(t, pty.Resize(80, 80))
clitest.Start(t, inv)

// Wait for startup
Expand All @@ -270,8 +268,9 @@
// normally shown to the user, so we'll ignore them.
ignoreLines := []string{
"isn't externally reachable",
"install.sh will be unavailable",
"open install.sh: file does not exist",
"telemetry disabled, unable to notify of security issues",
"installed terraform version newer than expected",
}

countLines := func(fullOutput string) int {
Expand All @@ -282,9 +281,11 @@
for _, line := range linesByNewline {
for _, ignoreLine := range ignoreLines {
if strings.Contains(line, ignoreLine) {
t.Logf("Ignoring: %q", line)
continue lineLoop
}
}
t.Logf("Counting: %q", line)
if line == "" {
// Empty lines take up one line.
countByWidth++
Expand All @@ -295,17 +296,10 @@
return countByWidth
}

stdout, err := io.ReadAll(&stdoutRW)
if err != nil {
t.Fatalf("failed to read stdout: %v", err)
}
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)
out := pty.ReadAll()
numLines := countLines(string(out))
t.Logf("numLines: %d", numLines)
require.Less(t, numLines, 12, "expected less than 12 lines of output (terminal width 80), got %d", numLines)
})

t.Run("OAuth2GitHubDefaultProvider", func(t *testing.T) {
Expand Down Expand Up @@ -2357,18 +2351,18 @@
}

// syncWriter provides a thread-safe io.ReadWriter implementation
type syncReaderWriter struct {

Check failure on line 2354 in cli/server_test.go

View workflow job for this annotation

GitHub Actions / lint

type `syncReaderWriter` is unused (unused)
buf bytes.Buffer
mu sync.Mutex
}

func (w *syncReaderWriter) Write(p []byte) (n int, err error) {

Check failure on line 2359 in cli/server_test.go

View workflow job for this annotation

GitHub Actions / lint

func `(*syncReaderWriter).Write` is unused (unused)
w.mu.Lock()
defer w.mu.Unlock()
return w.buf.Write(p)
}

func (w *syncReaderWriter) Read(p []byte) (n int, err error) {

Check failure on line 2365 in cli/server_test.go

View workflow job for this annotation

GitHub Actions / lint

func `(*syncReaderWriter).Read` is unused (unused)
w.mu.Lock()
defer w.mu.Unlock()

Expand Down
17 changes: 17 additions & 0 deletions pty/ptytest/ptytest.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,11 @@ func (e *outExpecter) ReadLine(ctx context.Context) 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 {
e.t.Helper()

Expand Down Expand Up @@ -460,6 +465,18 @@ func newStdbuf() *stdbuf {
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) {
if b.r == nil {
return b.readOrWaitForMore(p)
Expand Down
Loading