Skip to content

Commit 530cb05

Browse files
committed
- Update approvals.bash to v0.5.0
1 parent 9244f83 commit 530cb05

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

lib/bashly/libraries/test/approvals.bash

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
# approvals.bash v0.4.2
1+
# approvals.bash v0.5.0
22
#
33
# Interactive approval testing for Bash.
44
# https://github.com/DannyBen/approvals.bash
5+
#
6+
# shellcheck disable=SC2059
7+
# Disabling SC2059 (quoted format string) because we use dynamic format strings
58
approve() {
69
local expected approval approval_file actual cmd
710
approvals_dir=${APPROVALS_DIR:=approvals}
@@ -21,9 +24,9 @@ approve() {
2124
if [[ -f "$approval_file" ]]; then
2225
expected=$(cat "$approval_file")
2326
else
24-
echo "--- [$(blue "new: $cmd")] ---"
27+
printf -- "$new_diff_string\n" "$cmd"
2528
printf "%b\n" "$actual"
26-
echo "--- [$(blue "new: $cmd")] ---"
29+
printf -- "$new_diff_string\n" "$cmd"
2730
expected="$actual"
2831
user_approval "$cmd" "$actual" "$approval_file"
2932
return
@@ -32,9 +35,9 @@ approve() {
3235
if [[ "$(printf "%b" "$actual")" = "$(printf "%b" "$expected")" ]]; then
3336
pass "$cmd"
3437
else
35-
echo "--- [$(blue "diff: $cmd")] ---"
38+
printf -- "$changed_diff_string\n" "$cmd"
3639
$diff_cmd <(printf "%b" "$expected\n") <(printf "%b" "$actual\n") | tail -n +4
37-
echo "--- [$(blue "diff: $cmd")] ---"
40+
printf -- "$changed_diff_string\n" "$cmd"
3841
user_approval "$cmd" "$actual" "$approval_file"
3942
fi
4043
}
@@ -44,38 +47,38 @@ allow_diff() {
4447
}
4548

4649
describe() {
47-
echo
48-
blue "= $*"
50+
printf "$describe_string\n" "$*"
4951
}
5052

5153
context() {
52-
echo
53-
magenta "= $*"
54+
printf "$context_string\n" "$*"
5455
}
5556

5657
fail() {
57-
red " FAILED: $*"
58+
printf "$fail_string\n" "$*"
5859
exit 1
5960
}
6061

6162
pass() {
62-
green " approved: $*"
63+
printf "$pass_string\n" "$*"
6364
return 0
6465
}
6566

6667
expect_exit_code() {
6768
if [[ $last_exit_code == "$1" ]]; then
6869
pass "exit $last_exit_code"
6970
else
70-
fail "Expected exit code $1, got $last_exit_code"
71+
fail "expected exit code $1, got $last_exit_code"
7172
fi
7273
}
7374

74-
red() { printf "\e[31m%b\e[0m\n" "$*"; }
75-
green() { printf "\e[32m%b\e[0m\n" "$*"; }
75+
bold() { printf "\e[1m%b\e[0m\n" "$*"; }
7676
blue() { printf "\e[34m%b\e[0m\n" "$*"; }
77-
magenta() { printf "\e[35m%b\e[0m\n" "$*"; }
7877
cyan() { printf "\e[36m%b\e[0m\n" "$*"; }
78+
green() { printf "\e[32m%b\e[0m\n" "$*"; }
79+
magenta() { printf "\e[35m%b\e[0m\n" "$*"; }
80+
red() { printf "\e[31m%b\e[0m\n" "$*"; }
81+
yellow() { printf "\e[33m%b\e[0m\n" "$*"; }
7982

8083
# Private
8184

@@ -89,9 +92,9 @@ user_approval() {
8992
fi
9093

9194
echo
92-
printf "[A]pprove? \n"
95+
printf "$approval_string"
9396
response=$(bash -c "read -n 1 key; echo \$key")
94-
printf "\r"
97+
printf "\b%.s" $(seq 1 $((${#approval_string} + 1)))
9598
if [[ $response =~ [Aa] ]]; then
9699
printf "%b\n" "$actual" >"$approval_file"
97100
pass "$cmd"
@@ -103,21 +106,32 @@ user_approval() {
103106
onexit() {
104107
exitcode=$?
105108
if [[ "$exitcode" == 0 ]]; then
106-
green "\nFinished successfully"
109+
printf "$exit_success_string\n" "$0"
107110
else
108-
red "\nFinished with failures"
111+
printf "$exit_failed_string\n" "$0"
109112
fi
113+
echo
110114
exit $exitcode
111115
}
112116

113117
onerror() {
114-
fail "Caller: $(caller)"
118+
fail "caller: $(caller)"
115119
}
116120

117121
set -e
118122
trap 'onexit' EXIT
119123
trap 'onerror' ERR
120124

125+
describe_string="$(blue ▌ describe) %s"
126+
context_string="$(magenta ▌ context) %s"
127+
fail_string=" $(red FAILED) %s"
128+
pass_string=" $(green approved) %s"
129+
exit_success_string="$(green ▌ exit) $(bold %s finished successfully)"
130+
exit_failed_string="$(red ▌ exit) $(bold %s finished with errors)"
131+
new_diff_string="────┤ $(yellow new): $(bold %s)) ├────"
132+
changed_diff_string="────┤ $(cyan changed): $(bold %s)) ├────"
133+
approval_string="[A]pprove? "
134+
121135
if diff --help | grep -- --color >/dev/null 2>&1; then
122136
diff_cmd="diff --unified --color=always"
123137
else

0 commit comments

Comments
 (0)