Skip to content

fix: inquire returning short env vars - #4074

Merged
germa89 merged 8 commits into
mainfrom
fix/inquire
Jul 4, 2025
Merged

fix: inquire returning short env vars#4074
germa89 merged 8 commits into
mainfrom
fix/inquire

Conversation

@germa89

@germa89 germa89 commented Jul 4, 2025

Copy link
Copy Markdown
Collaborator

Description

Due to MAPDL strings being limited to 248 chars, env vars which content is longer than that are limited to said length.
We cannot avoid it, but we can raise a warning and propose and alternative method.

Issue linked

Close #4072

Checklist

Summary by Sourcery

Warn on trimmed environment variable responses in inquire and provide a printenv alternative, while significantly expanding and restructuring inquire tests into discrete, focused cases.

Enhancements:

  • Warn users when inquire responses reach the 248-character MAPDL limit and suggest using mapdl.sys('printenv') as an alternative.
  • Rename and break up the monolithic inquire test into separate, focused tests for APDL info, environment variables (with parameterization and expected warnings for long values), titles, job names, file existence, and non-interactive mode.

Tests:

  • Add pytest parameterized test for environment variables with skipping logic and warning expectations for truncated values.
  • Add tests for inquire title retrieval, job name string validation, file existence boolean return, and non-interactive behavior.

Copilot AI review requested due to automatic review settings July 4, 2025 09:43
@germa89
germa89 requested a review from a team as a code owner July 4, 2025 09:43
@ansys-reviewer-bot

Copy link
Copy Markdown
Contributor

Thanks for opening a Pull Request. If you want to perform a review write a comment saying:

@ansys-reviewer-bot review

@sourcery-ai

sourcery-ai Bot commented Jul 4, 2025

Copy link
Copy Markdown
Contributor

Reviewer's Guide

This PR adds a warning in the inquire method when responses hit the 248-char MAPDL string limit and refactors the existing test_inquire into multiple focused pytest cases—including a parameterized test for environment variables with skip logic and dedicated tests for title, jobname, existence checks, and non-interactive behavior.

Sequence diagram for Mapdl.inquire with long environment variable response

sequenceDiagram
    participant User
    participant Mapdl
    participant warnings
    User->>Mapdl: inquire(strarray, func, ...)
    Mapdl->>Mapdl: response = ...
    Mapdl->>Mapdl: if len(response) >= 248
    Mapdl->>warnings: warn("Response might have been trimmed...")
    Mapdl-->>User: response
Loading

Class diagram for updated Mapdl.inquire method

classDiagram
    class Mapdl {
        +inquire(strarray, func, arg1, arg2, **kwargs)
    }
    Mapdl : +inquire() now warns if response >= 248 chars
    Mapdl : +inquire() suggests alternative method for env vars
Loading

File-Level Changes

Change Details Files
Warn on long inquire responses due to MAPDL string limits
  • Check if response length is ≥ 248 characters
  • Emit a UserWarning suggesting use of mapdl.sys('printenv') on Linux
  • Include guidance about MAPDL’s 248-char limitation in the warning text
src/ansys/mapdl/core/mapdl_extended.py
Refactor and expand inquire tests into modular pytest functions
  • Rename test_inquire to test_inquire_apdl
  • Create a parameterized test_inquire_env with skip-if logic for missing vars
  • Add separate tests for title, jobname, exist checks, and non-interactive mode
  • Replace platform-specific branches with pytest.mark.skipif and parametrize
tests/test_mapdl.py

Assessment against linked issues

Issue Objective Addressed Explanation
#4072 Fix the failing test_inquire test due to environment variables exceeding MAPDL's string length limit.
#4072 Implement a warning mechanism when environment variables are trimmed due to MAPDL's string length limitation.

Possibly linked issues


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 @germa89 - I've reviewed your changes - here's some feedback:

  • Extract the hard‐coded 248-character limit into a named constant (e.g. MAX_MAPDL_STRING_LENGTH) to improve clarity and avoid magic numbers.
  • Adjust the warning condition to trigger only when the response length exceeds 248 characters (i.e. > 248) to prevent false positives on exact-length responses.
  • Include the queried environment variable name in the warning message so it’s clear which variable may have been truncated.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Extract the hard‐coded 248-character limit into a named constant (e.g. MAX_MAPDL_STRING_LENGTH) to improve clarity and avoid magic numbers.
- Adjust the warning condition to trigger only when the response length exceeds 248 characters (i.e. > 248) to prevent false positives on exact-length responses.
- Include the queried environment variable name in the warning message so it’s clear which variable may have been truncated.

## Individual Comments

### Comment 1
<location> `src/ansys/mapdl/core/mapdl_extended.py:1465` </location>
<code_context>
         response = response.split("=")[1].strip()

+        if len(response) >= 248:
+            warnings.warn(
+                "Response might have been trimmed to 248 characters because of "
+                "MAPDL string limitations. "
+                "Check the output of 'mapdl.inquire' carefully. "
+                "Alternatively, you can use 'mapdl.sys('printenv') to obtain "
+                "the environment variables on Linux."
+            )
+
</code_context>

<issue_to_address>
The warning message could clarify that the response is truncated, not just trimmed.

Consider updating the message to: "Response truncated to 248 characters due to MAPDL string limitations" for greater clarity.
</issue_to_address>

### Comment 2
<location> `tests/test_mapdl.py:1259` </location>
<code_context>
+        pytest.skip(f"Environment variable {envvar} not found")
+
+    value = env_vars[envvar]
+    if envvar in ["PATH", "LD_LIBRARY_PATH"] and len(value) > 248:
+        # MAPDL warns about long environment variables because it trims them to
+        # 248
+        with pytest.warns(UserWarning):
+            assert mapdl.inquire("", "ENV", envvar, 0) in value
+
     else:
</code_context>

<issue_to_address>
Assertion could be more precise for trimmed environment variables.

Consider asserting that the returned value equals the first 248 characters of the environment variable to precisely verify the documented trimming behavior.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
        with pytest.warns(UserWarning):
            assert mapdl.inquire("", "ENV", envvar, 0) in value
=======
        with pytest.warns(UserWarning):
            assert mapdl.inquire("", "ENV", envvar, 0) == value[:248]
>>>>>>> REPLACE

</suggested_fix>

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.

Comment thread src/ansys/mapdl/core/mapdl_extended.py
Comment thread tests/test_mapdl.py Outdated
Comment thread tests/test_mapdl.py
Comment thread tests/test_mapdl.py Outdated
Comment thread tests/test_mapdl.py Outdated

Copilot AI 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.

Pull Request Overview

This PR adds a warning when inquire returns a truncated string (≥248 chars) due to MAPDL limitations and expands/refactors tests to cover APDL, environment variables, titles, job names, file existence, and non‐interactive modes.

  • Warn in mapdl.inquire if the response may have been trimmed to 248 chars.
  • Rename and split existing inquire test into multiple focused tests.
  • Add parametrized tests for environment variables (with warning on long values), title, jobname, exist checks, and non‐interactive behavior.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
tests/test_mapdl.py Renamed test_inquire, added test_inquire_env, refactored other inquire tests
src/ansys/mapdl/core/mapdl_extended.py Inserted a warning when response length ≥ 248 characters
Comments suppressed due to low confidence (1)

tests/test_mapdl.py:1265

  • The else: here is indented by 6 spaces but should match the 4-space indent of its corresponding if in test_inquire_env, otherwise this will cause a syntax error.
    else:

Comment thread tests/test_mapdl.py Outdated
Comment thread src/ansys/mapdl/core/mapdl_extended.py
@github-actions github-actions Bot added the bug Issue, problem or error in PyMAPDL label Jul 4, 2025
@codecov

codecov Bot commented Jul 4, 2025

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.80%. Comparing base (4aad52c) to head (1ca478c).
Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4074      +/-   ##
==========================================
- Coverage   91.82%   91.80%   -0.03%     
==========================================
  Files         187      187              
  Lines       14993    15023      +30     
==========================================
+ Hits        13768    13792      +24     
- Misses       1225     1231       +6     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

germa89 and others added 3 commits July 4, 2025 14:07
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
Comment thread src/ansys/mapdl/core/mapdl_extended.py Outdated
@germa89

germa89 commented Jul 4, 2025

Copy link
Copy Markdown
Collaborator Author

@pyansys-ci-bot LGTM.

@germa89
germa89 enabled auto-merge (squash) July 4, 2025 18:54

@pyansys-ci-bot pyansys-ci-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.

✅ Approving this PR because germa89 said so in here 😬

LGTM

@germa89
germa89 merged commit e45c8a4 into main Jul 4, 2025
@germa89
germa89 deleted the fix/inquire branch July 4, 2025 18:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Issue, problem or error in PyMAPDL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

inquire testing failing

3 participants