Skip to content

fix(python): validate BEST_EFFORTS requires VIRTUAL_ENV - #570

Open
a-oren wants to merge 5 commits into
guacsec:mainfrom
a-oren:worktree-TC-5477
Open

fix(python): validate BEST_EFFORTS requires VIRTUAL_ENV#570
a-oren wants to merge 5 commits into
guacsec:mainfrom
a-oren:worktree-TC-5477

Conversation

@a-oren

@a-oren a-oren commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Add early validation in PythonControllerBase.getDependencies() that throws when TRUSTIFY_DA_PYTHON_INSTALL_BEST_EFFORTS=true is set without TRUSTIFY_DA_PYTHON_VIRTUAL_ENV=true
  • Without this check, the best-efforts install logic is silently skipped because it is gated behind automaticallyInstallPackageOnEnvironment(), which returns false for PythonControllerRealEnv
  • Add reproducer test in PythonControllerRealEnvTest verifying the error message names both environment variables
  • Change RuntimeException to IllegalStateException for both conflicting-settings validations in getDependencies(), aligning with the codebase convention (16 instances of IllegalStateException for configuration conflicts across providers)
  • Add explicit @SetSystemProperty(key = PROP_TRUSTIFY_DA_PYTHON_VIRTUAL_ENV, value = "false") to BEST_EFFORTS test for proper test isolation

Implements TC-5477
Implements TC-5493
Implements TC-5492

Test plan

  • New test best_Efforts_Without_Virtual_Env_Should_Throw_Runtime_Exception passes (now asserts IllegalStateException)
  • Existing PythonControllerRealEnvTest tests pass (no regression)
  • Existing PythonControllerVirtualEnvTest tests pass (BEST_EFFORTS+VIRTUAL_ENV flow unchanged)
  • Spotless formatting check passes

Add early validation in PythonControllerBase.getDependencies() that
throws a RuntimeException when BEST_EFFORTS=true is set without
VIRTUAL_ENV=true. Without this check, the best-efforts install logic
is silently skipped because it is gated behind
automaticallyInstallPackageOnEnvironment(), which returns false for
PythonControllerRealEnv.

Implements TC-5477

Assisted-by: Claude Code
@sourcery-ai

sourcery-ai Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Adds an early validation in PythonControllerBase.getDependencies to enforce that BEST_EFFORTS installs are only allowed when VIRTUAL_ENV is enabled, and introduces a unit test in PythonControllerRealEnvTest that reproduces and asserts the new behavior and error messaging.

Sequence diagram for BEST_EFFORTS and VIRTUAL_ENV validation in getDependencies

sequenceDiagram
    participant Caller
    participant PythonControllerBase
    participant Environment

    Caller->>PythonControllerBase: getDependencies(pathToPythonBin, requirementsPath)
    PythonControllerBase->>PythonControllerBase: isVirtualEnv()
    PythonControllerBase->>PythonControllerBase: isRealEnv()
    alt isVirtualEnv or isRealEnv
        PythonControllerBase->>PythonControllerBase: prepareEnvironment(pathToPythonBin)
    end

    PythonControllerBase->>Environment: getBoolean(PROP_TRUSTIFY_DA_PYTHON_INSTALL_BEST_EFFORTS, false)
    Environment-->>PythonControllerBase: bestEfforts

    alt bestEfforts and not automaticallyInstallPackageOnEnvironment()
        PythonControllerBase->>Caller: throw RuntimeException
    else
        PythonControllerBase->>PythonControllerBase: automaticallyInstallPackageOnEnvironment()
        alt automaticallyInstallPackageOnEnvironment
            PythonControllerBase->>Environment: getBoolean(PROP_TRUSTIFY_DA_PYTHON_INSTALL_BEST_EFFORTS, false)
            Environment-->>PythonControllerBase: installBestEfforts
            PythonControllerBase->>PythonControllerBase: installAndResolveDependencies(installBestEfforts)
        else
            PythonControllerBase->>PythonControllerBase: resolveDependenciesWithoutInstall()
        end
        PythonControllerBase-->>Caller: dependencies
    end
Loading

File-Level Changes

Change Details Files
Enforce BEST_EFFORTS requires VIRTUAL_ENV at the start of dependency resolution.
  • Insert a check in getDependencies that reads PROP_TRUSTIFY_DA_PYTHON_INSTALL_BEST_EFFORTS and automaticallyInstallPackageOnEnvironment, and throws a RuntimeException if BEST_EFFORTS is enabled while automatic installs are disabled (real env).
  • Reuse PROP_TRUSTIFY_DA_PYTHON_INSTALL_BEST_EFFORTS and PROP_TRUSTIFY_DA_PYTHON_VIRTUAL_ENV constants to build a descriptive error message naming both properties.
  • Ensure the validation runs after environment preparation but before any automatic installation logic is executed.
src/main/java/io/github/guacsec/trustifyda/utils/PythonControllerBase.java
Add a regression test ensuring BEST_EFFORTS without VIRTUAL_ENV fails with a descriptive error.
  • Import BEST_EFFORTS and VIRTUAL_ENV property constants into PythonControllerRealEnvTest for use in assertions.
  • Add best_Efforts_Without_Virtual_Env_Should_Throw_Runtime_Exception test configuring BEST_EFFORTS=true with no VIRTUAL_ENV and calling getDependencies on PythonControllerRealEnv.
  • Assert that a RuntimeException is thrown and that its message includes both BEST_EFFORTS and VIRTUAL_ENV property names, verifying the error is explicit and user-facing.
src/test/java/io/github/guacsec/trustifyda/utils/PythonControllerRealEnvTest.java

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

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey - I've found 1 issue, and left some high level feedback:

  • Consider throwing a more specific exception type (e.g., IllegalStateException) instead of a generic RuntimeException for the conflicting BEST_EFFORTS/VIRTUAL_ENV configuration to make failure modes clearer and easier to catch.
  • You call Environment.getBoolean(PROP_TRUSTIFY_DA_PYTHON_INSTALL_BEST_EFFORTS, false) twice in getDependencies; consider reading it once into a local variable to avoid duplication and ensure consistency.
  • The BEST_EFFORTS/VIRTUAL_ENV configuration check currently runs after prepareEnvironment; if prepareEnvironment can have side effects, it may be cleaner to validate configuration before preparing the environment so invalid setups fail as early as possible.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider throwing a more specific exception type (e.g., IllegalStateException) instead of a generic RuntimeException for the conflicting BEST_EFFORTS/VIRTUAL_ENV configuration to make failure modes clearer and easier to catch.
- You call Environment.getBoolean(PROP_TRUSTIFY_DA_PYTHON_INSTALL_BEST_EFFORTS, false) twice in getDependencies; consider reading it once into a local variable to avoid duplication and ensure consistency.
- The BEST_EFFORTS/VIRTUAL_ENV configuration check currently runs after prepareEnvironment; if prepareEnvironment can have side effects, it may be cleaner to validate configuration before preparing the environment so invalid setups fail as early as possible.

## Individual Comments

### Comment 1
<location path="src/test/java/io/github/guacsec/trustifyda/utils/PythonControllerRealEnvTest.java" line_range="339-343" />
<code_context>

+  /** Verifies that BEST_EFFORTS=true without VIRTUAL_ENV=true throws a descriptive error. */
+  @Test
+  @RestoreSystemProperties
+  @SetSystemProperty(key = PROP_TRUSTIFY_DA_PYTHON_INSTALL_BEST_EFFORTS, value = "true")
+  void best_Efforts_Without_Virtual_Env_Should_Throw_Runtime_Exception() {
+    String requirementsTxt = getFileFromString("requirements.txt", "flask==9.9.9\n");
+    RuntimeException runtimeException =
+        assertThrows(
+            RuntimeException.class,
+            () -> pythonControllerRealEnv.getDependencies(requirementsTxt, true));
+    assertTrue(
+        runtimeException.getMessage().contains(PROP_TRUSTIFY_DA_PYTHON_INSTALL_BEST_EFFORTS));
+    assertTrue(runtimeException.getMessage().contains(PROP_TRUSTIFY_DA_PYTHON_VIRTUAL_ENV));
+  }
+
</code_context>
<issue_to_address>
**suggestion (testing):** Make the VIRTUAL_ENV setting explicit in the test to avoid reliance on global state

This test should cover the case where BEST_EFFORTS=true and VIRTUAL_ENV is explicitly disabled/absent, but it currently only sets BEST_EFFORTS and depends on whatever VIRTUAL_ENV state happens to exist. Please explicitly set or clear `PROP_TRUSTIFY_DA_PYTHON_VIRTUAL_ENV` (e.g., with another `@SetSystemProperty` or a helper) so the scenario is isolated from other tests and not affected by global configuration leakage.

```suggestion
  /** Verifies that BEST_EFFORTS=true without VIRTUAL_ENV=true throws a descriptive error. */
  @Test
  @RestoreSystemProperties
  @SetSystemProperty(key = PROP_TRUSTIFY_DA_PYTHON_INSTALL_BEST_EFFORTS, value = "true")
  @SetSystemProperty(key = PROP_TRUSTIFY_DA_PYTHON_VIRTUAL_ENV, value = "false")
  void best_Efforts_Without_Virtual_Env_Should_Throw_Runtime_Exception() {
```
</issue_to_address>

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.

@codecov-commenter

codecov-commenter commented Aug 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (main@8c1e08d). Learn more about missing BASE report.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #570   +/-   ##
=======================================
  Coverage        ?   68.74%           
  Complexity      ?     1002           
=======================================
  Files           ?       65           
  Lines           ?     4255           
  Branches        ?      746           
=======================================
  Hits            ?     2925           
  Misses          ?      990           
  Partials        ?      340           
Flag Coverage Δ
integration-tests 68.74% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@a-oren

a-oren commented Aug 3, 2026

Copy link
Copy Markdown
Contributor Author

[sdlc-workflow/verify-pr] Re: @sourcery-ai[bot] review —

1. IllegalStateException instead of RuntimeException: Classified as code change request (upgraded from suggestion) — this matches project convention: 16 instances of IllegalStateException for invalid/conflicting configuration states across CargoProvider, JavaScriptProviderFactory, PythonPyprojectProvider, GoModulesProvider, JavaMavenProvider, ExhortApi, Ecosystem, PythonUvProvider; CONVENTIONS.md Error Handling section documents "Runtime exceptions preferred over checked" but the codebase differentiates IllegalStateException (configuration conflicts) from RuntimeException (operational failures). Sub-task TC-5493 created to address this feedback.

2. Read Environment.getBoolean once into local variable: Classified as suggestion — no documented convention or consistent codebase pattern requiring local variable extraction for repeated method calls. No sub-task created.

3. Validate configuration before prepareEnvironment: Classified as suggestion — CONVENTIONS.md mentions "Environment validation at initialization time" but no codebase pattern mandates validation-before-setup ordering within method bodies. No sub-task created.

@a-oren

a-oren commented Aug 3, 2026

Copy link
Copy Markdown
Contributor Author

Verification Report for TC-5477 (commit 21e42b8)

Check Result Details
Review Feedback WARN 2 code change requests → sub-tasks TC-5492, TC-5493 created
Root-Cause Investigation DONE TC-5494 (implement-task skill gap), TC-5495 (convention gap)
Scope Containment PASS PR files match task specification exactly (2/2 files)
Diff Size PASS 26 additions across 2 files, proportionate to validation + test task
Commit Traceability PASS Commit 21e42b8 references TC-5477 via Implements TC-5477
Sensitive Patterns PASS No secrets detected in 26 added lines
CI Status WARN Unit tests pass; 39 integration tests fail due to backend 503 read-only mode (unrelated infrastructure)
Acceptance Criteria PASS All 4 acceptance criteria satisfied
Test Quality PASS No repetitive tests, doc comments present, Eval Quality: N/A
Test Change Classification ADDITIVE 1 new test method added, 0 removed or weakened
Verification Commands N/A Not specified in task

Overall: WARN

Two WARN checks:

  1. Review Feedback: sourcery-ai[bot] review identified 1 code change request (add explicit @SetSystemProperty for VIRTUAL_ENV) and 1 suggestion was upgraded to code change request via convention check (use IllegalStateException instead of RuntimeException — 16 codebase instances support this pattern). Sub-tasks created for both.

  2. CI Status: All unit tests and build checks pass. 39 integration tests fail across all ecosystems because the backend at exhort.stage.devshift.net returns 503 "read-only mode" — this is an infrastructure issue unrelated to the PR changes.


This comment was AI-generated by sdlc-workflow/verify-pr v0.13.8.

a-oren added 2 commits August 3, 2026 19:09
Change RuntimeException to IllegalStateException for conflicting
configuration state validations in PythonControllerBase.getDependencies().
This aligns with the codebase convention where IllegalStateException is
used for invalid/conflicting configuration states (16 existing instances
across providers).

Both conflicting-settings throw statements are updated:
- BEST_EFFORTS=true without VIRTUAL_ENV=true (line 83)
- BEST_EFFORTS=true with MATCH_MANIFEST_VERSIONS=true (line 102)

The test assertion in PythonControllerRealEnvTest is updated to expect
IllegalStateException. The VirtualEnvTest assertion still passes since
IllegalStateException extends RuntimeException.

Implements TC-5493

Assisted-by: Claude Code
…epareEnvironment

Read Environment.getBoolean(PROP_TRUSTIFY_DA_PYTHON_INSTALL_BEST_EFFORTS)
once into a local variable to avoid duplication and ensure consistency.
Move the BEST_EFFORTS/VIRTUAL_ENV validation before prepareEnvironment()
so invalid configurations fail before any side effects occur.

Implements TC-5493

Assisted-by: Claude Code
@a-oren
a-oren requested review from Strum355 and ruromero August 3, 2026 16:20
a-oren added 2 commits August 4, 2026 10:37
…_EFFORTS test

Set PROP_TRUSTIFY_DA_PYTHON_VIRTUAL_ENV to "false" explicitly instead
of relying on the default state, improving test isolation.

Implements TC-5492

Assisted-by: Claude Code
Update README to clarify that BEST_EFFORTS=true requires both
VIRTUAL_ENV=true and MATCH_MANIFEST_VERSIONS=false.

Implements TC-5477

Assisted-by: Claude Code
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.

2 participants