Skip to content

Commit 4153f03

Browse files
authored
Merge pull request #345 from skinner-m-c/feature/remove-dependencies
Remove curl perl and coreutils as required dependencies
2 parents 9ab0b6d + 54966e5 commit 4153f03

20 files changed

+449
-45
lines changed

.github/CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ docker run --rm -it -v "$(pwd)":/project -w /project ubuntu:latest \
9191
make test/alpine
9292
# or
9393
docker run --rm -it -v "$(pwd)":/project -w /project alpine:latest \
94-
sh -c "apk add bash make shellcheck git curl perl && make test"
94+
sh -c "apk add bash make shellcheck git && make test"
9595
```
9696

9797
## Coding Guidelines

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ jobs:
5858
run: |
5959
docker run --rm -v "$(pwd)":/project alpine:latest /bin/sh -c " \
6060
apk update && \
61-
apk add --no-cache bash make git curl perl coreutils && \
61+
apk add --no-cache bash make git && \
6262
adduser -D builder && \
6363
chown -R builder /project && \
6464
su - builder -c 'cd /project; make test';"

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
- Added defer expressions with `eval` when using standalone assertions
1313
- Fixed simple output for non-successful states
1414
- Remove deprecated assertions
15+
- Some required dependencies now optional:
16+
- perl
17+
- coreutils
18+
- Switch to testing the environment of capabilities rather than assuming various operating systems and Linux
19+
distributions have programs installed.
20+
- Upgrade and install script can now use `wget` if `curl` is not installed
21+
- Tests can be also be timed by making use of `EPOCHREALTIME` on supported system.
1522

1623
## [0.16.0](https://github.com/TypedDevs/bashunit/compare/0.15.0...0.16.0) - 2024-09-15
1724

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ test/watch: $(TEST_SCRIPTS)
7070

7171
docker/alpine:
7272
@docker run --rm -it -v "$(shell pwd)":/project -w /project alpine:latest \
73-
sh -c "apk add bash make shellcheck git curl perl coreutils && bash"
73+
sh -c "apk add bash make shellcheck git && bash"
7474

7575
env/example:
7676
@echo "Copying variables without the values from .env into .env.example"

bashunit

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ export BASHUNIT_ROOT_DIR
1010

1111
source "$BASHUNIT_ROOT_DIR/src/dev/debug.sh"
1212
source "$BASHUNIT_ROOT_DIR/src/str.sh"
13+
source "$BASHUNIT_ROOT_DIR/src/dependencies.sh"
14+
source "$BASHUNIT_ROOT_DIR/src/io.sh"
15+
source "$BASHUNIT_ROOT_DIR/src/math.sh"
1316
source "$BASHUNIT_ROOT_DIR/src/default_env_config.sh"
1417
source "$BASHUNIT_ROOT_DIR/src/env_configuration.sh"
1518
source "$BASHUNIT_ROOT_DIR/src/check_os.sh"
@@ -30,6 +33,9 @@ _ASSERT_FN=""
3033
_FILTER=""
3134
_ARGS=()
3235

36+
check_os::init
37+
clock::init
38+
3339
while [[ $# -gt 0 ]]; do
3440
argument="$1"
3541
case $argument in

install.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,13 @@ function install() {
4545
echo "> Downloading the latest version: '$TAG'"
4646
fi
4747

48-
curl -L -O -J "https://github.com/TypedDevs/bashunit/releases/download/$TAG/bashunit" 2>/dev/null
48+
if command -v curl > /dev/null 2>&1; then
49+
curl -L -O -J "https://github.com/TypedDevs/bashunit/releases/download/$TAG/bashunit" 2>/dev/null
50+
elif command -v wget > /dev/null 2>&1; then
51+
wget "https://github.com/TypedDevs/bashunit/releases/download/$TAG/bashunit" 2>/dev/null
52+
else
53+
echo "Cannot download bashunit: curl or wget not found."
54+
fi
4955
chmod u+x "bashunit"
5056
}
5157

src/check_os.sh

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,63 @@
44
_OS="Unknown"
55
_DISTRO="Unknown"
66

7-
if [[ "$(uname)" == "Linux" ]]; then
8-
_OS="Linux"
9-
if command -v apt > /dev/null; then
10-
_DISTRO="Ubuntu"
11-
elif command -v apk > /dev/null; then
12-
_DISTRO="Alpine"
7+
function check_os::init() {
8+
if check_os::is_linux; then
9+
_OS="Linux"
10+
if check_os::is_ubuntu; then
11+
_DISTRO="Ubuntu"
12+
elif check_os::is_alpine; then
13+
_DISTRO="Alpine"
14+
else
15+
_DISTRO="Other"
16+
fi
17+
elif check_os::is_macos; then
18+
_OS="OSX"
19+
elif check_os::is_windows; then
20+
_OS="Windows"
1321
else
14-
_DISTRO="Other"
22+
_OS="Unknown"
23+
_DISTRO="Unknown"
1524
fi
16-
elif [[ "$(uname)" == "Darwin" ]]; then
17-
_OS="OSX"
18-
elif [[ "$(uname)" == *"MINGW"* ]]; then
19-
_OS="Windows"
20-
fi
25+
}
26+
27+
function check_os::is_ubuntu() {
28+
command -v apt > /dev/null
29+
}
30+
31+
function check_os::is_alpine() {
32+
command -v apk > /dev/null
33+
}
34+
35+
function check_os::is_linux() {
36+
[[ "$(uname)" == "Linux" ]]
37+
}
38+
39+
function check_os::is_macos() {
40+
[[ "$(uname)" == "Darwin" ]]
41+
}
42+
43+
function check_os::is_windows() {
44+
[[ "$(uname)" == *"MINGW"* ]]
45+
}
46+
47+
function check_os::is_busybox() {
48+
49+
case "$_DISTRO" in
50+
51+
"Alpine")
52+
return 0
53+
;;
54+
*)
55+
return 1
56+
;;
57+
esac
58+
}
59+
60+
check_os::init
2161

2262
export _OS
2363
export _DISTRO
64+
export -f check_os::is_alpine
65+
export -f check_os::is_busybox
66+
export -f check_os::is_ubuntu

src/clock.sh

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,72 @@
11
#!/bin/bash
22

33
function clock::now() {
4-
if perl -MTime::HiRes -e "" > /dev/null 2>&1; then
5-
perl -MTime::HiRes -e 'printf("%.0f\n", Time::HiRes::time() * 1000)'
6-
elif [[ "${_OS:-}" != "OSX" ]]; then
7-
date +%s%N
8-
else
9-
echo ""
4+
if dependencies::has_perl && perl -MTime::HiRes -e "" > /dev/null 2>&1; then
5+
if perl -MTime::HiRes -e 'printf("%.0f\n",Time::HiRes::time()*1000000000)'; then
6+
return 0
7+
fi
8+
fi
9+
10+
if check_os::is_windows && dependencies::has_powershell; then
11+
powershell -Command "
12+
\$unixEpoch = [DateTime]'1970-01-01 00:00:00';
13+
\$now = [DateTime]::UtcNow;
14+
\$ticksSinceEpoch = (\$now - \$unixEpoch).Ticks;
15+
\$nanosecondsSinceEpoch = \$ticksSinceEpoch * 100;
16+
Write-Output \$nanosecondsSinceEpoch
17+
"
18+
return 0
19+
fi
20+
21+
if ! check_os::is_macos && ! check_os::is_alpine; then
22+
local result
23+
result=$(date +%s%N)
24+
if [[ "$result" != *N ]] && [[ "$result" -gt 0 ]]; then
25+
echo "$result"
26+
return 0
27+
fi
1028
fi
29+
30+
local shell_time has_shell_time
31+
shell_time="$(clock::shell_time)"
32+
has_shell_time="$?"
33+
if [[ "$has_shell_time" -eq 0 ]]; then
34+
local seconds microseconds
35+
seconds=$(echo "$shell_time" | cut -f 1 -d '.')
36+
microseconds=$(echo "$shell_time" | cut -f 2 -d '.')
37+
38+
math::calculate "($seconds * 1000000000) + ($microseconds * 1000)"
39+
return 0
40+
fi
41+
42+
echo ""
43+
return 1
44+
}
45+
46+
function clock::shell_time() {
47+
# Get time directly from the shell rather than a program.
48+
[[ -n ${EPOCHREALTIME+x} && -n "$EPOCHREALTIME" ]] && LC_ALL=C echo "$EPOCHREALTIME"
1149
}
1250

13-
_START_TIME=$(clock::now)
1451

1552
function clock::total_runtime_in_milliseconds() {
1653
end_time=$(clock::now)
1754
if [[ -n $end_time ]]; then
18-
echo $(( end_time - _START_TIME ))
55+
math::calculate "($end_time-$_START_TIME)/1000000"
56+
else
57+
echo ""
58+
fi
59+
}
60+
61+
function clock::total_runtime_in_nanoseconds() {
62+
end_time=$(clock::now)
63+
if [[ -n $end_time ]]; then
64+
math::calculate "($end_time-$_START_TIME)"
1965
else
2066
echo ""
2167
fi
2268
}
69+
70+
function clock::init() {
71+
_START_TIME=$(clock::now)
72+
}

src/console_results.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ function console_results::print_failed_snapshot_test() {
175175
line="$(printf "${_COLOR_FAILED}✗ Failed${_COLOR_DEFAULT}: %s
176176
${_COLOR_FAINT}Expected to match the snapshot${_COLOR_DEFAULT}\n" "$function_name")"
177177

178-
if command -v git > /dev/null; then
178+
if dependencies::has_git; then
179179
local actual_file="${snapshot_file}.tmp"
180180
echo "$actual" > "$actual_file"
181181

src/dependencies.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
function dependencies::has_perl() {
5+
command -v perl >/dev/null 2>&1
6+
}
7+
8+
function dependencies::has_powershell() {
9+
command -v powershell > /dev/null 2>&1
10+
}
11+
12+
function dependencies::has_adjtimex() {
13+
command -v adjtimex >/dev/null 2>&1
14+
}
15+
16+
function dependencies::has_bc() {
17+
command -v bc >/dev/null 2>&1
18+
}
19+
20+
function dependencies::has_awk() {
21+
command -v awk >/dev/null 2>&1
22+
}
23+
24+
function dependencies::has_git() {
25+
command -v git >/dev/null 2>&1
26+
}
27+
28+
function dependencies::has_curl() {
29+
command -v curl >/dev/null 2>&1
30+
}
31+
32+
function dependencies::has_wget() {
33+
command -v wget >/dev/null 2>&1
34+
}

0 commit comments

Comments
 (0)