-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
110e632
commit 47c5d1c
Showing
4 changed files
with
136 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/usr/bin/env bash | ||
|
||
function P () { | ||
tput "$@" 2>/dev/null | ||
} | ||
|
||
_normal_="$(P sgr0)" | ||
|
||
_bold_="$(P bold)" | ||
_uline_="$(P smul)" | ||
|
||
COLORS_ARRAY=( black red green yellow blue magenta cyan white unused default ) | ||
|
||
for i in "${!COLORS_ARRAY[@]}"; do | ||
eval "_fg_${COLORS_ARRAY[$i]}_=\"$(P setaf "$i")\"" | ||
eval "_bg_${COLORS_ARRAY[$i]}_=\"$(P setab "$i")\"" | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,91 @@ | ||
#!/usr/bin/env bash | ||
#shellcheck disable=SC2086,SC2154 source=/dev/null | ||
|
||
set -Eumo pipefail | ||
|
||
SELF_DIR="$(cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)" | ||
cd "$SELF_DIR/.." | ||
|
||
TEST_COMP_TMP_PATH=/tmp/__test_comp_intermediate_storage_dir__ | ||
|
||
rm -rf "$TEST_COMP_TMP_PATH" | ||
mkdir -p "$TEST_COMP_TMP_PATH" | ||
DEBUG="${DEBUG:-}" | ||
|
||
source _scripts/colors.sh | ||
source <( grep -P "^EXIT_CODE_[A-Z_]+=.+" ./comp ) | ||
|
||
# __test_comp <description> <arguments> <input> <validaty_check> <error_msg> <debug?> | ||
function __test_comp () { | ||
local DEBUG="${6:-}" | ||
FINAL_EXIT_CODE=0 | ||
|
||
TEST_COMP_TMP_PATH=/tmp/__test_comp_intermediate_storage_dir__ | ||
rm -rf "$TEST_COMP_TMP_PATH" | ||
mkdir -p "$TEST_COMP_TMP_PATH" | ||
|
||
echo "[>] Test '$1': " | ||
# SETUP <test description> <comp arguments> <stdin content> | ||
function SETUP () { | ||
echo -e "\n${_fg_white_}${_bg_black_}${_bold_}[#]${_normal_} Test $1: " | ||
|
||
local io_path_prefix="$TEST_COMP_TMP_PATH/$1" | ||
local err_path="$io_path_prefix.err" | ||
local out_path="$io_path_prefix.out" | ||
COMP_ERR_PATH="$io_path_prefix.err" | ||
COMP_OUT_PATH="$io_path_prefix.out" | ||
|
||
if [ -n "$DEBUG" ]; then | ||
echo "cmd: ./comp $2" | ||
echo "in: $3" | ||
echo "COMMAND: ./comp $2" | ||
echo "INPUT: $3" | ||
fi | ||
echo "$3" | ./comp $2 >"$out_path" 2>"$err_path" | ||
local exit_code=$? | ||
|
||
echo "$3" | ./comp $2 >"$COMP_OUT_PATH" 2>"$COMP_ERR_PATH" | ||
COMP_EXIT_CODE=$? | ||
if [ -n "$DEBUG" ]; then | ||
echo "\$err_path contents:" | ||
cat "$err_path" | ||
echo "\$out_path contents:" | ||
cat "$out_path" | ||
echo "exit_code: $exit_code" | ||
echo -e "condition:\n $4" | ||
echo "\$COMP_ERR_PATH contents:" | ||
cat "$COMP_ERR_PATH" | ||
echo "\$COMP_OUT_PATH contents:" | ||
cat "$COMP_OUT_PATH" | ||
echo "COMP_EXIT_CODE: $COMP_EXIT_CODE" | ||
fi | ||
} | ||
|
||
function CHECK { | ||
[ -z "$DEBUG" ] || echo "CHECK: $1" | ||
|
||
eval -- $4 \ | ||
&& echo "[+] SUCCESS!" \ | ||
|| echo "[-] FAILURE [$5]" | ||
echo | ||
if eval -- $1 ; then | ||
echo " ${_fg_green_}+${_normal_} $2: ${_fg_green_}SUCCESS${_normal_}" | ||
else | ||
echo " ${_fg_red_}${_bold_}-${_normal_} $2: ${_fg_red_}${_bold_}FAILURE${_normal_}" | ||
FINAL_EXIT_CODE=1 | ||
fi | ||
} | ||
|
||
DEBUG_ALL="${DEBUG:-}" | ||
|
||
__test_comp \ | ||
"Usage on invocation without args" \ | ||
"" \ | ||
"" \ | ||
$'[ $exit_code -eq $EXIT_CODE_USAGE_ERROR ]' \ | ||
"Expected EXIT_CODE_USAGE_ERROR" \ | ||
"$DEBUG_ALL" | ||
|
||
__test_comp \ | ||
"Error on status of non-existent composition" \ | ||
"status unknown_comp" \ | ||
"" \ | ||
$'[ $exit_code -eq $EXIT_CODE_COMPOSITION_NOT_FOUND ] | ||
&& | ||
grep -sq "is not a base directory" "$err_path"' \ | ||
"Expected EXIT_CODE_COMPOSITION_NOT_FOUND and helpful error message" \ | ||
"$DEBUG_ALL" | ||
|
||
__test_comp \ | ||
"Confirmation before deleting data (deny)" \ | ||
"clean tang" \ | ||
"n" \ | ||
$'[ $exit_code -eq 0 ] | ||
&& | ||
grep -sq "Remove \'tang/data\' (y/N)?" "$out_path" | ||
&& | ||
grep -vsq "removed \'.env\'" "$out_path"' \ | ||
"Expected EXIT_CODE = 0, confirmation prompt, and data preserved" \ | ||
"$DEBUG_ALL" | ||
|
||
__test_comp \ | ||
"Confirmation before deleting data (allow)" \ | ||
"clean tang" \ | ||
"y" \ | ||
$'[ $exit_code -eq 0 ] | ||
&& | ||
grep -sq "Remove \'tang/data\' (y/N)?" "$out_path" | ||
&& | ||
grep -sq "removed \'.env\'" "$out_path"' \ | ||
"Expected EXIT_CODE = 0, confirmation prompt, and data deleted" \ | ||
"$DEBUG_ALL" | ||
./comp status tang &> /dev/null | ||
|
||
SETUP "invocation without args" \ | ||
"" \ | ||
"" | ||
CHECK $'[ $COMP_EXIT_CODE -eq $EXIT_CODE_USAGE_ERROR ]' \ | ||
"Expected EXIT_CODE_USAGE_ERROR" | ||
|
||
SETUP "status of non-existent composition" \ | ||
"status unknown_comp" \ | ||
"" | ||
CHECK $'[ $COMP_EXIT_CODE -eq $EXIT_CODE_COMPOSITION_NOT_FOUND ]'\ | ||
"Expected EXIT_CODE_COMPOSITION_NOT_FOUND" | ||
CHECK $'grep -sq "is not a base directory" "$COMP_ERR_PATH"' \ | ||
"Expected a helpful error message" | ||
|
||
SETUP "deny deleting data" \ | ||
"clean tang" \ | ||
"n" | ||
CHECK $'[ $COMP_EXIT_CODE -eq 0 ]' \ | ||
"Expected EXIT_CODE = 0" | ||
CHECK $'grep -sq "Remove \'tang/data\' (y/N)?" "$COMP_OUT_PATH"' \ | ||
"Expected confirmation prompt before deletion" | ||
CHECK $'[ -f tang/.env ]' \ | ||
"Expected data is not deleted" | ||
|
||
SETUP "allow deleting data" \ | ||
"clean tang" \ | ||
"y" | ||
CHECK $'[ $COMP_EXIT_CODE -eq 0 ]' \ | ||
"Expected EXIT_CODE = 0" | ||
CHECK $'grep -sq "Remove \'tang/data\' (y/N)?" "$COMP_OUT_PATH"' \ | ||
"Expected confirmation prompt before deletion" | ||
CHECK $'! [ -f tang/.env ]' \ | ||
"Expected data is deleted" | ||
|
||
exit "$FINAL_EXIT_CODE" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters