23307c3c5f
Files changed: - CHANGES.md - VERSION - instructions/dev/testing-conventions.md - tools/chemenu/lint_core.py - tools/chemenu/tests/conftest.py - tools/chemenu/tests/test_hermetic_env.py - tools/chemenu/tests/test_lint.py
97 lines
4.3 KiB
Python
97 lines
4.3 KiB
Python
"""The `hermetic_environment` fixture, tested as the guard it is.
|
|
|
|
Everything else in this suite depends on that fixture without ever mentioning
|
|
it - which is the point, and also the risk: a fixture nothing asserts against
|
|
can be weakened, or lose a variable, and every test stays green until the
|
|
suite next runs on a machine that has the variable set. So the guard gets its
|
|
own tests, and `config.default_author()` gets the coverage the fixture makes
|
|
writable for the first time: what it returns on a machine that knows nobody.
|
|
"""
|
|
import os
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
import chemenu.config as config
|
|
from chemenu.tests.conftest import _GIT_ENV, _WIKITOOL_ENV
|
|
|
|
|
|
def test_home_points_into_the_test_s_own_tmp_path(tmp_path: Path, hermetic_environment: Path):
|
|
assert hermetic_environment == tmp_path / "home"
|
|
assert Path(os.environ["HOME"]) == hermetic_environment
|
|
assert hermetic_environment.is_dir()
|
|
assert not list(hermetic_environment.iterdir())
|
|
|
|
|
|
def test_the_tool_s_own_environment_is_cleared():
|
|
for name in _WIKITOOL_ENV:
|
|
assert name not in os.environ, f"{name} leaked into the test environment"
|
|
for name in _GIT_ENV:
|
|
assert name not in os.environ, f"{name} leaked into the test environment"
|
|
|
|
|
|
def test_tracing_stays_on_and_redirected(isolated_trace_dir: Path):
|
|
"""`hermetic_environment` clears WIKI_TRACE, `isolated_trace_dir` sets
|
|
WIKI_TRACE_DIR - and the ordering between the two autouse fixtures must
|
|
leave both in that state, or the telemetry tests break."""
|
|
assert "WIKI_TRACE" not in os.environ # i.e. the default, which is on
|
|
assert Path(os.environ["WIKI_TRACE_DIR"]) == isolated_trace_dir
|
|
|
|
|
|
def test_git_sees_no_configuration_from_this_machine(tmp_path: Path):
|
|
"""The failure behind Gitea #8, asserted directly: outside a repository
|
|
with a local identity, `git config user.name` must answer nothing."""
|
|
result = subprocess.run(
|
|
["git", "config", "user.name"],
|
|
cwd=tmp_path, capture_output=True, text=True, check=False,
|
|
)
|
|
assert result.stdout.strip() == "", (
|
|
"global or system git configuration is still visible to the suite"
|
|
)
|
|
|
|
|
|
def test_default_author_is_none_without_an_identity(tmp_path: Path,
|
|
monkeypatch: pytest.MonkeyPatch):
|
|
"""The branch that used to be untestable: no WIKI_AUTHOR, no git identity
|
|
anywhere, so `new source` has nothing to stamp and must say so."""
|
|
monkeypatch.setattr(config, "ROOT", tmp_path)
|
|
assert config.default_author() is None
|
|
|
|
|
|
def test_default_author_reads_a_local_git_identity(tmp_path: Path,
|
|
monkeypatch: pytest.MonkeyPatch):
|
|
subprocess.run(["git", "init", "-q", "-b", "main"], cwd=tmp_path, check=True)
|
|
subprocess.run(["git", "config", "user.name", "Local Identity"], cwd=tmp_path, check=True)
|
|
monkeypatch.setattr(config, "ROOT", tmp_path)
|
|
assert config.default_author() == "Local Identity"
|
|
|
|
|
|
def test_wiki_author_overrides_the_git_identity(tmp_path: Path,
|
|
monkeypatch: pytest.MonkeyPatch):
|
|
subprocess.run(["git", "init", "-q", "-b", "main"], cwd=tmp_path, check=True)
|
|
subprocess.run(["git", "config", "user.name", "Local Identity"], cwd=tmp_path, check=True)
|
|
monkeypatch.setattr(config, "ROOT", tmp_path)
|
|
monkeypatch.setenv("WIKI_AUTHOR", "Env Override")
|
|
assert config.default_author() == "Env Override"
|
|
|
|
|
|
def test_kb_dir_repoints_the_configured_root_at_its_own_tree(kb_dir: Path, tmp_path: Path):
|
|
"""The other half of the isolation, and the one `kb_dir` was missing until
|
|
Gitea #44: a fixture that builds a corpus but leaves `config.ROOT` on the
|
|
real checkout hands every `config.KB_DIR` lookup the developer's own wiki -
|
|
which is how a test overwrote the repository's `.wikitool-kb.json`."""
|
|
assert config.ROOT == tmp_path
|
|
assert config.KB_DIR == kb_dir
|
|
|
|
|
|
def test_raw_dir_repoints_the_configured_root_at_its_own_tree(raw_dir: Path, tmp_path: Path):
|
|
assert config.ROOT == tmp_path
|
|
assert config.RAW_DIR == raw_dir
|
|
|
|
|
|
def test_both_corpus_fixtures_keep_the_shipped_type_specs_reachable(kb_dir: Path):
|
|
"""Repointing `ROOT` moves `TYPES_DIR` with it, so the repoint has to be
|
|
paired with `use_shipped_type_specs()` or no page type resolves at all."""
|
|
assert (config.TYPES_DIR / "entity.md").is_file()
|