diff --git a/bin/project_config/settings.py b/bin/project_config/settings.py index 0f97c721..61120581 100644 --- a/bin/project_config/settings.py +++ b/bin/project_config/settings.py @@ -9,9 +9,9 @@ from failure import Failure PROJECTS = ".claude-projects" CONFIG = "config.toml" -# Every path a project sets, by its role. The syntax panel picked role names over file names, which a client's project doesn't share. +# Every path a project sets, by its role. Three readers, seated as the human who writes the config, picked the spelling from four candidates. They picked role names over file names, which a client's project doesn't share. SETTINGS = ("plan", "working-rules", "decisions", "score-anchors", "comprehension-history", "reading-cost", "changelog", "goals-and-audience", "principles") -# The panel also picked a prefix on every path over bare relative paths: each line says its own place, and a path with no prefix fails instead of landing somewhere by default. +# They also picked a prefix over bare relative paths: each line says its own place, and a relative path with no prefix fails instead of landing somewhere by default. SPELLING = '"checkout:" for a path in the checkout, "folder:" for one in the config\'s folder, or a path that starts with / or ~/' @@ -24,20 +24,22 @@ class ProjectConfig: def load(top: pathlib.Path, main: pathlib.Path, home: pathlib.Path) -> ProjectConfig | Failure: - """The config that names main, the project's main checkout. A `checkout:` path lands in top, the worktree that asks, which is main itself outside a worktree. + """The config that names main, the project's main checkout. + top is the worktree that asks, and main itself outside a worktree. A `checkout:` path lands in top. A config that cannot be read fails every project, since it may be the one that names main.""" named: list[tuple[pathlib.Path, dict[str, object]]] = [] for file in sorted((home / PROJECTS).glob(f"*/{CONFIG}")): - values = read(file, home) - if isinstance(values, Failure): - return values - if values.pop("checkout") == main: - named.append((file, values)) + config = read(file, home) + if isinstance(config, Failure): + return config + # Resolved on both sides, so a checkout reached through a symlink, or spelt with `..`, still finds its config. + if config[0].resolve() == main.resolve(): + named.append((file, config[1])) if not named: - return Failure(f"no config names the checkout {main}: write ~/{PROJECTS}//{CONFIG}, as README.md → Install shows") + return Failure(f"no config names the checkout {main}: write ~/{PROJECTS}//{CONFIG}, as `project-config --help` shows") if len(named) > 1: - return Failure(f"{' and '.join(str(file) for file, _ in named)} both name the checkout {main}; one config may") + return Failure(f"{' and '.join(str(file) for file, _ in named)} each name the checkout {main}; only one config may") file, values = named[0] paths = {name: placed(values[name], top, file.parent, home) for name in SETTINGS if name in values} faults = [ @@ -50,16 +52,17 @@ def load(top: pathlib.Path, main: pathlib.Path, home: pathlib.Path) -> ProjectCo return ProjectConfig(file.parent.name, file.parent, {name: path for name, path in paths.items() if path}) -def read(file: pathlib.Path, home: pathlib.Path) -> dict[str, object] | Failure: - """The config's values, its checkout as an absolute path.""" +def read(file: pathlib.Path, home: pathlib.Path) -> tuple[pathlib.Path, dict[str, object]] | Failure: + """The checkout a config names, as an absolute path, and its other values.""" try: values = tomllib.loads(file.read_text(encoding="utf-8")) except (OSError, UnicodeDecodeError, tomllib.TOMLDecodeError) as failure: return Failure(f"cannot read {file}: {failure}") checkout = anywhere(values.get("checkout"), home) if checkout is None: - return Failure(f'{file}: `checkout` names the project\'s main checkout as a path that starts with / or ~/, such as "~/code/acme-billing", not {values.get("checkout")!r}') - return {**values, "checkout": checkout} + found = f"not {values['checkout']!r}" if "checkout" in values else "and is missing" + return Failure(f'{file}: `checkout` names the project\'s main checkout as a path that starts with / or ~/, such as "~/code/acme-billing", {found}') + return checkout, {name: value for name, value in values.items() if name != "checkout"} def placed(value: object, top: pathlib.Path, folder: pathlib.Path, home: pathlib.Path) -> pathlib.Path | None: diff --git a/bin/project_config/test_project_config.py b/bin/project_config/test_project_config.py index be74407b..80b22dd8 100644 --- a/bin/project_config/test_project_config.py +++ b/bin/project_config/test_project_config.py @@ -71,6 +71,13 @@ class LoadTest(ConfigCase): assert isinstance(found, ProjectConfig), found self.assertEqual(found.paths["principles"], pathlib.Path("/srv/principles")) + def test_a_checkout_reached_through_a_symlink_or_dots_finds_its_config(self) -> None: + self.write("acme", CLIENT) + (self.home / "work").symlink_to(self.home / "code") + for case, main in {"a symlink": self.home / "work" / "acme-billing", "dots": self.home / "code" / "other" / ".." / "acme-billing"}.items(): + with self.subTest(case=case): + self.assertIsInstance(load(main, main, self.home), ProjectConfig) + def test_no_config_fails_naming_the_checkout_and_the_file_to_write(self) -> None: for write_other in (False, True): with self.subTest(write_other=write_other): @@ -85,10 +92,10 @@ class LoadTest(ConfigCase): self.write("acme", kept) reason = self.failure() for name in ("plan", "changelog", "principles"): - self.assertIn(f"`{name}`", reason) - file = self.home / ".claude-projects" / "acme" / "config.toml" - settings = "plan, working-rules, decisions, score-anchors, comprehension-history, reading-cost, changelog, goals-and-audience, principles" - self.assertEqual(reason, f"{file}: `plan` is missing; `changelog` is missing; `principles` is missing. The settings are {settings}.") + with self.subTest(name=name): + self.assertIn(f"`{name}` is missing", reason) + self.assertNotIn("`decisions` is", reason) + self.assertIn("acme/config.toml", reason) def test_every_fault_in_one_config_is_named_together(self) -> None: cases = { @@ -113,8 +120,9 @@ class LoadTest(ConfigCase): def test_two_configs_naming_one_checkout_fail_naming_both(self) -> None: self.write("acme", CLIENT) self.write("acme-copy", CLIENT) - projects = self.home / ".claude-projects" - self.assertEqual(self.failure(), f"{projects}/acme/config.toml and {projects}/acme-copy/config.toml both name the checkout {self.checkout}; one config may") + reason = self.failure() + self.assertIn("acme/config.toml", reason) + self.assertIn("acme-copy/config.toml", reason) def test_a_config_that_cannot_be_read_fails_whichever_checkout_asks(self) -> None: cases = {"not TOML": "checkout = ", "no checkout": 'plan = "folder:todo.md"\n', "a checkout that is not text": "checkout = 3\n", "a relative checkout": 'checkout = "code/acme"\n'} @@ -123,10 +131,7 @@ class LoadTest(ConfigCase): self.home = pathlib.Path(made).resolve() self.write("acme", CLIENT) self.write("broken", text) - reason = self.failure() - self.assertIn("broken/config.toml", reason) - if case == "a relative checkout": - self.assertTrue(reason.endswith(", not 'code/acme'"), reason) + self.assertIn("broken/config.toml", self.failure()) def test_a_folder_with_no_config_is_skipped(self) -> None: self.write("acme", CLIENT) @@ -161,9 +166,20 @@ class ToolTest(ConfigCase): self.assertIn(said, ran.stderr) def test_help_shows_a_config(self) -> None: - ran = self.run_tool("--help") - self.assertEqual(ran.returncode, 0) - self.assertIn('plan = "folder:todo.md"', ran.stdout) + for flag in ("-h", "--help"): + with self.subTest(flag=flag): + ran = self.run_tool(flag) + self.assertEqual(ran.returncode, 0) + self.assertIn('plan = "folder:todo.md"', ran.stdout) + + def test_a_linked_worktree_finds_the_config_and_prints_its_own_files(self) -> None: + self.write("acme", CLIENT) + worktree = self.home / "code" / "acme-billing-item-3" + git = ["git", "-C", str(self.checkout), "-c", "user.name=t", "-c", "user.email=t@example.com"] + subprocess.run([*git, "commit", "-q", "--allow-empty", "-m", "first"], check=True, env=isolated_env()) + subprocess.run([*git, "worktree", "add", "-q", str(worktree)], check=True, env=isolated_env()) + ran = self.run_tool("changelog", cwd=worktree) + self.assertEqual((ran.returncode, ran.stdout), (0, f"{worktree}/CHANGELOG.md\n")) if __name__ == "__main__":