Skip to content

Commit

Permalink
Merge pull request #14 from DannyBen/add/auto-approve
Browse files Browse the repository at this point in the history
Add auto-approval with `AUTO_APPROVE` environment variable
  • Loading branch information
DannyBen authored Mar 8, 2024
2 parents 8299064 + 6d78f9c commit 9ecde1c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ expected output stored in the approvals folder.
test will pass.
- When a new/updated approval is rejected, your tests will exit immediately
with exit code 1.
- When running in a CI environment (CI variable exists), or on GitHub
Actions (GITHUB_ACTIONS variable exists), your tests will run in
- When running in a CI environment (`CI` variable exists), or on GitHub
Actions (`GITHUB_ACTIONS` variable exists), your tests will run in
non-interactive mode - tests will fail automatically if they do not match.


Expand Down Expand Up @@ -131,6 +131,12 @@ approve "..."
```


## Auto-approval

Setting `AUTO_APPROVE=1` prior to running your tests will automatically approve
all tests, as if the user has pressed `a` to approve each changed or new test.


## Compatibility

approvals.bash was tested in **bash** (version 4.0 or higher) and **zsh**,
Expand Down
19 changes: 12 additions & 7 deletions approvals.bash
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,19 @@ user_approval() {
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 @@ -128,8 +133,8 @@ fail_string=" $(red 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)) ├────"
new_diff_string="────┤ $(yellow new): $(bold %s) ├────"
changed_diff_string="────┤ $(cyan changed): $(bold %s) ├────"
approval_string="[A]pprove? "

if diff --help | grep -- --color >/dev/null 2>&1; then
Expand Down
1 change: 1 addition & 0 deletions test/approvals/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ls
13 changes: 12 additions & 1 deletion test/approve
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,15 @@ context "when APPROVALS_DIR is set"
APPROVALS_DIR=alt-approvals

describe "writes approvals to a different directory"
approve "./sample-cli-app.sh"
approve "./sample-cli-app.sh"

unset APPROVALS_DIR

context "when AUTO_APPROVE is set"
AUTO_APPROVE=1
rm -f approvals/ls

describe "writes approvals without prompting the user"
approve "ls"

unset AUTO_APPROVE

0 comments on commit 9ecde1c

Please sign in to comment.