Skip to content

Add tests for switch_working_directory helper#10766

Open
SergioChan wants to merge 3 commits intopython-poetry:mainfrom
SergioChan:fix/issue-9161
Open

Add tests for switch_working_directory helper#10766
SergioChan wants to merge 3 commits intopython-poetry:mainfrom
SergioChan:fix/issue-9161

Conversation

@SergioChan
Copy link

@SergioChan SergioChan commented Mar 10, 2026

Summary

  • add test that switch_working_directory() restores the original cwd when an exception is raised
  • add test that remove=True deletes the temporary working directory after exiting the context manager

Fixes #9161

Summary by Sourcery

Add coverage for the switch_working_directory helper behavior.

Tests:

  • Add a test ensuring switch_working_directory restores the original working directory when an exception is raised inside the context.
  • Add a test ensuring switch_working_directory removes the temporary working directory when used with remove=True.

@sourcery-ai
Copy link

sourcery-ai bot commented Mar 10, 2026

Reviewer's Guide

This PR adds two pytest-based unit tests for the switch_working_directory helper to verify it correctly restores the original working directory on exceptions and optionally deletes the temporary directory when requested.

File-Level Changes

Change Details Files
Add tests ensuring switch_working_directory correctly restores cwd on error and deletes temporary directory when remove=True.
  • Import pytest and switch_working_directory into the shared test_helpers module.
  • Add a test that raises a RuntimeError inside switch_working_directory and asserts the cwd is restored after the exception.
  • Add a test that uses switch_working_directory with remove=True and asserts the temporary directory exists inside the context and is removed afterwards.
tests/test_helpers.py

Assessment against linked issues

Issue Objective Addressed Explanation
#9161 Add test coverage for the switch_working_directory helper in tests/helpers.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!

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

@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 - I've reviewed your changes and they look great!


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.

@SergioChan
Copy link
Author

Follow-up pushed in a7f78f6 to address failing mypy checks.

Change

  • Added explicit type annotations for tmp_path fixture arguments in:
    • test_switch_working_directory_restores_original_cwd_on_error
    • test_switch_working_directory_remove_deletes_directory
  • Added from pathlib import Path import.

Targeted validation

  • python -m mypy --follow-imports=silent --ignore-missing-imports tests/test_helpers.py

@SergioChan
Copy link
Author

Pushed follow-up commit 0facb5c to fix the failing pre-commit.ci check.

What changed (auto-fixes from the repo hooks):

  • adjusted typing import style to satisfy Ruff (TYPE_CHECKING + deferred Path import)
  • applied Ruff formatting to the new tests
  • simplified the exception test context usage with combined context managers

Validation:

  • python -m pre_commit run --files tests/test_helpers.py

Copy link
Member

@radoering radoering left a comment

Choose a reason for hiding this comment

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

The two tests seem arbitrary. A complete test should test both aspects (cwd is changed (back) and cwd is only deleted if remove=True) - both under normal conditions and if there is an error. I think one parameterized test makes sense.

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.

Add test coverage for tests/helpers.py

2 participants