fix: catch misconfiguration of site_dir and docs_dir (#780)

Signed-off-by: Timothée Mazzucotelli <dev@pawamoy.fr>
This commit is contained in:
Timothée Mazzucotelli
2026-07-03 11:56:58 +02:00
committed by GitHub
parent 30afc96725
commit 7158eab75a
2 changed files with 148 additions and 14 deletions
+118
View File
@@ -0,0 +1,118 @@
# Copyright (c) 2025-2026 Zensical and contributors
# SPDX-License-Identifier: MIT
# All contributions are certified under the DCO
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
from __future__ import annotations
from textwrap import dedent
from typing import TYPE_CHECKING
import pytest
from zensical.config import ConfigurationError, parse_config
if TYPE_CHECKING:
from pathlib import Path
def test_site_dir_cant_be_empty(tmp_path: Path) -> None:
tmp_path.joinpath("docs").mkdir()
config_file = tmp_path / "zensical.toml"
config_file.write_text(
dedent("""
[project]
site_name = "test"
site_dir = ""
""")
)
with pytest.raises(ConfigurationError, match="empty"):
parse_config(str(config_file))
def test_site_dir_cant_go_up(tmp_path: Path) -> None:
tmp_path.joinpath("docs").mkdir()
config_file = tmp_path / "zensical.toml"
config_file.write_text(
dedent("""
[project]
site_name = "test"
site_dir = "../site"
""")
)
with pytest.raises(ConfigurationError, match="within"):
parse_config(str(config_file))
def test_docs_dir_cant_be_empty(tmp_path: Path) -> None:
tmp_path.joinpath("docs").mkdir()
config_file = tmp_path / "zensical.toml"
config_file.write_text(
dedent("""
[project]
site_name = "test"
docs_dir = ""
""")
)
with pytest.raises(ConfigurationError, match="empty"):
parse_config(str(config_file))
def test_docs_dir_cant_go_up(tmp_path: Path) -> None:
tmp_path.joinpath("docs").mkdir()
config_file = tmp_path / "zensical.toml"
config_file.write_text(
dedent("""
[project]
site_name = "test"
docs_dir = "../docs"
""")
)
with pytest.raises(ConfigurationError, match="within"):
parse_config(str(config_file))
def test_docs_dir_must_exist(tmp_path: Path) -> None:
config_file = tmp_path / "zensical.toml"
config_file.write_text(
dedent("""
[project]
site_name = "test"
docs_dir = "docs"
""")
)
with pytest.raises(ConfigurationError, match="does not exist"):
parse_config(str(config_file))
def test_site_dir_docs_dir_cant_be_equal(tmp_path: Path) -> None:
tmp_path.joinpath("docs").mkdir()
config_file = tmp_path / "zensical.toml"
config_file.write_text(
dedent("""
[project]
site_name = "test"
site_dir = "same"
docs_dir = "same"
""")
)
with pytest.raises(ConfigurationError, match="must be different"):
parse_config(str(config_file))
+30 -14
View File
@@ -368,27 +368,43 @@ def _apply_defaults(config: dict, path: str) -> dict:
We must set all properties, as well as nested properties to `None`, or PyO3
will refuse to convert them, as the key must definitely exist.
"""
project_root = config["root_dir"] = os.path.dirname(path)
config["root_dir"] = os.path.dirname(path)
project_root = Path(config["root_dir"]).resolve()
if "site_name" not in config:
raise ConfigurationError("Missing required setting: site_name")
# Set site directory
set_default(config, "site_dir", "site", str)
if ".." in config.get("site_dir", ""):
raise ConfigurationError("site_dir must not contain '..'")
if config["site_dir"] == "":
raise ConfigurationError("site_dir must not be empty")
site_dir = Path(config["site_dir"])
if site_dir.is_absolute():
site_dir = site_dir.resolve()
else:
site_dir = project_root.joinpath(site_dir).resolve()
if not site_dir.is_relative_to(project_root):
raise ConfigurationError("site_dir must be within project root")
# Set docs directory
set_default(config, "docs_dir", "docs", str)
if ".." in config.get("docs_dir", ""):
raise ConfigurationError("docs_dir must not contain '..'")
if config["docs_dir"] == "":
raise ConfigurationError("docs_dir must not be empty")
docs_dir = Path(config["docs_dir"])
if docs_dir.is_absolute():
docs_dir = docs_dir.resolve()
else:
docs_dir = project_root.joinpath(docs_dir).resolve()
if not docs_dir.is_relative_to(project_root):
raise ConfigurationError("docs_dir must be within project root")
# Validate that site_dir is not the same as docs_dir
if site_dir == docs_dir:
raise ConfigurationError("site_dir and docs_dir must be different")
# Validate that docs directory exists
docs_dir_path = os.path.join(project_root, config["docs_dir"])
if not os.path.isdir(docs_dir_path):
raise ConfigurationError(
f"Docs directory does not exist: {docs_dir_path}"
)
if not docs_dir.is_dir():
raise ConfigurationError(f"Docs directory does not exist: {docs_dir}")
# Set defaults for core settings
set_default(config, "site_url", None, str)
@@ -414,16 +430,16 @@ def _apply_defaults(config: dict, path: str) -> dict:
set_default(config, "edit_uri", None, str)
# Set defaults for repository name settings
docs_dir = config.get("docs_dir")
repo_names = {
"github.com": "GitHub",
"gitlab.com": "Gitlab",
"bitbucket.org": "Bitbucket",
}
rel_docs_dir = docs_dir.relative_to(project_root)
edit_uris = {
"github.com": f"edit/master/{docs_dir}",
"gitlab.com": f"edit/master/{docs_dir}",
"bitbucket.org": f"src/default/{docs_dir}",
"github.com": f"edit/master/{rel_docs_dir}",
"gitlab.com": f"edit/master/{rel_docs_dir}",
"bitbucket.org": f"src/default/{rel_docs_dir}",
}
repo_url = config.get("repo_url")
if repo_url: