| 1 | Wrong path to test file when directory changed in fixture |
| 2 | Files are shown as relative to new directory when working directory is changed in a fixture. This makes it impossible to jump to the error as the editor is unaware of the directory change. The displayed directory should stay relative to the original directory. |
| 3 | |
| 4 | test_path_error.py: |
| 5 | ```python |
| 6 | import os |
| 7 | import errno |
| 8 | import shutil |
| 9 | |
| 10 | import pytest |
| 11 | |
| 12 | |
| 13 | @pytest.fixture |
| 14 | def private_dir(): # or (monkeypatch) |
| 15 | out_dir = 'ddd' |
| 16 | |
| 17 | try: |
| 18 | shutil.rmtree(out_dir) |
| 19 | except OSError as ex: |
| 20 | if ex.errno != errno.ENOENT: |
| 21 | raise |
| 22 | os.mkdir(out_dir) |
| 23 | |
| 24 | old_dir = os.getcwd() |
| 25 | os.chdir(out_dir) |
| 26 | yield out_dir |
| 27 | os.chdir(old_dir) |
| 28 | |
| 29 | # Same issue if using: |
| 30 | # monkeypatch.chdir(out_dir) |
| 31 | |
| 32 | |
| 33 | def test_show_wrong_path(private_dir): |
| 34 | assert False |
| 35 | ``` |
| 36 | |
| 37 | ```diff |
| 38 | + Expected: test_path_error.py:29: AssertionError |
| 39 | - Displayed: ../test_path_error.py:29: AssertionError |
| 40 | ``` |
| 41 | |
| 42 | The full output is: |
| 43 | ``` |
| 44 | -*- mode: compilation; default-directory: "~/src/pytest_path_error/" -*- |
| 45 | Compilation started at Fri Jan 10 00:05:52 |
| 46 | |
| 47 | nox |
| 48 | nox > Running session test |
| 49 | nox > Creating virtual environment (virtualenv) using python3.7 in .nox/test |
| 50 | nox > pip install pytest>=5.3 |
| 51 | nox > pip freeze |
| 52 | attrs==19.3.0 |
| 53 | importlib-metadata==1.3.0 |
| 54 | more-itertools==8.0.2 |
| 55 | packaging==20.0 |
| 56 | pluggy==0.13.1 |
| 57 | py==1.8.1 |
| 58 | pyparsing==2.4.6 |
| 59 | pytest==5.3.2 |
| 60 | six==1.13.0 |
| 61 | wcwidth==0.1.8 |
| 62 | zipp==0.6.0 |
| 63 | nox > pytest |
| 64 | ================================= test session starts ================================= |
| 65 | platform linux -- Python 3.7.5, pytest-5.3.2, py-1.8.1, pluggy-0.13.1 |
| 66 | rootdir: /home/lhn/src/pytest_path_error |
| 67 | collected 1 item |
| 68 | |
| 69 | test_path_error.py F [100%] |
| 70 | |
| 71 | ====================================== FAILURES ======================================= |
| 72 | ________________________________ test_show_wrong_path _________________________________ |
| 73 | |
| 74 | private_dir = 'ddd' |
| 75 | |
| 76 | def test_show_wrong_path(private_dir): |
| 77 | > assert False |
| 78 | E assert False |
| 79 | |
| 80 | ../test_path_error.py:29: AssertionError |
| 81 | ================================== 1 failed in 0.03s ================================== |
| 82 | nox > Command pytest failed with exit code 1 |
| 83 | nox > Session test failed. |
| 84 | |
| 85 | Compilation exited abnormally with code 1 at Fri Jan 10 00:06:01 |
| 86 | ``` |
| 87 | |
| 88 | noxfile.py: |
| 89 | ```python |
| 90 | import nox |
| 91 | |
| 92 | @nox.session(python='3.7') |
| 93 | def test(session): |
| 94 | session.install('pytest>=5.3') |
| 95 | session.run('pip', 'freeze') |
| 96 | session.run('pytest') |
| 97 | ``` |