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()
f := func(t *testing.T) {
t.Helper()
var buf bytes.Buffer
var buffer SyncBuffer
opts := append(
tt.executorOpts,
task.WithStdout(&buf),
task.WithStderr(&buf),
task.WithStdout(&buffer),
task.WithStderr(&buffer),
)
// 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 {
require.Error(t, err)
tt.writeFixtureErrSetup(t, g, err)
tt.writeFixtureBuffer(t, g, buf)
tt.writeFixtureBuffer(t, g, buffer.buf)
return
} else {
require.NoError(t, err)
@@ -192,7 +192,7 @@ func (tt *ExecutorTest) run(t *testing.T) {
if err := e.Run(ctx, call); tt.wantRunError {
require.Error(t, err)
tt.writeFixtureErrRun(t, g, err)
tt.writeFixtureBuffer(t, g, buf)
tt.writeFixtureBuffer(t, g, buffer.buf)
return
} else {
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)

View File

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