Skip to content

Commit

Permalink
update approvals.bash
Browse files Browse the repository at this point in the history
  • Loading branch information
DannyBen committed Mar 8, 2024
1 parent 9e24884 commit 7078109
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 60 deletions.
73 changes: 53 additions & 20 deletions test/approvals.bash
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# approvals.bash v0.5.0
# approvals.bash v0.5.1
#
# Interactive approval testing for Bash.
# https://github.com/DannyBen/approvals.bash
Expand Down Expand Up @@ -54,6 +54,10 @@ context() {
printf "$context_string\n" "$*"
}

it() {
printf "$it_string\n" "$*"
}

fail() {
printf "$fail_string\n" "$*"
exit 1
Expand All @@ -72,29 +76,57 @@ expect_exit_code() {
fi
}

bold() { printf "\e[1m%b\e[0m\n" "$*"; }
blue() { printf "\e[34m%b\e[0m\n" "$*"; }
cyan() { printf "\e[36m%b\e[0m\n" "$*"; }
green() { printf "\e[32m%b\e[0m\n" "$*"; }
magenta() { printf "\e[35m%b\e[0m\n" "$*"; }
red() { printf "\e[31m%b\e[0m\n" "$*"; }
yellow() { printf "\e[33m%b\e[0m\n" "$*"; }

# Private

print_in_color() {
local color="$1"
shift
if [[ -z ${NO_COLOR+x} ]]; then
printf "$color%b\e[0m\n" "$*"
else
printf "%b\n" "$*"
fi
}

red() { print_in_color "\e[31m" "$*"; }
green() { print_in_color "\e[32m" "$*"; }
yellow() { print_in_color "\e[33m" "$*"; }
blue() { print_in_color "\e[34m" "$*"; }
magenta() { print_in_color "\e[35m" "$*"; }
cyan() { print_in_color "\e[36m" "$*"; }
bold() { print_in_color "\e[1m" "$*"; }
underlined() { print_in_color "\e[4m" "$*"; }
red_bold() { print_in_color "\e[1;31m" "$*"; }
green_bold() { print_in_color "\e[1;32m" "$*"; }
yellow_bold() { print_in_color "\e[1;33m" "$*"; }
blue_bold() { print_in_color "\e[1;34m" "$*"; }
magenta_bold() { print_in_color "\e[1;35m" "$*"; }
cyan_bold() { print_in_color "\e[1;36m" "$*"; }
red_underlined() { print_in_color "\e[4;31m" "$*"; }
green_underlined() { print_in_color "\e[4;32m" "$*"; }
yellow_underlined() { print_in_color "\e[4;33m" "$*"; }
blue_underlined() { print_in_color "\e[4;34m" "$*"; }
magenta_underlined() { print_in_color "\e[4;35m" "$*"; }
cyan_underlined() { print_in_color "\e[4;36m" "$*"; }

user_approval() {
local cmd="$1"
local actual="$2"
local approval_file="$3"

if [[ -v CI || -v GITHUB_ACTIONS ]]; then
if [[ -v CI || -v GITHUB_ACTIONS ]] && [[ -z "${AUTO_APPROVE+x}" ]]; then
fail "$cmd"
fi

echo
printf "$approval_string"
response=$(bash -c "read -n 1 key; echo \$key")
printf "\b%.s" $(seq 1 $((${#approval_string} + 1)))
if [[ -v AUTO_APPROVE ]]; then
response=a
else
echo
printf "$approval_string"
response=$(bash -c "read -n 1 key; echo \$key")
printf "\b%.s" $(seq 1 $((${#approval_string} + 1)))
fi

if [[ $response =~ [Aa] ]]; then
printf "%b\n" "$actual" >"$approval_file"
pass "$cmd"
Expand Down Expand Up @@ -122,14 +154,15 @@ set -e
trap 'onexit' EXIT
trap 'onerror' ERR

describe_string="$(blue ▌ describe) %s"
context_string="$(magenta ▌ context) %s"
fail_string=" $(red FAILED) %s"
describe_string="$(bold ▌ describe) %s"
context_string="$(bold ▌ context) %s"
it_string="$(bold ▌ it) %s"
fail_string=" $(red_bold failed) %s"
pass_string=" $(green approved) %s"
exit_success_string="$(green ▌ exit) $(bold %s finished successfully)"
exit_failed_string="$(red ▌ exit) $(bold %s finished with errors)"
new_diff_string="────┤ $(yellow new): $(bold %s)) ├────"
changed_diff_string="────┤ $(cyan changed): $(bold %s)) ├────"
exit_failed_string="$(red_bold ▌ exit) $(bold %s finished with errors)"
new_diff_string="────┤ $(yellow new): $(bold %s) ├────"
changed_diff_string="────┤ $(blue changed): $(bold %s) ├────"
approval_string="[A]pprove? "

if diff --help | grep -- --color >/dev/null 2>&1; then
Expand Down
87 changes: 47 additions & 40 deletions test/approve
Original file line number Diff line number Diff line change
Expand Up @@ -12,92 +12,99 @@ mkdir -p tmp/one/two
mkdir -p "$HOME/.fuzzycd"

context "when the shell is non-interactive"

describe "cd is builtin"
approve "type -t cd" "type_builtin"
describe "cd"
it "is a builtin"
approve "type -t cd" "type_builtin"


context "when the shell is interactive"

export FUZZYCD_TTY_FORCE=1
source ../fuzzycd

describe "cd is a function"
approve "type -t cd" "type_function"
describe "cd"
it "is a function"
approve "type -t cd" "type_function"

describe "cd -h shows help"
approve "cd -h"
describe "cd -h"
it "shows help"
approve "cd -h"

describe "cd -v shows version"
approve "cd -v"
describe "cd -v"
it "shows version"
approve "cd -v"

describe "cd DIR adds it to history"
cd tmp/one/two > /dev/null
[[ $PWD == *tmp/one/two ]] || fail "Expected tmp/one/twp, got $PWD"
grep -q one/two "$FUZZYCD_HISTORY_FILE" || fail "Expected history file to contain one/two"
describe "cd DIR"
it "adds it to history"
cd tmp/one/two > /dev/null
[[ $PWD == *tmp/one/two ]] || fail "Expected tmp/one/twp, got $PWD"
grep -q one/two "$FUZZYCD_HISTORY_FILE" || fail "Expected history file to contain one/two"

describe "cd DIR with a fuzzy string works"
cd ontwo > /dev/null
[[ $PWD == *tmp/one/two ]] || fail "Expected tmp/one/twp, got $PWD"
describe "cd DIR with a fuzzy string"
it "changes to the requested directory"
cd ontwo > /dev/null
[[ $PWD == *tmp/one/two ]] || fail "Expected tmp/one/twp, got $PWD"


context "when the history file contains some entries"

rm -f "$FUZZYCD_HISTORY_FILE"
cd /usr/local/bin > /dev/null
cd /usr/local/lib > /dev/null
cd /etc > /dev/null
EDITOR="cat"

describe "cd -s shows history file"
approve "cd -s"
describe "cd -s"
it "shows history file"
approve "cd -s"

describe "cd -e opens the history file"
approve "cd -e"
describe "cd -e"
it "opens the history file"
approve "cd -e"

describe "cd -d DIR removes DIR from history"
approve "cd -d /etc"
approve "cd -s" "cd_s_after_delete"
describe "cd -d DIR"
it "removes DIR from history"
approve "cd -d /etc"
approve "cd -s" "cd_s_after_delete"

describe "cd -d removes current directory from history"
cd /usr/local/lib
approve "cd -d"
approve "cd -s" "cd_s_after_delete2"
describe "cd -d"
it "removes current directory from history"
cd /usr/local/lib
approve "cd -d"
approve "cd -s" "cd_s_after_delete2"

cd "$basedir"
rm -f "$FUZZYCD_HISTORY_FILE"

context "when the history file contains gone directories"

echo /usr/local/bin > "$FUZZYCD_HISTORY_FILE"
echo /no/such/dir >> "$FUZZYCD_HISTORY_FILE"

describe "cd DIR deletes the gone directory"
approve "cd suchdir" || true
expect_exit_code 1
approve "cat $FUZZYCD_HISTORY_FILE" "cat_histfile"
describe "cd DIR"
it "deletes the gone directory"
approve "cd suchdir" || true
expect_exit_code 1
approve "cat $FUZZYCD_HISTORY_FILE" "cat_histfile"


context "when CDPATH contains entries"

echo /usr/local/bin > "$FUZZYCD_HISTORY_FILE"
export CDPATH=".:/usr:/etc"

describe "cd DIR prefers paths that are sub directories of a CDPATH path"
approve "cd local"
describe "cd DIR"
it "prefers paths that are sub directories of a CDPATH path"
approve "cd local"

unset CDPATH


context "when FUZZYCD_MODE=m"

export FUZZYCD_MODE=m
rm -f "$FUZZYCD_HISTORY_FILE"
cd /usr/local/bin > /dev/null
cd /usr/local/lib > /dev/null

describe "cd with a fuzzy string goes to best match"
approve "cd localb"
describe "cd with a fuzzy string"
it "goes to best match"
approve "cd localb"

cd "$basedir"
rm -f "$FUZZYCD_HISTORY_FILE"
Expand Down

0 comments on commit 7078109

Please sign in to comment.