Skip to content

test(context): add coverage for WorkContext#1076

Merged
mergify[bot] merged 1 commit intopython-wheel-build:mainfrom
mnadzam:test_context
May 5, 2026
Merged

test(context): add coverage for WorkContext#1076
mergify[bot] merged 1 commit intopython-wheel-build:mainfrom
mnadzam:test_context

Conversation

@mnadzam
Copy link
Copy Markdown
Contributor

@mnadzam mnadzam commented Apr 21, 2026

  • Add dedicated unit tests for setup(), package_build_info(),
    enable_parallel_builds(), wheels_build, pip_wheel_server_args,
    uv_clean_cache(), write_to_graph_to_file(), and clean_build_dirs() in context.py.
  • Decompose shared setup into _make_context() and _all_setup_dirs().
  • Increase unit test coverage of context.py from 67% to 94%.

Closes #1075

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 21, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: b2cdf4e8-d2bb-4b22-b5d5-f3421949219e

📥 Commits

Reviewing files that changed from the base of the PR and between f90a52c and aa0cfcb.

📒 Files selected for processing (1)
  • tests/test_context.py
✅ Files skipped from review due to trivial changes (1)
  • tests/test_context.py

📝 Walkthrough

Walkthrough

Adds comprehensive unit tests to tests/test_context.py for WorkContext. Introduces _make_context and _all_setup_dirs helpers. New tests cover: pip constraints handling; setup() directory creation and idempotency; package_build_info() behavior with Requirement vs string inputs; wheels_build path before and after enable_parallel_builds(); write_to_graph_to_file() invoking dependency_graph.serialize and creating the graph file; pip_wheel_server_args for HTTPS vs HTTP (trusted host); uv_clean_cache() validation and invocation of external run with UV_CACHE_DIR; and clean_build_dirs() safety checks and cleanup flag behavior.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: adding test coverage for the WorkContext class.
Description check ✅ Passed The description is directly related to the changeset, listing specific methods tested and quantifying the coverage improvement from 67% to 94%.
Linked Issues check ✅ Passed All acceptance criteria from issue #1075 are addressed: setup() idempotency, package_build_info() delegation, enable_parallel_builds() + wheels_build threading, write_to_graph_to_file() file creation, pip_wheel_server_args URL handling, uv_clean_cache() validation and command construction, and clean_build_dirs() safety and cleanup behavior.
Out of Scope Changes check ✅ Passed All changes are contained to tests/test_context.py and align with issue #1075 objectives of adding unit test coverage for WorkContext methods; no unrelated code modifications detected.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


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.

@mergify mergify Bot added the ci label Apr 21, 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.

Actionable comments posted: 2

🤖 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/test_context.py`:
- Around line 174-181: After asserting ctx.clean_build_dirs raises, add
assertions to verify the safety check did not remove directories: assert that
sdist_root.exists() and build_env.path.exists() (and optionally that their
contents remain intact) after the pytest.raises block. Locate the test using
sdist_root, build_env, and the call to ctx.clean_build_dirs and add these
post-raise existence checks to ensure no delete-then-raise regression can pass.
- Around line 108-114: The test currently only checks that
tmp_context.wheels_build is a child of tmp_context.wheels_build_base; instead,
exercise the thread-specific behavior by creating a second thread that calls
tmp_context.wheels_build and captures its value, then assert that the path
returned in the worker thread is different from the main thread's
tmp_context.wheels_build and that both still share the same
tmp_context.wheels_build_base; update test_wheels_build_parallel to spawn a
worker thread (or use threading.Thread), store the worker_path, join the thread,
and add an assertion comparing worker_path != main_thread_path while keeping the
existing parent/is_dir assertions on the appropriate values.
🪄 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: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: bb0752cf-2ded-4366-904f-420e1efc1eeb

📥 Commits

Reviewing files that changed from the base of the PR and between c833650 and 4086085.

📒 Files selected for processing (1)
  • tests/test_context.py

Comment thread tests/test_context.py
Comment thread tests/test_context.py
@mnadzam mnadzam marked this pull request as ready for review April 21, 2026 18:59
@mnadzam mnadzam requested a review from a team as a code owner April 21, 2026 18:59
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.

♻️ Duplicate comments (1)
tests/test_context.py (1)

108-114: ⚠️ Potential issue | 🟡 Minor

Exercise an actual worker thread here.

This still only proves wheels_build returns a child directory; it would pass if every thread got the same path. Capture wheels_build from a second thread and assert it differs from the main thread path.

Suggested test strengthening
+import threading
 from unittest.mock import Mock, patch
 def test_wheels_build_parallel(tmp_context: context.WorkContext) -> None:
     tmp_context.enable_parallel_builds()
 
-    result = tmp_context.wheels_build
+    main_thread_path = tmp_context.wheels_build
+    worker_paths: list[pathlib.Path] = []
+
+    def capture_worker_path() -> None:
+        worker_paths.append(tmp_context.wheels_build)
+
+    worker = threading.Thread(target=capture_worker_path)
+    worker.start()
+    worker.join()
 
-    assert result.parent == tmp_context.wheels_build_base
-    assert result.is_dir()
+    assert main_thread_path.parent == tmp_context.wheels_build_base
+    assert worker_paths[0].parent == tmp_context.wheels_build_base
+    assert worker_paths[0] != main_thread_path
+    assert main_thread_path.is_dir()
+    assert worker_paths[0].is_dir()

As per coding guidelines, tests/**: “Verify test actually tests the intended behavior. Check for missing edge cases.”

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/test_context.py` around lines 108 - 114, The test
test_wheels_build_parallel currently only checks that tmp_context.wheels_build
is a child directory; to exercise parallel behavior spawn a second worker thread
(e.g., using threading.Thread or concurrent.futures) after calling
tmp_context.enable_parallel_builds(), have that thread call
tmp_context.wheels_build and return or store its path, then assert the second
thread’s path is not equal to the main thread’s tmp_context.wheels_build and
that both exist; locate symbols test_wheels_build_parallel,
tmp_context.enable_parallel_builds(), and tmp_context.wheels_build to implement
this change.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Duplicate comments:
In `@tests/test_context.py`:
- Around line 108-114: The test test_wheels_build_parallel currently only checks
that tmp_context.wheels_build is a child directory; to exercise parallel
behavior spawn a second worker thread (e.g., using threading.Thread or
concurrent.futures) after calling tmp_context.enable_parallel_builds(), have
that thread call tmp_context.wheels_build and return or store its path, then
assert the second thread’s path is not equal to the main thread’s
tmp_context.wheels_build and that both exist; locate symbols
test_wheels_build_parallel, tmp_context.enable_parallel_builds(), and
tmp_context.wheels_build to implement this change.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: e26a76c7-cf0f-4924-919a-e3103cf7447f

📥 Commits

Reviewing files that changed from the base of the PR and between 4086085 and 041269f.

📒 Files selected for processing (1)
  • tests/test_context.py

Copy link
Copy Markdown
Contributor

@rd4398 rd4398 left a comment

Choose a reason for hiding this comment

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

Overall looks good, I have added a comment
Can you please add detailed commit message? Just "closes xyz" is not enough. Adding a detailed commit message helps when we look at git history.

Comment thread tests/test_context.py
@mnadzam mnadzam force-pushed the test_context branch 2 times, most recently from f90a52c to b12a5b8 Compare April 22, 2026 08:08
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.

♻️ Duplicate comments (1)
tests/test_context.py (1)

3-3: ⚠️ Potential issue | 🟡 Minor

Exercise wheels_build from a second thread.

This still only checks that the main-thread path is a child of wheels_build_base; it would not catch a regression where all threads share the same build directory.

Suggested test strengthening
 import os
 import pathlib
+import threading
 from unittest.mock import Mock, patch
 def test_wheels_build_parallel(tmp_context: context.WorkContext) -> None:
     tmp_context.enable_parallel_builds()
 
     result = tmp_context.wheels_build
+    worker_results: list[pathlib.Path] = []
+
+    def capture_worker_result() -> None:
+        worker_results.append(tmp_context.wheels_build)
+
+    worker = threading.Thread(target=capture_worker_result)
+    worker.start()
+    worker.join()
 
     assert result.parent == tmp_context.wheels_build_base
+    assert worker_results[0].parent == tmp_context.wheels_build_base
+    assert worker_results[0] != result
     assert result.is_dir()
+    assert worker_results[0].is_dir()

As per coding guidelines, tests/**: “Verify test actually tests the intended behavior. Check for missing edge cases.”

Also applies to: 108-114

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/test_context.py` at line 3, Add a second-thread execution of the
wheels_build path to ensure threads get distinct build dirs: in the test (e.g.,
test_wheels_build_thread_isolation) spawn a new Thread that calls wheels_build()
while the main thread also calls wheels_build(); then assert both returned build
directories are children of wheels_build_base and are not equal, referencing the
wheels_build function and wheels_build_base constant to locate the code to
update.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Duplicate comments:
In `@tests/test_context.py`:
- Line 3: Add a second-thread execution of the wheels_build path to ensure
threads get distinct build dirs: in the test (e.g.,
test_wheels_build_thread_isolation) spawn a new Thread that calls wheels_build()
while the main thread also calls wheels_build(); then assert both returned build
directories are children of wheels_build_base and are not equal, referencing the
wheels_build function and wheels_build_base constant to locate the code to
update.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 41c9c08b-de80-412a-a8bc-897563a5d418

📥 Commits

Reviewing files that changed from the base of the PR and between 041269f and f90a52c.

📒 Files selected for processing (1)
  • tests/test_context.py

@mnadzam
Copy link
Copy Markdown
Contributor Author

mnadzam commented Apr 22, 2026

I have added a more detailed commit message. Is it good like this? Or should it also describe each test?

@mnadzam mnadzam requested a review from rd4398 April 22, 2026 08:35
Copy link
Copy Markdown
Contributor

@rd4398 rd4398 left a comment

Choose a reason for hiding this comment

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

This looks good! Thanks

@rd4398
Copy link
Copy Markdown
Contributor

rd4398 commented Apr 29, 2026

@mergify rebase

@mergify
Copy link
Copy Markdown
Contributor

mergify Bot commented Apr 29, 2026

rebase

❌ Unable to rebase: Mergify can't impersonate rd4398

Details

User rd4398 used as bot_account is unknown. Please make sure {login} exists and has logged into the Mergify dashboard.

@LalatenduMohanty
Copy link
Copy Markdown
Member

@mergify rebase

- Add dedicated unit tests for setup(), package_build_info(),
enable_parallel_builds(), wheels_build, pip_wheel_server_args,
uv_clean_cache(), write_to_graph_to_file(), and clean_build_dirs() in context.py.
- Decompose shared setup into _make_context() and _all_setup_dirs().
- Increase unit test coverage of context.py from 67% to 94%.

Closes python-wheel-build#1075

Signed-off-by: Marcel Nadzam <mnadzam@redhat.com>

Co-authored-by: Cursor
@mergify
Copy link
Copy Markdown
Contributor

mergify Bot commented May 5, 2026

Deprecation notice: This pull request comes from a fork and was rebased using bot_account impersonation. This capability will be removed on July 1, 2026. After this date, the rebase action will no longer be able to rebase fork pull requests with this configuration. Please switch to the update action/command to ensure compatibility going forward.

@mergify
Copy link
Copy Markdown
Contributor

mergify Bot commented May 5, 2026

rebase

✅ Branch has been successfully rebased

@LalatenduMohanty
Copy link
Copy Markdown
Member

User rd4398 used as bot_account is unknown. Please make sure {login} exists and has logged into the Mergify dashboard.

@rd4398 you need to login to the mergify dashboard I think, so that it can use your account to rebase.

@mergify mergify Bot merged commit f82c1e6 into python-wheel-build:main May 5, 2026
39 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test: add coverage for WorkContext (context.py)

3 participants