From ab451beefac51fa08020163f2fe5b0169f866c4b Mon Sep 17 00:00:00 2001 From: Matt Skinner <62670961+skinner-m-c@users.noreply.github.com> Date: Mon, 30 Sep 2024 19:17:20 -0400 Subject: [PATCH 1/6] feature: remove curl perl and coreutils as required dependencies --- .github/CONTRIBUTING.md | 2 +- .github/workflows/tests.yml | 2 +- CHANGELOG.md | 7 +++ Makefile | 2 +- bashunit | 6 +++ install.sh | 8 +++- src/check_os.sh | 67 +++++++++++++++++++++----- src/clock.sh | 49 ++++++++++++++++--- src/console_results.sh | 2 +- src/dependencies.sh | 30 ++++++++++++ src/io.sh | 13 ++++++ src/math.sh | 12 +++++ src/runner.sh | 8 +++- src/upgrade.sh | 6 ++- tests/globals.sh | 64 +++++++++++++++++++++++++ tests/unit/assert_snapshot_test.sh | 8 +++- tests/unit/check_os_test.sh | 75 ++++++++++++++++++++++++++++++ tests/unit/clock_test.sh | 20 ++++---- tests/unit/console_results_test.sh | 21 ++++++++- tests/unit/dependencies_test.sh | 37 +++++++++++++++ 20 files changed, 399 insertions(+), 40 deletions(-) create mode 100644 src/dependencies.sh create mode 100644 src/io.sh create mode 100644 src/math.sh create mode 100644 tests/unit/check_os_test.sh create mode 100644 tests/unit/dependencies_test.sh diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index 3bf5705c..2a604f75 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -91,7 +91,7 @@ docker run --rm -it -v "$(pwd)":/project -w /project ubuntu:latest \ make test/alpine # or docker run --rm -it -v "$(pwd)":/project -w /project alpine:latest \ - sh -c "apk add bash make shellcheck git curl perl && make test" + sh -c "apk add bash make shellcheck git && make test" ``` ## Coding Guidelines diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index ddae3640..18e159a3 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -58,7 +58,7 @@ jobs: run: | docker run --rm -v "$(pwd)":/project alpine:latest /bin/sh -c " \ apk update && \ - apk add --no-cache bash make git curl perl coreutils && \ + apk add --no-cache bash make git && \ adduser -D builder && \ chown -R builder /project && \ su - builder -c 'cd /project; make test';" diff --git a/CHANGELOG.md b/CHANGELOG.md index 3aeff465..64efd439 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,13 @@ - Display failing tests after running the entire suite - Added defer expressions with `eval` when using standalone assertions - Fixed simple output for non-successful states +- Some required dependencies now optional: + - perl + - coreutils +- Switch to testing the environment of capabilities rather than assuming various operating systems and Linux + distributions have programs installed. +- Upgrade and install script can now use `wget` if `curl` is not installed +- Tests can be also be timed by making use of `EPOCHREALTIME` on supported system. ## [0.16.0](https://github.com/TypedDevs/bashunit/compare/0.15.0...0.16.0) - 2024-09-15 diff --git a/Makefile b/Makefile index f1fe0f66..1a1a3cf2 100644 --- a/Makefile +++ b/Makefile @@ -70,7 +70,7 @@ test/watch: $(TEST_SCRIPTS) docker/alpine: @docker run --rm -it -v "$(shell pwd)":/project -w /project alpine:latest \ - sh -c "apk add bash make shellcheck git curl perl coreutils && bash" + sh -c "apk add bash make shellcheck git && bash" env/example: @echo "Copying variables without the values from .env into .env.example" diff --git a/bashunit b/bashunit index 8ac501e8..4b2034ba 100755 --- a/bashunit +++ b/bashunit @@ -10,6 +10,9 @@ export BASHUNIT_ROOT_DIR source "$BASHUNIT_ROOT_DIR/src/dev/debug.sh" source "$BASHUNIT_ROOT_DIR/src/str.sh" +source "$BASHUNIT_ROOT_DIR/src/dependencies.sh" +source "$BASHUNIT_ROOT_DIR/src/io.sh" +source "$BASHUNIT_ROOT_DIR/src/math.sh" source "$BASHUNIT_ROOT_DIR/src/default_env_config.sh" source "$BASHUNIT_ROOT_DIR/src/env_configuration.sh" source "$BASHUNIT_ROOT_DIR/src/check_os.sh" @@ -30,6 +33,9 @@ _ASSERT_FN="" _FILTER="" _ARGS=() +check_os::init +clock::init + while [[ $# -gt 0 ]]; do argument="$1" case $argument in diff --git a/install.sh b/install.sh index 3efc08f3..b22db651 100755 --- a/install.sh +++ b/install.sh @@ -45,7 +45,13 @@ function install() { echo "> Downloading the latest version: '$TAG'" fi - curl -L -O -J "https://github.com/TypedDevs/bashunit/releases/download/$TAG/bashunit" 2>/dev/null + if command -v curl > /dev/null 2>&1; then + curl -L -O -J "https://github.com/TypedDevs/bashunit/releases/download/$TAG/bashunit" 2>/dev/null + elif command -v wget > /dev/null 2>&1; then + wget "https://github.com/TypedDevs/bashunit/releases/download/$TAG/bashunit" 2>/dev/null + else + echo "Cannot download bashunit: curl or wget not found." + fi chmod u+x "bashunit" } diff --git a/src/check_os.sh b/src/check_os.sh index 4baf6502..0c394eea 100644 --- a/src/check_os.sh +++ b/src/check_os.sh @@ -4,20 +4,63 @@ _OS="Unknown" _DISTRO="Unknown" -if [[ "$(uname)" == "Linux" ]]; then - _OS="Linux" - if command -v apt > /dev/null; then - _DISTRO="Ubuntu" - elif command -v apk > /dev/null; then - _DISTRO="Alpine" +function check_os::init() { + if check_os::is_linux; then + _OS="Linux" + if check_os::is_ubuntu; then + _DISTRO="Ubuntu" + elif check_os::is_alpine; then + _DISTRO="Alpine" + else + _DISTRO="Other" + fi + elif check_os::is_macos; then + _OS="OSX" + elif check_os::is_windows; then + _OS="Windows" else - _DISTRO="Other" + _OS="Unknown" + _DISTRO="Unknown" fi -elif [[ "$(uname)" == "Darwin" ]]; then - _OS="OSX" -elif [[ "$(uname)" == *"MINGW"* ]]; then - _OS="Windows" -fi +} + +function check_os::is_ubuntu() { + command -v apt > /dev/null +} + +function check_os::is_alpine() { + command -v apk > /dev/null +} + +function check_os::is_linux() { + [[ "$(uname)" == "Linux" ]] +} + +function check_os::is_macos() { + [[ "$(uname)" == "Darwin" ]] +} + +function check_os::is_windows() { + [[ "$(uname)" == *"MINGW"* ]] +} + +function check_os::is_busybox() { + + case "$_DISTRO" in + + "Alpine") + return 0 + ;; + *) + return 1 + ;; + esac +} + +check_os::init export _OS export _DISTRO +export -f check_os::is_alpine +export -f check_os::is_busybox +export -f check_os::is_ubuntu diff --git a/src/clock.sh b/src/clock.sh index 44acbf20..c8f51d78 100644 --- a/src/clock.sh +++ b/src/clock.sh @@ -1,22 +1,57 @@ #!/bin/bash function clock::now() { - if perl -MTime::HiRes -e "" > /dev/null 2>&1; then - perl -MTime::HiRes -e 'printf("%.0f\n", Time::HiRes::time() * 1000)' - elif [[ "${_OS:-}" != "OSX" ]]; then + if dependencies::has_perl && perl -MTime::HiRes -e "" > /dev/null 2>&1; then + if perl -MTime::HiRes -e 'printf("%.0f\n",Time::HiRes::time()*1000000000)'; then + return 0 + fi + fi + + if ! check_os::is_macos && ! check_os::is_alpine; then date +%s%N - else - echo "" + return 0 + fi + + local shell_time has_shell_time + shell_time="$(clock::shell_time)" + has_shell_time="$?" + if [[ "$has_shell_time" -eq 0 ]]; then + local seconds microseconds + seconds=$(echo "$shell_time" | cut -f 1 -d '.') + microseconds=$(echo "$shell_time" | cut -f 2 -d '.') + + math::calculate "($seconds * 1000000000) + ($microseconds * 1000)" + return 0 fi + + echo "" + return 1 +} + +function clock::shell_time() { + # Get time directly from the shell rather than a program. + [[ -n ${EPOCHREALTIME+x} && -n "$EPOCHREALTIME" ]] && LC_ALL=C echo "$EPOCHREALTIME" } -_START_TIME=$(clock::now) function clock::total_runtime_in_milliseconds() { end_time=$(clock::now) if [[ -n $end_time ]]; then - echo $(( end_time - _START_TIME )) + math::calculate "($end_time-$_START_TIME)/1000000" else echo "" fi } + +function clock::total_runtime_in_nanoseconds() { + end_time=$(clock::now) + if [[ -n $end_time ]]; then + math::calculate "($end_time-$_START_TIME)" + else + echo "" + fi +} + +function clock::init() { + _START_TIME=$(clock::now) +} diff --git a/src/console_results.sh b/src/console_results.sh index 73e086a9..0530a3cf 100644 --- a/src/console_results.sh +++ b/src/console_results.sh @@ -175,7 +175,7 @@ function console_results::print_failed_snapshot_test() { line="$(printf "${_COLOR_FAILED}✗ Failed${_COLOR_DEFAULT}: %s ${_COLOR_FAINT}Expected to match the snapshot${_COLOR_DEFAULT}\n" "$function_name")" - if command -v git > /dev/null; then + if dependencies::has_git; then local actual_file="${snapshot_file}.tmp" echo "$actual" > "$actual_file" diff --git a/src/dependencies.sh b/src/dependencies.sh new file mode 100644 index 00000000..ccf93c9b --- /dev/null +++ b/src/dependencies.sh @@ -0,0 +1,30 @@ +#!/bin/bash +set -euo pipefail + +function dependencies::has_perl() { + command -v perl >/dev/null 2>&1 +} + +function dependencies::has_adjtimex() { + command -v adjtimex >/dev/null 2>&1 +} + +function dependencies::has_bc() { + command -v bc >/dev/null 2>&1 +} + +function dependencies::has_awk() { + command -v awk >/dev/null 2>&1 +} + +function dependencies::has_git() { + command -v git >/dev/null 2>&1 +} + +function dependencies::has_curl() { + command -v curl >/dev/null 2>&1 +} + +function dependencies::has_wget() { + command -v wget >/dev/null 2>&1 +} diff --git a/src/io.sh b/src/io.sh new file mode 100644 index 00000000..cbbdb04b --- /dev/null +++ b/src/io.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +function io::download_to() { + local url="$1" + local output="$2" + if dependencies::has_curl; then + curl -L -J -o "$output" "$url" 2>/dev/null + elif dependencies::has_wget; then + wget -q -O "$output" "$url" 2>/dev/null + else + return 1 + fi +} diff --git a/src/math.sh b/src/math.sh new file mode 100644 index 00000000..fa370dc9 --- /dev/null +++ b/src/math.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +if dependencies::has_bc; then + # bc is better than awk because bc has no integer limits. + function math::calculate() { + echo "$*" | bc + } +elif dependencies::has_awk; then + function math::calculate() { + awk "BEGIN { print ""$*"" }" + } +fi diff --git a/src/runner.sh b/src/runner.sh index f4d07e43..dee0c494 100755 --- a/src/runner.sh +++ b/src/runner.sh @@ -179,7 +179,7 @@ function runner::run_test() { echo "$test_execution_result" |\ tail -n 1 |\ sed -E -e 's/.*##TEST_OUTPUT=(.*)##.*/\1/g' |\ - base64 --decode + base64 -d ) if [[ -n "$subshell_output" ]]; then @@ -213,7 +213,11 @@ function runner::run_test() { local end_time end_time=$(clock::now) - local duration=$((end_time - start_time)) + local duration + local duration_ns + duration_ns=$(math::calculate "($end_time - $start_time) ") + duration=$(math::calculate "$duration_ns / 1000000") +# if [[ -n $runtime_error ]]; then state::add_tests_failed diff --git a/src/upgrade.sh b/src/upgrade.sh index 71fdb29a..265ac782 100644 --- a/src/upgrade.sh +++ b/src/upgrade.sh @@ -13,7 +13,11 @@ function upgrade::upgrade() { echo "> Upgrading bashunit to latest version" cd "$script_path" || exit - curl -L -J -o bashunit "https://github.com/TypedDevs/bashunit/releases/download/$latest_tag/bashunit" 2>/dev/null + + if ! io::download_to "https://github.com/TypedDevs/bashunit/releases/download/$latest_tag/bashunit" "bashunit"; then + echo "Failed to download bashunit" + fi + chmod u+x "bashunit" echo "> bashunit upgraded successfully to latest version $latest_tag" diff --git a/tests/globals.sh b/tests/globals.sh index 50e90788..3c1960c2 100644 --- a/tests/globals.sh +++ b/tests/globals.sh @@ -8,3 +8,67 @@ function current_dir() { function current_filename() { basename "${BASH_SOURCE[1]}" } + + +function mock_non_existing_fn() { + return 127; +} + +function mock_false() { + return 1; +} + +function mock_true() { + return 0; +} + +function mock_unknown_linux_os() { + mock check_os::is_linux mock_true + + mock check_os::is_ubuntu mock_false + mock check_os::is_alpine mock_false + mock check_os::is_busybox mock_false + mock check_os::is_macos mock_false + mock check_os::is_windows mock_false +} + + +function mock_ubuntu_os() { + mock check_os::is_linux mock_true + mock check_os::is_ubuntu mock_true + + mock check_os::is_alpine mock_false + mock check_os::is_busybox mock_false + mock check_os::is_macos mock_false + mock check_os::is_windows mock_false +} + +function mock_alpine_os() { + mock check_os::is_linux mock_true + mock check_os::is_alpine mock_true + mock check_os::is_busybox mock_true + + mock check_os::is_ubuntu mock_false + mock check_os::is_macos mock_false + mock check_os::is_windows mock_false +} + +function mock_macos() { + mock check_os::is_macos mock_true + + mock check_os::is_linux mock_false + mock check_os::is_alpine mock_false + mock check_os::is_ubuntu mock_false + mock check_os::is_busybox mock_false + mock check_os::is_windows mock_false +} + +function mock_windows_os() { + mock check_os::is_windows mock_true + + mock check_os::is_linux mock_false + mock check_os::is_alpine mock_false + mock check_os::is_ubuntu mock_false + mock check_os::is_busybox mock_false + mock check_os::is_is_macos mock_false +} diff --git a/tests/unit/assert_snapshot_test.sh b/tests/unit/assert_snapshot_test.sh index f0ef035d..5cd6ba6c 100644 --- a/tests/unit/assert_snapshot_test.sh +++ b/tests/unit/assert_snapshot_test.sh @@ -25,9 +25,15 @@ function test_creates_a_snapshot() { function test_unsuccessful_assert_match_snapshot() { local expected - expected="$(printf "✗ Failed: Unsuccessful assert match snapshot + + if dependencies::has_git; then + expected="$(printf "✗ Failed: Unsuccessful assert match snapshot Expected to match the snapshot [-Actual-]{+Expected+} snapshot[-text-]")" + else + expected="$(printf "✗ Failed: Unsuccessful assert match snapshot + Expected to match the snapshot")" + fi local actual actual="$(assert_match_snapshot "Expected snapshot")" diff --git a/tests/unit/check_os_test.sh b/tests/unit/check_os_test.sh new file mode 100644 index 00000000..d6376895 --- /dev/null +++ b/tests/unit/check_os_test.sh @@ -0,0 +1,75 @@ +#!/bin/bash + +function test_default_os() { + mock uname echo "bogus OS" + + check_os::init + assert_equals "Unknown" "$_OS" +} + +function test_detect_linux_os() { + mock uname echo "Linux" + mock grep mock_non_existing_fn + + check_os::init + assert_equals "Linux" "$_OS" +} + +function test_detect_alpine_linux_os() { + mock uname echo "Linux" + mock check_os::is_ubuntu mock_false + mock check_os::is_alpine mock_true + check_os::init + + assert_equals "Linux" "$_OS" + assert_equals "Alpine" "$_DISTRO" +} + +function test_detect_alpine_os_file() { + mock uname echo "Linux" + mock check_os::is_ubuntu mock_false + mock check_os::is_alpine mock_true + + assert_successful_code "$(check_os::is_alpine)" +} + +function test_detect_osx_os() { + mock uname echo "Darwin" + + check_os::init + assert_equals "OSX" "$_OS" +} + +# data_provider window_linux_variations +function test_detect_windows_os() { + local windows_linux="$1" + mock uname echo "$windows_linux" + + check_os::init + assert_equals "Windows" "$_OS" +} + +function window_linux_variations() { + echo "MINGW" + echo "junkMINGWjunk" +} + +function test_alpine_is_busybox() { + + mock uname echo "Linux" + mock check_os::is_ubuntu mock_false + mock check_os::is_alpine mock_true + check_os::init + assert_successful_code "$(check_os::is_alpine)" + assert_successful_code "$(check_os::is_busybox)" +} + +function test_not_alpine_is_not_busybox() { + + mock uname echo "Linux" + mock check_os::is_ubuntu mock_true + mock check_os::is_alpine mock_false + check_os::init + assert_general_error "$(check_os::is_alpine)" + assert_general_error "$(check_os::is_busybox)" +} diff --git a/tests/unit/clock_test.sh b/tests/unit/clock_test.sh index 803a7d3d..9c5a26e9 100644 --- a/tests/unit/clock_test.sh +++ b/tests/unit/clock_test.sh @@ -21,8 +21,7 @@ function test_now_with_perl() { } function test_now_on_linux_unknown() { - export _OS="Linux" - export _DISTRO="Unknown" + mock_unknown_linux_os mock perl mock_non_existing_fn mock date echo "1720705883457" @@ -30,26 +29,26 @@ function test_now_on_linux_unknown() { } function test_now_on_linux_alpine() { - export _OS="Linux" - export _DISTRO="Alpine" + mock_alpine_os mock perl echo "1720705883457" assert_same "1720705883457" "$(clock::now)" } function test_now_on_windows_without_perl() { - export _OS="Windows" - mock perl mock_non_existing_fn + mock_windows_os + mock dependencies::has_perl mock_false mock date echo "1720705883457" assert_same "1720705883457" "$(clock::now)" } function test_now_on_osx_without_perl() { - export _OS="OSX" - mock perl mock_non_existing_fn + mock_macos + mock dependencies::has_perl mock_false + mock clock::shell_time echo "1727708708.326957" - assert_same "" "$(clock::now)" + assert_same "1727708708326957000" "$(clock::now)" } function test_runtime_in_milliseconds_when_not_empty_time() { @@ -59,8 +58,9 @@ function test_runtime_in_milliseconds_when_not_empty_time() { } function test_runtime_in_milliseconds_when_empty_time() { - export _OS="OSX" + mock_macos mock perl mock_non_existing_fn + mock clock::shell_time mock_non_existing_fn assert_empty "$(clock::total_runtime_in_milliseconds)" } diff --git a/tests/unit/console_results_test.sh b/tests/unit/console_results_test.sh index f720f07b..f1d76d17 100644 --- a/tests/unit/console_results_test.sh +++ b/tests/unit/console_results_test.sh @@ -287,10 +287,27 @@ function test_not_render_execution_time() { assert_not_matches "Time taken" "$render_result" } -function test_render_execution_time_on_osx() { +function test_render_execution_time_on_osx_without_perl() { local render_result + mock_macos + mock dependencies::has_perl mock_false render_result=$( - _OS='OSX' + console_results::render_result + ) + + assert_matches "Time taken: [[:digit:]]+ ms" "$render_result" +} + +function test_render_execution_time_on_osx_with_perl() { + local render_result + mock_macos + mock dependencies::has_adjtimex mock_false + mock dependencies::has_perl mock_true + _START_TIME="1726393394574382186" + mock perl echo "1726393394574372186" + mock uname echo "Darwin" + render_result=$( + mock perl echo "1726393394574372186"; console_results::render_result ) diff --git a/tests/unit/dependencies_test.sh b/tests/unit/dependencies_test.sh new file mode 100644 index 00000000..a4a3af44 --- /dev/null +++ b/tests/unit/dependencies_test.sh @@ -0,0 +1,37 @@ +#!/bin/bash + +function test_has_perl_search_path_for_perl() { + spy command + dependencies::has_perl + + assert_have_been_called_with "-v perl" command +} + +function test_has_adjtimex() { + spy command + dependencies::has_adjtimex + + assert_have_been_called_with "-v adjtimex" command +} + +function test_has_bc() { + spy command + + dependencies::has_bc + + assert_have_been_called_with "-v bc" command +} + +function test_has_awk() { + spy command + dependencies::has_awk + + assert_have_been_called_with "-v awk" command +} + +function test_has_git() { + spy command + dependencies::has_git + + assert_have_been_called_with "-v git" command +} From bd44743d40523dc7e27ad7b5561c31b3fe07a995 Mon Sep 17 00:00:00 2001 From: Chemaclass Date: Tue, 1 Oct 2024 09:52:15 +0200 Subject: [PATCH 2/6] fix: windows test on mac --- src/clock.sh | 11 +++++++++++ src/dependencies.sh | 4 ++++ tests/globals.sh | 2 +- tests/unit/clock_test.sh | 16 +++++++++++++--- 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/clock.sh b/src/clock.sh index c8f51d78..68e835fc 100644 --- a/src/clock.sh +++ b/src/clock.sh @@ -7,6 +7,17 @@ function clock::now() { fi fi + if check_os::is_windows && dependencies::has_powershell; then + powershell -Command " + \$unixEpoch = [DateTime]'1970-01-01 00:00:00'; + \$now = [DateTime]::UtcNow; + \$ticksSinceEpoch = (\$now - \$unixEpoch).Ticks; + \$nanosecondsSinceEpoch = \$ticksSinceEpoch * 100; + Write-Output \$nanosecondsSinceEpoch + " + return 0 + fi + if ! check_os::is_macos && ! check_os::is_alpine; then date +%s%N return 0 diff --git a/src/dependencies.sh b/src/dependencies.sh index ccf93c9b..9c55f192 100644 --- a/src/dependencies.sh +++ b/src/dependencies.sh @@ -5,6 +5,10 @@ function dependencies::has_perl() { command -v perl >/dev/null 2>&1 } +function dependencies::has_powershell() { + command -v powershell > /dev/null 2>&1 +} + function dependencies::has_adjtimex() { command -v adjtimex >/dev/null 2>&1 } diff --git a/tests/globals.sh b/tests/globals.sh index 3c1960c2..8c633987 100644 --- a/tests/globals.sh +++ b/tests/globals.sh @@ -70,5 +70,5 @@ function mock_windows_os() { mock check_os::is_alpine mock_false mock check_os::is_ubuntu mock_false mock check_os::is_busybox mock_false - mock check_os::is_is_macos mock_false + mock check_os::is_macos mock_false } diff --git a/tests/unit/clock_test.sh b/tests/unit/clock_test.sh index 9c5a26e9..073fe50d 100644 --- a/tests/unit/clock_test.sh +++ b/tests/unit/clock_test.sh @@ -35,12 +35,22 @@ function test_now_on_linux_alpine() { assert_same "1720705883457" "$(clock::now)" } -function test_now_on_windows_without_perl() { +function test_now_on_windows_without_with_powershell() { mock_windows_os mock dependencies::has_perl mock_false - mock date echo "1720705883457" + mock dependencies::has_powershell mock_true + mock powershell echo "1727768183281580800" - assert_same "1720705883457" "$(clock::now)" + assert_same "1727768183281580800" "$(clock::now)" +} + +function test_now_on_windows_without_without_powershell() { + mock_windows_os + mock dependencies::has_perl mock_false + mock dependencies::has_powershell mock_false + mock date echo "1727768951" + + assert_same "1727768951" "$(clock::now)" } function test_now_on_osx_without_perl() { From 16d13961475d2e1069f2dcd206c9afc4f72b3db5 Mon Sep 17 00:00:00 2001 From: Chemaclass Date: Tue, 1 Oct 2024 09:58:31 +0200 Subject: [PATCH 3/6] fix: test_render_execution_time_on_osx_without_perl --- src/clock.sh | 10 +++++++--- src/runner.sh | 5 +---- tests/unit/console_results_test.sh | 1 + 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/clock.sh b/src/clock.sh index 68e835fc..c2339378 100644 --- a/src/clock.sh +++ b/src/clock.sh @@ -18,9 +18,13 @@ function clock::now() { return 0 fi - if ! check_os::is_macos && ! check_os::is_alpine; then - date +%s%N - return 0 + if ! check_os::is_macos && ! check_os::is_alpine; then + local result + result=$(date +%s%N) + if [[ "$result" != *N ]] && [[ "$result" -gt 0 ]]; then + echo "$result" + return 0 + fi fi local shell_time has_shell_time diff --git a/src/runner.sh b/src/runner.sh index dee0c494..fa21054a 100755 --- a/src/runner.sh +++ b/src/runner.sh @@ -211,13 +211,10 @@ function runner::run_test() { local total_assertions total_assertions="$(state::calculate_total_assertions "$test_execution_result")" - local end_time + local end_time duration_ns duration end_time=$(clock::now) - local duration - local duration_ns duration_ns=$(math::calculate "($end_time - $start_time) ") duration=$(math::calculate "$duration_ns / 1000000") -# if [[ -n $runtime_error ]]; then state::add_tests_failed diff --git a/tests/unit/console_results_test.sh b/tests/unit/console_results_test.sh index f1d76d17..8075a730 100644 --- a/tests/unit/console_results_test.sh +++ b/tests/unit/console_results_test.sh @@ -291,6 +291,7 @@ function test_render_execution_time_on_osx_without_perl() { local render_result mock_macos mock dependencies::has_perl mock_false + EPOCHREALTIME=1727769354.7084441185 render_result=$( console_results::render_result ) From 6cbeb1e44ea0c7732cfb1769830b5926f5b52ec5 Mon Sep 17 00:00:00 2001 From: Chemaclass Date: Tue, 1 Oct 2024 10:38:42 +0200 Subject: [PATCH 4/6] test: skip osx unit tests on windows --- tests/unit/clock_test.sh | 5 +++++ tests/unit/console_results_test.sh | 14 ++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/tests/unit/clock_test.sh b/tests/unit/clock_test.sh index 073fe50d..1c819f93 100644 --- a/tests/unit/clock_test.sh +++ b/tests/unit/clock_test.sh @@ -54,6 +54,11 @@ function test_now_on_windows_without_without_powershell() { } function test_now_on_osx_without_perl() { + if check_os::is_windows; then + skip + return + fi + mock_macos mock dependencies::has_perl mock_false mock clock::shell_time echo "1727708708.326957" diff --git a/tests/unit/console_results_test.sh b/tests/unit/console_results_test.sh index 8075a730..236f982f 100644 --- a/tests/unit/console_results_test.sh +++ b/tests/unit/console_results_test.sh @@ -288,10 +288,15 @@ function test_not_render_execution_time() { } function test_render_execution_time_on_osx_without_perl() { - local render_result + if check_os::is_windows; then + skip + return + fi + mock_macos mock dependencies::has_perl mock_false - EPOCHREALTIME=1727769354.7084441185 + EPOCHREALTIME=1727771758.0664479733 + local render_result render_result=$( console_results::render_result ) @@ -300,6 +305,11 @@ function test_render_execution_time_on_osx_without_perl() { } function test_render_execution_time_on_osx_with_perl() { + if check_os::is_windows; then + skip + return + fi + local render_result mock_macos mock dependencies::has_adjtimex mock_false From f22cf751110a8c8e421ece08b9105d497d040cd0 Mon Sep 17 00:00:00 2001 From: Chemaclass Date: Tue, 1 Oct 2024 13:00:52 +0200 Subject: [PATCH 5/6] fix: test_render_execution_time_on_osx_without_perl --- tests/unit/console_results_test.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/console_results_test.sh b/tests/unit/console_results_test.sh index 236f982f..0a4365a2 100644 --- a/tests/unit/console_results_test.sh +++ b/tests/unit/console_results_test.sh @@ -295,9 +295,9 @@ function test_render_execution_time_on_osx_without_perl() { mock_macos mock dependencies::has_perl mock_false - EPOCHREALTIME=1727771758.0664479733 local render_result render_result=$( + export EPOCHREALTIME=1727780419.9194760323 console_results::render_result ) From 54966e5435cdff673f3de68efd7512baf71287b9 Mon Sep 17 00:00:00 2001 From: Chemaclass Date: Tue, 1 Oct 2024 13:03:44 +0200 Subject: [PATCH 6/6] fix: mock _START_TIME on test_render_execution_time_on_osx_without_perl --- tests/unit/console_results_test.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/unit/console_results_test.sh b/tests/unit/console_results_test.sh index 0a4365a2..e2b3569a 100644 --- a/tests/unit/console_results_test.sh +++ b/tests/unit/console_results_test.sh @@ -295,9 +295,12 @@ function test_render_execution_time_on_osx_without_perl() { mock_macos mock dependencies::has_perl mock_false + + _START_TIME=1727771758.0664479733 + EPOCHREALTIME=1727780556.4266040325 + local render_result render_result=$( - export EPOCHREALTIME=1727780419.9194760323 console_results::render_result )