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
132 changes: 78 additions & 54 deletions src/assert.sh

Large diffs are not rendered by default.

14 changes: 10 additions & 4 deletions src/assert_arrays.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ function assert_array_contains() {
local -a actual
actual=("$@")

if ! [[ "${actual[*]:-}" == *"$expected"* ]]; then
case "${actual[*]:-}" in
*"$expected"*)
;;
*)
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${actual[*]}" "to contain" "${expected}"
return
fi
;;
esac

bashunit::state::add_assertions_passed
}
Expand All @@ -34,11 +38,13 @@ function assert_array_not_contains() {
local -a actual
actual=("$@")

if [[ "${actual[*]:-}" == *"$expected"* ]]; then
case "${actual[*]:-}" in
*"$expected"*)
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${actual[*]}" "to not contain" "${expected}"
return
fi
;;
esac

bashunit::state::add_assertions_passed
}
14 changes: 7 additions & 7 deletions src/assert_dates.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function bashunit::date::to_epoch() {
return 0
}
# Try GNU date with normalized (space-separated) input
if [[ "$normalized" != "$input" ]]; then
if [ "$normalized" != "$input" ]; then
epoch=$(date -d "$normalized" +%s 2>/dev/null) && {
echo "$epoch"
return 0
Expand Down Expand Up @@ -71,7 +71,7 @@ function assert_date_equals() {
local actual
actual="$(bashunit::date::to_epoch "$2")"

if [[ "$actual" -ne "$expected" ]]; then
if [ "$actual" -ne "$expected" ]; then
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
Expand All @@ -92,7 +92,7 @@ function assert_date_before() {
local actual
actual="$(bashunit::date::to_epoch "$2")"

if ! [[ "$actual" -lt "$expected" ]]; then
if [ "$actual" -ge "$expected" ]; then
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
Expand All @@ -113,7 +113,7 @@ function assert_date_after() {
local actual
actual="$(bashunit::date::to_epoch "$2")"

if ! [[ "$actual" -gt "$expected" ]]; then
if [ "$actual" -le "$expected" ]; then
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
Expand All @@ -136,7 +136,7 @@ function assert_date_within_range() {
local actual
actual="$(bashunit::date::to_epoch "$3")"

if [[ "$actual" -lt "$from" ]] || [[ "$actual" -gt "$to" ]]; then
if [ "$actual" -lt "$from" ] || [ "$actual" -gt "$to" ]; then
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
Expand All @@ -159,11 +159,11 @@ function assert_date_within_delta() {
local delta="$3"

local diff=$((actual - expected))
if [[ "$diff" -lt 0 ]]; then
if [ "$diff" -lt 0 ]; then
diff=$((-diff))
fi

if [[ "$diff" -gt "$delta" ]]; then
if [ "$diff" -gt "$delta" ]; then
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
Expand Down
12 changes: 6 additions & 6 deletions src/assert_files.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function assert_file_exists() {
test_fn="$(bashunit::helper::find_test_function_name)"
local label="${3:-$(bashunit::helper::normalize_test_function_name "$test_fn")}"

if [[ ! -f "$expected" ]]; then
if [ ! -f "$expected" ]; then
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${expected}" "to exist but" "do not exist"
return
Expand All @@ -25,7 +25,7 @@ function assert_file_not_exists() {
test_fn="$(bashunit::helper::find_test_function_name)"
local label="${3:-$(bashunit::helper::normalize_test_function_name "$test_fn")}"

if [[ -f "$expected" ]]; then
if [ -f "$expected" ]; then
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${expected}" "to not exist but" "the file exists"
return
Expand All @@ -42,7 +42,7 @@ function assert_is_file() {
test_fn="$(bashunit::helper::find_test_function_name)"
local label="${3:-$(bashunit::helper::normalize_test_function_name "$test_fn")}"

if [[ ! -f "$expected" ]]; then
if [ ! -f "$expected" ]; then
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${expected}" "to be a file" "but is not a file"
return
Expand All @@ -59,7 +59,7 @@ function assert_is_file_empty() {
test_fn="$(bashunit::helper::find_test_function_name)"
local label="${3:-$(bashunit::helper::normalize_test_function_name "$test_fn")}"

if [[ -s "$expected" ]]; then
if [ -s "$expected" ]; then
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${expected}" "to be empty" "but is not empty"
return
Expand All @@ -74,7 +74,7 @@ function assert_files_equals() {
local expected="$1"
local actual="$2"

if [[ "$(diff -u "$expected" "$actual")" != '' ]]; then
if [ "$(diff -u "$expected" "$actual")" != '' ]; then
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
Expand All @@ -95,7 +95,7 @@ function assert_files_not_equals() {
local expected="$1"
local actual="$2"

if [[ "$(diff -u "$expected" "$actual")" == '' ]]; then
if [ "$(diff -u "$expected" "$actual")" = '' ]; then
local test_fn
test_fn="$(bashunit::helper::find_test_function_name)"
local label
Expand Down
18 changes: 9 additions & 9 deletions src/assert_folders.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function assert_directory_exists() {
test_fn="$(bashunit::helper::find_test_function_name)"
local label="${2:-$(bashunit::helper::normalize_test_function_name "$test_fn")}"

if [[ ! -d "$expected" ]]; then
if [ ! -d "$expected" ]; then
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${expected}" "to exist but" "do not exist"
return
Expand All @@ -25,7 +25,7 @@ function assert_directory_not_exists() {
test_fn="$(bashunit::helper::find_test_function_name)"
local label="${2:-$(bashunit::helper::normalize_test_function_name "$test_fn")}"

if [[ -d "$expected" ]]; then
if [ -d "$expected" ]; then
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${expected}" "to not exist but" "the directory exists"
return
Expand All @@ -42,7 +42,7 @@ function assert_is_directory() {
test_fn="$(bashunit::helper::find_test_function_name)"
local label="${2:-$(bashunit::helper::normalize_test_function_name "$test_fn")}"

if [[ ! -d "$expected" ]]; then
if [ ! -d "$expected" ]; then
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${expected}" "to be a directory" "but is not a directory"
return
Expand All @@ -59,7 +59,7 @@ function assert_is_directory_empty() {
test_fn="$(bashunit::helper::find_test_function_name)"
local label="${2:-$(bashunit::helper::normalize_test_function_name "$test_fn")}"

if [[ ! -d "$expected" || -n "$(ls -A "$expected")" ]]; then
if [ ! -d "$expected" ] || [ -n "$(ls -A "$expected")" ]; then
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${expected}" "to be empty" "but is not empty"
return
Expand All @@ -76,7 +76,7 @@ function assert_is_directory_not_empty() {
test_fn="$(bashunit::helper::find_test_function_name)"
local label="${2:-$(bashunit::helper::normalize_test_function_name "$test_fn")}"

if [[ ! -d "$expected" || -z "$(ls -A "$expected")" ]]; then
if [ ! -d "$expected" ] || [ -z "$(ls -A "$expected")" ]; then
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${expected}" "to not be empty" "but is empty"
return
Expand All @@ -93,7 +93,7 @@ function assert_is_directory_readable() {
test_fn="$(bashunit::helper::find_test_function_name)"
local label="${2:-$(bashunit::helper::normalize_test_function_name "$test_fn")}"

if [[ ! -d "$expected" || ! -r "$expected" || ! -x "$expected" ]]; then
if [ ! -d "$expected" ] || [ ! -r "$expected" ] || [ ! -x "$expected" ]; then
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${expected}" "to be readable" "but is not readable"
return
Expand All @@ -110,7 +110,7 @@ function assert_is_directory_not_readable() {
test_fn="$(bashunit::helper::find_test_function_name)"
local label="${2:-$(bashunit::helper::normalize_test_function_name "$test_fn")}"

if [[ ! -d "$expected" ]] || [[ -r "$expected" && -x "$expected" ]]; then
if [ ! -d "$expected" ] || { [ -r "$expected" ] && [ -x "$expected" ]; }; then
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${expected}" "to be not readable" "but is readable"
return
Expand All @@ -127,7 +127,7 @@ function assert_is_directory_writable() {
test_fn="$(bashunit::helper::find_test_function_name)"
local label="${2:-$(bashunit::helper::normalize_test_function_name "$test_fn")}"

if [[ ! -d "$expected" || ! -w "$expected" ]]; then
if [ ! -d "$expected" ] || [ ! -w "$expected" ]; then
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${expected}" "to be writable" "but is not writable"
return
Expand All @@ -144,7 +144,7 @@ function assert_is_directory_not_writable() {
test_fn="$(bashunit::helper::find_test_function_name)"
local label="${2:-$(bashunit::helper::normalize_test_function_name "$test_fn")}"

if [[ ! -d "$expected" || -w "$expected" ]]; then
if [ ! -d "$expected" ] || [ -w "$expected" ]; then
bashunit::assert::mark_failed
bashunit::console_results::print_failed_test "${label}" "${expected}" "to be not writable" "but is writable"
return
Expand Down
6 changes: 3 additions & 3 deletions src/assert_snapshot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function assert_match_snapshot() {
test_fn="$(bashunit::helper::find_test_function_name)"
local snapshot_file=$(bashunit::snapshot::resolve_file "${2:-}" "$test_fn")

if [[ ! -f "$snapshot_file" ]]; then
if [ ! -f "$snapshot_file" ]; then
bashunit::snapshot::initialize "$snapshot_file" "$actual"
return
fi
Expand All @@ -21,7 +21,7 @@ function assert_match_snapshot_ignore_colors() {
test_fn="$(bashunit::helper::find_test_function_name)"
local snapshot_file=$(bashunit::snapshot::resolve_file "${2:-}" "$test_fn")

if [[ ! -f "$snapshot_file" ]]; then
if [ ! -f "$snapshot_file" ]; then
bashunit::snapshot::initialize "$snapshot_file" "$actual"
return
fi
Expand Down Expand Up @@ -56,7 +56,7 @@ function bashunit::snapshot::resolve_file() {
local file_hint="$1"
local func_name="$2"

if [[ -n "$file_hint" ]]; then
if [ -n "$file_hint" ]; then
echo "$file_hint"
else
local dir="./$(dirname "${BASH_SOURCE[2]}")/snapshots"
Expand Down
44 changes: 20 additions & 24 deletions src/benchmark.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,33 @@ function bashunit::benchmark::parse_annotations() {
local annotation
annotation=$(awk "/function[[:space:]]+${fn_name}[[:space:]]*\(/ {print prev; exit} {prev=\$0}" "$script")

local _re='@revs=([0-9]+)'
if [[ "$annotation" =~ $_re ]]; then
revs="${BASH_REMATCH[1]}"
local _extracted
_extracted=$(echo "$annotation" | sed -n 's/.*@revs=\([0-9][0-9]*\).*/\1/p')
if [ -n "$_extracted" ]; then
revs="$_extracted"
else
_re='@revolutions=([0-9]+)'
if [[ "$annotation" =~ $_re ]]; then
revs="${BASH_REMATCH[1]}"
_extracted=$(echo "$annotation" | sed -n 's/.*@revolutions=\([0-9][0-9]*\).*/\1/p')
if [ -n "$_extracted" ]; then
revs="$_extracted"
fi
fi

_re='@its=([0-9]+)'
if [[ "$annotation" =~ $_re ]]; then
its="${BASH_REMATCH[1]}"
_extracted=$(echo "$annotation" | sed -n 's/.*@its=\([0-9][0-9]*\).*/\1/p')
if [ -n "$_extracted" ]; then
its="$_extracted"
else
_re='@iterations=([0-9]+)'
if [[ "$annotation" =~ $_re ]]; then
its="${BASH_REMATCH[1]}"
_extracted=$(echo "$annotation" | sed -n 's/.*@iterations=\([0-9][0-9]*\).*/\1/p')
if [ -n "$_extracted" ]; then
its="$_extracted"
fi
fi

_re='@max_ms=([0-9.]+)'
if [[ "$annotation" =~ $_re ]]; then
max_ms="${BASH_REMATCH[1]}"
else
_re='@max_ms=([0-9.]+)'
if [[ "$annotation" =~ $_re ]]; then
max_ms="${BASH_REMATCH[1]}"
fi
_extracted=$(echo "$annotation" | sed -n 's/.*@max_ms=\([0-9.][0-9.]*\).*/\1/p')
if [ -n "$_extracted" ]; then
max_ms="$_extracted"
fi

if [[ -n "$max_ms" ]]; then
if [ -n "$max_ms" ]; then
echo "$revs" "$its" "$max_ms"
else
echo "$revs" "$its"
Expand Down Expand Up @@ -122,7 +118,7 @@ function bashunit::benchmark::print_results() {
local has_threshold=false
local val
for val in "${_BASHUNIT_BENCH_MAX_MILLIS[@]+"${_BASHUNIT_BENCH_MAX_MILLIS[@]}"}"; do
if [[ -n "$val" ]]; then
if [ -n "$val" ]; then
has_threshold=true
break
fi
Expand All @@ -142,12 +138,12 @@ function bashunit::benchmark::print_results() {
local avg="${_BASHUNIT_BENCH_AVERAGES[$i]:-}"
local max_ms="${_BASHUNIT_BENCH_MAX_MILLIS[$i]:-}"

if [[ -z "$max_ms" ]]; then
if [ -z "$max_ms" ]; then
printf '%-40s %6s %6s %10s\n' "$name" "$revs" "$its" "$avg"
continue
fi

if [[ "$avg" -le "$max_ms" ]]; then
if [ "$avg" -le "$max_ms" ]; then
local raw="≤ ${max_ms}"
local padded
padded=$(printf "%14s" "$raw")
Expand Down
10 changes: 5 additions & 5 deletions src/check_os.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,26 @@ function bashunit::check_os::init() {
}

function bashunit::check_os::is_ubuntu() {
command -v apt >/dev/null
command -v apt >/dev/null 2>&1
}

function bashunit::check_os::is_alpine() {
command -v apk >/dev/null
command -v apk >/dev/null 2>&1
}

function bashunit::check_os::is_nixos() {
[[ -f /etc/NIXOS ]] && return 0
[ -f /etc/NIXOS ] && return 0
grep -q '^ID=nixos' /etc/os-release 2>/dev/null
}

_BASHUNIT_UNAME="$(uname)"

function bashunit::check_os::is_linux() {
[[ "$_BASHUNIT_UNAME" == "Linux" ]]
[ "$_BASHUNIT_UNAME" = "Linux" ]
}

function bashunit::check_os::is_macos() {
[[ "$_BASHUNIT_UNAME" == "Darwin" ]]
[ "$_BASHUNIT_UNAME" = "Darwin" ]
}

function bashunit::check_os::is_windows() {
Expand Down
Loading
Loading