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.ini issue #1312

Closed
benjamin-kirkbride opened this issue Jun 30, 2023 · 6 comments · Fixed by #1316
Closed

pytest.ini issue #1312

benjamin-kirkbride opened this issue Jun 30, 2023 · 6 comments · Fixed by #1316

Comments

@benjamin-kirkbride
Copy link
Contributor

What is this line for?

testpaths = porcupine/plugins/autoindent.py tests/

Specifically the porcupine/plugins/autoindent.py portion. It causes an error for me when trying to run tests:

_____________________________________________________________ ERROR collecting porcupine/plugins/autoindent.py ______________________________________________________________
.venv/lib/python3.11/site-packages/_pytest/runner.py:311: in from_call
    result: Optional[TResult] = func()
.venv/lib/python3.11/site-packages/_pytest/runner.py:341: in <lambda>
    call = CallInfo.from_call(lambda: list(collector.collect()), "collect")
.venv/lib/python3.11/site-packages/_pytest/doctest.py:532: in collect
    module = import_path(self.fspath)
.venv/lib/python3.11/site-packages/_pytest/pathlib.py:544: in import_path
    raise ImportPathMismatchError(module_name, module_file, path)
E   _pytest.pathlib.ImportPathMismatchError: ('porcupine.plugins.autoindent', '/home/bkirkbride/projects/porcupine/.venv/lib/python3.11/site-packages/porcupine/plugins/autoindent.py', PosixPath('/home/bkirkbride/projects/porcupine/porcupine/plugins/autoindent.py'))
_____________________________________________________________ ERROR collecting porcupine/plugins/autoindent.py ______________________________________________________________
import file mismatch:
imported module 'porcupine.plugins.autoindent' has this __file__ attribute:
  /home/bkirkbride/projects/porcupine/.venv/lib/python3.11/site-packages/porcupine/plugins/autoindent.py
which is not the same as the test file we want to collect:
  /home/bkirkbride/projects/porcupine/porcupine/plugins/autoindent.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules
@benjamin-kirkbride
Copy link
Contributor Author

benjamin-kirkbride commented Jun 30, 2023

Tests (mostly) work when I remove that path.

@Akuli
Copy link
Owner

Akuli commented Jun 30, 2023

The autoindent plugin is the only file that contains doctests, so we tell pytest to find tests from it too (by default it only looks in tests). Ideally there would be a comment that mentions this just above the confusing line :)

Why do you have Porcupine installed into your development venv? It is unnecessary and apparently causes problems too.

@Akuli
Copy link
Owner

Akuli commented Jul 1, 2023

I told pytest to find tests in the porcupine folder by changing the hacky line to testpaths = porcupine/ tests/.

diff --git a/pytest.ini b/pytest.ini
index 481e16da..b9c2c03a 100644
--- a/pytest.ini
+++ b/pytest.ini
@@ -1,7 +1,7 @@
 [pytest]
 # why we disable nose plugin: https://github.com/pytest-dev/pytest/issues/10825
 addopts = --doctest-modules --capture=no -p no:nose
-testpaths = porcupine/plugins/autoindent.py tests/
+testpaths = porcupine/ tests/
 markers = pastebin_test
 
 # uncomment this if you dare... i like how pytest hides the shittyness

When I run python3 -m pytest, I get:

============================= test session starts ==============================
platform linux -- Python 3.9.2, pytest-7.4.0, pluggy-0.13.1
rootdir: /home/akuli/porcu
configfile: pytest.ini
testpaths: porcupine/, tests/
plugins: cov-4.0.0, mock-3.10.0
collected 1578 items / 1 error                                                 

==================================== ERRORS ====================================
____________ ERROR collecting porcupine/plugins/run/windows_run.py _____________
env/lib/python3.9/site-packages/_pytest/runner.py:341: in from_call
    result: Optional[TResult] = func()
env/lib/python3.9/site-packages/_pytest/runner.py:372: in <lambda>
    call = CallInfo.from_call(lambda: list(collector.collect()), "collect")
env/lib/python3.9/site-packages/_pytest/doctest.py:547: in collect
    module = import_path(
env/lib/python3.9/site-packages/_pytest/pathlib.py:565: in import_path
    importlib.import_module(module_name)
/usr/lib/python3.9/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
<frozen importlib._bootstrap>:1030: in _gcd_import
    ???
<frozen importlib._bootstrap>:1007: in _find_and_load
    ???
<frozen importlib._bootstrap>:986: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:680: in _load_unlocked
    ???
<frozen importlib._bootstrap_external>:790: in exec_module
    ???
<frozen importlib._bootstrap>:228: in _call_with_frames_removed
    ???
porcupine/plugins/run/windows_run.py:14: in <module>
    prog, directory, command = sys.argv
E   ValueError: not enough values to unpack (expected 3, got 1)
=========================== short test summary info ============================
ERROR porcupine/plugins/run/windows_run.py - ValueError: not enough values to unpack (expected 3, got 1)
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 1.01s ===============================

The problem is that pytest is trying to import porcupine/plugins/run/windows_run.py to find out if it has any tests (I care only about doctests), but that file is not meant to be imported at all.

@benjamin-kirkbride
Copy link
Contributor Author

benjamin-kirkbride commented Jul 1, 2023

Could we put a if __name__ == "__main__:" in there?

@benjamin-kirkbride
Copy link
Contributor Author

Adding --ignore porcupine/plugins/run/windows_run.py to addopts in pytest.ini works as well.

@Akuli
Copy link
Owner

Akuli commented Jul 1, 2023

Thanks for bringing this up! Of course, I don't like things that hard-code an oddly specific thing without any explanation :)

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 a pull request may close this issue.

2 participants