Skip to content

monkeypatching PYTHONPATH breaks real PYTHONPATH #4913

@hroncok

Description

@hroncok

In Fedora, we invoke tests with custom PATH and PYTHONPATH set:

PATH=%{buildroot}%{_bindir}:${PATH} \
PYTHONPATH=%{buildroot}%{python2_sitelib} \
  %{buildroot}%{_bindir}/pytest-%{python2_version} -r s testing

PATH=%{buildroot}%{_bindir}:${PATH} \
PYTHONPATH=%{buildroot}%{python3_sitelib} \
  %{buildroot}%{_bindir}/pytest-%{python3_version} -r s testing

The monkeypatching in

monkeypatch.setenv("PYTHONPATH", str(testdir.tmpdir))

breaks that:

=================================== FAILURES ===================================
______________________ test_collect_pyargs_with_testpaths ______________________
testdir = <Testdir local('/tmp/pytest-of-mockbuild/pytest-0/test_collect_pyargs_with_testpaths0')>
monkeypatch = <_pytest.monkeypatch.MonkeyPatch object at 0x7f9018546b50>
    def test_collect_pyargs_with_testpaths(testdir, monkeypatch):
        testmod = testdir.mkdir("testmod")
        # NOTE: __init__.py is not collected since it does not match python_files.
        testmod.ensure("__init__.py").write("def test_func(): pass")
        testmod.ensure("test_file.py").write("def test_func(): pass")
    
        root = testdir.mkdir("root")
        root.ensure("pytest.ini").write(
            textwrap.dedent(
                """
            [pytest]
            addopts = --pyargs
            testpaths = testmod
        """
            )
        )
        monkeypatch.setenv("PYTHONPATH", str(testdir.tmpdir))
        with root.as_cwd():
            result = testdir.runpytest_subprocess()
>       result.stdout.fnmatch_lines(["*1 passed in*"])
E       Failed: remains unmatched: '*1 passed in*'
/builddir/build/BUILD/pytest-4.3.0/testing/test_collection.py:1114: Failed
----------------------------- Captured stdout call -----------------------------
running: /usr/bin/python2 -mpytest --basetemp=/tmp/pytest-of-mockbuild/pytest-0/test_collect_pyargs_with_testpaths0/runpytest-0
     in: /tmp/pytest-of-mockbuild/pytest-0/test_collect_pyargs_with_testpaths0/root
----------------------------- Captured stderr call -----------------------------
/usr/bin/python2: No module named pytest
=============================== warnings summary ===============================
testing/test_warnings.py::test_group_warnings_by_message

I think the fix is to read PYTHONPATH and only prepend the patched value.
Will try to see if it helps.

Metadata

Metadata

Assignees

Labels

good first issueeasy issue that is friendly to new contributorplatform: linuxlinux platform-specific problemtype: bugproblem that needs to be addressedtype: regressionindicates a problem that was introduced in a release which was working previously

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions