Skip to content

Commit 65ff193

Browse files
authored
Merge pull request #28 from jgrigg/concurrent_stubs
Isolate stubs to each individual test
2 parents 1833eed + 7bc90c0 commit 65ff193

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

‎stub.bash‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
BATS_MOCK_TMPDIR="${BATS_RUN_TMPDIR}"
1+
BATS_MOCK_TMPDIR="${BATS_TEST_TMPDIR}"
22
BATS_MOCK_BINDIR="${BATS_MOCK_TMPDIR}/bin"
33

44
BATS_MOCK_REAL_mkdir=$(which mkdir)

‎tests/concurrent_jobs.bats‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
load '../stub'
2+
3+
# Generates a temporary test file with the specified number of tests.
4+
# Each test stubs the SAME command and then unstubs to validate.
5+
generate_stubbed_test_file() {
6+
local num_tests="$1"
7+
local cmd_to_stub="$2"
8+
local output_file="$3"
9+
local bats_test_placeholder="@test"
10+
echo "load '$(cd "${BATS_TEST_DIRNAME}/.." && echo "$(pwd)/stub")'" >"${output_file}"
11+
12+
local test_number
13+
for test_number in $(seq 1 "${num_tests}"); do
14+
cat >>"${output_file}" <<EOF
15+
${bats_test_placeholder} "concurrent stub '${cmd_to_stub} ${test_number}'" {
16+
stub ${cmd_to_stub} "${test_number} : echo '${cmd_to_stub} called with arg ${test_number}"
17+
18+
run ${cmd_to_stub} ${test_number}
19+
20+
unstub '${cmd_to_stub}'
21+
}
22+
23+
EOF
24+
done
25+
}
26+
27+
@test "stubs do not interfere with each other when using 'bats --jobs'" {
28+
local test_file_to_run="${BATS_TEST_TMPDIR}/concurrent_stub_tests.bats"
29+
generate_stubbed_test_file 10 'mycommand' "${test_file_to_run}"
30+
31+
run bats --jobs 4 "${test_file_to_run}"
32+
33+
[ "${status}" -eq 0 ]
34+
[[ ! "${output}" =~ "unstub 'mycommand'' failed" ]]
35+
}

0 commit comments

Comments
 (0)