Project: pygitweb

import json
from pathlib import Path
import pytest
from pygitweb.config import Settings
from pygitweb.settings_config import init_settings, settings_batch_update, write_settings_file
@pytest.fixture
def settings_path(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
path = tmp_path / "settings.json"
monkeypatch.setenv("PYGITWEB_SETTINGS_CONFIG", str(path))
monkeypatch.setenv("PYGITWEB_AUTH", "0")
return path
def test_creates_settings_file_from_defaults_not_env(settings_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("PYGITWEB_SITE_NAME", "FromEnv")
settings = init_settings()
assert settings_path.is_file()
data = json.loads(settings_path.read_text(encoding="utf-8"))
assert data["SITE_NAME"] == "PyGitWeb"
assert data["AUTH"] is True
assert settings.SITE_NAME == "FromEnv"
assert settings.AUTH is False
def test_loads_existing_settings_file(settings_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
settings_path.write_text(
json.dumps({
"PROJECTROOT": "/var/git",
"PROJECTS_LIST": "/var/git",
"PROJECT_MAXDEPTH": 2,
"SITE_NAME": "FromFile",
"EXPORT_OK": "",
"LIST_ALL": True,
"STRICT_EXPORT": False,
"GIT": "git",
"MAXLOAD": None,
"AUTH": False,
"AUTH_CONFIG": "",
"SETTINGS_CONFIG": "",
})
+ "\n",
encoding="utf-8",
)
monkeypatch.delenv("PYGITWEB_SITE_NAME", raising=False)
settings = init_settings()
assert settings.SITE_NAME == "FromFile"
assert settings.PROJECT_MAXDEPTH == 2
def test_env_overrides_settings_file_ephemerally(
settings_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
) -> None:
settings_path.write_text(
json.dumps({
"PROJECTROOT": "/var/git",
"PROJECTS_LIST": "/var/git",
"PROJECT_MAXDEPTH": 1,
"SITE_NAME": "FromFile",
"EXPORT_OK": "",
"LIST_ALL": True,
"STRICT_EXPORT": False,
"GIT": "git",
"MAXLOAD": None,
"AUTH": True,
"AUTH_CONFIG": "",
"SETTINGS_CONFIG": "",
})
+ "\n",
encoding="utf-8",
)
monkeypatch.setenv("PYGITWEB_SITE_NAME", "FromEnv")
monkeypatch.setenv("PYGITWEB_AUTH", "0")
settings = init_settings()
assert settings.SITE_NAME == "FromEnv"
assert settings.AUTH is False
data = json.loads(settings_path.read_text(encoding="utf-8"))
assert data["SITE_NAME"] == "FromFile"
assert data["AUTH"] is True
err = capsys.readouterr().err
assert "environment overrides settings file" in err
assert "not saved to the file" in err
assert "SITE_NAME" in err
assert "AUTH" in err
def test_env_auth_does_not_stick_after_unset(settings_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
settings_path.write_text(
json.dumps({
"PROJECTROOT": "/var/git",
"PROJECTS_LIST": "/var/git",
"PROJECT_MAXDEPTH": 1,
"SITE_NAME": "FromFile",
"EXPORT_OK": "",
"LIST_ALL": True,
"STRICT_EXPORT": False,
"GIT": "git",
"MAXLOAD": None,
"AUTH": True,
"AUTH_CONFIG": "",
"SETTINGS_CONFIG": "",
})
+ "\n",
encoding="utf-8",
)
monkeypatch.setenv("PYGITWEB_AUTH", "0")
assert init_settings().AUTH is False
assert json.loads(settings_path.read_text(encoding="utf-8"))["AUTH"] is True
monkeypatch.delenv("PYGITWEB_AUTH", raising=False)
assert init_settings().AUTH is True
def test_setattr_persists_to_file_without_env_overrides(settings_path: Path) -> None:
settings = init_settings()
assert settings.AUTH is False
settings.SITE_NAME = "Updated"
data = json.loads(settings_path.read_text(encoding="utf-8"))
assert data["SITE_NAME"] == "Updated"
assert data["AUTH"] is True
def test_batch_update_persists_once(settings_path: Path) -> None:
settings = init_settings()
with settings_batch_update(settings):
settings.SITE_NAME = "BatchOne"
settings.GIT = "/usr/bin/git"
data = json.loads(settings_path.read_text(encoding="utf-8"))
assert data["SITE_NAME"] == "BatchOne"
assert data["GIT"] == "/usr/bin/git"
assert data["AUTH"] is True
def test_write_settings_file_roundtrip(settings_path: Path) -> None:
settings = Settings(AUTH=False, SITE_NAME="Roundtrip")
write_settings_file(settings, settings_path)
loaded = Settings.model_validate_json(settings_path.read_text(encoding="utf-8"))
assert loaded.SITE_NAME == "Roundtrip"
assert loaded.AUTH is False
def test_enabled_plugins_roundtrip(settings_path: Path) -> None:
settings = init_settings()
assert settings.ENABLED_PLUGINS == []
settings.ENABLED_PLUGINS = ["pygitweb_demo", "pygitweb_other"]
data = json.loads(settings_path.read_text(encoding="utf-8"))
assert data["ENABLED_PLUGINS"] == ["pygitweb_demo", "pygitweb_other"]
reloaded = Settings.model_validate_json(settings_path.read_text(encoding="utf-8"))
assert reloaded.ENABLED_PLUGINS == ["pygitweb_demo", "pygitweb_other"]