aboutsummaryrefslogtreecommitdiffziptar.gz
path: root/tests/test_gate.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_gate.py')
-rw-r--r--tests/test_gate.py74
1 files changed, 74 insertions, 0 deletions
diff --git a/tests/test_gate.py b/tests/test_gate.py
new file mode 100644
index 0000000..7a33c1f
--- /dev/null
+++ b/tests/test_gate.py
@@ -0,0 +1,74 @@
+import sys
+import tempfile
+import unittest
+from pathlib import Path
+
+sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
+from wflib import config as C
+from test_claims import git
+from test_cli import TOML
+from test_setup import GitCli
+
+GATE = TOML + 'quick_gate = ["echo \\"$WF_MAIN\\" > gate.txt", "echo second >> gate.txt"]\n'
+
+
+class GateConfigTest(unittest.TestCase):
+ def setUp(self):
+ self.tmp = tempfile.TemporaryDirectory()
+ self.root = Path(self.tmp.name).resolve()
+
+ def tearDown(self):
+ self.tmp.cleanup()
+
+ def load(self, extra):
+ (self.root / "workflow.toml").write_text('format = 1\ntasks = "T.md"\narchive = "a.md"\n' + extra)
+ return C.load(self.root)
+
+ def test_default_empty(self):
+ self.assertEqual(self.load("").quick_gate, [])
+
+ def test_list(self):
+ self.assertEqual(self.load('quick_gate = ["make quick"]\n').quick_gate, ["make quick"])
+
+ def test_wrong_type(self):
+ with self.assertRaisesRegex(C.ConfigError, "'quick_gate' must be a list of strings"):
+ self.load('quick_gate = "make quick"\n')
+
+
+class GateCliTest(GitCli):
+ toml = GATE
+
+ def setUp(self):
+ super().setUp()
+ self.wt = self.root / ".worktrees" / "opus"
+ git(self.root, "worktree", "add", "-q", str(self.wt), "-b", "opus/t-three")
+
+ def test_runs_in_worktree_with_wf_main(self):
+ code, out, err = self.wf("gate", project=False, cwd=self.wt / "docs")
+ self.assertEqual((code, err), (0, ""), out)
+ self.assertEqual((self.wt / "gate.txt").read_text(), f"{self.root}\nsecond\n")
+ self.assertFalse((self.root / "gate.txt").exists())
+ self.assertIn("$ echo second >> gate.txt\n", out)
+ self.assertTrue(out.endswith("quick_gate: 2 commands green\n"), out)
+
+ def test_runs_in_main_tree(self):
+ code, out, err = self.wf("gate", project=False, cwd=self.root)
+ self.assertEqual((code, err), (0, ""), out)
+ self.assertEqual((self.root / "gate.txt").read_text(), f"{self.root}\nsecond\n")
+
+ def test_red_exit_1_rest_skipped(self):
+ (self.wt / "workflow.toml").write_text(TOML + 'quick_gate = ["exit 4", "echo c > c.txt"]\n')
+ code, out, err = self.wf("gate", project=False, cwd=self.wt)
+ self.assertEqual(code, 1, out + err)
+ self.assertTrue(err.startswith("wf: quick_gate 'exit 4' red (exit 4): fix it before wf done; later commands skipped\n"), err)
+ self.assertIn("done+gate-red", err)
+ self.assertFalse((self.wt / "c.txt").exists())
+
+ def test_none_configured(self):
+ (self.wt / "workflow.toml").write_text(TOML)
+ code, out, err = self.wf("gate", project=False, cwd=self.wt)
+ self.assertEqual((code, out, err), (0, "no quick_gate in workflow.toml: nothing to do\n", ""))
+
+
+if __name__ == "__main__":
+ unittest.main()