Skip to content

Commit

Permalink
Merge pull request #345 from skinner-m-c/feature/remove-dependencies
Browse files Browse the repository at this point in the history
Remove curl perl and coreutils as required dependencies
  • Loading branch information
Chemaclass authored Oct 1, 2024
2 parents 9ab0b6d + 54966e5 commit 4153f03
Show file tree
Hide file tree
Showing 20 changed files with 449 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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';"
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
- Added defer expressions with `eval` when using standalone assertions
- Fixed simple output for non-successful states
- Remove deprecated assertions
- 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

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions bashunit
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -30,6 +33,9 @@ _ASSERT_FN=""
_FILTER=""
_ARGS=()

check_os::init
clock::init

while [[ $# -gt 0 ]]; do
argument="$1"
case $argument in
Expand Down
8 changes: 7 additions & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

Expand Down
67 changes: 55 additions & 12 deletions src/check_os.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
66 changes: 58 additions & 8 deletions src/clock.sh
Original file line number Diff line number Diff line change
@@ -1,22 +1,72 @@
#!/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
date +%s%N
else
echo ""
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_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
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
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)
}
2 changes: 1 addition & 1 deletion src/console_results.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
34 changes: 34 additions & 0 deletions src/dependencies.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash
set -euo pipefail

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
}

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
}
13 changes: 13 additions & 0 deletions src/io.sh
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 12 additions & 0 deletions src/math.sh
Original file line number Diff line number Diff line change
@@ -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
7 changes: 4 additions & 3 deletions src/runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -211,9 +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=$((end_time - start_time))
duration_ns=$(math::calculate "($end_time - $start_time) ")
duration=$(math::calculate "$duration_ns / 1000000")

if [[ -n $runtime_error ]]; then
state::add_tests_failed
Expand Down
6 changes: 5 additions & 1 deletion src/upgrade.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading

0 comments on commit 4153f03

Please sign in to comment.