Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
mafredri merged 3 commits intomainfrommafredri/test-cli-improve-spammy-logs
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes fromall commits
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
46 changes: 10 additions & 36 deletionscli/server_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -25,7 +25,6 @@ import (
"runtime"
"strconv"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
Expand DownExpand Up@@ -253,10 +252,8 @@ func TestServer(t *testing.T) {
"--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(20, 80))
clitest.Start(t, inv)

// Wait for startup
Expand All@@ -270,8 +267,9 @@ func TestServer(t *testing.T) {
// normally shown to the user, so we'll ignore them.
ignoreLines := []string{
"isn't externally reachable",
"install.sh will be unavailable",
"openinstall.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 +280,11 @@ func TestServer(t *testing.T) {
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 +295,10 @@ func TestServer(t *testing.T) {
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 DownExpand Up@@ -2355,22 +2348,3 @@ func mockTelemetryServer(t *testing.T) (*url.URL, chan *telemetry.Deployment, ch

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)
}
17 changes: 17 additions & 0 deletionspty/ptytest/ptytest.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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

[8]ページ先頭

©2009-2025 Movatter.jp