Skip to content

Commit

Permalink
Merge pull request #82 from TypedDevs/fix-async
Browse files Browse the repository at this point in the history
Fix async
  • Loading branch information
Chemaclass authored Sep 18, 2023
2 parents db26c9e + c9944f9 commit 7abeda6
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 106 deletions.
2 changes: 0 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
PARALLEL_RUN=

1 change: 1 addition & 0 deletions bashunit
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ readonly BASH_UNIT_ROOT_DIR="$(dirname "${BASH_SOURCE[0]}")"
source "$BASH_UNIT_ROOT_DIR/src/default_env_config.sh"
source "$BASH_UNIT_ROOT_DIR/src/env_configuration.sh"
source "$BASH_UNIT_ROOT_DIR/src/check_os.sh"
source "$BASH_UNIT_ROOT_DIR/src/state.sh"
source "$BASH_UNIT_ROOT_DIR/src/colors.sh"
source "$BASH_UNIT_ROOT_DIR/src/console_results.sh"
source "$BASH_UNIT_ROOT_DIR/src/helpers.sh"
Expand Down
2 changes: 1 addition & 1 deletion bin/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ echo "Running pre-commit checks..."
make pre_commit/run
EXIT_CODE=$?

if [ ${EXIT_CODE} -ne 0 ]; then
if [[ ${EXIT_CODE} -ne 0 ]]; then
echo "Pre Commit checks failed. Please fix the above issues before committing"
exit ${EXIT_CODE}
else
Expand Down
92 changes: 46 additions & 46 deletions src/assert.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,38 @@ function assertEquals() {
local label="${3:-$(Helper::normalizeTestFunctionName "${FUNCNAME[1]}")}"

if [[ "$expected" != "$actual" ]]; then
((_ASSERTIONS_FAILED++))
Console::printFailedTest "${label}" "${expected}" "but got" "${actual}"
State::addAssertionsFailed
Console::printFailedTest "${label}" "${expected}" "but got" "${actual}"
return
fi

((_ASSERTIONS_PASSED++))
State::addAssertionsPassed
}

function assertEmpty() {
local expected="$1"
local label="${3:-$(Helper::normalizeTestFunctionName "${FUNCNAME[1]}")}"

if [[ "$expected" != "" ]]; then
((_ASSERTIONS_FAILED++))
Console::printFailedTest "${label}" "to be empty" "but got" "${expected}"
State::addAssertionsFailed
Console::printFailedTest "${label}" "to be empty" "but got" "${expected}"
return
fi

((_ASSERTIONS_PASSED++))
State::addAssertionsPassed
}

function assertNotEmpty() {
local expected="$1"
local label="${3:-$(Helper::normalizeTestFunctionName "${FUNCNAME[1]}")}"

if [[ "$expected" == "" ]]; then
((_ASSERTIONS_FAILED++))
Console::printFailedTest "${label}" "to not be empty" "but got" "${expected}"
State::addAssertionsFailed
Console::printFailedTest "${label}" "to not be empty" "but got" "${expected}"
return
fi

((_ASSERTIONS_PASSED++))
State::addAssertionsPassed
}

function assertNotEquals() {
Expand All @@ -46,12 +46,12 @@ function assertNotEquals() {
local label="${3:-$(Helper::normalizeTestFunctionName "${FUNCNAME[1]}")}"

if [[ "$expected" == "$actual" ]]; then
((_ASSERTIONS_FAILED++))
Console::printFailedTest "${label}" "${expected}" "but got" "${actual}"
State::addAssertionsFailed
Console::printFailedTest "${label}" "${expected}" "but got" "${actual}"
return
fi

((_ASSERTIONS_PASSED++))
State::addAssertionsPassed
}

function assertContains() {
Expand All @@ -60,12 +60,12 @@ function assertContains() {
local label="${3:-$(Helper::normalizeTestFunctionName "${FUNCNAME[1]}")}"

if ! [[ $actual == *"$expected"* ]]; then
((_ASSERTIONS_FAILED++))
Console::printFailedTest "${label}" "${actual}" "to contain" "${expected}"
State::addAssertionsFailed
Console::printFailedTest "${label}" "${actual}" "to contain" "${expected}"
return
fi

((_ASSERTIONS_PASSED++))
State::addAssertionsPassed
}

function assertNotContains() {
Expand All @@ -74,12 +74,12 @@ function assertNotContains() {
local label="${3:-$(Helper::normalizeTestFunctionName "${FUNCNAME[1]}")}"

if [[ $actual == *"$expected"* ]]; then
((_ASSERTIONS_FAILED++))
Console::printFailedTest "${label}" "${actual}" "to not contain" "${expected}"
State::addAssertionsFailed
Console::printFailedTest "${label}" "${actual}" "to not contain" "${expected}"
return
fi

((_ASSERTIONS_PASSED++))
State::addAssertionsPassed
}

function assertMatches() {
Expand All @@ -88,12 +88,12 @@ function assertMatches() {
local label="${3:-$(Helper::normalizeTestFunctionName "${FUNCNAME[1]}")}"

if ! [[ $actual =~ $expected ]]; then
((_ASSERTIONS_FAILED++))
Console::printFailedTest "${label}" "${actual}" "to match" "${expected}"
State::addAssertionsFailed
Console::printFailedTest "${label}" "${actual}" "to match" "${expected}"
return
fi

((_ASSERTIONS_PASSED++))
State::addAssertionsPassed
}

function assertNotMatches() {
Expand All @@ -102,54 +102,54 @@ function assertNotMatches() {
local label="${3:-$(Helper::normalizeTestFunctionName "${FUNCNAME[1]}")}"

if [[ $actual =~ $expected ]]; then
((_ASSERTIONS_FAILED++))
Console::printFailedTest "${label}" "${actual}" "to not match" "${expected}"
State::addAssertionsFailed
Console::printFailedTest "${label}" "${actual}" "to not match" "${expected}"
return
fi

((_ASSERTIONS_PASSED++))
State::addAssertionsPassed
}

function assertExitCode() {
local actual_exit_code=$?
local expected_exit_code="$1"
local label="${3:-$(Helper::normalizeTestFunctionName "${FUNCNAME[1]}")}"

if [ $actual_exit_code -ne "$expected_exit_code" ]; then
((_ASSERTIONS_FAILED++))
Console::printFailedTest "${label}" "${actual_exit_code}" "to be" "${expected_exit_code}"
if [[ $actual_exit_code -ne "$expected_exit_code" ]]; then
State::addAssertionsFailed
Console::printFailedTest "${label}" "${actual_exit_code}" "to be" "${expected_exit_code}"
return
fi

((_ASSERTIONS_PASSED++))
State::addAssertionsPassed
}

function assertSuccessfulCode() {
local actual_exit_code=$?
local expected_exit_code=0
local label="${3:-$(Helper::normalizeTestFunctionName "${FUNCNAME[1]}")}"

if [[ $actual_exit_code != "$expected_exit_code" ]]; then
((_ASSERTIONS_FAILED++))
Console::printFailedTest "${label}" "${actual_exit_code}" "to be exactly" "${expected_exit_code}"
if [[ $actual_exit_code -ne "$expected_exit_code" ]]; then
State::addAssertionsFailed
Console::printFailedTest "${label}" "${actual_exit_code}" "to be exactly" "${expected_exit_code}"
return
fi

((_ASSERTIONS_PASSED++))
State::addAssertionsPassed
}

function assertGeneralError() {
local actual_exit_code=$?
local expected_exit_code=1
local label="${3:-$(Helper::normalizeTestFunctionName "${FUNCNAME[1]}")}"

if [ $actual_exit_code -ne "$expected_exit_code" ]; then
((_ASSERTIONS_FAILED++))
Console::printFailedTest "${label}" "${actual_exit_code}" "to be exactly" "${expected_exit_code}"
if [[ $actual_exit_code -ne "$expected_exit_code" ]]; then
State::addAssertionsFailed
Console::printFailedTest "${label}" "${actual_exit_code}" "to be exactly" "${expected_exit_code}"
return
fi

((_ASSERTIONS_PASSED++))
State::addAssertionsPassed
}


Expand All @@ -158,13 +158,13 @@ function assertCommandNotFound() {
local expected_exit_code=127
local label="${3:-$(Helper::normalizeTestFunctionName "${FUNCNAME[1]}")}"

if [ $actual_exit_code -ne "$expected_exit_code" ]; then
((_ASSERTIONS_FAILED++))
Console::printFailedTest "${label}" "${actual_exit_code}" "to be exactly" "${expected_exit_code}"
if [[ $actual_exit_code -ne "$expected_exit_code" ]]; then
State::addAssertionsFailed
Console::printFailedTest "${label}" "${actual_exit_code}" "to be exactly" "${expected_exit_code}"
return
fi

((_ASSERTIONS_PASSED++))
State::addAssertionsPassed
}

function assertArrayContains() {
Expand All @@ -174,12 +174,12 @@ function assertArrayContains() {
local actual=("$@")

if ! [[ "${actual[*]}" == *"$expected"* ]]; then
((_ASSERTIONS_FAILED++))
Console::printFailedTest "${label}" "${actual[*]}" "to contain" "${expected}"
State::addAssertionsFailed
Console::printFailedTest "${label}" "${actual[*]}" "to contain" "${expected}"
return
fi

((_ASSERTIONS_PASSED++))
State::addAssertionsPassed
}

function assertArrayNotContains() {
Expand All @@ -189,10 +189,10 @@ function assertArrayNotContains() {
local actual=("$@")

if [[ "${actual[*]}" == *"$expected"* ]]; then
((_ASSERTIONS_FAILED++))
Console::printFailedTest "${label}" "${actual[*]}" "to not contain" "${expected}"
State::addAssertionsFailed
Console::printFailedTest "${label}" "${actual[*]}" "to not contain" "${expected}"
return
fi

((_ASSERTIONS_PASSED++))
State::addAssertionsPassed
}
4 changes: 2 additions & 2 deletions src/check_os.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
# shellcheck disable=SC2034
_OS="Unknown"

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

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

function Console::renderResult() {
local tests_passed=$1
Expand Down Expand Up @@ -46,7 +42,7 @@ function Console::renderResult() {
}

function Console::printExecutionTime() {
if [ "$_OS" != "OSX" ]; then
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
Expand All @@ -70,6 +66,3 @@ ${_COLOR_FAILED}✗ Failed${_COLOR_DEFAULT}: %s
"${test_name}" "${expected}" "${failure_condition_message}" "${actual}"

}

# Set a trap to call renderResult when the script exits
trap 'Console::renderResult $_TESTS_PASSED $_TESTS_FAILED $_ASSERTIONS_PASSED $_ASSERTIONS_FAILED' EXIT
4 changes: 3 additions & 1 deletion src/default_env_config.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
#!/bin/bash
export _DEFAULT_PARALLEL_RUN=false

# shellcheck disable=SC2034
_DEFAULT_PARALLEL_RUN=false
2 changes: 1 addition & 1 deletion src/env_configuration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ set -o allexport
[[ -f ".env" ]] && source .env set
set +o allexport

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

0 comments on commit 7abeda6

Please sign in to comment.