Browse Source

test: cover cipy.script.Script

Sam Jaffe 3 tuần trước cách đây
mục cha
commit
a513e61f0e
1 tập tin đã thay đổi với 50 bổ sung1 xóa
  1. 50 1
      tests/script_test.py

+ 50 - 1
tests/script_test.py

@@ -11,7 +11,14 @@ import cipy
 import cipy.script
 
 
-def mock_run(args, stdout, stderr, check, **kwargs):
+def mock_run(
+    args: typing.Sequence[str],
+    *,
+    stdout: int | None = None,
+    stderr: int | None = None,
+    check: bool, # Force an error if any of our mocked runs doesnt specify check
+    **kwargs: typing.Any,
+):
     return subprocess.CompletedProcess(
         args=args,
         stdout=None if stdout is None else "",
@@ -49,3 +56,45 @@ def test_node_cleanup_runs_post() -> None:
     action.cleanup(cipy.Context())
 
     assert subprocess.run.call_args_list[0].args == (["node", "/dev/zero"],)
+
+
+def test_script_writes_to_a_file() -> None:
+    action = cipy.script.Script(script="""
+    echo Lorem Ipsum
+    """)
+
+    def check(args, **kwargs):
+        assert Path(args[-1]).is_file()
+        with open(args[-1], "r", encoding="utf8") as file:
+            assert file.read() == action.script
+        return mock_run(args, **kwargs)
+
+    subprocess.run.side_effect = check
+
+    action.run(cipy.Context())
+
+
+def test_script_performs_mustache_substitution() -> None:
+    action = cipy.script.Script(script="""
+    echo {{ foo }}
+    """)
+
+    def check(args, **kwargs):
+        assert Path(args[-1]).is_file()
+        with open(args[-1], "r", encoding="utf8") as file:
+            assert file.read() == "echo 1"
+        return mock_run(args, **kwargs)
+
+    subprocess.run.side_effect = check
+
+    action.run(cipy.Context(foo=1))
+
+
+def test_script_propogates_mustache_error() -> None:
+    action = cipy.script.Script(script="""
+    echo {{ foo }}
+    """)
+
+    subprocess.run.side_effect = RuntimeError("Should not be reached")
+
+    assert action.run(cipy.Context()) is cipy.Status.FAILURE