From 881e0fd75f18ab62f08c7716ad37d622492b949f Mon Sep 17 00:00:00 2001 From: Bryan Van Deusen Date: Tue, 1 Dec 2026 07:09:47 -0500 Subject: [PATCH] fix(test-guards): an expected error is an outcome check, so a refusal test is not mock-only A refusal test raises, then checks nothing was written with assert_not_called. The mock-only signal counted only plain asserts as outcome checks, so 28 of the 44 tests it flagged in Grimoire's own suite were refusals. pytest.raises and assertRaises now count. Co-Authored-By: Claude Opus 5.5 --- src/grimoire/services/test_guards.py | 8 +++++++- tests/test_test_guards.py | 3 +++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/grimoire/services/test_guards.py b/src/grimoire/services/test_guards.py index 7d37c7ab..44eda46c 100644 --- a/src/grimoire/services/test_guards.py +++ b/src/grimoire/services/test_guards.py @@ -75,7 +75,13 @@ SIGNALS = { _MOCK_ASSERT_RE = re.compile( r"\.assert_(?:called|awaited|not_called|not_awaited|any_call|has_calls)\w*\(|toHaveBeenCalled" ) -_PLAIN_ASSERT_RE = re.compile(r"^\s*assert\b|\bexpect\((?![^\n]*toHaveBeenCalled)", re.MULTILINE) +# An outcome check: a plain assert, an expect that is not about a mock, or an +# expected error. A refusal test that raises and then checks nothing was +# written has checked its outcome; the mock assertion is the "nothing" half. +_PLAIN_ASSERT_RE = re.compile( + r"^\s*assert\b|\bexpect\((?![^\n]*toHaveBeenCalled)|\bpytest\.raises\(|\.assertRaises\w*\(", + re.MULTILINE, +) def is_test_path(path: str) -> bool: diff --git a/tests/test_test_guards.py b/tests/test_test_guards.py index dad5736e..5667f947 100644 --- a/tests/test_test_guards.py +++ b/tests/test_test_guards.py @@ -116,6 +116,9 @@ def test_weak_signals_are_read_from_the_body(): assert tg.test_signals(" f()\n mock.assert_called_once()\n") == ["mock-only"] # A plain assert beside a mock check is a real check, so no signal. assert tg.test_signals(" f()\n mock.assert_called_once()\n assert out == 1\n") == [] + # So is an expected error: a refusal, then a check that nothing was written. + refusal = " with pytest.raises(ValueError):\n f()\n write.assert_not_awaited()\n" + assert tg.test_signals(refusal) == [] assert tg.test_signals(" text = path.read_text()\n assert 'x' in text\n") == ["source-text"]