import subprocess import unittest from pathlib import Path from test_cli import TOML, Cli from test_claims import git from test_merge import IDENT class SplitCli(Cli): """Private project (self.root, git, workflow.toml with code_root) + sibling code repo (self.code).""" toml = TOML + 'code_root = "../code"\n' def setUp(self): super().setUp() git(self.root, "init", "-q", "-b", "master") (self.root / ".gitignore").write_text(".wf/\nout/\n") git(self.root, "add", "-A") git(self.root, "commit", "-qm", "init") self.code = self.root.parent / "code" self.code.mkdir() git(self.code, "init", "-q", "-b", "master") (self.code / ".gitignore").write_text(".worktrees/\n.wf-home\n") (self.code / "lib.txt").write_text("a\n") git(self.code, "add", "-A") git(self.code, "commit", "-qm", "init") def add_wt(self, lane="sonnet", id="t-three"): """Code worktree on branch / with .wf-home (what wf start makes).""" wt = self.code / ".worktrees" / lane git(self.code, "worktree", "add", "-q", str(wt), "-b", f"{lane}/{id}") (wt / ".wf-home").write_text(f"{self.root}\n") return wt def log(self, repo, rev="master"): return subprocess.run(["git", "log", "--format=%s", rev], cwd=repo, capture_output=True, text=True).stdout def sha(self, repo, rev="master"): return subprocess.run(["git", "rev-parse", "--short", rev], cwd=repo, capture_output=True, text=True).stdout.strip() class DiscoveryTest(SplitCli): def test_wf_in_code_worktree_finds_private_project(self): wt = self.add_wt() code, out, err = self.wf("show", "t-three", project=False, cwd=wt) self.assertEqual(code, 0, err) self.assertIn("**t-three**", out) def test_dangling_wf_home_is_config_error(self): wt = self.add_wt() (wt / ".wf-home").write_text(f"{self.root.parent / 'gone'}\n") code, out, err = self.wf("show", "t-three", project=False, cwd=wt) self.assertNotEqual(code, 0) self.assertIn(".wf-home points to", err) self.assertNotIn("Traceback", err) def test_split_and_code_main(self): import sys sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) from wflib import config cfg = config.load(self.root) self.assertTrue(cfg.split) self.assertEqual(config.code_main(cfg), self.code) self.assertEqual(cfg.push, []) def test_code_root_inside_same_repo_not_split(self): import sys sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) from wflib import config (self.root / "workflow.toml").write_text(TOML + 'code_root = "."\npush = ["true"]\n') cfg = config.load(self.root) self.assertFalse(cfg.split) self.assertIsNone(config.code_main(cfg)) self.assertEqual(cfg.push, ["true"])