From eb5beb7e7ac52baebdeacf7cee5cc20bbd03566f Mon Sep 17 00:00:00 2001 From: Danny Ben Shitrit Date: Thu, 7 Mar 2024 17:06:13 +0000 Subject: [PATCH 1/3] - Add auto-approval with AUTO_APPROVE environment variable --- approvals.bash | 17 +++++++++++------ test/approvals/.gitignore | 1 + test/approve | 13 ++++++++++++- 3 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 test/approvals/.gitignore diff --git a/approvals.bash b/approvals.bash index 8a861e1..096a092 100644 --- a/approvals.bash +++ b/approvals.bash @@ -91,10 +91,15 @@ user_approval() { 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" @@ -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 diff --git a/test/approvals/.gitignore b/test/approvals/.gitignore new file mode 100644 index 0000000..3fbb950 --- /dev/null +++ b/test/approvals/.gitignore @@ -0,0 +1 @@ +ls \ No newline at end of file diff --git a/test/approve b/test/approve index 023decb..5e8e9df 100755 --- a/test/approve +++ b/test/approve @@ -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" \ No newline at end of file + 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 From 5cf4fd0c498adc8e7d1a32bbcb25af9d22af07c6 Mon Sep 17 00:00:00 2001 From: Danny Ben Shitrit Date: Thu, 7 Mar 2024 17:12:17 +0000 Subject: [PATCH 2/3] fix auto-approval in ci --- approvals.bash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/approvals.bash b/approvals.bash index 096a092..7c0c556 100644 --- a/approvals.bash +++ b/approvals.bash @@ -87,7 +87,7 @@ 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 From 6d78f9c61a65a7bb5019aead6d44fee37d1e0694 Mon Sep 17 00:00:00 2001 From: Danny Ben Shitrit Date: Thu, 7 Mar 2024 17:22:38 +0000 Subject: [PATCH 3/3] mention AUTO_APPROVE in readme --- README.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0cdfb42..4f3e3dc 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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**,