aboutsummaryrefslogtreecommitdiffziptar.gz
path: root/tests/test_split.py
blob: 2026e33c4c1335f3b19824c1d32bef64076afc37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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 <lane>/<id> 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"])