Skip to content

Commit 832ca96

Browse files
committed
Merge branch 'main' into codex/add-assert_match_snapshot_ignore_colors
Conflicts: CHANGELOG.md
2 parents e85fca8 + 1f4d6e3 commit 832ca96

22 files changed

Lines changed: 291 additions & 90 deletions

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ jobs:
4242
- name: Run tests
4343
shell: bash
4444
run: |
45-
./bashunit tests/${{ matrix.test_chunk }}/*_test.sh
45+
./bashunit --parallel tests/${{ matrix.test_chunk }}/*_test.sh
4646
4747
alpine:
4848
name: "On alpine-latest"

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
- Fix asserts on test doubles in subshell
66
- Allow interpolating arguments in data providers output
7+
- Deprecate `# data_provider` in favor of `# @data_provider`
8+
- Allow `assert_have_been_called_with` to check arguments of specific calls
9+
- Enable parallel tests on Windows
10+
- Add `assert_not_called`
11+
- Improve `find_total_tests` performance
712
- Added `assert_match_snapshot_ignore_colors`
813

914
## [0.19.1](https://github.com/TypedDevs/bashunit/compare/0.19.0...0.19.1) - 2025-05-23

adrs/adr-004-metadata-prefix.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Title: Prefix metadata comments with @
2+
3+
* Status: accepted
4+
* Authors: @Chemaclass
5+
* Date: 2025-05-29
6+
7+
8+
## Context and Problem Statement
9+
10+
Data providers are defined via a special comment `# data_provider`. We want to
11+
clearly differentiate these meta comments from ordinary comments.
12+
13+
## Considered Options
14+
15+
* Keep using `# data_provider` as is.
16+
* Introduce an `@` prefix for special comments while supporting the old syntax.
17+
18+
## Decision Outcome
19+
20+
We decided to prefix the metadata provider directives with `@`,
21+
eg: using `# @data_provider provider_name`.
22+
23+
> The previous form without the prefix is still supported for backward compatibility but is now deprecated.
24+
25+
### Positive Consequences
26+
27+
* Highlights special bashunit directives clearly.
28+
* Allows future directives to consistently use the `@` prefix.
29+
30+
### Negative Consequences
31+
32+
* Projects must eventually update old comments to the new syntax.
33+
34+
## Technical Details
35+
36+
`helper::get_provider_data` now matches both `# @data_provider` and the old
37+
`# data_provider` when locating provider functions.

docs/blog/2024-09-01-release-0-15.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ BASHUNIT_REPORT_HTML=
5151

5252
::: code-group
5353
```bash [example_test.sh]
54-
# data_provider provider_directories
54+
# @data_provider provider_directories
5555
function test_directory_exists() {
5656
local outro=$1
5757
local directory=$2

docs/command-line.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ Creates a report XML file that follows the JUnit XML format and contains informa
130130
bashunit provides an option to run each test in a separate child process, allowing you to parallelize the test execution and potentially speed up the testing process. When running in parallel mode, the execution order of tests is randomized.
131131

132132
::: warning
133-
Parallel mode is currently only supported on **macOS** and **Ubuntu**. On other
134-
systems (like Alpine Linux or Windows) the option is automatically disabled due
135-
to inconsistent results. In those environments consider using `--no-parallel`.
133+
Parallel mode is supported on **macOS**, **Ubuntu**, and **Windows**. On other
134+
systems (like Alpine Linux) the option is automatically disabled due to
135+
inconsistent results. In those environments consider using `--no-parallel`.
136136
:::
137137

138138
::: code-group

docs/data-providers.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ Each run is treated as a separate test, so it can pass or fail independently. Pl
1313

1414
A data provider function is specified as follows:
1515

16+
> **Note**: The previous `# data_provider` syntax is still supported but
17+
> deprecated. Prefer using the `@` prefix going forward.
18+
1619
::: code-group
1720
```bash [Example]
18-
# data_provider provider_function_name
21+
# @data_provider provider_function_name
1922
function test_my_test_case() {
2023
...
2124
}
@@ -32,7 +35,7 @@ argument position.
3235

3336
::: code-group
3437
```bash [example_test.sh]
35-
# data_provider fizz_numbers
38+
# @data_provider fizz_numbers
3639
function test_returns_fizz_when_multiple_of_::1::_like_::2::_given() {
3740
# ...
3841
}
@@ -53,7 +56,7 @@ Running example_test.sh
5356

5457
::: code-group
5558
```bash [example_test.sh]
56-
# data_provider provider_directories
59+
# @data_provider provider_directories
5760
function test_directories_exists() {
5861
local dir1=$1
5962
local dir2=$2
@@ -78,7 +81,7 @@ Running example_test.sh
7881

7982
::: code-group
8083
```bash [example_test.sh]
81-
# data_provider provider_directories
84+
# @data_provider provider_directories
8285
function test_directory_exists() {
8386
local directory=$1
8487

@@ -103,7 +106,7 @@ Running example_test.sh
103106

104107
::: code-group
105108
```bash [example_test.sh]
106-
# data_provider provider_directories
109+
# @data_provider provider_directories
107110
function test_directory_exists() {
108111
local outro=$1
109112
local directory=$2

docs/test-doubles.md

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
When creating tests, you might need to override existing function to be able to write isolated tests from external behaviour. To accomplish this, you can use mocks. You can also check that a function was called with certain arguments or even a number of times with a spy.
44

5+
Temporary files created by spies are isolated per test run, so they work reliably when executing tests in parallel.
6+
7+
Spies record their calls in temporary files scoped to each test run.
8+
This avoids clashes between processes and allows spies to work reliably when tests execute in parallel using `BASHUNIT_PARALLEL_RUN`.
9+
510
## mock
611
> `mock "function" "body"`
712
@@ -97,26 +102,30 @@ function test_failure() {
97102
:::
98103

99104
## assert_have_been_called_with
100-
> `assert_have_been_called_with "expected" "spy"`
105+
> `assert_have_been_called_with "expected" "spy" [call_index]`
101106
102-
Reports an error if `callable` is not called with `expected`.
107+
Reports an error if `spy` is not called with `expected`. When `call_index` is
108+
provided, the assertion checks the arguments of that specific call (starting at
109+
1). Without `call_index` it checks the last invocation.
103110

104111
::: code-group
105112
```bash [Example]
106113
function test_success() {
107114
spy ps
108115

109-
ps foo bar
116+
ps foo
117+
ps bar
110118

111-
assert_have_been_called_with "foo bar" ps
119+
assert_have_been_called_with "foo" ps 1
120+
assert_have_been_called_with "bar" ps 2
112121
}
113122

114123
function test_failure() {
115124
spy ps
116125

117-
ps bar foo
126+
ps bar
118127

119-
assert_have_been_called_with "foo bar" ps
128+
assert_have_been_called_with "foo" ps 1
120129
}
121130
```
122131
:::
@@ -147,3 +156,26 @@ function test_failure() {
147156
}
148157
```
149158
:::
159+
160+
## assert_not_called
161+
> `assert_not_called "spy"`
162+
163+
Reports an error if `spy` has been executed at least once.
164+
165+
::: code-group
166+
```bash [Example]
167+
function test_success() {
168+
spy ps
169+
170+
assert_not_called ps
171+
}
172+
173+
function test_failure() {
174+
spy ps
175+
176+
ps
177+
178+
assert_not_called ps
179+
}
180+
```
181+
:::

src/globals.sh

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,29 @@ function random_str() {
4040
function temp_file() {
4141
local prefix=${1:-bashunit}
4242
mkdir -p /tmp/bashunit/tmp && chmod -R 777 /tmp/bashunit/tmp
43-
mktemp /tmp/bashunit/tmp/"$prefix".XXXXXXX
43+
local test_prefix=""
44+
if [[ -n "${BASHUNIT_CURRENT_TEST_ID:-}" ]]; then
45+
test_prefix="${BASHUNIT_CURRENT_TEST_ID}_"
46+
fi
47+
mktemp /tmp/bashunit/tmp/"${test_prefix}${prefix}".XXXXXXX
4448
}
4549

4650
function temp_dir() {
4751
local prefix=${1:-bashunit}
4852
mkdir -p /tmp/bashunit/tmp && chmod -R 777 /tmp/bashunit/tmp
49-
mktemp -d /tmp/bashunit/tmp/"$prefix".XXXXXXX
53+
local test_prefix=""
54+
if [[ -n "${BASHUNIT_CURRENT_TEST_ID:-}" ]]; then
55+
test_prefix="${BASHUNIT_CURRENT_TEST_ID}_"
56+
fi
57+
mktemp -d /tmp/bashunit/tmp/"${test_prefix}${prefix}".XXXXXXX
5058
}
5159

5260
function cleanup_temp_files() {
53-
rm -rf /tmp/bashunit/tmp/*
61+
if [[ -n "${BASHUNIT_CURRENT_TEST_ID:-}" ]]; then
62+
rm -rf /tmp/bashunit/tmp/"${BASHUNIT_CURRENT_TEST_ID}"_*
63+
else
64+
rm -rf /tmp/bashunit/tmp/*
65+
fi
5466
}
5567

5668
# shellcheck disable=SC2145

src/helpers.sh

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,9 @@ function helper::get_provider_data() {
158158
fi
159159

160160
data_provider_function=$(\
161-
grep -B 1 "function $function_name()" "$script" |\
162-
grep "# data_provider " |\
163-
sed -E -e 's/\ *# data_provider (.*)$/\1/g'\
161+
grep -B 2 "function $function_name()" "$script" |\
162+
grep -E "# *@?data_provider " |\
163+
sed -E -e 's/\ *# *@?data_provider (.*)$/\1/g'\
164164
|| true
165165
)
166166

@@ -190,19 +190,18 @@ function helpers::get_latest_tag() {
190190
function helpers::find_total_tests() {
191191
local filter=${1:-}
192192
local files=("${@:2}")
193-
local total_count=0
194-
195-
for file in "${files[@]}"; do
196-
local count
197-
if [[ -n "$filter" ]]; then
198-
count=$(grep -r -E "^\s*function\s+test.*$filter" "$file" --include=\*.sh 2>/dev/null | wc -l)
199-
else
200-
count=$(grep -r -E '^\s*function\s+test' "$file" --include=\*.sh 2>/dev/null | wc -l)
201-
fi
202-
total_count=$((total_count + count))
203-
done
204-
205-
echo "$total_count"
193+
194+
if [[ ${#files[@]} -eq 0 ]]; then
195+
echo 0
196+
return
197+
fi
198+
199+
local pattern='^\s*function\s+test'
200+
if [[ -n "$filter" ]]; then
201+
pattern+=".*$filter"
202+
fi
203+
204+
grep -r -E "$pattern" --include="*.sh" "${files[@]}" 2>/dev/null | wc -l | xargs
206205
}
207206

208207
function helper::load_test_files() {

src/main.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ function main::exec_tests() {
2020
trap '[[ $? -eq $EXIT_CODE_STOP_ON_FAILURE ]] && main::handle_stop_on_failure_sync' EXIT
2121

2222
if env::is_parallel_run_enabled && ! parallel::is_enabled; then
23-
printf "%sWarning: Parallel tests are working only for macOS and Ubuntu.\n" "${_COLOR_INCOMPLETE}"
24-
printf "For other OS (Linux/Alpine, Windows), --parallel is not enabled due to inconsistent results,\n"
23+
printf "%sWarning: Parallel tests are supported on macOS, Ubuntu and Windows.\n" "${_COLOR_INCOMPLETE}"
24+
printf "For other OS (like Alpine), --parallel is not enabled due to inconsistent results,\n"
2525
printf "particularly involving race conditions.%s " "${_COLOR_DEFAULT}"
2626
printf "%sFallback using --no-parallel%s\n" "${_COLOR_SKIPPED}" "${_COLOR_DEFAULT}"
2727
fi

0 commit comments

Comments
 (0)