Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ Installation: https://github.com/koalaman/shellcheck#installing
#### Example of usage

```bash
shellcheck ./**/**/**.sh -C
# using make
make lint

# using shellcheck itself
shellcheck ./**/*.sh -C
```

#### We recommend
Expand Down
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ help:
@echo " test/list List all the test under the tests directory"
@echo " test/watch Automatically run the test every second"
@echo " env/example Makes a copy of the keys on your .env file"
@echo " pre_commit/install installs the pre-commit hook"
@echo " pre_commit/run function that will be called when the pre-commit runs"
@echo " pre_commit/install Installs the pre-commit hook"
@echo " pre_commit/run Function that will be called when the pre-commit runs"
@echo " lint Run shellcheck"

SRC_SCRIPTS_DIR=src
TEST_SCRIPTS_DIR=tests
Expand All @@ -68,3 +69,6 @@ pre_commit/install:
cp $(PRE_COMMIT_SCRIPTS_FILE) ./.git/hooks/

pre_commit/run: test env/example

lint:
shellcheck ./**/*.sh -C && echo "Shellcheck: OK!"
2 changes: 1 addition & 1 deletion bashunit
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ readonly BASH_UNIT_ROOT_DIR="$(dirname "${BASH_SOURCE[0]}")"
source "$BASH_UNIT_ROOT_DIR/src/env_configuration.sh"
source "$BASH_UNIT_ROOT_DIR/src/check_os.sh"
source "$BASH_UNIT_ROOT_DIR/src/colors.sh"
source "$BASH_UNIT_ROOT_DIR/src/assert.sh"
source "$BASH_UNIT_ROOT_DIR/src/console_results.sh"
source "$BASH_UNIT_ROOT_DIR/src/assert.sh"

source "$BASH_UNIT_ROOT_DIR/src/test_runner.sh" "$@"

Expand Down
23 changes: 9 additions & 14 deletions example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,17 @@ An example using this **bashunit** testing library.
This demo uses **bashunit** itself as [git-submodule](https://git-scm.com/book/de/v2/Git-Tools-Submodule) inside the `tools/bashunit` directory.

1) Install the git submodule

```bash
git submodule update --init --recursive
```

```bash
git submodule update --init --recursive
```
2) Update to the latest version

```bash
git submodule update --remote
```

```bash
git submodule update --remote
```
3) Run the tests

```bash
tools/bashunit/bashunit logic_test.sh
```
```bash
tools/bashunit/bashunit logic_test.sh
```

<img alt="Demo using the bashunit from different paths" src="demo.png" width="800" >

Expand Down
93 changes: 41 additions & 52 deletions src/assert.sh
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
#!/bin/bash

export TEST=true

export assertEquals
export assertContains
export assertNotContains
export assertMatches
export assertNotMatches

_TOTAL_ASSERTIONS_FAILED=0
_TOTAL_ASSERTIONS_PASSED=0

normalizeFnName() {
local originalFnName="$1"
function normalizeFunctionName() {
local original_function_name="$1"
local result

# Remove "test_" prefix
result="${originalFnName#test_}"
result="${original_function_name#test_}"
# Replace underscores with spaces
result="${result//_/ }"
# Remove "test" prefix
Expand All @@ -27,83 +16,83 @@ normalizeFnName() {
echo "$result"
}

assertEquals() {
function assertEquals() {
local expected="$1"
local actual="$2"
local label="${3:-$(normalizeFnName "${FUNCNAME[1]}")}"
local label="${3:-$(normalizeFunctionName "${FUNCNAME[1]}")}"

if [[ "$expected" != "$actual" ]]; then
((_TOTAL_ASSERTIONS_FAILED++))
((_ASSERTIONS_FAILED++))
printFailedTest "${label}" "${expected}" "but got" "${actual}"
exit 1
return 1
else
((_TOTAL_ASSERTIONS_PASSED++))
printf "${COLOR_PASSED}%s${COLOR_DEFAULT}: ${label}\n" "✓ Passed"
((_ASSERTIONS_PASSED++))
return 0
fi
}

assertContains() {
function assertContains() {
local expected="$1"
local actual="$2"
local label="${3:-$(normalizeFnName "${FUNCNAME[1]}")}"
local label="${3:-$(normalizeFunctionName "${FUNCNAME[1]}")}"

case "$actual" in
*"$expected"*)
((_TOTAL_ASSERTIONS_PASSED++))
printSuccessfulTest "${label}"
((_ASSERTIONS_PASSED++))
return 0
;;
*)
((_TOTAL_ASSERTIONS_FAILED++))
((_ASSERTIONS_FAILED++))
printFailedTest "${label}" "${actual}" "to contain" "${expected}"
exit 1
return 1
;;
esac
}

assertNotContains() {
function assertNotContains() {
local expected="$1"
local actual="$2"
local label="${3:-$(normalizeFnName "${FUNCNAME[1]}")}"
local actual="$2"
local label="${3:-$(normalizeFunctionName "${FUNCNAME[1]}")}"

case "$actual" in
*"$expected"*)
((_TOTAL_ASSERTIONS_FAILED++))
printFailedTest "${label}" "${actual}" "to not contain" "${expected}"
exit 1
;;
*)
((_TOTAL_ASSERTIONS_PASSED++))
printSuccessfulTest "${label}"
;;
esac
case "$actual" in
*"$expected"*)
((_ASSERTIONS_FAILED++))
printFailedTest "${label}" "${actual}" "to not contain" "${expected}"
return 1
;;
*)
((_ASSERTIONS_PASSED++))
return 0
;;
esac
}

assertMatches() {
function assertMatches() {
local expected="$1"
local actual="$2"
local label="${3:-$(normalizeFnName "${FUNCNAME[1]}")}"
local label="${3:-$(normalizeFunctionName "${FUNCNAME[1]}")}"

if [[ $actual =~ $expected ]]; then
((_TOTAL_ASSERTIONS_PASSED++))
printSuccessfulTest "${label}"
((_ASSERTIONS_PASSED++))
return 0
else
((_TOTAL_ASSERTIONS_FAILED++))
((_ASSERTIONS_FAILED++))
printFailedTest "${label}" "${actual}" "to match" "${expected}"
exit 1
return 1
fi
}

assertNotMatches() {
function assertNotMatches() {
local expected="$1"
local actual="$2"
local label="${3:-$(normalizeFnName "${FUNCNAME[1]}")}"
local label="${3:-$(normalizeFunctionName "${FUNCNAME[1]}")}"

if [[ $actual =~ $expected ]]; then
((_TOTAL_ASSERTIONS_FAILED++))
((_ASSERTIONS_FAILED++))
printFailedTest "${label}" "${actual}" "to not match" "${expected}"
exit 1
return 1
else
((_TOTAL_ASSERTIONS_PASSED++))
printSuccessfulTest "${label}"
((_ASSERTIONS_PASSED++))
return 0
fi
}
9 changes: 5 additions & 4 deletions src/check_os.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#!/bin/bash

export OS="Unknown"
# shellcheck disable=SC2034
_OS="Unknown"

if [ "$(uname)" == "Linux" ]; then
OS="Linux"
_OS="Linux"
elif [ "$(uname)" == "Darwin" ]; then
OS="OSX"
_OS="OSX"
elif [[ $(uname) == *"MINGW"* ]]; then
OS="Windows"
_OS="Windows"
fi
13 changes: 7 additions & 6 deletions src/colors.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/bin/bash

export COLOR_DEFAULT="\e[0m"
export COLOR_BOLD="\e[1m"
export COLOR_FAINT="\e[2m"
export COLOR_FAILED="\e[31m"
export COLOR_PASSED="\e[32m"
export COLOR_ALL_PASSED="\e[42m"
# shellcheck disable=SC2034
_COLOR_DEFAULT=$'\e[0m'
_COLOR_BOLD=$'\e[1m'
_COLOR_FAINT=$'\e[2m'
_COLOR_FAILED=$'\e[31m'
_COLOR_PASSED=$'\e[32m'
_COLOR_ALL_PASSED=$'\e[42m'
85 changes: 52 additions & 33 deletions src/console_results.sh
Original file line number Diff line number Diff line change
@@ -1,56 +1,75 @@
#!/bin/bash

export renderResult
export printSuccessfulTest
_START_TIME=$(date +%s%N);
_TESTS_PASSED=0
_TESTS_FAILED=0
_ASSERTIONS_PASSED=0
_ASSERTIONS_FAILED=0

function renderResult() {
local totalTests=$1
local totalPassed=$2
local totalFailed=$3
local tests_passed=$1
local tests_failed=$2
local assertions_passed=$3
local assertions_failed=$4

echo ""
local totalAssertions=$((totalPassed + totalFailed))
printf "\
${COLOR_FAINT}%s${COLOR_DEFAULT} ${COLOR_BOLD}${totalTests}${COLOR_DEFAULT}
${COLOR_FAINT}%s${COLOR_DEFAULT} ${COLOR_BOLD}${totalAssertions}${COLOR_DEFAULT}\n" \
"Total tests:" "Total assertions:"

if [ "$totalFailed" -gt 0 ]; then
printf "${COLOR_FAINT}%s${COLOR_DEFAULT} ${COLOR_BOLD}${COLOR_FAILED}${totalFailed}${COLOR_DEFAULT}\n"\
"Total assertions failed:"
printExecTime
local total_tests=$((tests_passed + tests_failed))
local total_assertions=$((assertions_passed + assertions_failed))

printf "%sTests: %s" "$_COLOR_FAINT" "$_COLOR_DEFAULT"
if [[ $tests_passed -gt 0 ]] || [[ $assertions_passed -gt 0 ]]; then
printf " %s%s passed%s," "$_COLOR_PASSED" "$tests_passed" "$_COLOR_DEFAULT"
fi
if [[ $tests_failed -gt 0 ]]; then
printf " %s%s failed%s," "$_COLOR_FAILED" "$tests_failed" "$_COLOR_DEFAULT"
fi
printf " %s total\n" "$total_tests"


printf "%sAssertions:%s" "$_COLOR_FAINT" "$_COLOR_DEFAULT"
if [[ $tests_passed -gt 0 ]] || [[ $assertions_passed -gt 0 ]]; then
printf " %s%s passed%s," "$_COLOR_PASSED" "$assertions_passed" "$_COLOR_DEFAULT"
fi
if [[ $tests_failed -gt 0 ]]; then
printf " %s%s failed%s," "$_COLOR_FAILED" "$assertions_failed" "$_COLOR_DEFAULT"
fi
printf " %s total\n" "$total_assertions"

if [[ "$tests_failed" -gt 0 ]]; then
printExecutionTime
exit 1
else
printf "${COLOR_ALL_PASSED}%s${COLOR_DEFAULT}\n" "All assertions passed."
fi

printExecTime
printf "%s%s%s\n" "$_COLOR_ALL_PASSED" "All tests passed" "$_COLOR_DEFAULT"
printExecutionTime
exit 0
}

function printExecTime() {
if [[ $OS != "OSX" ]]; then
_TIME_TERMINATION=$((($(date +%s%N) - "$_TIME_START") / 1000000))
printf "${COLOR_BOLD}%s${COLOR_DEFAULT}\n" "Time taken: ${_TIME_TERMINATION} ms"
function printExecutionTime() {
if [ "$_OS" != "OSX" ]; then
_EXECUTION_TIME=$((($(date +%s%N) - "$_START_TIME") / 1000000))
printf "${_COLOR_BOLD}%s${_COLOR_DEFAULT}\n" "Time taken: ${_EXECUTION_TIME} ms"
fi
}

function printSuccessfulTest() {
testName=$1
printf "${COLOR_PASSED}✓ Passed${COLOR_DEFAULT}: %s\n" "${testName}"
local test_name=$1
printf "%s✓ Passed%s: %s\n" "$_COLOR_PASSED" "$_COLOR_DEFAULT" "${test_name}"
}

function printFailedTest() {
testName=$1
expected=$2
failureConditionMessage=$3
actual=$4
local test_name=$1
local expected=$2
local failure_condition_message=$3
local actual=$4

printf "\
${COLOR_FAILED}✗ Failed${COLOR_DEFAULT}: %s
${COLOR_FAINT}Expected${COLOR_DEFAULT} ${COLOR_BOLD}'%s'${COLOR_DEFAULT}
${COLOR_FAINT}%s${COLOR_DEFAULT} ${COLOR_BOLD}'%s'${COLOR_DEFAULT}\n"\
"${testName}" "${expected}" "${failureConditionMessage}" "${actual}"
${_COLOR_FAILED}✗ Failed${_COLOR_DEFAULT}: %s
${_COLOR_FAINT}Expected${_COLOR_DEFAULT} ${_COLOR_BOLD}'%s'${_COLOR_DEFAULT}
${_COLOR_FAINT}%s${_COLOR_DEFAULT} ${_COLOR_BOLD}'%s'${_COLOR_DEFAULT}\n"\
"${test_name}" "${expected}" "${failure_condition_message}" "${actual}"

}

# Set a trap to call renderResult when the script exits
trap 'renderResult $_TOTAL_TESTS $_TOTAL_ASSERTIONS_PASSED $_TOTAL_ASSERTIONS_FAILED' EXIT
trap 'renderResult $_TESTS_PASSED $_TESTS_FAILED $_ASSERTIONS_PASSED $_ASSERTIONS_FAILED' EXIT
5 changes: 3 additions & 2 deletions src/env_configuration.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#!/bin/bash

set -o allexport
# shellcheck source=/dev/null
source .env set
set +o allexport

if [[ -z "$PARALLEL_RUN" ]]; then
PARALLEL_RUN=true
if [ -z "$PARALLEL_RUN" ]; then
PARALLEL_RUN=false
fi
Loading