1
0
mirror of https://github.com/go-task/task.git synced 2025-11-23 22:24:45 +02:00

fix: address concurrent group output causing flaky tests (#2350)

This commit is contained in:
Timothy Rule
2025-11-11 18:36:32 +01:00
committed by GitHub
parent c71241bcbd
commit 39c86992bd
2 changed files with 13 additions and 8 deletions

View File

@@ -143,12 +143,12 @@ func (tt *ExecutorTest) run(t *testing.T) {
t.Helper() t.Helper()
f := func(t *testing.T) { f := func(t *testing.T) {
t.Helper() t.Helper()
var buf bytes.Buffer var buffer SyncBuffer
opts := append( opts := append(
tt.executorOpts, tt.executorOpts,
task.WithStdout(&buf), task.WithStdout(&buffer),
task.WithStderr(&buf), task.WithStderr(&buffer),
) )
// If the test has input, create a reader for it and add it to the // If the test has input, create a reader for it and add it to the
@@ -171,7 +171,7 @@ func (tt *ExecutorTest) run(t *testing.T) {
if err := e.Setup(); tt.wantSetupError { if err := e.Setup(); tt.wantSetupError {
require.Error(t, err) require.Error(t, err)
tt.writeFixtureErrSetup(t, g, err) tt.writeFixtureErrSetup(t, g, err)
tt.writeFixtureBuffer(t, g, buf) tt.writeFixtureBuffer(t, g, buffer.buf)
return return
} else { } else {
require.NoError(t, err) require.NoError(t, err)
@@ -192,7 +192,7 @@ func (tt *ExecutorTest) run(t *testing.T) {
if err := e.Run(ctx, call); tt.wantRunError { if err := e.Run(ctx, call); tt.wantRunError {
require.Error(t, err) require.Error(t, err)
tt.writeFixtureErrRun(t, g, err) tt.writeFixtureErrRun(t, g, err)
tt.writeFixtureBuffer(t, g, buf) tt.writeFixtureBuffer(t, g, buffer.buf)
return return
} else { } else {
require.NoError(t, err) require.NoError(t, err)
@@ -205,7 +205,7 @@ func (tt *ExecutorTest) run(t *testing.T) {
} }
} }
tt.writeFixtureBuffer(t, g, buf) tt.writeFixtureBuffer(t, g, buffer.buf)
} }
// Run the test (with a name if it has one) // Run the test (with a name if it has one)

View File

@@ -44,10 +44,15 @@ func (gw *groupWriter) close() error {
// don't print begin/end messages if there's no buffered entries // don't print begin/end messages if there's no buffered entries
return nil return nil
} }
if _, err := io.WriteString(gw.writer, gw.begin); err != nil { if len(gw.begin) > 0 {
return err // Rewrite the gw.buff with the beginning text.
s := gw.buff.String()
gw.buff.Reset()
gw.buff.WriteString(gw.begin)
gw.buff.WriteString(s)
} }
gw.buff.WriteString(gw.end) gw.buff.WriteString(gw.end)
// Return the entire gw.buff to ensure the group is written atomically to stdout.
_, err := io.Copy(gw.writer, &gw.buff) _, err := io.Copy(gw.writer, &gw.buff)
return err return err
} }