Browse Source

test: add logger check for mustache error

Sam Jaffe 3 weeks ago
parent
commit
aa6e40b8bb
3 changed files with 37 additions and 3 deletions
  1. 12 0
      tests/conftest.py
  2. 16 0
      tests/matchers.py
  3. 9 3
      tests/script_test.py

+ 12 - 0
tests/conftest.py

@@ -0,0 +1,12 @@
+import logging
+import pytest
+import unittest.mock
+
+from cipy import _handler
+
+
+@pytest.fixture(scope="function")
+def ci_logger(monkeypatch: pytest.MonkeyPatch) -> unittest.mock.Mock:
+    mock = unittest.mock.Mock(spec=logging.Formatter)
+    monkeypatch.setattr(_handler, "formatter", mock)
+    yield mock

+ 16 - 0
tests/matchers.py

@@ -0,0 +1,16 @@
+import sys
+import typing
+import types
+
+
+class HasAttributes(types.SimpleNamespace):
+    def __eq__(self, other: typing.Any) -> bool:
+        for key, value in vars(self).items():
+            if not hasattr(other, key):
+                print(f"Where there is no value at {key}", file=sys.stderr)
+                return False
+            if value != getattr(other, key):
+                print(f"Where the value at {key} is '{getattr(other, key)}'", file=sys.stderr)
+                return False
+
+        return True

+ 9 - 3
tests/script_test.py

@@ -10,13 +10,15 @@ from pathlib import Path
 import cipy
 import cipy.script
 
+from matchers import HasAttributes
+
 
 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
+    check: bool,  # Force an error if any of our mocked runs doesnt specify check
     **kwargs: typing.Any,
 ):
     return subprocess.CompletedProcess(
@@ -39,7 +41,7 @@ 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"],)
+    assert subprocess.run.call_args_list[0].args == (["node", "/dev/null"],)
 
 
 def test_node_cleanup_noop_without_post() -> None:
@@ -90,7 +92,7 @@ def test_script_performs_mustache_substitution() -> None:
     action.run(cipy.Context(foo=1))
 
 
-def test_script_propogates_mustache_error() -> None:
+def test_script_propogates_mustache_error(ci_logger) -> None:
     action = cipy.script.Script(script="""
     echo {{ foo }}
     """)
@@ -98,3 +100,7 @@ def test_script_propogates_mustache_error() -> None:
     subprocess.run.side_effect = RuntimeError("Should not be reached")
 
     assert action.run(cipy.Context()) is cipy.Status.FAILURE
+    record = ci_logger.format.call_args_list[0][0][0]
+    ci_logger.format.assert_called_with(
+        HasAttributes(name="Script", message="Could not find key 'foo'\n")
+    )