51 lines
2.6 KiB
Python
51 lines
2.6 KiB
Python
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
from site_engine.web.config import SitePaths
|
|
|
|
DOCKER_ENV = {"SITE_ROOT": "/data/site", "SITE_DIST": "/data/dist", "SITE_STATE_DIR": "/data/state"}
|
|
OLD_ENV_EXAMPLE = {"SITE_ROOT": "examples/studio-site", "SITE_DIST": "dist"} # строки, которые когда-то были включены в .env.example
|
|
|
|
|
|
class SitePathsTest(unittest.TestCase):
|
|
def test_docker_defaults_resolve_inside_the_data_volume(self):
|
|
p = SitePaths.from_env(DOCKER_ENV, cwd=Path("/app"))
|
|
self.assertEqual((p.root, p.dist, p.state), (Path("/data/site"), Path("/data/dist"), Path("/data/state")))
|
|
self.assertEqual(p.relative, ())
|
|
|
|
def test_leftover_relative_paths_from_old_env_are_called_out(self):
|
|
env = {**DOCKER_ENV, **OLD_ENV_EXAMPLE}
|
|
p = SitePaths.from_env(env, cwd=Path("/app"))
|
|
self.assertEqual((p.root, p.dist), (Path("/app/examples/studio-site"), Path("/app/dist")))
|
|
text = "\n".join(p.problems(env))
|
|
for needle in ("SITE_ROOT=examples/studio-site", "/app/examples/studio-site", "SITE_DIST=dist", "/app/dist",
|
|
"Удалите строку из .env", "каталога сайта нет"):
|
|
self.assertIn(needle, text)
|
|
|
|
def test_state_defaults_to_dot_engine_inside_site(self):
|
|
p = SitePaths.from_env({"SITE_ROOT": "/srv/site", "SITE_DIST": "/srv/dist"}, cwd=Path("/app"))
|
|
self.assertEqual(p.state, Path("/srv/site/.engine"))
|
|
|
|
def test_no_problems_for_a_healthy_layout(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
(Path(tmp) / "site").mkdir()
|
|
(Path(tmp) / "site" / "site.yaml").write_text("name: x\n", encoding="utf-8")
|
|
env = {"SITE_ROOT": f"{tmp}/site", "SITE_DIST": f"{tmp}/dist", "SITE_STATE_DIR": f"{tmp}/state"}
|
|
self.assertEqual(SitePaths.from_env(env).problems(env), [])
|
|
|
|
def test_missing_site_yaml_and_dist_inside_root(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
env = {"SITE_ROOT": tmp, "SITE_DIST": f"{tmp}/dist"}
|
|
text = "\n".join(SitePaths.from_env(env).problems(env))
|
|
self.assertIn("нет site.yaml", text)
|
|
self.assertIn("внутри SITE_ROOT", text)
|
|
|
|
def test_relative_paths_resolve_against_cwd_for_local_runs(self):
|
|
p = SitePaths.from_env({"SITE_ROOT": "examples/studio-site", "SITE_DIST": "dist"}, cwd=Path("/work/project"))
|
|
self.assertEqual((p.root, p.dist), (Path("/work/project/examples/studio-site"), Path("/work/project/dist")))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|