tankslab.com/logbook/source

tests/test_rename_migration.py

79 lines, exactly as they ship in the download.

"""test_rename_migration.py - the Recall -> Logbook rename never leaves anyone's data behind. Each case runs paths.root() in a fresh process against a throwaway XDG_DATA_HOME. """ import os import subprocess import sys import tempfile from pathlib import Path BIN = Path(__file__).resolve().parent.parent / "bin" OLD = "friday-" + "recall" OLD_DB = "recall" + ".db" fails = 0 def root_in(data_home, extra_env=None): env = dict(os.environ, XDG_DATA_HOME=str(data_home), HOME=str(data_home)) env.pop("FRIDAY_LOGBOOK_ROOT", None) env.update(extra_env or {}) r = subprocess.run([sys.executable, "-B", "-c", "import paths; print(paths.root())"], cwd=BIN, env=env, capture_output=True, text=True) return r.stdout.strip(), r.returncode def check(name, ok): global fails print((" ok " if ok else " FAIL ") + name) fails += not ok if sys.platform.startswith(("win", "darwin")): print(" skip (XDG layout is the Linux path; the rename logic is shared)") sys.exit(0) with tempfile.TemporaryDirectory() as t: d = Path(t) (d / OLD).mkdir() (d / OLD / OLD_DB).write_text("rows") (d / OLD / (OLD_DB + "-wal")).write_text("wal") (d / OLD / "baton").mkdir() out, rc = root_in(d) new = d / "friday-logbook" check("old folder renamed to friday-logbook", rc == 0 and out == str(new) and new.is_dir() and not (d / OLD).exists()) check("database files left under their own name, WAL beside it (never split from it)", (new / OLD_DB).read_text() == "rows" and (new / (OLD_DB + "-wal")).read_text() == "wal" and not (new / "logbook.db").exists()) r = subprocess.run([sys.executable, "-B", "-c", "import paths; print(paths.db())"], cwd=BIN, env=dict(os.environ, XDG_DATA_HOME=str(d), HOME=str(d)), capture_output=True, text=True) check("db() opens the migrated database under its old name", r.stdout.strip() == str(new / OLD_DB)) check("everything else carried over", (new / "baton").is_dir()) with tempfile.TemporaryDirectory() as t: d = Path(t) (d / OLD).mkdir() (d / OLD / OLD_DB).write_text("old") (d / "friday-logbook").mkdir() (d / "friday-logbook" / "logbook.db").write_text("new") root_in(d) check("both exist: never merges or overwrites", (d / OLD / OLD_DB).read_text() == "old" and (d / "friday-logbook" / "logbook.db").read_text() == "new") with tempfile.TemporaryDirectory() as t: d = Path(t) (d / OLD).mkdir() (d / OLD / OLD_DB).write_text("old") out, _ = root_in(d, {"FRIDAY_LOGBOOK_ROOT": str(d / "elsewhere")}) check("explicit FRIDAY_LOGBOOK_ROOT: old folder untouched", out == str(d / "elsewhere") and (d / OLD).is_dir()) with tempfile.TemporaryDirectory() as t: d = Path(t) out, _ = root_in(d) check("fresh machine: no folder invented", out == str(d / "friday-logbook") and not any(d.iterdir())) r = subprocess.run([sys.executable, "-B", "-c", "import paths; print(paths.db())"], cwd=BIN, env=dict(os.environ, XDG_DATA_HOME=str(d), HOME=str(d)), capture_output=True, text=True) check("fresh machine: db() is logbook.db", r.stdout.strip() == str(d / "friday-logbook" / "logbook.db")) print(" ---- rename migration: %d failed ----" % fails) sys.exit(1 if fails else 0)