From 4f5da9831b6f1945dc734fc0e921d9f51dfe8fa7 Mon Sep 17 00:00:00 2001 From: Chemaclass Date: Wed, 4 Jun 2025 00:39:39 +0200 Subject: [PATCH 1/3] refactor: improve clock performance --- src/clock.sh | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/clock.sh b/src/clock.sh index 5e851e95..2435a8d3 100644 --- a/src/clock.sh +++ b/src/clock.sh @@ -31,11 +31,12 @@ function clock::now() { 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 '.') + local seconds microseconds + seconds="${shell_time%%.*}" + microseconds="${shell_time#*.}" + microseconds="${microseconds:-0}" - math::calculate "($seconds * 1000000000) + ($microseconds * 1000)" + echo $((seconds * 1000000000 + microseconds * 1000)) return 0 fi @@ -52,7 +53,7 @@ function clock::shell_time() { function clock::total_runtime_in_milliseconds() { end_time=$(clock::now) if [[ -n $end_time ]]; then - math::calculate "($end_time-$_START_TIME)/1000000" + echo $(((end_time - _START_TIME)/1000000)) else echo "" fi @@ -61,7 +62,7 @@ function clock::total_runtime_in_milliseconds() { function clock::total_runtime_in_nanoseconds() { end_time=$(clock::now) if [[ -n $end_time ]]; then - math::calculate "($end_time-$_START_TIME)" + echo $((end_time - _START_TIME)) else echo "" fi From f0158e710abe8191f2881019179ab8aa347e6904 Mon Sep 17 00:00:00 2001 From: Chemaclass Date: Wed, 4 Jun 2025 00:39:53 +0200 Subject: [PATCH 2/3] docs: update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fcfb3f19..2d8c25cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Fix typo "to has been called" - Add weekly downloads to the docs +- Improve clock performance ## [0.20.0](https://github.com/TypedDevs/bashunit/compare/0.19.1...0.20.0) - 2025-06-01 From 510c3f8383a27704329a660541292cbfa0d8b1b4 Mon Sep 17 00:00:00 2001 From: Chemaclass Date: Wed, 4 Jun 2025 09:59:17 +0200 Subject: [PATCH 3/3] fix: alpine failing tests --- src/clock.sh | 12 +++++++++--- src/env.sh | 4 ++-- tests/unit/directory_test.sh | 18 ++++++++++++------ 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/clock.sh b/src/clock.sh index 2435a8d3..0d33d44d 100644 --- a/src/clock.sh +++ b/src/clock.sh @@ -32,9 +32,15 @@ function clock::now() { has_shell_time="$?" if [[ "$has_shell_time" -eq 0 ]]; then local seconds microseconds - seconds="${shell_time%%.*}" - microseconds="${shell_time#*.}" - microseconds="${microseconds:-0}" + if [[ "$shell_time" == *.* ]]; then + seconds="${shell_time%%.*}" + microseconds="${shell_time#*.}" + microseconds="$(echo "$microseconds" | sed 's/^0*//')" + microseconds="${microseconds:-0}" + else + seconds="$shell_time" + microseconds=0 + fi echo $((seconds * 1000000000 + microseconds * 1000)) return 0 diff --git a/src/env.sh b/src/env.sh index 50cc5165..765d5235 100644 --- a/src/env.sh +++ b/src/env.sh @@ -79,10 +79,10 @@ function env::active_internet_connection() { function env::find_terminal_width() { local cols="" - if [[ -z "$cols" ]] && command -v stty > /dev/null; then + if command -v tput > /dev/null; then cols=$(tput cols 2>/dev/null) fi - if [[ -n "$TERM" ]] && command -v tput > /dev/null; then + if [[ -z "$cols" ]] && command -v stty > /dev/null; then cols=$(stty size 2>/dev/null | cut -d' ' -f2) fi diff --git a/tests/unit/directory_test.sh b/tests/unit/directory_test.sh index eeb26046..d3da3cf3 100644 --- a/tests/unit/directory_test.sh +++ b/tests/unit/directory_test.sh @@ -111,7 +111,8 @@ function test_unsuccessful_assert_is_directory_readable_when_a_file_is_given() { } function test_unsuccessful_assert_is_directory_readable_without_execution_permission() { - if [[ "$_OS" == "Windows" || $_DISTRO = "Alpine" ]]; then + if [[ "$_OS" == "Windows" || $_DISTRO = "Alpine" || $(id -u) -eq 0 ]]; then + skip "permission checks unreliable as root" return fi @@ -126,7 +127,8 @@ function test_unsuccessful_assert_is_directory_readable_without_execution_permis } function test_unsuccessful_assert_is_directory_readable_without_read_permission() { - if [[ "$_OS" == "Windows" || $_DISTRO = "Alpine" ]]; then + if [[ "$_OS" == "Windows" || $_DISTRO = "Alpine" || $(id -u) -eq 0 ]]; then + skip "permission checks unreliable as root" return fi @@ -141,7 +143,8 @@ function test_unsuccessful_assert_is_directory_readable_without_read_permission( } function test_successful_assert_is_directory_not_readable_without_read_permission() { - if [[ "$_OS" == "Windows" || $_DISTRO = "Alpine" ]]; then + if [[ "$_OS" == "Windows" || $_DISTRO = "Alpine" || $(id -u) -eq 0 ]]; then + skip "permission checks unreliable as root" return fi @@ -152,7 +155,8 @@ function test_successful_assert_is_directory_not_readable_without_read_permissio } function test_successful_assert_is_directory_not_readable_without_execution_permission() { - if [[ "$_OS" == "Windows" || $_DISTRO = "Alpine" ]]; then + if [[ "$_OS" == "Windows" || $_DISTRO = "Alpine" || $(id -u) -eq 0 ]]; then + skip "permission checks unreliable as root" return fi @@ -178,7 +182,8 @@ function test_successful_assert_is_directory_writable() { } function test_unsuccessful_assert_is_directory_writable() { - if [[ "$_OS" == "Windows" || $_DISTRO = "Alpine" ]]; then + if [[ "$_OS" == "Windows" || $_DISTRO = "Alpine" || $(id -u) -eq 0 ]]; then + skip "permission checks unreliable as root" return fi @@ -202,7 +207,8 @@ function test_unsuccessful_assert_is_directory_writable_when_a_file_is_given() { } function test_successful_assert_is_directory_not_writable() { - if [[ "$_OS" == "Windows" || $_DISTRO = "Alpine" ]]; then + if [[ "$_OS" == "Windows" || $_DISTRO = "Alpine" || $(id -u) -eq 0 ]]; then + skip "permission checks unreliable as root" return fi