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 @@ -4,6 +4,7 @@

### Fixed
- Custom assertions now display the correct test function name in failure messages
- Data providers now work when `set_up_before_script` changes directory

## [0.28.0](https://github.com/TypedDevs/bashunit/compare/0.27.0...0.28.0) - 2025-12-01

Expand Down
4 changes: 4 additions & 0 deletions bashunit
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ declare -r BASHUNIT_VERSION="0.28.0"
declare -r BASHUNIT_ROOT_DIR="$(dirname "${BASH_SOURCE[0]}")"
export BASHUNIT_ROOT_DIR

# Capture working directory at startup (before any test changes it)
declare -r BASHUNIT_WORKING_DIR="$PWD"
export BASHUNIT_WORKING_DIR

source "$BASHUNIT_ROOT_DIR/src/dev/debug.sh"
source "$BASHUNIT_ROOT_DIR/src/check_os.sh"
source "$BASHUNIT_ROOT_DIR/src/str.sh"
Expand Down
11 changes: 11 additions & 0 deletions src/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ function helper::decode_base64() {
function helper::check_duplicate_functions() {
local script="$1"

# Handle directory changes in set_up_before_script (issue #529)
if [[ ! -f "$script" && -n "${BASHUNIT_WORKING_DIR:-}" ]]; then
script="$BASHUNIT_WORKING_DIR/$script"
fi

local filtered_lines
filtered_lines=$(grep -E '^[[:space:]]*(function[[:space:]]+)?test[a-zA-Z_][a-zA-Z0-9_]*\s*\(\)\s*\{' "$script")

Expand Down Expand Up @@ -244,6 +249,12 @@ function helper::get_provider_data() {
local function_name="$1"
local script="$2"

# Handle directory changes in set_up_before_script (issue #529)
# If relative path doesn't exist, try with BASHUNIT_WORKING_DIR
if [[ ! -f "$script" && -n "${BASHUNIT_WORKING_DIR:-}" ]]; then
script="$BASHUNIT_WORKING_DIR/$script"
fi

if [[ ! -f "$script" ]]; then
return
fi
Expand Down
20 changes: 20 additions & 0 deletions tests/functional/provider_with_cd_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash
set -euo pipefail

# Regression test for https://github.com/TypedDevs/bashunit/issues/529
# Data providers should work even when set_up_before_script changes directory

function set_up_before_script() {
cd "$(temp_dir)" || return 1
}

# @data_provider provide_data
function test_data_provider_works_after_cd_in_set_up_before_script() {
local value="$1"

assert_equals "expected_value" "$value"
}

function provide_data() {
echo "expected_value"
}
Loading