Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pytest Fails to Run When Passing Long Command Line Arguments #11394

Closed
iandonnelly opened this issue Sep 5, 2023 · 11 comments · Fixed by #11404
Closed

Pytest Fails to Run When Passing Long Command Line Arguments #11394

iandonnelly opened this issue Sep 5, 2023 · 11 comments · Fixed by #11404
Labels
status: help wanted developers would like help from experts on this topic type: bug problem that needs to be addressed

Comments

@iandonnelly
Copy link

iandonnelly commented Sep 5, 2023

I am seeing an issue when passing a very long command-line argument to pytest. It fails to run with the following error:

INTERNALERROR>   File "/venv/lib/python3.11/site-packages/_pytest/main.py", line 333, in pytest_collection
INTERNALERROR>     session.perform_collect()
INTERNALERROR>   File "/venv/lib/python3.11/site-packages/_pytest/main.py", line 638, in perform_collect
INTERNALERROR>     fspath, parts = resolve_collection_argument(
INTERNALERROR>                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR>   File "/venv/lib/python3.11/site-packages/_pytest/main.py", line 889, in resolve_collection_argument
INTERNALERROR>   File "/venv/lib/python3.11/site-packages/_pytest/main.py", line 889, in resolve_collection_argument
INTERNALERROR>     if not fspath.exists():
INTERNALERROR>            ^^^^^^^^^^^^^^^
INTERNALERROR>   File "/usr/local/lib/python3.11/pathlib.py", line 1235, in exists
INTERNALERROR>     self.stat()
INTERNALERROR>   File "/usr/local/lib/python3.11/pathlib.py", line 1013, in stat
INTERNALERROR>     return os.stat(self, follow_symlinks=follow_symlinks)
INTERNALERROR>            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR> OSError: [Errno 36] File name too long: '/pwd/tests/very,long,comma-seperated,value,for,long_list'

Pytest version:
Tested with 7.3.2 and 7.4.1

pip list output:

--------------------- ---------
allure-pytest         2.13.2
allure-python-commons 2.13.2
ansi2html             1.8.0
attrs                 23.1.0
boto3                 1.28.40
botocore              1.31.40
certifi               2023.7.22
chardet               5.2.0
charset-normalizer    3.2.0
click                 8.1.3
contourpy             1.0.7
cycler                0.11.0
datadog               0.45.0
execnet               1.9.0
filelock              3.12.0
fonttools             4.39.4
gitdb                 4.0.10
GitPython             3.1.31
idna                  3.4
iniconfig             2.0.0
Jinja2                3.1.2
jmespath              1.0.1
kiwisolver            1.4.4
markdown-it-py        2.2.0
MarkupSafe            2.1.3
matplotlib            3.7.1
mdurl                 0.1.2
numpy                 1.24.3
packaging             23.1
pandas                1.5.3
Pillow                9.5.0
pip                   22.3.1
pluggy                1.0.0
py                    1.11.0
Pygments              2.15.1
pyparsing             3.0.9
pytest                7.4.1
pytest-html           4.0.0
pytest-metadata       2.0.4
pytest-xdist          3.3.1
python-dateutil       2.8.2
pytz                  2022.7.1
requests              2.31.0
rich                  13.3.5
s3transfer            0.6.2
setuptools            65.5.0
six                   1.16.0
smmap                 5.0.0
urllib3               1.26.16

Example usage:

conftest.py:

import pytest

def pytest_addoption(parser):
    parser.addoption("--long-list", dest="long_list", action="store", default="all", help="List of things")

@pytest.fixture(scope="module")
def specified_feeds(request):
    list_string = request.config.getoption("--long-list")
    return list_string.split(',')

Invocation:
pytest /test_my_thing.py --long-list a,b,c,d,e,f

Note: the actual length of the string passed to --long-list is 396 characters

This seems to be similar to #10169

@iandonnelly
Copy link
Author

@nicoddemus Tagging you since this is similar to a bug you closed.

@nicoddemus
Copy link
Member

@iandonnelly

Note: the actual length of the string passed to --long-list is 396 characters

So the path that appears in the message ('/pwd/tests/very,long,comma-seperated,value,for,long_list') is also very long, correct?

Seems indeed like we need to also surround that exists call with a try/except then.

@iandonnelly
Copy link
Author

Exactly, that's my thought as well.

@RonnyPfannschmidt RonnyPfannschmidt added type: bug problem that needs to be addressed status: help wanted developers would like help from experts on this topic labels Sep 6, 2023
nicoddemus added a commit to nicoddemus/pytest that referenced this issue Sep 7, 2023
nicoddemus added a commit to nicoddemus/pytest that referenced this issue Sep 7, 2023
nicoddemus added a commit to nicoddemus/pytest that referenced this issue Sep 7, 2023
nicoddemus added a commit to nicoddemus/pytest that referenced this issue Sep 7, 2023
nicoddemus added a commit to nicoddemus/pytest that referenced this issue Sep 7, 2023
nicoddemus added a commit to nicoddemus/pytest that referenced this issue Sep 7, 2023
nicoddemus added a commit to nicoddemus/pytest that referenced this issue Sep 7, 2023
@iandonnelly
Copy link
Author

iandonnelly commented Sep 7, 2023

@nicoddemus Now I'm just getting an error that the file or directory isn't found. This command line argument is just a a string that represents a CSV list --arg=a,b,c,word,whatever,pretty_long123,list

The error message is ERROR: file or directory not found: a,b,c,word,whatever,pretty_long123,list

@nicoddemus
Copy link
Member

Now I'm just getting an error that the file or directory isn't found.

What changed? Did you try the recently released 7.4.2 version?

@iandonnelly
Copy link
Author

@nicoddemus I grabbed from your dev branch, just tried 7.4.2 though and it's the same error I described above. What's the code path look like for parsing command-line arguments? It's correct that that argument isn't a path but why doesn't it just forward the argument as a string?

@nicoddemus
Copy link
Member

@iandonnelly can you post the full traceback?

@iandonnelly
Copy link
Author

There's no traceback for this, I think the argument just isn't being successfully parsed by pytest. I'll try stepping through the code Monday and see if I can figure out why.

@nicoddemus
Copy link
Member

Thanks!

@iandonnelly
Copy link
Author

@nicoddemus Sorry for the delay, I figured out what was going on. Our --long-list value had a space in it so pytest was reading it as two arguments (correctly) and there is no file present for that second argument. I think everything is working as expected now and I think handling the OSError is a real improvement anyway so I think we're all ok now.

@nicoddemus
Copy link
Member

Nice, thanks for the follow up!

jsuchenia pushed a commit to jsuchenia/adventofcode that referenced this issue Dec 2, 2023
This PR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [pytest](https://docs.pytest.org/en/latest/) ([source](https://github.com/pytest-dev/pytest), [changelog](https://docs.pytest.org/en/stable/changelog.html)) | patch | `==7.4.0` -> `==7.4.2` |

---

### Release Notes

<details>
<summary>pytest-dev/pytest (pytest)</summary>

### [`v7.4.2`](https://github.com/pytest-dev/pytest/releases/tag/7.4.2): pytest 7.4.2 (2023-09-07)

[Compare Source](pytest-dev/pytest@7.4.1...7.4.2)

### Bug Fixes

-   [#&#8203;11237](pytest-dev/pytest#11237): Fix doctest collection of `functools.cached_property` objects.

-   [#&#8203;11306](pytest-dev/pytest#11306): Fixed bug using `--importmode=importlib` which would cause package `__init__.py` files to be imported more than once in some cases.

-   [#&#8203;11367](pytest-dev/pytest#11367): Fixed bug where `user_properties` where not being saved in the JUnit XML file if a fixture failed during teardown.

-   [#&#8203;11394](pytest-dev/pytest#11394): Fixed crash when parsing long command line arguments that might be interpreted as files.

### Improved Documentation

-   [#&#8203;11391](pytest-dev/pytest#11391): Improved disclaimer on pytest plugin reference page to better indicate this is an automated, non-curated listing.

### [`v7.4.1`](https://github.com/pytest-dev/pytest/releases/tag/7.4.1): pytest 7.4.1 (2023-09-02)

[Compare Source](pytest-dev/pytest@7.4.0...7.4.1)

## Bug Fixes

-   [#&#8203;10337](pytest-dev/pytest#10337): Fixed bug where fake intermediate modules generated by `--import-mode=importlib` would not include the
    child modules as attributes of the parent modules.

-   [#&#8203;10702](pytest-dev/pytest#10702): Fixed error assertion handling in `pytest.approx` when `None` is an expected or received value when comparing dictionaries.

-   [#&#8203;10811](pytest-dev/pytest#10811): Fixed issue when using `--import-mode=importlib` together with `--doctest-modules` that caused modules
    to be imported more than once, causing problems with modules that have import side effects.

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNi44LjExIiwidXBkYXRlZEluVmVyIjoiMzYuMTA3LjIiLCJ0YXJnZXRCcmFuY2giOiJtYXN0ZXIifQ==-->

Reviewed-on: https://git.apud.pl/jacek/adventofcode/pulls/32
Co-authored-by: Renovate <renovate@apud.pl>
Co-committed-by: Renovate <renovate@apud.pl>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: help wanted developers would like help from experts on this topic type: bug problem that needs to be addressed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants