Skip to content

Commit e78ee50

Browse files
authored
Merge pull request #456 from TypedDevs/fix/parallel-on-nixos
Fix parallel and compgen issue on NixOS
2 parents 7ed61b1 + 315202a commit e78ee50

File tree

11 files changed

+49
-39
lines changed

11 files changed

+49
-39
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
- Fix parallel and `compgen` issue on NixOS
6+
37
## [0.22.2](https://github.com/TypedDevs/bashunit/compare/0.22.1...0.22.2) - 2025-07-26
48

59
- Fix broken core snapshot tests

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ test/list:
6161
@echo $(TEST_SCRIPTS) | tr ' ' '\n'
6262

6363
test: $(TEST_SCRIPTS)
64-
@./bashunit $(TEST_SCRIPTS)
64+
@bash ./bashunit $(TEST_SCRIPTS)
6565

6666
test/watch: $(TEST_SCRIPTS)
67-
@./bashunit $(TEST_SCRIPTS)
68-
@fswatch -m poll_monitor -or $(SRC_SCRIPTS_DIR) $(TEST_SCRIPTS_DIR) .env Makefile | xargs -n1 ./bashunit $(TEST_SCRIPTS)
67+
@bash ./bashunit $(TEST_SCRIPTS)
68+
@fswatch -m poll_monitor -or $(SRC_SCRIPTS_DIR) $(TEST_SCRIPTS_DIR) .env Makefile | xargs -n1 bash ./bashunit $(TEST_SCRIPTS)
6969

7070
docker/alpine:
7171
@docker run --rm -it -v "$(shell pwd)":/project -w /project alpine:latest \

src/env.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
set -o allexport
66
# shellcheck source=/dev/null
7-
[[ -f ".env" ]] && source .env set
7+
[[ -f ".env" ]] && source .env
88
set +o allexport
99

1010
_DEFAULT_DEFAULT_PATH="tests"
@@ -143,7 +143,7 @@ function env::print_verbose() {
143143
EXIT_CODE_STOP_ON_FAILURE=4
144144
# Use a unique directory per run to avoid conflicts when bashunit is invoked
145145
# recursively or multiple instances are executed in parallel.
146-
TEMP_DIR_PARALLEL_TEST_SUITE="/tmp/bashunit/parallel/${_OS:-Unknown}/$(random_str 8)"
146+
TEMP_DIR_PARALLEL_TEST_SUITE="${TMPDIR:-/tmp}/bashunit/parallel/${_OS:-Unknown}/$(random_str 8)"
147147
TEMP_FILE_PARALLEL_STOP_ON_FAILURE="$TEMP_DIR_PARALLEL_TEST_SUITE/.stop-on-failure"
148148
TERMINAL_WIDTH="$(env::find_terminal_width)"
149149
FAILURES_OUTPUT_PATH=$(mktemp)

src/globals.sh

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,30 +39,32 @@ function random_str() {
3939

4040
function temp_file() {
4141
local prefix=${1:-bashunit}
42-
mkdir -p /tmp/bashunit/tmp && chmod -R 777 /tmp/bashunit/tmp
42+
local base_dir="${TMPDIR:-/tmp}/bashunit/tmp"
43+
mkdir -p "$base_dir" && chmod -R 777 "$base_dir"
4344
local test_prefix=""
4445
if [[ -n "${BASHUNIT_CURRENT_TEST_ID:-}" ]]; then
4546
test_prefix="${BASHUNIT_CURRENT_TEST_ID}_"
4647
fi
47-
mktemp /tmp/bashunit/tmp/"${test_prefix}${prefix}".XXXXXXX
48+
mktemp "$base_dir/${test_prefix}${prefix}.XXXXXXX"
4849
}
4950

5051
function temp_dir() {
5152
local prefix=${1:-bashunit}
52-
mkdir -p /tmp/bashunit/tmp && chmod -R 777 /tmp/bashunit/tmp
53+
local base_dir="${TMPDIR:-/tmp}/bashunit/tmp"
54+
mkdir -p "$base_dir" && chmod -R 777 "$base_dir"
5355
local test_prefix=""
5456
if [[ -n "${BASHUNIT_CURRENT_TEST_ID:-}" ]]; then
5557
test_prefix="${BASHUNIT_CURRENT_TEST_ID}_"
5658
fi
57-
mktemp -d /tmp/bashunit/tmp/"${test_prefix}${prefix}".XXXXXXX
59+
mktemp -d "$base_dir/${test_prefix}${prefix}.XXXXXXX"
5860
}
5961

6062
function cleanup_temp_files() {
6163
internal_log "cleanup_temp_files"
6264
if [[ -n "${BASHUNIT_CURRENT_TEST_ID:-}" ]]; then
63-
rm -rf /tmp/bashunit/tmp/"${BASHUNIT_CURRENT_TEST_ID}"_*
65+
rm -rf "${TMPDIR:-/tmp}/bashunit/tmp/${BASHUNIT_CURRENT_TEST_ID}"_*
6466
else
65-
rm -rf /tmp/bashunit/tmp/*
67+
rm -rf "${TMPDIR:-/tmp}/bashunit/tmp"/*
6668
fi
6769
}
6870

src/math.sh

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#!/usr/bin/env bash
22

3-
if dependencies::has_bc; then
4-
# bc is better than awk because bc has no integer limits.
5-
function math::calculate() {
3+
function math::calculate() {
4+
if dependencies::has_bc; then
65
echo "$*" | bc
7-
}
8-
elif dependencies::has_awk; then
9-
function math::calculate() {
10-
awk "BEGIN { print ""$*"" }"
11-
}
12-
fi
6+
elif [[ "$*" == *.* ]] && dependencies::has_awk; then
7+
# Use awk for floating point calculations when bc is unavailable
8+
awk "BEGIN { print ($*) }"
9+
else
10+
# Fallback to shell arithmetic which has good integer precision
11+
local result=$(( $* ))
12+
echo "$result"
13+
fi
14+
}

src/parallel.sh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@ function parallel::aggregate_test_results() {
1212
local total_snapshot=0
1313

1414
for script_dir in "$temp_dir_parallel_test_suite"/*; do
15-
if ! compgen -G "$script_dir"/*.result > /dev/null; then
15+
shopt -s nullglob
16+
local result_files=("$script_dir"/*.result)
17+
shopt -u nullglob
18+
19+
if [ ${#result_files[@]} -eq 0 ]; then
1620
printf "%sNo tests found%s" "$_COLOR_SKIPPED" "$_COLOR_DEFAULT"
1721
continue
1822
fi
1923

20-
for result_file in "$script_dir"/*.result; do
24+
for result_file in "${result_files[@]}"; do
2125
local result_line
2226
result_line=$(tail -n 1 "$result_file")
2327

src/runner.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,8 @@ function runner::run_test() {
235235
exec 3>&-
236236

237237
local end_time=$(clock::now)
238-
local duration_ns=$(math::calculate "($end_time - $start_time) ")
239-
local duration=$(math::calculate "$duration_ns / 1000000")
238+
local duration_ns=$((end_time - start_time))
239+
local duration=$((duration_ns / 1000000))
240240

241241
if env::is_verbose_enabled; then
242242
if env::is_simple_output_enabled; then

tests/acceptance/bashunit_init_test.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ function test_bashunit_init_creates_structure() {
1919
# generate test scaffolding
2020
../../bashunit --init > /tmp/init.log
2121
# perform the assertions
22-
assert_file_exists tests/example_test.sh
23-
assert_file_exists tests/bootstrap.sh
22+
assert_file_exists "tests/example_test.sh"
23+
assert_file_exists "tests/bootstrap.sh"
2424
# return to the original working directory
2525
popd >/dev/null
2626
}
2727

2828
function test_bashunit_init_custom_directory() {
2929
pushd "$TMP_DIR" >/dev/null
3030
../../bashunit --init custom > /tmp/init.log
31-
assert_file_exists custom/example_test.sh
32-
assert_file_exists custom/bootstrap.sh
31+
assert_file_exists "custom/example_test.sh"
32+
assert_file_exists "custom/bootstrap.sh"
3333
popd >/dev/null
3434
}
3535

@@ -39,8 +39,8 @@ function test_bashunit_init_updates_env() {
3939
pushd "$TMP_DIR" >/dev/null
4040
echo "BASHUNIT_BOOTSTRAP=old/bootstrap.sh" > .env
4141
../../bashunit --init custom > /tmp/init.log
42-
assert_file_exists custom/example_test.sh
43-
assert_file_exists custom/bootstrap.sh
42+
assert_file_exists "custom/example_test.sh"
43+
assert_file_exists "custom/bootstrap.sh"
4444
assert_file_contains .env "#BASHUNIT_BOOTSTRAP=old/bootstrap.sh"
4545
assert_file_contains .env "BASHUNIT_BOOTSTRAP=custom/bootstrap.sh"
4646
popd >/dev/null

tests/acceptance/parallel_spy_parallel_test.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#!/usr/bin/env bash
2+
# shellcheck disable=SC2155
23
set -euo pipefail
34

45
function test_spies_work_in_parallel() {
5-
local file1=tests/acceptance/fixtures/test_parallel_spy_file1.sh
6-
local file2=tests/acceptance/fixtures/test_parallel_spy_file2.sh
6+
local file1="$(current_dir)/fixtures/test_parallel_spy_file1.sh"
7+
local file2="$(current_dir)/fixtures/test_parallel_spy_file2.sh"
78

89
./bashunit --parallel "$file1" "$file2"
910
assert_successful_code

tests/unit/clock_test.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,8 @@ function test_now_on_windows_without_without_powershell() {
6969
}
7070

7171
function test_now_on_osx_without_perl() {
72-
if ! check_os::is_macos; then
73-
skip
74-
return
72+
if check_os::is_windows; then
73+
skip && return
7574
fi
7675

7776
mock_macos

0 commit comments

Comments
 (0)