Skip to content

fix(doubles): call assertions silently pass when the command was never spied #895

Description

@Chemaclass

Context

Call assertions resolve their state from a dynamically-named global
(_BASHUNIT_SPY_<name>_TIMES_FILE). When the name was never spied, the global is
unset, bashunit::spy::times_to_slot reports 0 calls
(src/test_doubles.sh:20-23), and the assertion behaves as if the command exists
and was simply never invoked.

Evidence

Both of these pass today:

function test_typo_in_spy_name_passes_silently() {
  bashunit::spy touch
  touch x
  assert_not_called tuoch          # PASSES — typo, asserts nothing
}

function test_zero_times_on_unspied_command() {
  assert_have_been_called_times 0 never_spied_at_all   # PASSES
}

And asserting arguments on a name that was never spied produces a diff with no
hint about the real cause:

✗ Failed: Assert without spy gives no hint
    Expected 'foo'
    but got  ''

Proposal

Track registered spies (the name is already appended to
_BASHUNIT_MOCKED_FUNCTIONS in bashunit::spy) and, when a call assertion
targets a name that is not in that list, fail with a distinct message instead of
reporting zero calls:

✗ Failed: Assert not called
    'tuoch' was never registered as a spy
    Did you mean 'touch'? Call bashunit::spy 'tuoch' first.

The "did you mean" suggestion is optional; the hard requirement is that
asserting on an unregistered name fails rather than silently passing.

Note the deliberate exception: assert_have_been_called_times 0 <spy> /
assert_not_called <spy> on a registered spy must keep passing — that is the
whole point of those assertions.

Why this matters for agentic coding

assert_not_called and assert_have_been_called_times 0 are exactly the
assertions an agent reaches for to prove a guard clause works ("the destructive
command was not run"). A typo in the command name makes that proof vacuous while
still printing green, and there is nothing in the output for the agent to notice.
An agent has no intuition to re-read the spy name; it needs the framework to say
so.

Acceptance criteria

  • Asserting on a never-spied name fails with a message naming the cause
  • assert_not_called / _times 0 on a registered spy still pass
  • bashunit::unmocking a spy and then asserting on it fails the same way
  • Tests in tests/functional/doubles_test.sh (use assert_fails)
  • Documented in docs/test-doubles.md + docs/ai-agents.md
  • Bash 3.0+ compatible, make sa + make lint green

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingpure testingPure testing related

Type

No type

Projects

Status
Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions