diff --git a/pytest_django/plugin.py b/pytest_django/plugin.py index 5d4f323a7..232798bdf 100644 --- a/pytest_django/plugin.py +++ b/pytest_django/plugin.py @@ -11,7 +11,7 @@ import sys import types -import py +import pathlib import pytest from .django_compat import is_django_unittest # noqa @@ -87,13 +87,6 @@ def pytest_addoption(parser): type='bool', default=False) -def _exists(path, ignore=EnvironmentError): - try: - return path.check() - except ignore: - return False - - PROJECT_FOUND = ('pytest-django found a Django project in %s ' '(it contains manage.py) and added it to the Python path.\n' 'If this is wrong, add "django_find_project = false" to ' @@ -120,21 +113,28 @@ def _handle_import_error(extra_message): def _add_django_project_to_path(args): - args = [x for x in args if not str(x).startswith("-")] - - if not args: - args = [py.path.local()] - - for arg in args: - arg = py.path.local(arg) - - for base in arg.parts(reverse=True): - manage_py_try = base.join('manage.py') - - if _exists(manage_py_try): - sys.path.insert(0, str(base)) - return PROJECT_FOUND % base + def is_django_project(path): + return path.is_dir() and (path / 'manage.py').exists() + + def find_django_path(args): + args = [pathlib.Path(x) for x in args if not str(x).startswith("-")] + args = [p for p in args if p.is_dir()] + + if not args: + args = [pathlib.Path.cwd()] + + for arg in args: + if is_django_project(arg): + return arg + for parent in arg.parents: + if is_django_project(parent): + return parent + return None + project_dir = find_django_path(args) + if project_dir: + sys.path.insert(0, str(project_dir)) + return PROJECT_FOUND % project_dir return PROJECT_NOT_FOUND diff --git a/setup.py b/setup.py index 0901054df..f53e112eb 100755 --- a/setup.py +++ b/setup.py @@ -30,12 +30,22 @@ def read(fname): long_description=read('README.rst'), python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*', setup_requires=['setuptools_scm>=1.11.1'], - install_requires=['pytest>=3.6'], + install_requires=[ + 'pytest>=3.6', + 'pathlib;python_version<"3.4"', + ], extras_require={ 'docs': [ 'sphinx', 'sphinx_rtd_theme', - ] + ], + 'testing': [ + 'pytest>=3.6', + 'Django', + 'django-configurations==2.0', + 'pytest-xdist==1.15', + 'six', + ], }, classifiers=['Development Status :: 5 - Production/Stable', 'Framework :: Django', diff --git a/tests/conftest.py b/tests/conftest.py index 8ea211222..debdc7442 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,15 +2,16 @@ import shutil from textwrap import dedent -import py +import pathlib import pytest +import six from django.conf import settings from pytest_django_test.db_helpers import DB_NAME, TEST_DB_NAME pytest_plugins = 'pytester' -REPOSITORY_ROOT = py.path.local(__file__).join('..') +REPOSITORY_ROOT = pathlib.Path(__file__).parent def pytest_configure(config): @@ -99,12 +100,12 @@ def django_testdir(request, testdir, monkeypatch): tpkg_path.ensure('__init__.py') - app_source = REPOSITORY_ROOT.dirpath('pytest_django_test/app') + app_source = REPOSITORY_ROOT / '../pytest_django_test/app' test_app_path = tpkg_path.join('app') # Copy the test app to make it available in the new test run - shutil.copytree(py.builtin._totext(app_source), - py.builtin._totext(test_app_path)) + shutil.copytree(six.text_type(app_source), + six.text_type(test_app_path)) tpkg_path.join("the_settings.py").write(test_settings) monkeypatch.setenv('DJANGO_SETTINGS_MODULE', 'tpkg.the_settings') diff --git a/tox.ini b/tox.ini index 068760b21..3d945e526 100644 --- a/tox.ini +++ b/tox.ini @@ -7,10 +7,8 @@ envlist = - py{36,py27}-checkqa [testenv] +extras = testing deps = - pytest>=3.6 - django-configurations==2.0 - pytest-xdist==1.15 {env:_PYTESTDJANGO_TOX_EXTRA_DEPS:} djmaster: https://github.com/django/django/archive/master.tar.gz