From 7fb969d1544afdd01ff215e87d88a05ce9cb2883 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Mon, 20 Aug 2018 15:23:11 +0200 Subject: [PATCH 1/5] Fix sys.path.insert for project path: insert absolute path Fixes https://github.com/pytest-dev/pytest-django/issues/637 --- pytest_django/plugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytest_django/plugin.py b/pytest_django/plugin.py index ce9591c23..e70772b90 100644 --- a/pytest_django/plugin.py +++ b/pytest_django/plugin.py @@ -139,7 +139,7 @@ def find_django_path(args): project_dir = find_django_path(args) if project_dir: - sys.path.insert(0, str(project_dir)) + sys.path.insert(0, str(project_dir.absolute())) return PROJECT_FOUND % project_dir return PROJECT_NOT_FOUND From aac34747d67b246e4432d93c076799d56af5e89b Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Mon, 20 Aug 2018 15:48:59 +0200 Subject: [PATCH 2/5] fixup! Fix sys.path.insert for project path: insert absolute path --- tests/test_manage_py_scan.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/test_manage_py_scan.py b/tests/test_manage_py_scan.py index 5218f9db9..3f88b7fc5 100644 --- a/tests/test_manage_py_scan.py +++ b/tests/test_manage_py_scan.py @@ -23,6 +23,27 @@ def test_foobar(): assert outcomes['passed'] == 1 +@pytest.mark.django_project(project_root='django_project_root', + create_manage_py=True) +def test_django_project_found_absolute(django_testdir, monkeypatch): + django_testdir.create_test_module(""" + def test_foobar(): + import os, sys + + # The one inserted by Python, see comment for test above. + assert sys.path[0] == os.getcwd() + + # The one inserted by us. It should be absolute. + assert sys.path[1] == os.getcwd() + """) + monkeypatch.chdir('django_project_root') + result = django_testdir.runpytest_subprocess('-s', '.') + assert result.ret == 0 + + outcomes = result.parseoutcomes() + assert outcomes['passed'] == 1 + + @pytest.mark.django_project(project_root='django_project_root', create_manage_py=True) def test_django_project_found_invalid_settings(django_testdir, monkeypatch): From f620d5b041bca02a672afd5e2a7d943d779b873e Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Mon, 20 Aug 2018 16:06:23 +0200 Subject: [PATCH 3/5] fixup! fixup! Fix sys.path.insert for project path: insert absolute path --- tests/test_manage_py_scan.py | 37 ++++++++++-------------------------- 1 file changed, 10 insertions(+), 27 deletions(-) diff --git a/tests/test_manage_py_scan.py b/tests/test_manage_py_scan.py index 3f88b7fc5..24b3e0e8f 100644 --- a/tests/test_manage_py_scan.py +++ b/tests/test_manage_py_scan.py @@ -3,41 +3,24 @@ @pytest.mark.django_project(project_root='django_project_root', create_manage_py=True) -def test_django_project_found(django_testdir): - # XXX: Important: Do not chdir() to django_project_root since runpytest_subprocess - # will call "python /path/to/pytest.py", which will impliclity add cwd to - # the path. By instead calling "python /path/to/pytest.py - # django_project_root", we avoid impliclity adding the project to sys.path - # This matches the behaviour when pytest is called directly as an - # executable (cwd is not added to the Python path) - - django_testdir.create_test_module(""" - def test_foobar(): - assert 1 + 1 == 2 - """) - - result = django_testdir.runpytest_subprocess('django_project_root') - assert result.ret == 0 - - outcomes = result.parseoutcomes() - assert outcomes['passed'] == 1 - - -@pytest.mark.django_project(project_root='django_project_root', - create_manage_py=True) -def test_django_project_found_absolute(django_testdir, monkeypatch): +def test_django_project_found(django_testdir, monkeypatch): + # NOTE: Important: the chdir() to django_project_root causes + # runpytest_subprocess to call "python /path/to/pytest.py", which will + # impliclity add cwd to the path. django_testdir.create_test_module(""" def test_foobar(): import os, sys - # The one inserted by Python, see comment for test above. - assert sys.path[0] == os.getcwd() + cwd = os.getcwd() + + # The one inserted by Python, see comment above. + assert sys.path[0] == cwd # The one inserted by us. It should be absolute. - assert sys.path[1] == os.getcwd() + assert sys.path[1] == cwd """) monkeypatch.chdir('django_project_root') - result = django_testdir.runpytest_subprocess('-s', '.') + result = django_testdir.runpytest_subprocess('.') assert result.ret == 0 outcomes = result.parseoutcomes() From 823fe27737775e4ee25c9eea771166941cd8c02d Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Mon, 20 Aug 2018 16:31:27 +0200 Subject: [PATCH 4/5] fixup! fixup! fixup! Fix sys.path.insert for project path: insert absolute path --- tests/test_manage_py_scan.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/test_manage_py_scan.py b/tests/test_manage_py_scan.py index 24b3e0e8f..a44e1655c 100644 --- a/tests/test_manage_py_scan.py +++ b/tests/test_manage_py_scan.py @@ -17,7 +17,12 @@ def test_foobar(): assert sys.path[0] == cwd # The one inserted by us. It should be absolute. - assert sys.path[1] == cwd + # py37 has it in sys.path[1] already, but otherwise there is an empty + # entry first. + if sys.path[1] == '': + assert sys.path[2] == cwd + else: + assert sys.path[1] == cwd """) monkeypatch.chdir('django_project_root') result = django_testdir.runpytest_subprocess('.') From fb9e6eecdfab1d2cd9ced94ceb91466e6bb9c1ee Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Mon, 20 Aug 2018 17:10:37 +0200 Subject: [PATCH 5/5] fixup! fixup! fixup! fixup! Fix sys.path.insert for project path: insert absolute path --- tests/test_manage_py_scan.py | 41 ++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/tests/test_manage_py_scan.py b/tests/test_manage_py_scan.py index a44e1655c..e07553aed 100644 --- a/tests/test_manage_py_scan.py +++ b/tests/test_manage_py_scan.py @@ -3,29 +3,38 @@ @pytest.mark.django_project(project_root='django_project_root', create_manage_py=True) -def test_django_project_found(django_testdir, monkeypatch): - # NOTE: Important: the chdir() to django_project_root causes - # runpytest_subprocess to call "python /path/to/pytest.py", which will - # impliclity add cwd to the path. +def test_django_project_found(django_testdir): + # XXX: Important: Do not chdir() to django_project_root since runpytest_subprocess + # will call "python /path/to/pytest.py", which will impliclity add cwd to + # the path. By instead calling "python /path/to/pytest.py + # django_project_root", we avoid impliclity adding the project to sys.path + # This matches the behaviour when pytest is called directly as an + # executable (cwd is not added to the Python path) + django_testdir.create_test_module(""" def test_foobar(): - import os, sys + assert 1 + 1 == 2 + """) + + result = django_testdir.runpytest_subprocess('django_project_root') + assert result.ret == 0 - cwd = os.getcwd() + outcomes = result.parseoutcomes() + assert outcomes['passed'] == 1 - # The one inserted by Python, see comment above. - assert sys.path[0] == cwd - # The one inserted by us. It should be absolute. - # py37 has it in sys.path[1] already, but otherwise there is an empty - # entry first. - if sys.path[1] == '': - assert sys.path[2] == cwd - else: - assert sys.path[1] == cwd +@pytest.mark.django_project(project_root='django_project_root', + create_manage_py=True) +def test_django_project_found_absolute(django_testdir, monkeypatch): + """This only tests that "." is added as an absolute path (#637).""" + django_testdir.create_test_module(""" + def test_dot_not_in_syspath(): + import sys + assert '.' not in sys.path[:5] """) monkeypatch.chdir('django_project_root') - result = django_testdir.runpytest_subprocess('.') + # NOTE: the "." here is important to test for an absolute path being used. + result = django_testdir.runpytest_subprocess('-s', '.') assert result.ret == 0 outcomes = result.parseoutcomes()