1212from _pytest .main import EXIT_NOTESTSCOLLECTED , EXIT_USAGEERROR
1313
1414
15+ def prepend_pythonpath (* dirs ):
16+ cur = os .getenv ("PYTHONPATH" )
17+ if cur :
18+ dirs += (cur ,)
19+ return os .pathsep .join (str (p ) for p in dirs )
20+
21+
1522class TestGeneralUsage (object ):
1623 def test_config_error (self , testdir ):
1724 testdir .copy_example ("conftest_usageerror/conftest.py" )
@@ -570,14 +577,8 @@ def test_cmdline_python_package(self, testdir, monkeypatch):
570577 assert result .ret == 0
571578 result .stdout .fnmatch_lines (["*1 passed*" ])
572579
573- def join_pythonpath (what ):
574- cur = os .environ .get ("PYTHONPATH" )
575- if cur :
576- return str (what ) + os .pathsep + cur
577- return what
578-
579580 empty_package = testdir .mkpydir ("empty_package" )
580- monkeypatch .setenv ("PYTHONPATH" , str ( join_pythonpath ( empty_package ) ))
581+ monkeypatch .setenv ("PYTHONPATH" , prepend_pythonpath ( empty_package ))
581582 # the path which is not a package raises a warning on pypy;
582583 # no idea why only pypy and not normal python warn about it here
583584 with warnings .catch_warnings ():
@@ -586,7 +587,7 @@ def join_pythonpath(what):
586587 assert result .ret == 0
587588 result .stdout .fnmatch_lines (["*2 passed*" ])
588589
589- monkeypatch .setenv ("PYTHONPATH" , str ( join_pythonpath ( testdir ) ))
590+ monkeypatch .setenv ("PYTHONPATH" , prepend_pythonpath ( testdir ))
590591 result = testdir .runpytest ("--pyargs" , "tpkg.test_missing" , syspathinsert = True )
591592 assert result .ret != 0
592593 result .stderr .fnmatch_lines (["*not*found*test_missing*" ])
@@ -626,13 +627,8 @@ def test_cmdline_python_namespace_package(self, testdir, monkeypatch):
626627 # ├── __init__.py
627628 # └── test_world.py
628629
629- def join_pythonpath (* dirs ):
630- cur = os .environ .get ("PYTHONPATH" )
631- if cur :
632- dirs += (cur ,)
633- return os .pathsep .join (str (p ) for p in dirs )
634-
635- monkeypatch .setenv ("PYTHONPATH" , join_pythonpath (* search_path ))
630+ # NOTE: the different/reversed ordering is intentional here.
631+ monkeypatch .setenv ("PYTHONPATH" , prepend_pythonpath (* search_path ))
636632 for p in search_path :
637633 monkeypatch .syspath_prepend (p )
638634
@@ -688,8 +684,6 @@ def test_cmdline_python_package_symlink(self, testdir, monkeypatch):
688684 pytest .skip (six .text_type (e .args [0 ]))
689685 monkeypatch .delenv ("PYTHONDONTWRITEBYTECODE" , raising = False )
690686
691- search_path = ["lib" , os .path .join ("local" , "lib" )]
692-
693687 dirname = "lib"
694688 d = testdir .mkdir (dirname )
695689 foo = d .mkdir ("foo" )
@@ -722,13 +716,9 @@ def test_cmdline_python_package_symlink(self, testdir, monkeypatch):
722716 # ├── conftest.py
723717 # └── test_bar.py
724718
725- def join_pythonpath (* dirs ):
726- cur = os .getenv ("PYTHONPATH" )
727- if cur :
728- dirs += (cur ,)
729- return os .pathsep .join (str (p ) for p in dirs )
730-
731- monkeypatch .setenv ("PYTHONPATH" , join_pythonpath (* search_path ))
719+ # NOTE: the different/reversed might not be intentional here (copied).
720+ search_path = ["lib" , os .path .join ("local" , "lib" )]
721+ monkeypatch .setenv ("PYTHONPATH" , prepend_pythonpath (* search_path ))
732722 for p in search_path :
733723 monkeypatch .syspath_prepend (p )
734724
0 commit comments