Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- Add support for `.bash` test files
- Add runtime check for Bash >= 3.2

## [0.22.3](https://github.com/TypedDevs/bashunit/compare/0.22.2...0.22.3) - 2025-07-27

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ It boasts hundreds of assertions and functionalities like spies, mocks, provider

You can find the complete documentation for **bashunit** online, including installation instructions and the various features it provides, in the [official bashunit documentation](https://bashunit.typeddevs.com).

## Requirements

bashunit requires **Bash 3.2** or newer.

## Contribute

You are welcome to contribute reporting issues, sharing ideas,
Expand Down
20 changes: 20 additions & 0 deletions bashunit
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
#!/usr/bin/env bash
set -euo pipefail

declare -r BASHUNIT_MIN_BASH_VERSION="3.2"

function _check_bash_version() {
local current_version
current_version="$(bash --version | head -n1 | cut -d' ' -f4 | cut -d. -f1,2)"
Copy link

@akinomyoga akinomyoga Jul 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work. It finds bash in the PATH, which isn't necessarily the current Bash version. In particular, when I would like to test a Bash script with different Bash versions, I would do

$ bash-3.0 ./bashunit ...
$ bash-3.1 ./bashunit ...
$ ...
$ bash-5.3 ./bashunit ...

However, this _check_bash_version always sees bash in PATH, which doesn't work.

Instead, the shell variable BASH_VERSION should be used. If you can assume bash >= 2.0, you may also use BASH_VERSINFO array. Actually, you can simply check BASH_VERSINFO because if it doesn't exist, it's anyway out of support.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I normally use zsh, so the change I think would be the best compromise (to support different terminal), is:

  1. check first for the native Bash version with $BASH_VERSINFO in case such variable exists
  2. Otherwise, as fallback then use the bash -v

if [[ -n ${BASHUNIT_TEST_BASH_VERSION:-} ]]; then
current_version="${BASHUNIT_TEST_BASH_VERSION}"
fi

local major minor
IFS=. read -r major minor _ <<< "$current_version"

if (( major < 3 )) || { (( major == 3 )) && (( minor < 2 )); }; then
printf 'Bashunit requires Bash >= %s. Current version: %s\n' "$BASHUNIT_MIN_BASH_VERSION" "$current_version" >&2
exit 1
fi
}

_check_bash_version

# shellcheck disable=SC2034
declare -r BASHUNIT_VERSION="0.22.3"

Expand Down
4 changes: 4 additions & 0 deletions docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ you can add **bashunit** as a dependency in your repository according to your pr

Here, we provide different options that you can use to install **bashunit** in your application.

## Requirements

bashunit requires **Bash 3.2** or newer.

## install.sh

There is a tool that will generate an executable with the whole library in a single file:
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/bash_version_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

function test_fail_with_old_bash_version() {
output=$(BASHUNIT_TEST_BASH_VERSION=3.1 ./bashunit --version 2>&1)
exit_code=$?
assert_contains "Bashunit requires Bash >= 3.2. Current version: 3.1" "$output"
assert_general_error "$output" "" "$exit_code"
}
Loading