1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
|
import os
import tempfile
import uuid
from collections.abc import Generator
from contextlib import contextmanager
from typing import Any
from unittest import mock
import pytest
from mypy_django_plugin.config import DjangoPluginConfig
TEMPLATE = """
(config)
...
[mypy.plugins.django-stubs]
django_settings_module = str (default: `os.getenv("DJANGO_SETTINGS_MODULE")`)
strict_settings = bool (default: true)
strict_model_abstract_attrs = bool (default: true)
...
(django-stubs) mypy: error: {}
"""
TEMPLATE_TOML = """
(config)
...
[tool.django-stubs]
django_settings_module = str (default: `os.getenv("DJANGO_SETTINGS_MODULE")`)
strict_settings = bool (default: true)
strict_model_abstract_attrs = bool (default: true)
...
(django-stubs) mypy: error: {}
"""
@contextmanager
def write_to_file(file_contents: str, suffix: str | None = None) -> Generator[str, None, None]:
with tempfile.NamedTemporaryFile(mode="w+", suffix=suffix) as config_file:
config_file.write(file_contents)
config_file.seek(0)
yield config_file.name
@pytest.mark.parametrize(
("config_file_contents", "message_part"),
[
pytest.param(
["[not-really-django-stubs]"],
"no section [mypy.plugins.django-stubs] found",
id="missing-section",
),
pytest.param(
["[mypy.plugins.django-stubs]", "\tnot_django_not_settings_module = badbadmodule"],
(
"missing required 'django_settings_module' config.\n"
"Either specify this config or set your `DJANGO_SETTINGS_MODULE` env var"
),
id="missing-settings-module",
),
pytest.param(
["[mypy.plugins.django-stubs]"],
(
"missing required 'django_settings_module' config.\n"
"Either specify this config or set your `DJANGO_SETTINGS_MODULE` env var"
),
id="no-settings-given",
),
pytest.param(
["[mypy.plugins.django-stubs]", "django_settings_module = some.module", "strict_settings = bad"],
"invalid 'strict_settings': the setting must be a boolean",
id="invalid-strict_settings",
),
pytest.param(
[
"[mypy.plugins.django-stubs]",
"django_settings_module = some.module",
"strict_model_abstract_attrs = bad",
],
"invalid 'strict_model_abstract_attrs': the setting must be a boolean",
id="invalid-strict_model_abstract_attrs",
),
],
)
def test_misconfiguration_handling(capsys: Any, config_file_contents: list[str], message_part: str) -> None:
"""Invalid configuration raises `SystemExit` with a precise error message."""
contents = "\n".join(config_file_contents).expandtabs(4)
with write_to_file(contents) as filename:
with pytest.raises(SystemExit, match="2"):
DjangoPluginConfig(filename)
error_message = "usage: " + TEMPLATE.format(message_part)
assert error_message == capsys.readouterr().err
@pytest.mark.parametrize(
"filename",
[
pytest.param(uuid.uuid4().hex, id="not matching an existing file"),
pytest.param("", id="as empty string"),
pytest.param(None, id="as none"),
],
)
def test_handles_filename(capsys: Any, filename: str) -> None:
with pytest.raises(SystemExit, match="2"):
DjangoPluginConfig(filename)
error_message = "usage: " + TEMPLATE.format("mypy config file is not specified or found")
assert error_message == capsys.readouterr().err
@pytest.mark.parametrize(
("config_file_contents", "message_part"),
[
pytest.param(
"""
[tool.django-stubs]
django_settings_module = 123
""",
"invalid 'django_settings_module': the setting must be a string",
id="django_settings_module not string",
),
pytest.param(
"""
[tool.not-really-django-stubs]
django_settings_module = "my.module"
""",
"no section [tool.django-stubs] found",
id="missing django-stubs section",
),
pytest.param(
"""
[tool.django-stubs]
not_django_not_settings_module = "badbadmodule"
""",
(
"missing required 'django_settings_module' config.\n"
"Either specify this config or set your `DJANGO_SETTINGS_MODULE` env var"
),
id="missing django_settings_module",
),
pytest.param(
"tool.django-stubs]",
"could not load configuration file",
id="invalid toml",
),
pytest.param(
"""
[tool.django-stubs]
django_settings_module = "some.module"
strict_settings = "a"
""",
"invalid 'strict_settings': the setting must be a boolean",
id="invalid strict_settings type",
),
pytest.param(
"""
[tool.django-stubs]
django_settings_module = "some.module"
strict_model_abstract_attrs = "a"
""",
"invalid 'strict_model_abstract_attrs': the setting must be a boolean",
id="invalid strict_model_abstract_attrs type",
),
],
)
def test_toml_misconfiguration_handling(capsys: Any, config_file_contents: str, message_part: str) -> None:
with write_to_file(config_file_contents, suffix=".toml") as filename:
with pytest.raises(SystemExit, match="2"):
DjangoPluginConfig(filename)
error_message = "usage: " + TEMPLATE_TOML.format(message_part)
assert error_message == capsys.readouterr().err
@pytest.mark.parametrize("boolean_value", ["true", "false"])
def test_correct_toml_configuration(boolean_value: str) -> None:
config_file_contents = f"""
[tool.django-stubs]
some_other_setting = "setting"
django_settings_module = "my.module"
strict_settings = {boolean_value}
"""
with write_to_file(config_file_contents, suffix=".toml") as filename:
config = DjangoPluginConfig(filename)
assert config.django_settings_module == "my.module"
assert config.strict_settings is (boolean_value == "true")
@pytest.mark.parametrize("boolean_value", ["true", "True", "false", "False"])
def test_correct_configuration(boolean_value: str) -> None:
"""Django settings module gets extracted given valid configuration."""
config_file_contents = "\n".join(
[
"[mypy.plugins.django-stubs]",
"some_other_setting = setting",
"django_settings_module = my.module",
f"strict_settings = {boolean_value}",
]
)
with write_to_file(config_file_contents) as filename:
config = DjangoPluginConfig(filename)
assert config.django_settings_module == "my.module"
assert config.strict_settings is (boolean_value.lower() == "true")
@pytest.mark.parametrize("boolean_value", ["true", "false"])
def test_correct_toml_configuration_with_django_setting_from_env(boolean_value: str) -> None:
config_file_contents = f"""
[tool.django-stubs]
some_other_setting = "setting"
strict_settings = {boolean_value}
"""
django_settings_env_value = "my.module"
with write_to_file(config_file_contents, suffix=".toml") as filename:
with mock.patch.dict(os.environ, {"DJANGO_SETTINGS_MODULE": django_settings_env_value}):
config = DjangoPluginConfig(filename)
assert config.django_settings_module == django_settings_env_value
assert config.strict_settings is (boolean_value == "true")
@pytest.mark.parametrize("boolean_value", ["true", "True", "false", "False"])
def test_correct_configuration_with_django_setting_from_env(boolean_value: str) -> None:
"""Django settings module gets extracted given valid configuration."""
config_file_contents = "\n".join(
[
"[mypy.plugins.django-stubs]",
"some_other_setting = setting",
f"strict_settings = {boolean_value}",
]
)
django_settings_env_value = "my.module"
with write_to_file(config_file_contents) as filename:
with mock.patch.dict(os.environ, {"DJANGO_SETTINGS_MODULE": django_settings_env_value}):
config = DjangoPluginConfig(filename)
assert config.django_settings_module == django_settings_env_value
assert config.strict_settings is (boolean_value.lower() == "true")
|