Skip to content

test: fix os.chdir leak and ConfigManager disk writes in install tests#395

Merged
bigcat88 merged 1 commit intomainfrom
fix/test-hygiene-install-execute
Mar 28, 2026
Merged

test: fix os.chdir leak and ConfigManager disk writes in install tests#395
bigcat88 merged 1 commit intomainfrom
fix/test-hygiene-install-execute

Conversation

@bigcat88
Copy link
Copy Markdown
Contributor

Summary

Follow-up to #394. Fixes two test hygiene issues found during review in tests that call install.execute():

  • CWD leak: execute() and pip_install_comfyui_dependencies() call os.chdir(repo_dir) as a side effect, leaking the changed working directory into subsequent tests. Added an autouse _preserve_cwd fixture in tests/comfy_cli/conftest.py that saves and restores os.getcwd() around every test.

  • ConfigManager disk writes: Tests calling execute(skip_manager=True) hit the real ConfigManager().set() which writes to ~/.config/comfy-cli/config.ini. Added patch("comfy_cli.config_manager.ConfigManager") to the 4 affected callsites in test_install_python_resolution.py and test_global_python_install.py.

@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Mar 28, 2026
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 28, 2026

📝 Walkthrough

Walkthrough

Adds a pytest autouse fixture to restore the process CWD after each test and expands mocking to include comfy_cli.config_manager.ConfigManager in install-related tests to intercept ConfigManager usage during install.execute() runs — tidy tests, no more lost directories (pun intended).

Changes

Cohort / File(s) Summary
Test Infrastructure
tests/comfy_cli/conftest.py
Added autouse fixture _preserve_cwd that records os.getcwd() before each test and restores it with os.chdir(original) after the test.
Install Test Mocking
tests/comfy_cli/test_global_python_install.py, tests/comfy_cli/test_install_python_resolution.py
Added patches for comfy_cli.config_manager.ConfigManager in install-related test helpers/scenarios so install.execute() runs with ConfigManager mocked; existing mocks and assertions unchanged.
Removed Global CWD Fixture
tests/comfy_cli/test_file_utils.py
Removed previous autouse fixture that reset CWD after each test and deleted related imports; tests rely on per-test monkeypatch.chdir(...) instead.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/test-hygiene-install-execute
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch fix/test-hygiene-install-execute

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 28, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

@@           Coverage Diff           @@
##             main     #395   +/-   ##
=======================================
  Coverage   74.71%   74.71%           
=======================================
  Files          33       33           
  Lines        3924     3924           
=======================================
  Hits         2932     2932           
  Misses        992      992           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@tests/comfy_cli/conftest.py`:
- Line 1: Add a module-level docstring at the top of the
tests/comfy_cli/conftest.py module to satisfy the C0114 lint rule; open the
conftest.py file (where the current top symbol is the module import line "import
os") and insert a concise triple-quoted string describing the purpose of this
test configuration module (e.g., "Test fixtures and configuration for comfy_cli
tests") as the first statement in the file.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 3813ec41-4fab-4805-9fae-fe5a5530e79d

📥 Commits

Reviewing files that changed from the base of the PR and between 4f16c4f and 20f7423.

📒 Files selected for processing (3)
  • tests/comfy_cli/conftest.py
  • tests/comfy_cli/test_global_python_install.py
  • tests/comfy_cli/test_install_python_resolution.py

Comment thread tests/comfy_cli/conftest.py
@bigcat88 bigcat88 force-pushed the fix/test-hygiene-install-execute branch from 20f7423 to 44fae09 Compare March 28, 2026 12:55
@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:S This PR changes 10-29 lines, ignoring generated files. labels Mar 28, 2026
Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
tests/comfy_cli/test_install_python_resolution.py (1)

48-133: 🧹 Nitpick | 🔵 Trivial

Consider extracting common patch setup to reduce repetition.

All three test methods in TestExecute share nearly identical context manager blocks with 6-7 patches. A helper method or fixture could reduce duplication:

♻️ Optional refactor using a helper
def _execute_with_mocks(self, tmp_path, **execute_kwargs):
    """Run install.execute with standard mocks, return key mock objects."""
    repo_dir = str(tmp_path)
    
    with (
        patch("comfy_cli.command.install.ensure_workspace_python", return_value="/resolved/python") as mock_ensure,
        patch("comfy_cli.command.install.clone_comfyui"),
        patch("comfy_cli.command.install.check_comfy_repo", return_value=(True, None)),
        patch("comfy_cli.command.install.pip_install_comfyui_dependencies") as mock_pip_deps,
        patch("comfy_cli.command.install.DependencyCompiler") as MockCompiler,
        patch("comfy_cli.command.install.WorkspaceManager"),
        patch("comfy_cli.config_manager.ConfigManager"),
        patch.object(install.workspace_manager, "skip_prompting", True),
        patch.object(install.workspace_manager, "setup_workspace_manager"),
    ):
        MockCompiler.Install_Build_Deps = MagicMock()
        MockCompiler.return_value = MagicMock()
        
        install.execute(comfy_path=repo_dir, **execute_kwargs)
        
        return mock_ensure, mock_pip_deps, MockCompiler
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/comfy_cli/test_install_python_resolution.py` around lines 48 - 133,
Extract the repeated patch setup in TestExecute into a small helper (e.g.,
_execute_with_mocks) that centralizes the common patches for
ensure_workspace_python, clone_comfyui, check_comfy_repo,
pip_install_comfyui_dependencies (or DependencyCompiler depending on test),
WorkspaceManager, and ConfigManager and the two patch.object calls on
install.workspace_manager; have the helper accept tmp_path and kwargs for
install.execute and return the key mock objects (mock_ensure, mock_pip_deps,
MockCompiler, etc.) so each test (test_calls_ensure_and_passes_resolved_python,
test_fast_deps_passes_python_to_dependency_compiler,
test_fast_deps_forwards_skip_torch) calls the helper to run install.execute and
then asserts on the returned mocks instead of repeating the with(...) block.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@tests/comfy_cli/test_install_python_resolution.py`:
- Around line 48-133: Extract the repeated patch setup in TestExecute into a
small helper (e.g., _execute_with_mocks) that centralizes the common patches for
ensure_workspace_python, clone_comfyui, check_comfy_repo,
pip_install_comfyui_dependencies (or DependencyCompiler depending on test),
WorkspaceManager, and ConfigManager and the two patch.object calls on
install.workspace_manager; have the helper accept tmp_path and kwargs for
install.execute and return the key mock objects (mock_ensure, mock_pip_deps,
MockCompiler, etc.) so each test (test_calls_ensure_and_passes_resolved_python,
test_fast_deps_passes_python_to_dependency_compiler,
test_fast_deps_forwards_skip_torch) calls the helper to run install.execute and
then asserts on the returned mocks instead of repeating the with(...) block.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 1d04af2d-a117-4df4-9042-43bcc17cbada

📥 Commits

Reviewing files that changed from the base of the PR and between 20f7423 and 44fae09.

📒 Files selected for processing (4)
  • tests/comfy_cli/conftest.py
  • tests/comfy_cli/test_file_utils.py
  • tests/comfy_cli/test_global_python_install.py
  • tests/comfy_cli/test_install_python_resolution.py
💤 Files with no reviewable changes (1)
  • tests/comfy_cli/test_file_utils.py

@bigcat88 bigcat88 merged commit 9c0636e into main Mar 28, 2026
9 checks passed
@bigcat88 bigcat88 deleted the fix/test-hygiene-install-execute branch March 28, 2026 13:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant