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 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
|
import sys
import textwrap
from pathlib import Path
from typing import Any
from unittest import mock
import pytest
from packaging.version import Version
from sphinx_theme_builder._internal.project import (
ImproperProjectMetadata,
InvalidProjectStructure,
Project,
read_toml_file,
)
@mock.patch(f"{'tomllib' if sys.version_info > (3, 11) else 'tomli'}.load")
def test_read_toml_file(patched_load: mock.Mock, tmp_path: Path) -> None:
# GIVEN
file = tmp_path / "foo.toml"
file.write_text("")
# WHEN
read_toml_file(file)
# THEN
patched_load.assert_called_once()
assert patched_load.call_args[0][0].name == str(file)
class TestProjectFromPath:
def test_works_on_valid_pyproject(self, tmp_path: Path) -> None:
# GIVEN
(tmp_path / "pyproject.toml").write_text(
textwrap.dedent(
"""
[project]
name = 'magic-project'
version = '1.0'
license = { text = "MIT" }
[tool.sphinx-theme-builder]
node-version = "16.13.0"
"""
)
)
(tmp_path / "src" / "magic_project").mkdir(parents=True)
(tmp_path / "src" / "magic_project" / "__init__.py").write_text("")
# WHEN
project = Project.from_path(tmp_path)
# THEN
assert project.snake_name == "magic_project"
assert project.kebab_name == "magic-project"
assert project.location == tmp_path
assert project.metadata
assert project.metadata.version == Version("1.0")
assert project.python_package_path == tmp_path / "src" / "magic_project"
assert project.theme_path == (
tmp_path / "src" / "magic_project" / "theme" / "magic-project"
)
assert project.theme_conf_path == (
tmp_path
/ "src"
/ "magic_project"
/ "theme"
/ "magic-project"
/ "theme.conf"
)
assert project.theme_static_path == (
tmp_path / "src" / "magic_project" / "theme" / "magic-project" / "static"
)
assert project.output_script_path == (
project.theme_static_path / "scripts" / "magic-project.js"
)
assert project.output_stylesheet_path == (
project.theme_static_path / "styles" / "magic-project.css"
)
assert project.assets_path == tmp_path / "src" / "magic_project" / "assets"
assert project.input_stylesheets_path == (
tmp_path / "src" / "magic_project" / "assets" / "styles"
)
assert project.input_scripts_path == (
tmp_path / "src" / "magic_project" / "assets" / "scripts"
)
def test_rejects_without_pyproject_toml(self, tmp_path: Path) -> None:
# GIVEN
# an empty directory
# WHEN
with pytest.raises(InvalidProjectStructure) as ctx:
Project.from_path(tmp_path)
# THEN
error = ctx.value
assert "Could not find a `pyproject.toml`" in error.message
assert error.context
assert str(tmp_path) in error.context
assert error.reference == "pyproject-missing"
def test_rejects_on_invalid_pyproject_toml(self, tmp_path: Path) -> None:
# GIVEN
(tmp_path / "pyproject.toml").write_text("key = value")
# WHEN
with pytest.raises(ImproperProjectMetadata) as ctx:
Project.from_path(tmp_path)
# THEN
error = ctx.value
assert "Could not parse `pyproject.toml`" in error.message
assert error.context
assert str(tmp_path) in error.context
assert "pyproject.toml" in error.context
assert "line 1, column 7" in error.context
assert error.reference == "pyproject-could-not-parse"
def test_rejects_without_project_table(self, tmp_path: Path) -> None:
# GIVEN
(tmp_path / "pyproject.toml").write_text("key = 1")
# WHEN
with pytest.raises(ImproperProjectMetadata) as ctx:
Project.from_path(tmp_path)
# THEN
error = ctx.value
assert "Could not find [project] table" in error.message
assert error.context
assert str(tmp_path) in error.context
assert "pyproject.toml" in error.context
assert error.reference == "pyproject-no-project-table"
def test_rejects_without_name(self, tmp_path: Path) -> None:
# GIVEN
(tmp_path / "pyproject.toml").write_text("[project]\nkey = 1")
# WHEN
with pytest.raises(ImproperProjectMetadata) as ctx:
Project.from_path(tmp_path)
# THEN
error = ctx.value
assert "Could not find `name`" in error.message
assert "[project] table" in error.message
assert error.context
assert "pyproject.toml" in error.context
assert error.reference == "pyproject-no-name-in-project-table"
def test_rejects_non_canonical_names(self, tmp_path: Path) -> None:
# GIVEN
(tmp_path / "pyproject.toml").write_text("[project]\nname = 'MAGIC'")
# WHEN
with pytest.raises(ImproperProjectMetadata) as ctx:
Project.from_path(tmp_path)
# THEN
error = ctx.value
assert (
"Found non-canonical `name` declared in the [project] table"
in error.message
)
assert error.context
assert "pyproject.toml" in error.context
assert error.reference == "pyproject-non-canonical-name"
def test_works_with_proper_static_version(self, tmp_path: Path) -> None:
# GIVEN
(tmp_path / "pyproject.toml").write_text(
textwrap.dedent(
"""
[project]
name = "magic"
version = "0.1.2"
license = { text = "MIT" }
[tool.sphinx-theme-builder]
node-version = "16.13.0"
"""
)
)
(tmp_path / "src" / "magic").mkdir(parents=True)
(tmp_path / "src" / "magic" / "__init__.py").write_text("")
# WHEN
project = Project.from_path(tmp_path)
# THEN
assert project.metadata.version == Version("0.1.2")
def test_works_with_proper_dynamic_version(self, tmp_path: Path) -> None:
# GIVEN
(tmp_path / "pyproject.toml").write_text(
textwrap.dedent(
"""
[project]
name = "magic"
dynamic = ["version"]
license = { text = "MIT" }
[tool.sphinx-theme-builder]
node-version = "16.13.0"
"""
)
)
(tmp_path / "src" / "magic").mkdir(parents=True)
(tmp_path / "src" / "magic" / "__init__.py").write_text(
textwrap.dedent(
"""
version = "2.3.4" # not really, we just ignore this.
__version__ = "1.2.3"
"""
)
)
# WHEN
project = Project.from_path(tmp_path)
# THEN
assert project.metadata.version == Version("1.2.3")
def test_rejects_when_no_version_is_declared(self, tmp_path: Path) -> None:
# GIVEN
(tmp_path / "pyproject.toml").write_text(
textwrap.dedent(
"""
[project]
name = "magic"
"""
)
)
(tmp_path / "src" / "magic").mkdir(parents=True)
(tmp_path / "src" / "magic" / "__init__.py").write_text("")
# WHEN
with pytest.raises(InvalidProjectStructure) as ctx:
Project.from_path(tmp_path)
# THEN
error = ctx.value
assert "No version declaration found for project." in error.message
assert error.context
assert "__init__.py" in error.context
assert "pyproject.toml" in error.context
assert error.hint_stmt
assert "forget" in error.hint_stmt
assert "?" in error.hint_stmt
assert error.reference == "project-no-version-declaration"
def test_rejects_when_no_python_file_is_available(self, tmp_path: Path) -> None:
# GIVEN
(tmp_path / "pyproject.toml").write_text(
textwrap.dedent(
"""
[project]
name = "magic"
version = "1.0.0"
"""
)
)
# WHEN
with pytest.raises(InvalidProjectStructure) as ctx:
Project.from_path(tmp_path)
# THEN
error = ctx.value
assert "__init__.py is missing" in error.message
assert error.context
assert "project name is magic" in error.context
assert error.reference == "project-init-missing"
def test_rejects_with_double_declaration(self, tmp_path: Path) -> None:
# GIVEN
(tmp_path / "pyproject.toml").write_text(
textwrap.dedent(
"""
[project]
name = "magic"
version = "1.2.3"
"""
)
)
(tmp_path / "src" / "magic").mkdir(parents=True)
(tmp_path / "src" / "magic" / "__init__.py").write_text('__version__ = "2.3.4"')
# WHEN
with pytest.raises(InvalidProjectStructure) as ctx:
Project.from_path(tmp_path)
# THEN
error = ctx.value
assert "Found version declaration in both" in error.message
assert "pyproject.toml" in error.message
assert "__init__.py" in error.message
assert error.reference == "project-double-version-declaration"
def test_rejects_dynamic_with_version_in_pyproject(self, tmp_path: Path) -> None:
# GIVEN
(tmp_path / "pyproject.toml").write_text(
textwrap.dedent(
"""
[project]
name = "magic"
version = "2.3.4"
dynamic = ["version"]
"""
)
)
(tmp_path / "src" / "magic").mkdir(parents=True)
(tmp_path / "src" / "magic" / "__init__.py").write_text("")
# WHEN
with pytest.raises(ImproperProjectMetadata) as ctx:
Project.from_path(tmp_path)
# THEN
error = ctx.value
assert "dynamic" in error.message
assert "version" in error.message
assert "pyproject.toml" in error.message
assert error.context
assert "2.3.4" in error.context
assert error.hint_stmt
assert "removing `version`" in error.hint_stmt
assert error.reference == "project-improper-dynamic-version"
def test_rejects_no_dynamic_with_version_in_python_file(
self, tmp_path: Path
) -> None:
# GIVEN
(tmp_path / "pyproject.toml").write_text(
textwrap.dedent(
"""
[project]
name = "magic"
"""
)
)
(tmp_path / "src" / "magic").mkdir(parents=True)
(tmp_path / "src" / "magic" / "__init__.py").write_text('__version__ = "1.2.3"')
# WHEN
with pytest.raises(ImproperProjectMetadata) as ctx:
Project.from_path(tmp_path)
# THEN
error = ctx.value
assert "Found version in `__init__.py`" in error.message
assert '"version" in `project.dynamic' in error.message
assert error.context
assert "1.2.3" in error.context
assert error.hint_stmt
assert error.reference == "project-missing-dynamic-version"
@pytest.mark.parametrize("value", [1, 1.0, []])
def test_rejects_non_string_static_versions_toml(
self, tmp_path: Path, value: Any
) -> None:
# GIVEN
(tmp_path / "pyproject.toml").write_text(
textwrap.dedent(
f"""
[project]
name = "magic"
version = {value}
"""
)
)
(tmp_path / "src" / "magic").mkdir(parents=True)
(tmp_path / "src" / "magic" / "__init__.py").write_text("")
# WHEN
with pytest.raises(ImproperProjectMetadata) as ctx:
Project.from_path(tmp_path)
# THEN
error = ctx.value
assert "Expected " in error.message
assert "a string" in error.message
assert error.context
assert f"Got {value}" in error.context
assert f"{type(value)}" in error.context
assert error.reference == "pyproject-invalid-version"
def test_rejects_invalid_versions(self, tmp_path: Path) -> None:
# GIVEN
(tmp_path / "pyproject.toml").write_text(
textwrap.dedent(
"""
[project]
name = "magic"
version = "asdf"
"""
)
)
(tmp_path / "src" / "magic").mkdir(parents=True)
(tmp_path / "src" / "magic" / "__init__.py").write_text("")
# WHEN
with pytest.raises(ImproperProjectMetadata) as ctx:
Project.from_path(tmp_path)
# THEN
error = ctx.value
assert "invalid version" in error.message
assert "asdf" in error.message
assert error.context
assert "pyproject.toml" in error.context
assert error.reference == "project-invalid-version"
def test_rejects_invalid_python_files(self, tmp_path: Path) -> None:
# GIVEN
(tmp_path / "pyproject.toml").write_text(
textwrap.dedent(
"""
[project]
name = "magic"
dynamic = ["version"]
"""
)
)
(tmp_path / "src" / "magic").mkdir(parents=True)
(tmp_path / "src" / "magic" / "__init__.py").write_text(")")
# WHEN
with pytest.raises(InvalidProjectStructure) as ctx:
Project.from_path(tmp_path)
# THEN
error = ctx.value
assert "Could not parse" in error.message
assert error.context
assert "__init__.py" in error.context
assert "SyntaxError" in error.context
assert error.reference == "project-init-invalid-syntax"
|