Skip to content

Fix CaptureStderr restoration timing in testutil to resolve CI unit test failure#26687

Merged
pelikhan merged 4 commits intomainfrom
copilot/fix-github-actions-workflow-test-again
Apr 16, 2026
Merged

Fix CaptureStderr restoration timing in testutil to resolve CI unit test failure#26687
pelikhan merged 4 commits intomainfrom
copilot/fix-github-actions-workflow-test-again

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 16, 2026

The CI workflow failed in pkg/testutil because CaptureStderr did not restore os.Stderr before returning, violating the helper’s expected contract and failing TestSpec_PublicAPI_CaptureStderr_RestoresAfterCapture.

  • Root cause

    • CaptureStderr restored os.Stderr via t.Cleanup, which runs at test cleanup time rather than when CaptureStderr returns.
  • Behavioral fix in pkg/testutil/tempdir.go

    • Restore os.Stderr with a function-scoped defer, guaranteeing restoration at helper return.
    • Keep pipe lifecycle explicit:
      • fail fast if writer close fails
      • close reader after ReadFrom completes
  • Resulting contract

    • CaptureStderr now captures writes during fn and returns with os.Stderr already restored, matching the public API spec test.
origStderr := os.Stderr
os.Stderr = w
defer func() { os.Stderr = origStderr }()

fn()

if err = w.Close(); err != nil {
    t.Fatalf("CaptureStderr: failed to close writer pipe: %v", err)
}
if _, err = buf.ReadFrom(r); err != nil {
    t.Fatalf("CaptureStderr: failed to read pipe: %v", err)
}
if err = r.Close(); err != nil {
    t.Logf("Warning: failed to close stderr capture reader: %v", err)
}

Copilot AI and others added 3 commits April 16, 2026 16:52
Agent-Logs-Url: https://github.com/github/gh-aw/sessions/005cb2c2-4d54-4e3b-94df-a47692cb3734

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix failing GitHub Actions workflow test Fix CaptureStderr restoration timing in testutil to resolve CI unit test failure Apr 16, 2026
Copilot AI requested a review from pelikhan April 16, 2026 17:00
@pelikhan pelikhan marked this pull request as ready for review April 16, 2026 18:19
Copilot AI review requested due to automatic review settings April 16, 2026 18:19
@pelikhan pelikhan merged commit 2ebf132 into main Apr 16, 2026
@pelikhan pelikhan deleted the copilot/fix-github-actions-workflow-test-again branch April 16, 2026 18:19
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes pkg/testutil.CaptureStderr so os.Stderr is restored before the helper returns (rather than at test cleanup time), addressing CI failures in the public API spec test.

Changes:

  • Replace t.Cleanup-based restoration with function-scoped restoration.
  • Add explicit error handling for closing the writer pipe.
  • Close the reader pipe after draining captured output.
Show a summary per file
File Description
pkg/testutil/tempdir.go Updates CaptureStderr to restore os.Stderr during the helper call and tightens pipe close/read handling.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 1/1 changed files
  • Comments generated: 3

Comment thread pkg/testutil/tempdir.go
Comment on lines +83 to +89
defer func() { os.Stderr = origStderr }()

fn()

w.Close()
if err = w.Close(); err != nil {
t.Fatalf("CaptureStderr: failed to close writer pipe: %v", err)
}
Comment thread pkg/testutil/tempdir.go
Comment on lines +87 to +96
if err = w.Close(); err != nil {
t.Fatalf("CaptureStderr: failed to close writer pipe: %v", err)
}
var buf bytes.Buffer
if _, err = buf.ReadFrom(r); err != nil {
t.Fatalf("CaptureStderr: failed to read pipe: %v", err)
}
if err = r.Close(); err != nil {
t.Logf("Warning: failed to close stderr capture reader: %v", err)
}
Comment thread pkg/testutil/tempdir.go
Comment on lines +83 to 84
defer func() { os.Stderr = origStderr }()

@github-actions github-actions bot mentioned this pull request Apr 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants