Skip to content

Commit

Permalink
Merge pull request #94 from TypedDevs/fix/unset-setup-functions
Browse files Browse the repository at this point in the history
Unset setup functions
  • Loading branch information
Chemaclass authored Sep 15, 2023
2 parents 443445b + 1609ae2 commit fd5a3d7
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/assert.sh
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ function assertSuccessfulCode() {
local expected_exit_code=0
local label="${3:-$(normalizeTestFunctionName "${FUNCNAME[1]}")}"

if [ $actual_exit_code -ne "$expected_exit_code" ]; then
if [[ $actual_exit_code != "$expected_exit_code" ]]; then
((_ASSERTIONS_FAILED++))
printFailedTest "${label}" "${actual_exit_code}" "to be exactly" "${expected_exit_code}"
return
Expand Down
11 changes: 11 additions & 0 deletions src/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,14 @@ function executeFunctionIfExists() {
"$function_name"
fi
}

function unsetIfExists() {
local function_name=$1

if declare -F | awk '{print $3}' | grep -Eq "^${function_name}$"; then
unset "$function_name"
return 0
fi

return 1
}
12 changes: 12 additions & 0 deletions src/runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ function runTearDownAfterScript() {
executeFunctionIfExists 'tearDownAfterScript'
}

function cleanSetUpAndTearDownAfterTest() {
unsetIfExists 'setUp'
unsetIfExists 'tearDown'
}

function cleanSetUpAndTearDownAfterScript() {
unsetIfExists 'setUpBeforeScript'
unsetIfExists 'tearDownAfterScript'
}

###############
#### MAIN #####
###############
Expand Down Expand Up @@ -101,6 +111,8 @@ function loadTestFiles() {
wait
fi
runTearDownAfterScript

cleanSetUpAndTearDownAfterScript
done
}

Expand Down
17 changes: 17 additions & 0 deletions tests/unit/helpers_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,20 @@ function test_no_function_is_executed_with_execute_function_if_exists() {

assertEmpty "$(executeFunctionIfExists "$function_name")"
}

function test_unsuccessful_unsetIfExists() {
assertGeneralError "$(unsetIfExists "fake_function")"
}

function test_successful_unsetIfExists() {
# shellcheck disable=SC2317
function fake_function() {
return 0
}

assertSuccessfulCode "$(unsetIfExists "fake_function")"
}

function tearDown() {
unset fake_function
}

0 comments on commit fd5a3d7

Please sign in to comment.