Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- `init` writes the bootstrap path and a custom tests directory to `.env` quoted, so a directory with a space runs afterwards instead of breaking every next run (#1411)
- Failure source context stays inside a one-line test instead of listing the next function's assertions (#1420)
- Parallel runs count tests under relative hidden paths and keep their results separate from matching visible paths (#1393)
- Duplicate test functions are detected when the opening brace is on the next line instead of silently replacing the first definition (#1421)

### Changed

Expand Down
12 changes: 6 additions & 6 deletions src/helper/discovery.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ function bashunit::helper::check_duplicate_functions() {
script="$BASHUNIT_WORKING_DIR/$script"
fi

# One awk pass over the file finds each test-function definition and emits only
# the names seen more than once, folding the former grep + awk + sort + uniq
# chain into a single awk (#761). The END block insertion-sorts the (tiny,
# usually empty) duplicate list itself, so no `sort` fork is needed to keep
# the output deterministic.
# One awk pass over the file finds each test-function definition (the opening
# brace may sit on the next line) and emits only the names seen more than
# once, folding the former grep + awk + sort + uniq chain into a single awk
# (#761). The END block insertion-sorts the (tiny, usually empty) duplicate
# list itself, so no `sort` fork is needed to keep the output deterministic.
local duplicates
duplicates=$(awk '
/^[[:space:]]*(function[[:space:]]+)?test[a-zA-Z_][a-zA-Z0-9_]*[[:space:]]*\(\)[[:space:]]*\{/ {
/^[[:space:]]*(function[[:space:]]+)?test[a-zA-Z_][a-zA-Z0-9_]*[[:space:]]*\(\)[[:space:]]*(\{|$)/ {
for (i = 1; i <= NF; i++) {
if ($i ~ /^test[a-zA-Z_][a-zA-Z0-9_]*\(\)$/) {
name = $i
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/helper/helper_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,21 @@ function test_check_duplicate_functions_with_duplicates() {
assert_general_error "$(bashunit::helper::check_duplicate_functions "$file")"
}

function test_check_duplicate_functions_with_the_brace_on_the_next_line() {
local file detail
file="$(bashunit::temp_file duplicate_next_line).sh"
printf 'function %s()\n{\n :\n}\nfunction %s()\n{\n :\n}\n' \
test_twice test_twice >"$file"

assert_general_error "$(bashunit::helper::check_duplicate_functions "$file")"

detail=$(
bashunit::helper::check_duplicate_functions "$file" >/dev/null 2>&1 || true
bashunit::state::get_duplicated_function_details
)
assert_contains "test_twice (lines 1, 5)" "$detail"
}

function test_check_duplicate_functions_without_duplicates() {
local file
file="$(bashunit::current_dir)/../fixtures/no_duplicate_functions.sh"
Expand Down
Loading