Skip to content

Fix run_before_script() output capturing #959

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 2, 2025
Merged

Conversation

tony
Copy link
Member

@tony tony commented Feb 2, 2025

tmuxp 1.52.0's before scripts print out a lot of newlines.

Summary by Sourcery

Bug Fixes:

  • Fix issue where before scripts output was not being captured correctly.

Copy link

sourcery-ai bot commented Feb 2, 2025

Reviewer's Guide by Sourcery

This pull request modifies the run_before_script function to address excessive newlines in the output. It now captures stdout and stderr, and only prints to the terminal if it's a TTY. Additionally, it raises an exception if the script returns a non-zero exit code.

Sequence diagram for improved run_before_script execution flow

sequenceDiagram
    participant C as Caller
    participant R as run_before_script
    participant S as Shell Script
    participant T as Terminal

    C->>R: Execute script
    R->>S: Start process (Popen)
    activate S
    loop While script running
        S-->>R: Read stdout line
        alt is TTY
            R->>T: Write to stdout
        end
        S-->>R: Read stderr line
        alt is TTY
            R->>T: Write to stderr
        end
    end
    S-->>R: Return exit code
    deactivate S
    alt exit code != 0
        R->>R: Raise BeforeLoadScriptError
    end
    R-->>C: Return exit code
Loading

Flow diagram for run_before_script error handling

flowchart TD
    A[Start] --> B{Script exists?}
    B -->|No| C[Raise BeforeLoadScriptNotExists]
    B -->|Yes| D[Start process]
    D --> E{Read output}
    E --> F{Is TTY?}
    F -->|Yes| G[Write to terminal]
    F -->|No| H[Buffer output]
    G --> I{Process finished?}
    H --> I
    I -->|No| E
    I -->|Yes| J{Exit code = 0?}
    J -->|Yes| K[Return exit code]
    J -->|No| L[Raise BeforeLoadScriptError]
Loading

File-Level Changes

Change Details Files
Refactor the run_before_script function to capture and handle stdout and stderr streams.
  • The function now uses subprocess.Popen to execute the script.
  • It captures stdout and stderr using pipes.
  • It reads stdout and stderr line by line.
  • It writes to the terminal only if it's a TTY.
  • It stores the output in buffers.
  • It raises an exception if the script returns a non-zero exit code, including the captured stderr.
  • It handles FileNotFoundError exceptions.
src/tmuxp/util.py
Add a test case to verify that stdout is returned when the script succeeds.
  • The test case simulates sys.stdout.isatty() and sys.stderr.isatty() to ensure the output is written to the terminal.
tests/test_util.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

codecov bot commented Feb 2, 2025

Codecov Report

Attention: Patch coverage is 89.28571% with 3 lines in your changes missing coverage. Please review.

Project coverage is 73.11%. Comparing base (425bf3c) to head (03e6e68).
Report is 5 commits behind head on master.

Files with missing lines Patch % Lines
src/tmuxp/util.py 89.28% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #959      +/-   ##
==========================================
+ Coverage   72.92%   73.11%   +0.18%     
==========================================
  Files          26       26              
  Lines        1843     1856      +13     
  Branches      350      352       +2     
==========================================
+ Hits         1344     1357      +13     
- Misses        394      395       +1     
+ Partials      105      104       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @tony - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟡 Testing: 1 issue found
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@tony tony force-pushed the improve-run-before-script branch from 10987af to 03e6e68 Compare February 2, 2025 13:34
@tony tony merged commit 7bc8cc0 into master Feb 2, 2025
19 checks passed
@tony tony deleted the improve-run-before-script branch February 2, 2025 13:39
@tony tony changed the title Improve run before script Fix run_before_script() output capturing Feb 2, 2025
tony added a commit that referenced this pull request Feb 2, 2025
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.

1 participant