Skip to content

Commit

Permalink
- Add it annotation and reduce annotation color noise
Browse files Browse the repository at this point in the history
  • Loading branch information
DannyBen committed Mar 8, 2024
1 parent 9ecde1c commit 5f22af7
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 21 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,28 @@ approve "ls -s" "ls_size"
```


### Adding `context` and `describe` annotations
### Annotation commands (`context`, `describe`, `it`)

If your apptovals test files become too long or complex, you may use the
`describe` and `context` commands to annotate your tests. These commands are
purely decorative and will output the provided strings as captions.
If your apptovals test files become too long or complex, you may the
`describe`, `context` or `it` commands to annotate your tests. These commands
are purely decorative and will output the provided strings as captions.

```bash
context "basic operations"
describe "test ls"
approve "ls -s"
describe "ls"
it "shows the list of files"
approve "ls"

context "when in a non-empty directory"
cd ./tmp
describe "test ls in another directory"
describe "ls -s"
approve "ls -s"
approve "ls -s" "ls_size"

cd ../
```

The indentation is optional.
You can use any of the annotation commands, in any order. The indentation is
optional.


### Testing exit code
Expand Down
48 changes: 38 additions & 10 deletions approvals.bash
Original file line number Diff line number Diff line change
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,16 +76,39 @@ 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"
Expand Down Expand Up @@ -127,8 +154,9 @@ set -e
trap 'onexit' EXIT
trap 'onerror' ERR

describe_string="$(blue ▌ describe) %s"
context_string="$(magenta ▌ context) %s"
describe_string="$(bold ▌ describe) %s"
context_string="$(bold ▌ context) %s"
it_string="$(bold ▌ it) %s"
fail_string=" $(red FAILED) %s"
pass_string=" $(green approved) %s"
exit_success_string="$(green ▌ exit) $(bold %s finished successfully)"
Expand Down
4 changes: 2 additions & 2 deletions test/approve
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ context "standard operation"
context "when APPROVALS_DIR is set"
APPROVALS_DIR=alt-approvals

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

unset APPROVALS_DIR
Expand All @@ -30,7 +30,7 @@ context "when AUTO_APPROVE is set"
AUTO_APPROVE=1
rm -f approvals/ls

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

unset AUTO_APPROVE

0 comments on commit 5f22af7

Please sign in to comment.