fix: use $BATS_RUN_TMPDIR for $BATS_MOCK_TMPDIR #19
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Update $BATS_MOCK_TMPDIR to $BATS_RUN_TMPDIR instead of $BATS_TMPDIR
Observed Behavior
Re-running Bats tests that call
bats-mock
exhibit unusual behavior when not all mocks are used andunstub
is not called on all programs.I created a
bats
test file that usedbats-mock
and (admittedly) I didn't write the test properly and I didn't unstub one program correctly. IMO,bats-mock
should be resilient against my mistakes.The problem exhibits just running a test like the one below, using only
bats
(andbats
helper) code.Repeatedly running this test will alternate between passing and failing.
Why?
It appears that
bats-mock
uses$BATS_TMPDIR
under the hood as the mock location. The problem is, per https://bats-core.readthedocs.io/en/stable/writing-tests.html#special-variables , that is just$TMPDIR
or/tmp
.Since
bats-mock
does not clean up after itself, the${program}-stub-plan
and${program}-stub-run
files (that were not unstubbed by my bad test writing) persist across runs, leading to the alternating behavior.On the other hand,
$BATS_RUN_TMPDIR
(added inbats
v1.2.1) is cleaned up (or not ifbats --no-tempdir-cleanup
is used) after each run... And reuse of that directory is actively avoided bybats
's invocation ofmktemp
when creating$BATS_RUN_TMPDIR
.Testing
I wanted to add a test for this issue to
bats-mock
, however because the results change between instances ofbats
itself, I'm not sure how to write abats
or other test for the issue.I have tested the change and the test code above succeeds on every run. While that's not desired behavior (ideally
bats-mock
would throw an error if a program is stubbed and not unstubbed), it's at least consistent behavior which to my mind is preferable to inconsistency.All of my other tests that use
bats-mock
pass with these changes, as do thebats-mock/tests
.