diff --git a/.travis.yml b/.travis.yml index 3780604..820a3e0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,8 +8,7 @@ jobs: python: 3.7 script: | set -e - pip install pytest_plugin/ - pytest -p no:pytest-mypy-plugins + pytest - name: Typecheck Django test suite python: 3.7 diff --git a/pytest.ini b/pytest.ini index fe0dff4..e918bd2 100644 --- a/pytest.ini +++ b/pytest.ini @@ -2,7 +2,8 @@ testpaths = ./test-data addopts = --tb=native - --mypy-ini-file=./test-data/plugins.ini -s -v - --cache-clear \ No newline at end of file + --cache-clear + --mypy-ini-file=./test-data/plugins.ini + --mypy-extension-hook=scripts.tests_extension_hook.django_plugin_hook diff --git a/pytest_plugin/collect.py b/pytest_plugin/collect.py deleted file mode 100644 index f6925d8..0000000 --- a/pytest_plugin/collect.py +++ /dev/null @@ -1,42 +0,0 @@ -# noinspection PyUnresolvedReferences -from pytest_mypy.collect import ( # noqa: F401 - File, YamlTestFile, pytest_addoption, -) -from pytest_mypy.item import YamlTestItem - - -class DjangoYamlTestFile(YamlTestFile): - def get_test_class(self): - return NewSemanalDjangoTestItem - - -def pytest_collect_file(path, parent): - if path.ext in {'.yaml', '.yml'} and path.basename.startswith(('test-', 'test_')): - return DjangoYamlTestFile(path, parent=parent, config=parent.config) - - -class NewSemanalDjangoTestItem(YamlTestItem): - def custom_init(self): - settings = { - 'SECRET_KEY': '"1"', - } - additional_settings = self.parsed_test_data.get('additional_settings') - if additional_settings: - for item in additional_settings: - name, _, val = item.partition('=') - settings[name] = val - - installed_apps = self.parsed_test_data.get('installed_apps', None) - if installed_apps is not None: - installed_apps += ['django.contrib.contenttypes'] - installed_apps_as_str = '(' + ','.join([repr(app) for app in installed_apps]) + ',)' - - pyproject_toml_file = File(path='pyproject.toml', - content='[tool.django-stubs]\ndjango_settings_module=\'mysettings\'') - self.files.append(pyproject_toml_file) - - settings_contents = f'INSTALLED_APPS={installed_apps_as_str}\n' - settings_contents += '\n'.join([f'{key}={val}' for key, val in settings.items()]) - - mysettings_file = File(path='mysettings.py', content=settings_contents) - self.files.append(mysettings_file) diff --git a/pytest_plugin/setup.py b/pytest_plugin/setup.py deleted file mode 100644 index b1dda25..0000000 --- a/pytest_plugin/setup.py +++ /dev/null @@ -1,27 +0,0 @@ -from setuptools import setup - -dependencies = [ - 'pytest-mypy-plugins', -] - -setup( - name='pytest-django-stubs-newsemanal', - version='0.4.0', - license='MIT', - url="https://github.com/mkurnikov/pytest-mypy-plugins", - author="Maksim Kurnikov", - author_email="maxim.kurnikov@gmail.com", - # the following makes a plugin available to pytest - entry_points={ - 'pytest11': [ - 'pytest-django-stubs-newsemanal = pytest_plugin.collect' - ] - }, - install_requires=dependencies, - classifiers=[ - 'Development Status :: 3 - Alpha', - 'License :: OSI Approved :: MIT License', - 'Programming Language :: Python :: 3.6', - 'Programming Language :: Python :: 3.7' - ] -) diff --git a/pytest_plugin/__init__.py b/scripts/__init__.py similarity index 100% rename from pytest_plugin/__init__.py rename to scripts/__init__.py diff --git a/scripts/tests_extension_hook.py b/scripts/tests_extension_hook.py new file mode 100644 index 0000000..ac7aa83 --- /dev/null +++ b/scripts/tests_extension_hook.py @@ -0,0 +1,28 @@ +from pytest_mypy.collect import File +from pytest_mypy.item import YamlTestItem + + +def django_plugin_hook(test_item: YamlTestItem) -> None: + settings = { + 'SECRET_KEY': '"1"', + } + additional_settings = test_item.parsed_test_data.get('additional_settings') + if additional_settings: + for item in additional_settings: + name, _, val = item.partition('=') + settings[name] = val + + installed_apps = test_item.parsed_test_data.get('installed_apps', None) + if installed_apps is not None: + installed_apps += ['django.contrib.contenttypes'] + installed_apps_as_str = '(' + ','.join([repr(app) for app in installed_apps]) + ',)' + + pyproject_toml_file = File(path='pyproject.toml', + content='[tool.django-stubs]\ndjango_settings_module=\'mysettings\'') + test_item.files.append(pyproject_toml_file) + + settings_contents = f'INSTALLED_APPS={installed_apps_as_str}\n' + settings_contents += '\n'.join([f'{key}={val}' for key, val in settings.items()]) + + mysettings_file = File(path='mysettings.py', content=settings_contents) + test_item.files.append(mysettings_file)