mirror of
https://github.com/davidhalter/django-stubs.git
synced 2025-12-10 22:11:54 +08:00
* Adds support for pyproject.toml files Since mypy 0.900 the pyproject.toml files are supported. This PR adds a support for it. It searchs for a `tool.django-stubs` section. This is an example configuration: ``` [tool.django-stubs] django_settings_module = "config.settings.local" ``` Fixes #638 * Added TOML tests * Use textwrap.dedent instead of trying to manually replace spaces
137 lines
4.0 KiB
Python
137 lines
4.0 KiB
Python
import tempfile
|
|
import typing
|
|
|
|
import pytest
|
|
|
|
from mypy_django_plugin.main import extract_django_settings_module
|
|
|
|
TEMPLATE = """
|
|
(config)
|
|
...
|
|
[mypy.plugins.django_stubs]
|
|
django_settings_module: str (required)
|
|
...
|
|
(django-stubs) mypy: error: 'django_settings_module' is not set: {}
|
|
"""
|
|
|
|
TEMPLATE_TOML = """
|
|
(config)
|
|
...
|
|
[tool.django-stubs]
|
|
django_settings_module = str (required)
|
|
...
|
|
(django-stubs) mypy: error: 'django_settings_module' not found or invalid: {}
|
|
"""
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"config_file_contents,message_part",
|
|
[
|
|
pytest.param(
|
|
None,
|
|
"mypy config file is not specified or found",
|
|
id="missing-file",
|
|
),
|
|
pytest.param(
|
|
["[not-really-django-stubs]"],
|
|
"no section [mypy.plugins.django-stubs]",
|
|
id="missing-section",
|
|
),
|
|
pytest.param(
|
|
["[mypy.plugins.django-stubs]", "\tnot_django_not_settings_module = badbadmodule"],
|
|
"the setting is not provided",
|
|
id="missing-settings-module",
|
|
),
|
|
pytest.param(
|
|
["[mypy.plugins.django-stubs]"],
|
|
"the setting is not provided",
|
|
id="no-settings-given",
|
|
),
|
|
],
|
|
)
|
|
def test_misconfiguration_handling(capsys, config_file_contents, message_part):
|
|
# type: (typing.Any, typing.List[str], str) -> None
|
|
"""Invalid configuration raises `SystemExit` with a precise error message."""
|
|
with tempfile.NamedTemporaryFile(mode="w+") as config_file:
|
|
if not config_file_contents:
|
|
config_file.close()
|
|
else:
|
|
config_file.write("\n".join(config_file_contents).expandtabs(4))
|
|
config_file.seek(0)
|
|
|
|
with pytest.raises(SystemExit, match="2"):
|
|
extract_django_settings_module(config_file.name)
|
|
|
|
error_message = "usage: " + TEMPLATE.format(message_part)
|
|
assert error_message == capsys.readouterr().err
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"config_file_contents,message_part",
|
|
[
|
|
(
|
|
"""
|
|
[tool.django-stubs]
|
|
django_settings_module = 123
|
|
""",
|
|
"the setting must be a string",
|
|
),
|
|
(
|
|
"""
|
|
[tool.not-really-django-stubs]
|
|
django_settings_module = "my.module"
|
|
""",
|
|
"no section [tool.django-stubs]",
|
|
),
|
|
(
|
|
"""
|
|
[tool.django-stubs]
|
|
not_django_not_settings_module = "badbadmodule"
|
|
""",
|
|
"the setting is not provided",
|
|
),
|
|
],
|
|
)
|
|
def test_toml_misconfiguration_handling(capsys, config_file_contents, message_part):
|
|
with tempfile.NamedTemporaryFile(mode="w+", suffix=".toml") as config_file:
|
|
config_file.write(config_file_contents)
|
|
config_file.seek(0)
|
|
|
|
with pytest.raises(SystemExit, match="2"):
|
|
extract_django_settings_module(config_file.name)
|
|
|
|
error_message = "usage: " + TEMPLATE_TOML.format(message_part)
|
|
assert error_message == capsys.readouterr().err
|
|
|
|
|
|
def test_correct_toml_configuration() -> None:
|
|
config_file_contents = """
|
|
[tool.django-stubs]
|
|
some_other_setting = "setting"
|
|
django_settings_module = "my.module"
|
|
"""
|
|
|
|
with tempfile.NamedTemporaryFile(mode="w+", suffix=".toml") as config_file:
|
|
config_file.write(config_file_contents)
|
|
config_file.seek(0)
|
|
|
|
extracted = extract_django_settings_module(config_file.name)
|
|
|
|
assert extracted == "my.module"
|
|
|
|
|
|
def test_correct_configuration() -> None:
|
|
"""Django settings module gets extracted given valid configuration."""
|
|
config_file_contents = [
|
|
"[mypy.plugins.django-stubs]",
|
|
"\tsome_other_setting = setting",
|
|
"\tdjango_settings_module = my.module",
|
|
]
|
|
with tempfile.NamedTemporaryFile(mode="w+") as config_file:
|
|
config_file.write("\n".join(config_file_contents).expandtabs(4))
|
|
config_file.seek(0)
|
|
|
|
extracted = extract_django_settings_module(config_file.name)
|
|
|
|
assert extracted == "my.module"
|