diff --git a/.template_sync/manifest.yaml b/.template_sync/manifest.yaml index 7de3f90..1d15cd1 100644 --- a/.template_sync/manifest.yaml +++ b/.template_sync/manifest.yaml @@ -1,4 +1,4 @@ -template_version: 0.1.32 +template_version: 0.1.33 migrations: - from: 0.0.0 to: 0.0.1 @@ -102,3 +102,6 @@ migrations: - from: 0.1.31 to: 0.1.32 script: migrations/v0_1_31_to_v0_1_32.py + - from: 0.1.32 + to: 0.1.33 + script: migrations/v0_1_32_to_v0_1_33.py diff --git a/.template_sync/migrations/v0_1_32_to_v0_1_33.py b/.template_sync/migrations/v0_1_32_to_v0_1_33.py new file mode 100644 index 0000000..5e9e450 --- /dev/null +++ b/.template_sync/migrations/v0_1_32_to_v0_1_33.py @@ -0,0 +1,118 @@ +"""Template 0.1.32 -> 0.1.33: Tools v0.3.0-alpha.3 (Fiji opens results of 3 or 4 planes); the Windows Apps list shows the right version and icon. + +Design note: https://github.com/CellMigrationLab/LabConstrictor/blob/main/.tools/docs/migration_0_1_33.md + +Tools v0.3.0-alpha.2 wrote a result image of 3 or 4 planes (3 channels, 3 timepoints) as one page of colour samples, which Fiji cannot open +unless it is 8-bit (LabConstrictor-Tools #163); v0.3.0-alpha.3 writes it as grey pages. What this changes in an app repository (each step is +independent, idempotent and never deletes anything): + +1. `LC_TOOLS_PIN` in app/bash_bat_scripts/post_install.sh and post_install.bat moves from `v0.3.0-alpha.2` to `v0.3.0-alpha.3`, ONLY when the + line still holds exactly the old template value. An app with its own pin, a script without a pin line or without the Tools hook stays as it is, + with a note. + +2. post_install.bat registered the app in the Windows Apps list with the version written at initialisation (`DISPLAY_VERSION`; no + release ever changed it) and the icon `logo.ico`, a file no installer contains. The version is now read at install time from the + bundled construct.yaml (the setup-time value stays as a fallback), and the icon is the first .ico the installer copied into the app + folder; without one, no icon entry is written. Only the template's exact lines are replaced; a changed script stays as it is, with a note. + +Since template 0.1.32 the Tools guide no longer names the pin, so no document changes. Both changes reach users with the app's next release. +""" +from __future__ import annotations + +import re +from pathlib import Path +from typing import Any + + +DOCS_URL = "https://github.com/CellMigrationLab/LabConstrictor/blob/main/.tools/docs/migration_0_1_33.md" +TAG = "[migration 0.1.33]" +OLD_PIN = "v0.3.0-alpha.2" +NEW_PIN = "v0.3.0-alpha.3" +POST_INSTALL_SH = Path("app/bash_bat_scripts/post_install.sh") +POST_INSTALL_BAT = Path("app/bash_bat_scripts/post_install.bat") +SH_PIN_LINE = re.compile(r'^([ \t]*LC_TOOLS_PIN=")([^"]*)(".*)$') +# The Windows Apps list block of post_install.bat (CRLF in the script; matched without line endings). The app's own setup-time version is kept. +OLD_ARP = re.compile(r'^SET "DISPLAY_ICON=%PROJECT_ROOT%\\logo\.ico"\r?\nSET "DISPLAY_VERSION=([^"\r\n]*)"(?=\r?\n)', re.MULTILINE) +NEW_ARP_LINES = ( + "REM Windows Apps list: the version of this installer (the version: line of the bundled construct.yaml, written by each release),", + "REM and the app icon the installer copied (the first .ico in the app folder); no icon entry when there is none.", + 'SET "DISPLAY_VERSION={version}"', + 'IF EXIST "%PROJECT_ROOT%\\construct.yaml" FOR /F "usebackq tokens=1,* delims=: " %%A IN (`findstr /B /C:"version:" "%PROJECT_ROOT%\\construct.yaml"`) DO SET "DISPLAY_VERSION=%%~B"', + 'SET "DISPLAY_ICON="', + 'FOR %%I IN ("%PROJECT_ROOT%\\*.ico") DO IF NOT DEFINED DISPLAY_ICON SET "DISPLAY_ICON=%%~fI"', +) +OLD_ICON_REG = 'reg add "%ARP_KEY%" /v DisplayIcon /d "%DISPLAY_ICON%" /f >> "%LOG_FILE%" 2>&1' +NEW_ICON_REG = "IF DEFINED DISPLAY_ICON " + OLD_ICON_REG +BAT_PIN_LINE = re.compile(r'^([ \t]*SET "LC_TOOLS_PIN=)([^"]*)(".*)$', re.IGNORECASE) + + +def log(message: str) -> None: + print(f"{TAG} {message}") + + +def read(path: Path) -> str: + """The file's text with its line endings as they are (bytes decoded as UTF-8).""" + return path.read_bytes().decode("utf-8") + + +def write(path: Path, text: str) -> None: + path.write_bytes(text.encode("utf-8")) + + +def move_pin(path: Path, pattern: re.Pattern[str]) -> str: + text = read(path) + if "labconstrictor_tools" not in text: + return "has no Tools bridge hook: nothing to move" + lines = text.splitlines(keepends=True) + hits = [(index, match) for index, line in enumerate(lines) if (match := pattern.match(line.rstrip("\r\n")))] + if not hits: + return "has no LC_TOOLS_PIN line (unpinned source or its own Tools source): left as it is" + if len(hits) > 1: + return f"has {len(hits)} LC_TOOLS_PIN lines: left as it is, set the pin by hand to {NEW_PIN}" + index, match = hits[0] + current = match.group(2) + if current == NEW_PIN: + return f"already pins Tools {NEW_PIN}: unchanged" + if current != OLD_PIN: + return f"has its own pin {current}: left as it is (the template pin is now {NEW_PIN})" + ending = lines[index][len(lines[index].rstrip("\r\n")) :] + lines[index] = f"{match.group(1)}{NEW_PIN}{match.group(3)}{ending}" + write(path, "".join(lines)) + return f"updated: Tools pin {OLD_PIN} -> {NEW_PIN}" + + +def fix_apps_list(path: Path) -> str: + text = read(path) + if NEW_ICON_REG in text: + return "already reads the Apps-list version and icon at install time: unchanged" + match = OLD_ARP.search(text) + if match is None or text.count(OLD_ICON_REG) != 1: + return "has no template Apps-list block (changed by hand): left as it is" + newline = "\r\n" if "\r\n" in text else "\n" + block = newline.join(NEW_ARP_LINES).replace("{version}", match.group(1)) + text = text[: match.start()] + block + text[match.end() :] + write(path, text.replace(OLD_ICON_REG, NEW_ICON_REG)) + return "updated: the Windows Apps list shows the installed version and the app icon" + + +def migrate(repo_root: Path, context: dict[str, Any]) -> None: + """Move the Tools pin to v0.3.0-alpha.3 and fix the Windows Apps-list entry (idempotent, surgical, never raises for a customised file).""" + _ = context + changed = 0 + steps = ((POST_INSTALL_SH, lambda p: move_pin(p, SH_PIN_LINE)), (POST_INSTALL_BAT, lambda p: move_pin(p, BAT_PIN_LINE)), + (POST_INSTALL_BAT, fix_apps_list)) + for relative, step in steps: + path = repo_root / relative + if not path.is_file(): + log(f"{relative}: not present in this repository (nothing was created)") + continue + try: + status = step(path) + except (OSError, UnicodeDecodeError) as exc: + status = f"SKIPPED: {exc}; left unchanged. See {DOCS_URL}" + changed += status.startswith("updated") + log(f"{relative}: {status}") + if changed: + log(f"done: {changed} change(s). They reach users with the next release of the app. See {DOCS_URL}") + else: + log("done: no repository changes were required.") diff --git a/.tools/docs/migration_0_1_33.md b/.tools/docs/migration_0_1_33.md new file mode 100644 index 0000000..e63a090 --- /dev/null +++ b/.tools/docs/migration_0_1_33.md @@ -0,0 +1,25 @@ +# Template 0.1.33: Tools v0.3.0-alpha.3, and the right version and icon in the Windows Apps list + +Design note for the 0.1.33 migration (`.template_sync/migrations/v0_1_32_to_v0_1_33.py`). Context: [LabConstrictor-Tools #163](https://github.com/CellMigrationLab/LabConstrictor-Tools/pull/163). + +## Why + +LabConstrictor-Tools v0.3.0-alpha.2 wrote a result image with exactly 3 or 4 planes (for example 3 channels or 3 timepoints) as one page of colour +samples. Fiji cannot open such a file unless it is 8-bit, so the result of a tool like *Align channels* on a 3-channel image did not appear in Fiji +(napari showed it). v0.3.0-alpha.3 writes every result image of 3 or more dimensions as grey planes, except an 8-bit colour image +([LabConstrictor-Tools #163](https://github.com/CellMigrationLab/LabConstrictor-Tools/pull/163)). + +On Windows, `post_install.bat` also registers the app in *Settings > Apps*. It wrote the version given when the repository was initialised +(`DISPLAY_VERSION`), which no release ever changed (the release script looked for a `PKG_VERSION` line the script no longer has), and the +icon `logo.ico`, which no installer contains: the Apps list showed, for example, VLab4Mic 0.1.3 after installing 0.1.8, without an icon. + +## What changes in an app repository + +* `LC_TOOLS_PIN` in `app/bash_bat_scripts/post_install.sh` and `post_install.bat` moves from `v0.3.0-alpha.2` to `v0.3.0-alpha.3`, only when the + line still holds the template's old value. A pin you set yourself is left as it is, and the sync log says so. +* `post_install.bat` reads the version at install time from the `version:` line of the bundled `construct.yaml` (which each release + writes), keeping the initialisation value as a fallback, and uses the first `.ico` file the installer copied into the app folder as the + icon; without one it writes no icon entry. Only the template's exact lines are replaced; a block you changed is left as it is, with a note. + +Nothing else changes. Both changes reach users with the app's next release. The tag `v0.3.0-alpha.3` must exist before such an installer +is built: an installer that pins a missing tag installs the app without its napari / Fiji / QuPath tools (the installer log says so). diff --git a/app/bash_bat_scripts/post_install.bat b/app/bash_bat_scripts/post_install.bat index 2954b25..564afc5 100644 --- a/app/bash_bat_scripts/post_install.bat +++ b/app/bash_bat_scripts/post_install.bat @@ -184,15 +184,19 @@ IF ERRORLEVEL 1 ( SET "ARP_KEY=HKCU\Software\Microsoft\Windows\CurrentVersion\Uninstall\UNDERSCORED_PROJECT_NAME" SET "UNINSTALL_EXE=%PREFIX%\Uninstall-UNDERSCORED_PROJECT_NAME.exe" -SET "DISPLAY_ICON=%PROJECT_ROOT%\logo.ico" +REM Windows Apps list: the version of this installer (the version: line of the bundled construct.yaml, written by each release), +REM and the app icon the installer copied (the first .ico in the app folder); no icon entry when there is none. SET "DISPLAY_VERSION=VERSION_NUMBER" +IF EXIST "%PROJECT_ROOT%\construct.yaml" FOR /F "usebackq tokens=1,* delims=: " %%A IN (`findstr /B /C:"version:" "%PROJECT_ROOT%\construct.yaml"`) DO SET "DISPLAY_VERSION=%%~B" +SET "DISPLAY_ICON=" +FOR %%I IN ("%PROJECT_ROOT%\*.ico") DO IF NOT DEFINED DISPLAY_ICON SET "DISPLAY_ICON=%%~fI" SET "PUBLISHER=GITHUB_OWNER" echo Registering PROJECT_NAME in Windows Apps list >> "%LOG_FILE%" reg add "%ARP_KEY%" /v DisplayName /d "PROJECT_NAME" /f >> "%LOG_FILE%" 2>&1 reg add "%ARP_KEY%" /v DisplayVersion /d "%DISPLAY_VERSION%" /f >> "%LOG_FILE%" 2>&1 reg add "%ARP_KEY%" /v Publisher /d "%PUBLISHER%" /f >> "%LOG_FILE%" 2>&1 reg add "%ARP_KEY%" /v InstallLocation /d "%PREFIX%" /f >> "%LOG_FILE%" 2>&1 -reg add "%ARP_KEY%" /v DisplayIcon /d "%DISPLAY_ICON%" /f >> "%LOG_FILE%" 2>&1 +IF DEFINED DISPLAY_ICON reg add "%ARP_KEY%" /v DisplayIcon /d "%DISPLAY_ICON%" /f >> "%LOG_FILE%" 2>&1 reg add "%ARP_KEY%" /v UninstallString /d "\"%UNINSTALL_EXE%\"" /f >> "%LOG_FILE%" 2>&1 reg add "%ARP_KEY%" /v QuietUninstallString /d "\"%UNINSTALL_EXE%\" /S" /f >> "%LOG_FILE%" 2>&1 reg add "%ARP_KEY%" /v NoModify /t REG_DWORD /d 1 /f >> "%LOG_FILE%" 2>&1 @@ -235,7 +239,7 @@ REM This step must never fail the installation: a failure is logged as a WARNING REM LC_TOOLS_SPEC can point to another source (wheel, git URL, mirror). REM Default source: the GitHub archive of the labconstrictor-tools release TAG below (a plain zip: no git needed on the user's computer), REM because the package is not on PyPI yet. Once it is, use "labconstrictor-tools" here. To move the pin, change only this tag. -SET "LC_TOOLS_PIN=v0.3.0-alpha.2" +SET "LC_TOOLS_PIN=v0.3.0-alpha.3" IF NOT DEFINED LC_TOOLS_SPEC SET "LC_TOOLS_SPEC=https://github.com/CellMigrationLab/LabConstrictor-Tools/archive/refs/tags/%LC_TOOLS_PIN%.zip" SET "LC_TOOLS_READY=0" "%PYTHON_EXE%" -c "import importlib.util, sys; sys.exit(0 if importlib.util.find_spec('PYTHON_PROJ_NAME_lc_tools') else 1)" >> "%LOG_FILE%" 2>&1 diff --git a/app/bash_bat_scripts/post_install.sh b/app/bash_bat_scripts/post_install.sh index 329dfe1..7c92d7e 100644 --- a/app/bash_bat_scripts/post_install.sh +++ b/app/bash_bat_scripts/post_install.sh @@ -76,7 +76,7 @@ if [ -f "$PROJECT_ROOT/setup.py" ]; then # (it never fails the installation by itself). LC_TOOLS_SPEC can point to another source (wheel, git URL, mirror). # Default source: the GitHub archive of the labconstrictor-tools release TAG below (a plain zip: no git needed on the user's computer), # because the package is not on PyPI yet. Once it is, use "labconstrictor-tools" here. To move the pin, change only this tag. - LC_TOOLS_PIN="v0.3.0-alpha.2" + LC_TOOLS_PIN="v0.3.0-alpha.3" LC_TOOLS_MODULE="PYTHON_PROJ_NAME_lc_tools" LC_TOOLS_READY=0 if "$PYTHON_EXE" -c "import importlib.util, sys; sys.exit(0 if importlib.util.find_spec('$LC_TOOLS_MODULE') else 1)" >> "$LOG_FILE" 2>&1; then diff --git a/tests/_v0_1_32.py b/tests/_v0_1_32.py index 510d92f..4f9aaaf 100644 --- a/tests/_v0_1_32.py +++ b/tests/_v0_1_32.py @@ -11,6 +11,8 @@ from pathlib import Path from types import ModuleType +from _v0_1_33 import before_0_1_33 + ROOT = Path(__file__).resolve().parents[1] MIGRATION_PATH = ROOT / ".template_sync/migrations/v0_1_31_to_v0_1_32.py" @@ -26,6 +28,7 @@ def _migration() -> ModuleType: def before_0_1_32(relative: Path | str, data: bytes) -> bytes: """`data` (a live template file at `relative`) without the change 0.1.32 made to it; any other file is returned as it is.""" + data = before_0_1_33(relative, data) migration = _migration() relative = Path(relative) if relative not in migration.FILES: diff --git a/tests/_v0_1_33.py b/tests/_v0_1_33.py new file mode 100644 index 0000000..791dcec --- /dev/null +++ b/tests/_v0_1_33.py @@ -0,0 +1,40 @@ +"""Helper of the tests: a live template file as it was before template 0.1.33. + +Template 0.1.33 moves the Tools pin (`LC_TOOLS_PIN` in post_install.sh / .bat) from v0.3.0-alpha.2 to v0.3.0-alpha.3, and rewrites the +Windows Apps-list block of post_install.bat. The tests of the older migrations compare with the live files; `before_0_1_33` gives the bytes of +a live file with both changes undone, so they keep checking what they checked. The changes are tested in test_template_sync_0_1_33.py. +""" +from __future__ import annotations + +import importlib.util +from pathlib import Path + +PIN_0_1_28 = "v0.3.0-alpha.2" +PIN_0_1_33 = "v0.3.0-alpha.3" +PINNED_FILES = (Path("app/bash_bat_scripts/post_install.sh"), Path("app/bash_bat_scripts/post_install.bat")) + + +MIGRATION_PATH = Path(__file__).resolve().parents[1] / ".template_sync/migrations/v0_1_32_to_v0_1_33.py" + + +def _apps_list_blocks() -> tuple[str, str, str, str]: + """(new block, old block, new icon line, old icon line) of the template's post_install.bat, from the migration itself.""" + spec = importlib.util.spec_from_file_location("migration_0_1_33_for_older_tests", MIGRATION_PATH) + if spec is None or spec.loader is None: + raise RuntimeError(f"Unable to load {MIGRATION_PATH}") + migration = importlib.util.module_from_spec(spec) + spec.loader.exec_module(migration) + new = "\r\n".join(migration.NEW_ARP_LINES).replace("{version}", "VERSION_NUMBER") + old = 'SET "DISPLAY_ICON=%PROJECT_ROOT%\\logo.ico"\r\nSET "DISPLAY_VERSION=VERSION_NUMBER"' + return new, old, migration.NEW_ICON_REG, migration.OLD_ICON_REG + + +def before_0_1_33(relative: Path | str, data: bytes) -> bytes: + """`data` (a live template file at `relative`) with the 0.1.33 pin moved back and the old Apps-list block; any other file as it is.""" + if Path(relative) not in PINNED_FILES: + return data + data = data.replace(PIN_0_1_33.encode(), PIN_0_1_28.encode()) + if Path(relative).suffix == ".bat": + new, old, new_reg, old_reg = _apps_list_blocks() + data = data.replace(new.encode(), old.encode()).replace(new_reg.encode(), old_reg.encode()) + return data diff --git a/tests/smoke/post_install_hosts.py b/tests/smoke/post_install_hosts.py index cee3100..83ae60d 100644 --- a/tests/smoke/post_install_hosts.py +++ b/tests/smoke/post_install_hosts.py @@ -34,7 +34,7 @@ ("UNDERSCORED_PROJECT_NAME", "Demo_App"), ("PYTHON_PROJ_NAME", "demoapp"), ("PROJECT_NAME", APP), - ("VERSION_NUMBER", "1.2.3"), + ("VERSION_NUMBER", "0.0.1"), # the initialisation value; construct.yaml says 1.2.3 (the Apps list must show 1.2.3) ("GITHUB_OWNER", "CellMigrationLab"), ) SETUP_PY = 'from setuptools import find_packages, setup\nsetup(name="demoapp", version="1.2.3", package_dir={"": "src"}, packages=find_packages("src"))\n' @@ -100,6 +100,7 @@ def write_project(root: Path) -> None: (root / "requirements.txt").write_text("setuptools\n", encoding="utf-8") (root / "construct.yaml").write_text("name: Demo_App\nversion: 1.2.3\n", encoding="utf-8") (root / "notebook_launcher.json").write_text("{}", encoding="utf-8") + (root / "demo_app.ico").write_bytes(b"") # the icon the installer copies: the Windows Apps list points at it def render_script(base: Path) -> Path: @@ -155,6 +156,22 @@ def check_undeclared(name: str, entry: dict[str, object], log: str) -> None: print(f"OK {name}: no hosts, and the log says so", flush=True) +def check_apps_list(prefix: Path) -> None: + """Windows: the Apps-list entry has the version of construct.yaml (1.2.3, not the initialisation value) and the copied .ico.""" + key = r"HKCU\Software\Microsoft\Windows\CurrentVersion\Uninstall\Demo_App" + found: dict[str, str] = {} + for value in ("DisplayVersion", "DisplayIcon"): + out = subprocess.run(["reg", "query", key, "/v", value], capture_output=True, text=True, check=False).stdout + line = next((row for row in out.splitlines() if row.strip().startswith(value)), "") + found[value] = line.split("REG_SZ", 1)[1].strip() if "REG_SZ" in line else "" + subprocess.run(["reg", "delete", key, "/f"], capture_output=True, check=False) + if found["DisplayVersion"] != "1.2.3": + raise AssertionError(f"Apps list: DisplayVersion {found['DisplayVersion']!r}, expected the construct.yaml version 1.2.3") + if Path(found["DisplayIcon"]) != prefix / APP / "demo_app.ico": + raise AssertionError(f"Apps list: DisplayIcon {found['DisplayIcon']!r}, expected {prefix / APP / 'demo_app.ico'}") + print(f"OK Windows Apps list: version 1.2.3, icon {found['DisplayIcon']}", flush=True) + + def install_case(script: Path, prefix: Path, lc_home: Path) -> tuple[dict[str, object], str]: """Install once into a fresh registry home: (the registry entry of the app, the installer log).""" log = install(script, prefix, lc_home) @@ -174,6 +191,8 @@ def main() -> int: target.write_bytes(manifest.read_bytes()) entry, log = install_case(script, prefix, base / f"home_{manifest.stem}") check_declared(manifest.name, manifest, entry, log) + if WINDOWS and manifest == manifests[0]: + check_apps_list(prefix) target.write_bytes(b"\xef\xbb\xbfschema: 1\nname: Demo\n\"hosts\": [notebook, napari]\n") entry, log = install_case(script, prefix, base / "home_bom_quoted_key") if entry.get("hosts") != ["notebook", "napari"]: diff --git a/tests/test_template_sync_0_1_28.py b/tests/test_template_sync_0_1_28.py index 534154c..a7772b6 100644 --- a/tests/test_template_sync_0_1_28.py +++ b/tests/test_template_sync_0_1_28.py @@ -34,14 +34,14 @@ def load(path: Path, name: str) -> ModuleType: def live(relative: Path) -> bytes: - """The live template file without the later change of 0.1.32 (the guide no longer names the pin: tests/_v0_1_32.py).""" + """The live template file without the later changes of 0.1.32 (the guide no longer names the pin) and 0.1.33 (the next pin): tests/_v0_1_32.py.""" return before_0_1_32(relative, (ROOT / relative).read_bytes()) class TemplateTests(unittest.TestCase): def test_the_template_pins_the_new_tag_once_per_script_and_names_it_in_the_guide(self) -> None: for relative in (SH, BAT): - text = (ROOT / relative).read_bytes().decode("utf-8") + text = live(relative).decode("utf-8") with self.subTest(file=str(relative)): self.assertEqual(text.count(NEW), 1) self.assertNotIn(OLD, text) @@ -107,7 +107,7 @@ def test_the_sync_script_runs_the_migration_from_0_1_27(self) -> None: result = subprocess.run(command, capture_output=True, text=True, check=False) self.assertEqual(result.returncode, 0, result.stdout + result.stderr) self.assertIn("[migration 0.1.28]", result.stdout) - self.assertEqual((self.repo / SH).read_bytes(), (ROOT / SH).read_bytes()) + self.assertEqual((self.repo / SH).read_bytes(), live(SH)) if __name__ == "__main__": diff --git a/tests/test_template_sync_0_1_33.py b/tests/test_template_sync_0_1_33.py new file mode 100644 index 0000000..a836289 --- /dev/null +++ b/tests/test_template_sync_0_1_33.py @@ -0,0 +1,133 @@ +"""Tests of the 0.1.33 migration: the Tools pin moves to v0.3.0-alpha.3 (Tools #163), and the Windows Apps list gets the right version and icon.""" +from __future__ import annotations + +import importlib.util +import re +import shutil +import subprocess +import sys +import tempfile +import unittest +from contextlib import redirect_stdout +from io import StringIO +from pathlib import Path +from types import ModuleType + +from _v0_1_33 import before_0_1_33 + + +ROOT = Path(__file__).resolve().parents[1] +MIGRATION_PATH = ROOT / ".template_sync/migrations/v0_1_32_to_v0_1_33.py" + + +def load(path: Path, name: str) -> ModuleType: + spec = importlib.util.spec_from_file_location(name, path) + assert spec is not None and spec.loader is not None + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +MIGRATION = load(MIGRATION_PATH, "migration_0_1_33") +SH, BAT = MIGRATION.POST_INSTALL_SH, MIGRATION.POST_INSTALL_BAT +OLD, NEW = MIGRATION.OLD_PIN, MIGRATION.NEW_PIN + + +def live(relative: Path) -> bytes: + """The live template file (0.1.33 is the newest migration).""" + return (ROOT / relative).read_bytes() + + +class TemplateTests(unittest.TestCase): + def test_the_template_pins_the_new_tag_once_per_script(self) -> None: + for relative in (SH, BAT): + text = (ROOT / relative).read_bytes().decode("utf-8") + with self.subTest(file=str(relative)): + self.assertEqual(text.count(NEW), 1) + self.assertNotIn(OLD, text) + self.assertIn("\r\n", (ROOT / BAT).read_bytes().decode("utf-8"), "post_install.bat stays CRLF") + + def test_the_manifest_lists_the_migration_and_the_version(self) -> None: + text = (ROOT / ".template_sync/manifest.yaml").read_text(encoding="utf-8") + self.assertIn("template_version: 0.1.33", text) # the newest migration: update when a later one is added + self.assertIn("script: migrations/v0_1_32_to_v0_1_33.py", text) + self.assertTrue((ROOT / ".tools/docs/migration_0_1_33.md").is_file()) + + +class AppsListTemplateTests(unittest.TestCase): + def test_the_bat_reads_the_version_and_icon_at_install_time(self) -> None: + text = (ROOT / BAT).read_bytes().decode("utf-8") + self.assertNotIn("logo.ico", text) + self.assertEqual(text.count(MIGRATION.NEW_ICON_REG), 1) + self.assertIn('findstr /B /C:"version:" "%PROJECT_ROOT%\\construct.yaml"`) DO SET "DISPLAY_VERSION=%%~B"', text) + self.assertIn('FOR %%I IN ("%PROJECT_ROOT%\\*.ico") DO IF NOT DEFINED DISPLAY_ICON SET "DISPLAY_ICON=%%~fI"', text) + self.assertNotIn("\n", text.replace("\r\n", ""), "post_install.bat stays CRLF on every line") + + +class MigrationTests(unittest.TestCase): + def setUp(self) -> None: + self.repo = Path(tempfile.mkdtemp(prefix="lc_0_1_33_")) + for relative in (SH, BAT): + target = self.repo / relative + target.parent.mkdir(parents=True, exist_ok=True) + target.write_bytes(before_0_1_33(relative, live(relative)).replace(b"VERSION_NUMBER", b"0.1.3")) + + def tearDown(self) -> None: + shutil.rmtree(self.repo, ignore_errors=True) + + def run_migration(self) -> str: + out = StringIO() + with redirect_stdout(out): + MIGRATION.migrate(self.repo, {}) + return out.getvalue() + + def test_an_app_of_0_1_32_gets_the_template_files_and_a_second_run_changes_nothing(self) -> None: + log = self.run_migration() + for relative in (SH, BAT): + with self.subTest(file=str(relative)): + self.assertEqual((self.repo / relative).read_bytes(), live(relative).replace(b"VERSION_NUMBER", b"0.1.3")) + self.assertEqual(log.count(f"updated: Tools pin {OLD} -> {NEW}"), 2, log) + self.assertIn("3 change(s)", log) + self.assertIn("no repository changes were required", self.run_migration()) + + def test_an_own_pin_is_left_alone(self) -> None: + sh = self.repo / SH + sh.write_bytes(sh.read_bytes().replace(OLD.encode(), b"v0.2.0-rc.2")) + before = sh.read_bytes() + log = self.run_migration() + self.assertEqual(sh.read_bytes(), before) + self.assertIn("has its own pin v0.2.0-rc.2: left as it is", log) + + def test_the_app_keeps_its_own_setup_time_version_as_the_fallback(self) -> None: + self.run_migration() + self.assertIn(b'SET "DISPLAY_VERSION=0.1.3"\r\nIF EXIST', (self.repo / BAT).read_bytes()) + + def test_an_apps_list_block_changed_by_hand_is_left_alone(self) -> None: + bat = self.repo / BAT + bat.write_bytes(bat.read_bytes().replace(b"logo.ico", b"my_icon.ico")) + log = self.run_migration() + self.assertIn(b"my_icon.ico", bat.read_bytes()) + self.assertIn("has no template Apps-list block (changed by hand): left as it is", log) + + def test_a_script_without_the_hook_or_files_missing_are_left_alone(self) -> None: + (self.repo / SH).write_text("#!/bin/bash\necho hi\n", encoding="utf-8") + (self.repo / BAT).unlink() + log = self.run_migration() + self.assertIn("has no Tools bridge hook", log) + self.assertIn(f"{BAT}: not present in this repository", log) + self.assertFalse((self.repo / BAT).exists()) + + def test_the_sync_script_runs_the_migration_from_0_1_32(self) -> None: + shutil.copytree(ROOT / ".template_sync", self.repo / ".template_sync") + manifest = self.repo / ".template_sync/manifest.yaml" + manifest.write_text(re.sub(r"template_version: [0-9.]+", "template_version: 0.1.33", manifest.read_text(encoding="utf-8")), encoding="utf-8") + command = [sys.executable, str(self.repo / ".template_sync/sync.py"), "--from-version", "0.1.32", "--to-version", "0.1.33", "--repository-root", str(self.repo)] + result = subprocess.run(command, capture_output=True, text=True, check=False) + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) + self.assertIn("[migration 0.1.33]", result.stdout) + self.assertEqual((self.repo / SH).read_bytes(), (ROOT / SH).read_bytes()) + self.assertEqual((self.repo / BAT).read_bytes(), (ROOT / BAT).read_bytes().replace(b"VERSION_NUMBER", b"0.1.3")) + + +if __name__ == "__main__": + unittest.main()