Selaa lähdekoodia

test: cipy.script.NodeScript

Sam Jaffe 3 viikkoa sitten
vanhempi
commit
00ba4fba67
1 muutettua tiedostoa jossa 51 lisäystä ja 0 poistoa
  1. 51 0
      tests/script_test.py

+ 51 - 0
tests/script_test.py

@@ -0,0 +1,51 @@
+# mypy: disable-error-code="attr-defined"
+import io
+import pytest
+import subprocess
+import typing
+import unittest.mock
+
+from pathlib import Path
+
+import cipy
+import cipy.script
+
+
+def mock_run(args, stdout, stderr, check, **kwargs):
+    return subprocess.CompletedProcess(
+        args=args,
+        stdout=None if stdout is None else "",
+        stderr=None if stderr is None else "",
+        returncode=0,
+    )
+
+
+@pytest.fixture(scope="function", autouse=True)
+def disable_subprocess() -> typing.Iterator[None]:
+    mock = unittest.mock.Mock()
+    mock.side_effect = mock_run
+    with unittest.mock.patch("subprocess.run", mock):
+        yield
+
+
+def test_node_run_runs_main() -> None:
+    action = cipy.script.NodeScript(name="example", main=Path("/dev/null"))
+    action.run(cipy.Context())
+
+    assert subprocess.run.call_args_list[0][0] == (["node", "/dev/null"],)
+
+
+def test_node_cleanup_noop_without_post() -> None:
+    action = cipy.script.NodeScript(name="example", main=Path("/dev/null"))
+    action.cleanup(cipy.Context())
+
+    subprocess.run.assert_not_called()
+
+
+def test_node_cleanup_runs_post() -> None:
+    action = cipy.script.NodeScript(
+        name="example", main=Path("/dev/null"), post=Path("/dev/zero")
+    )
+    action.cleanup(cipy.Context())
+
+    assert subprocess.run.call_args_list[0].args == (["node", "/dev/zero"],)