From 2bd442f3679ea2e8f63b580b18b6d6c81c0bc0c0 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Mon, 20 Aug 2018 18:28:12 +0200 Subject: [PATCH] Fix sys.path.insert for project path: insert absolute path (#638) Fixes https://github.com/pytest-dev/pytest-django/issues/637 --- pytest_django/plugin.py | 2 +- tests/test_manage_py_scan.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 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 diff --git a/tests/test_manage_py_scan.py b/tests/test_manage_py_scan.py index 5218f9db9..e07553aed 100644 --- a/tests/test_manage_py_scan.py +++ b/tests/test_manage_py_scan.py @@ -23,6 +23,24 @@ 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): + """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') + # 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() + 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):