Skip to content

Commit a27ed75

Browse files
authored
Merge pull request #457 from TypedDevs/fix/nixos-support
Fix NixOS support
2 parents e78ee50 + a75c65d commit a27ed75

File tree

12 files changed

+120
-58
lines changed

12 files changed

+120
-58
lines changed

.github/CONTRIBUTING.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ docker run --rm -it -v "$(pwd)":/project -w /project alpine:latest \
9494
sh -c "apk add bash make shellcheck git && make test"
9595
```
9696

97+
##### NixOS
98+
99+
If you are using NixOS, you can enter a shell with all the required tools using the provided `shell.nix`:
100+
101+
```bash
102+
nix-shell --pure --run "./bashunit --simple"
103+
```
104+
97105
## Coding Guidelines
98106

99107
### ShellCheck

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
## Unreleased
44

5-
- Fix parallel and `compgen` issue on NixOS
5+
- Fix NixOS support
6+
- Fix parallel and `compgen`
7+
- Use `command -v` instead of `which`
68

79
## [0.22.2](https://github.com/TypedDevs/bashunit/compare/0.22.1...0.22.2) - 2025-07-26
810

install.sh

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,6 @@ function is_git_installed() {
66
command -v git > /dev/null 2>&1
77
}
88

9-
function get_latest_tag() {
10-
local repository_url=$1
11-
12-
git ls-remote --tags "$repository_url" |
13-
awk '{print $2}' |
14-
sed 's|^refs/tags/||' |
15-
sort -Vr |
16-
head -n 1
17-
}
18-
199
function build_and_install_beta() {
2010
echo "> Downloading non-stable version: 'beta'"
2111

@@ -92,11 +82,7 @@ elif [[ $# -eq 2 ]]; then
9282
fi
9383

9484
BASHUNIT_GIT_REPO="https://github.com/TypedDevs/bashunit"
95-
if is_git_installed; then
96-
LATEST_BASHUNIT_VERSION="$(get_latest_tag "$BASHUNIT_GIT_REPO")"
97-
else
98-
LATEST_BASHUNIT_VERSION="0.22.2"
99-
fi
85+
LATEST_BASHUNIT_VERSION="0.22.2"
10086
TAG="$LATEST_BASHUNIT_VERSION"
10187

10288
cd "$(dirname "$0")"

shell.nix

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{ pkgs ? import <nixpkgs> {} }:
2+
3+
pkgs.mkShell {
4+
buildInputs = [
5+
pkgs.bashInteractive
6+
pkgs.git
7+
pkgs.curl
8+
pkgs.perl
9+
];
10+
}

src/assert_snapshot.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function snapshot::match_with_placeholder() {
3535
local escaped=$(printf '%s' "$sanitized" | sed -e 's/[.[\\^$*+?{}()|]/\\&/g')
3636
local regex="^${escaped//$token/(.|\\n)*}$"
3737

38-
if which perl >/dev/null 2>&1; then
38+
if command -v perl >/dev/null 2>&1; then
3939
echo "$actual" | REGEX="$regex" perl -0 -e '
4040
my $r = $ENV{REGEX};
4141
my $input = join("", <STDIN>);
@@ -81,7 +81,7 @@ function snapshot::compare() {
8181
if ! snapshot::match_with_placeholder "$actual" "$snapshot"; then
8282
local label=$(helper::normalize_test_function_name "$func_name")
8383
state::add_assertions_failed
84-
console_results::print_failed_snapshot_test "$label" "$snapshot_path"
84+
console_results::print_failed_snapshot_test "$label" "$snapshot_path" "$actual"
8585
return 1
8686
fi
8787

src/console_results.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,15 @@ ${_COLOR_FAILED}✗ Failed${_COLOR_DEFAULT}: %s
185185
function console_results::print_failed_snapshot_test() {
186186
local function_name=$1
187187
local snapshot_file=$2
188+
local actual_content=${3-}
188189

189190
local line
190191
line="$(printf "${_COLOR_FAILED}✗ Failed${_COLOR_DEFAULT}: %s
191192
${_COLOR_FAINT}Expected to match the snapshot${_COLOR_DEFAULT}\n" "$function_name")"
192193

193194
if dependencies::has_git; then
194195
local actual_file="${snapshot_file}.tmp"
195-
echo "$actual" > "$actual_file"
196+
echo "$actual_content" > "$actual_file"
196197

197198
local git_diff_output
198199
git_diff_output="$(git diff --no-index --word-diff --color=always \

src/env.sh

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ function env::is_no_output_enabled() {
8787
}
8888

8989
function env::active_internet_connection() {
90+
if [[ "${BASHUNIT_NO_NETWORK:-}" == "true" ]]; then
91+
return 1
92+
fi
93+
94+
if command -v curl >/dev/null 2>&1; then
95+
curl -sfI https://github.com >/dev/null 2>&1 && return 0
96+
elif command -v wget >/dev/null 2>&1; then
97+
wget -q --spider https://github.com && return 0
98+
fi
99+
90100
if ping -c 1 -W 3 google.com &> /dev/null; then
91101
return 0
92102
fi
@@ -147,7 +157,7 @@ TEMP_DIR_PARALLEL_TEST_SUITE="${TMPDIR:-/tmp}/bashunit/parallel/${_OS:-Unknown}/
147157
TEMP_FILE_PARALLEL_STOP_ON_FAILURE="$TEMP_DIR_PARALLEL_TEST_SUITE/.stop-on-failure"
148158
TERMINAL_WIDTH="$(env::find_terminal_width)"
149159
FAILURES_OUTPUT_PATH=$(mktemp)
150-
CAT="$(which cat)"
160+
CAT="$(command -v cat)"
151161

152162
if env::is_dev_mode_enabled; then
153163
internal_log "info" "Dev log enabled" "file:$BASHUNIT_DEV_LOG"

src/helpers.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ function helper::trim() {
181181
}
182182

183183
function helpers::get_latest_tag() {
184+
if ! dependencies::has_git; then
185+
return 1
186+
fi
187+
184188
git ls-remote --tags "$BASHUNIT_GIT_REPO" |
185189
awk '{print $2}' |
186190
sed 's|^refs/tags/||' |

src/math.sh

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
#!/usr/bin/env bash
2+
# shellcheck disable=SC2155
23

34
function math::calculate() {
45
if dependencies::has_bc; then
56
echo "$*" | bc
67
elif [[ "$*" == *.* ]] && dependencies::has_awk; then
7-
# Use awk for floating point calculations when bc is unavailable
88
awk "BEGIN { print ($*) }"
9+
elif [[ "$*" == *.* ]]; then
10+
# Strip decimal parts and leading zeros
11+
local expression=$(echo "$*" | sed -E 's/([0-9]+)\.[0-9]+/\1/g' | sed -E 's/\b0*([1-9][0-9]*)/\1/g')
12+
local result=$(( expression ))
13+
echo "$result"
914
else
10-
# Fallback to shell arithmetic which has good integer precision
11-
local result=$(( $* ))
15+
# Strip leading zeros even for purely integer math
16+
local expression=$(echo "$*" | sed -E 's/\b0*([1-9][0-9]*)/\1/g')
17+
local result=$(( expression ))
1218
echo "$result"
1319
fi
1420
}

tests/acceptance/bashunit_upgrade_test.sh

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,22 @@ set +e
44

55
TMP_DIR="tmp"
66
TMP_BIN="$TMP_DIR/bashunit"
7-
ACTIVE_INTERNET=0
7+
ACTIVE_INTERNET=false
8+
HAS_DOWNLOADER=false
9+
HAS_GIT=false
810

911
function set_up_before_script() {
10-
env::active_internet_connection
11-
ACTIVE_INTERNET=$?
12+
if env::active_internet_connection; then
13+
ACTIVE_INTERNET=true
14+
fi
15+
16+
if dependencies::has_curl || dependencies::has_wget; then
17+
HAS_DOWNLOADER=true
18+
fi
19+
20+
if dependencies::has_git; then
21+
HAS_GIT=true
22+
fi
1223
}
1324

1425
function tear_down_after_script() {
@@ -17,7 +28,11 @@ function tear_down_after_script() {
1728

1829
function set_up() {
1930
./build.sh "$TMP_DIR" >/dev/null
20-
LATEST_VERSION="$(helpers::get_latest_tag)"
31+
if [[ "$ACTIVE_INTERNET" == true ]] && [[ "$HAS_GIT" == true ]]; then
32+
LATEST_VERSION="$(helpers::get_latest_tag)"
33+
else
34+
LATEST_VERSION="${BASHUNIT_VERSION}"
35+
fi
2136
TEST_ENV_FILE="tests/acceptance/fixtures/.env.default"
2237
}
2338

@@ -36,9 +51,15 @@ function test_do_not_upgrade_when_latest() {
3651
}
3752

3853
function test_upgrade_when_a_new_version_found() {
39-
if [[ "$ACTIVE_INTERNET" -eq 1 ]]; then
54+
if [[ "$ACTIVE_INTERNET" == false ]]; then
4055
skip "no internet connection" && return
4156
fi
57+
if [[ "$HAS_GIT" == false ]]; then
58+
skip "git not installed" && return
59+
fi
60+
if [[ "$HAS_DOWNLOADER" == false ]]; then
61+
skip "curl or wget not installed" && return
62+
fi
4263

4364
sed -i -e \
4465
's/declare -r BASHUNIT_VERSION="[0-9]\{1,\}\.[0-9]\{1,\}\.[0-9]\{1,\}"/declare -r BASHUNIT_VERSION="0.1.0"/' \
@@ -57,16 +78,22 @@ function test_upgrade_when_a_new_version_found() {
5778
}
5879

5980
function test_do_not_update_on_consecutive_calls() {
60-
if [[ "$ACTIVE_INTERNET" -eq 1 ]]; then
81+
if [[ "$ACTIVE_INTERNET" == false ]]; then
6182
skip "no internet connection" && return
6283
fi
84+
if [[ "$HAS_GIT" == false ]]; then
85+
skip "git not installed" && return
86+
fi
87+
if [[ "$HAS_DOWNLOADER" == false ]]; then
88+
skip "curl or wget not installed" && return
89+
fi
6390

6491
sed -i -e \
6592
's/declare -r BASHUNIT_VERSION="[0-9]\{1,\}\.[0-9]\{1,\}\.[0-9]\{1,\}"/declare -r BASHUNIT_VERSION="0.1.0"/' \
66-
$TMP_BIN
93+
"$TMP_BIN"
6794

6895
if [[ $_OS == "OSX" ]]; then
69-
rm $TMP_BIN-e
96+
rm -f "${TMP_BIN}-e"
7097
fi
7198

7299
$TMP_BIN --upgrade

0 commit comments

Comments
 (0)