Mock Maven in NPath unit tests#1230
Conversation
📝 WalkthroughWalkthroughThe NPath metric tests now stub Maven/PMD execution, generate temporary ChangesNPath metric test isolation
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
test/metrics/npath/test_all_types.py (1)
17-36: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueStub correctly mirrors the real
value()contract.gettempdir→str(tmp_path)anduuid4().hex→'fake-run'reproduceos.path.join(tempfile.gettempdir(), uuid.uuid4().hex), andfake_runwrites to{cwd}/target/pmd.xml, matching thesubprocess.run(..., cwd=root)call.One small robustness note:
fake_runreadskwargs['cwd'], which only works because the production call passescwdas a keyword. If that call ever switches to positional, this stub willKeyErrorinstead of failing clearly.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@test/metrics/npath/test_all_types.py` around lines 17 - 36, The `fake_run` stub in `test_all_types.py` assumes `subprocess.run` always receives `cwd` as a keyword argument, so it can raise an unhelpful `KeyError` if the call shape changes. Update `fake_run` to handle the current `npath_main.subprocess.run(..., cwd=root)` usage via the `cwd` keyword while remaining defensive by deriving the target directory from the passed arguments in a safe way, keeping the stub aligned with `_patch_maven_run` and `npath_main`.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@test/metrics/npath/test_all_types.py`:
- Around line 17-36: The `fake_run` stub in `test_all_types.py` assumes
`subprocess.run` always receives `cwd` as a keyword argument, so it can raise an
unhelpful `KeyError` if the call shape changes. Update `fake_run` to handle the
current `npath_main.subprocess.run(..., cwd=root)` usage via the `cwd` keyword
while remaining defensive by deriving the target directory from the passed
arguments in a safe way, keeping the stub aligned with `_patch_maven_run` and
`npath_main`.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: ec71d674-63ec-432a-8921-b9a7166b2395
📒 Files selected for processing (1)
test/metrics/npath/test_all_types.py
|
@yegor256 please review this PR. |
Summary
test/metrics/npath/test_all_types.pywith a stub that writes PMD XML fixturesmvnProblem
The top-level NPath tests exercise
NPathMetric.value()by actually invokingmvn pmd:pmd, which makes them integration tests rather than unit tests and requires Maven to be installed locally.Fix
Patch the NPath metric module’s temporary directory, UUID, and
subprocess.run()in the affected tests. The stubbed subprocess writes a synthetictarget/pmd.xmlreport, so the tests still execute the real parsing path without spawning Maven.Verification
TMPDIR=/Users/iliaprokofev/.tmp-codex-aibolit python3 -B -m pytest test/metrics/npath/test_all_types.py -qpython3 -B -m flake8 --max-line-length=120 test/metrics/npath/test_all_types.pyTMPDIR=/Users/iliaprokofev/.tmp-codex-aibolit python3 -B -m pylint test/metrics/npath/test_all_types.pyCloses #702
Summary by CodeRabbit