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

Commitcc946f1

Browse files
authored
test(cli): improve TestServer/SpammyLogs line count (#16814)
1 parent77479cd commitcc946f1

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+
"openinstall.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(fullOutputstring)int {
@@ -282,9 +280,11 @@ func TestServer(t *testing.T) {
282280
for_,line:=rangelinesByNewline {
283281
for_,ignoreLine:=rangeignoreLines {
284282
ifstrings.Contains(line,ignoreLine) {
283+
t.Logf("Ignoring: %q",line)
285284
continue lineLoop
286285
}
287286
}
287+
t.Logf("Counting: %q",line)
288288
ifline=="" {
289289
// Empty lines take up one line.
290290
countByWidth++
@@ -295,17 +295,10 @@ func TestServer(t *testing.T) {
295295
returncountByWidth
296296
}
297297

298-
stdout,err:=io.ReadAll(&stdoutRW)
299-
iferr!=nil {
300-
t.Fatalf("failed to read stdout: %v",err)
301-
}
302-
stderr,err:=io.ReadAll(&stderrRW)
303-
iferr!=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
returnserverURL,deployment,snapshot
23572350
}
2358-
2359-
// syncWriter provides a thread-safe io.ReadWriter implementation
2360-
typesyncReaderWriterstruct {
2361-
buf bytes.Buffer
2362-
mu sync.Mutex
2363-
}
2364-
2365-
func (w*syncReaderWriter)Write(p []byte) (nint,errerror) {
2366-
w.mu.Lock()
2367-
deferw.mu.Unlock()
2368-
returnw.buf.Write(p)
2369-
}
2370-
2371-
func (w*syncReaderWriter)Read(p []byte) (nint,errerror) {
2372-
w.mu.Lock()
2373-
deferw.mu.Unlock()
2374-
2375-
returnw.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
returnbuffer.String()
320320
}
321321

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

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

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp