Skip to content

Commit f898659

Browse files
committed
- Add approvals.bash testing library
1 parent 65d06d5 commit f898659

File tree

10 files changed

+167
-1
lines changed

10 files changed

+167
-1
lines changed

lib/bashly/commands/add.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class Add < Base
99
usage "bashly add colors [--force]"
1010
usage "bashly add yaml [--force]"
1111
usage "bashly add validations [--force]"
12+
usage "bashly add test [--force]"
1213
usage "bashly add comp FORMAT [OUTPUT --force]"
1314
usage "bashly add (-h|--help)"
1415

@@ -23,6 +24,7 @@ class Add < Base
2324
command "colors", "Add standard functions for printing colorful and formatted text to the lib directory."
2425
command "yaml", "Add standard functions for reading YAML files to the lib directory."
2526
command "validations", "Add argument validation functions to the lib directory."
27+
command "test", "Add approval testing."
2628
command "comp", "Generate a bash completions script or function."
2729

2830
example "bashly add strings --force"
@@ -55,6 +57,10 @@ def validations_command
5557
add_lib 'validations'
5658
end
5759

60+
def test_command
61+
add_lib 'test'
62+
end
63+
5864
def comp_command
5965
format = args['FORMAT']
6066
output = args['OUTPUT']

lib/bashly/libraries.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@ strings:
2323
- source: "templates/strings.yml"
2424
target: "%{user_source_dir}/bashly-strings.yml"
2525

26+
test:
27+
files:
28+
- source: "templates/test/approvals.bash"
29+
target: "%{user_target_dir}/test/approvals.bash"
30+
- source: "templates/test/approve"
31+
target: "%{user_target_dir}/test/approve"
32+
33+
post_install_message: |
34+
Edit your tests in !txtgrn!test/approve!txtrst! and then run:
35+
36+
!txtpur!$ ./test/approve!txtrst!"
37+
38+
Docs: !undblu!https://github.com/DannyBen/approvals.bash
39+
40+
2641
validations:
2742
files:
2843
- source: "templates/lib/validations/validate_dir_exists.sh"

lib/bashly/library.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ def config
5757
end
5858

5959
def target_file_args
60-
{ user_source_dir: Settings.source_dir }
60+
{
61+
user_source_dir: Settings.source_dir,
62+
user_target_dir: Settings.target_dir
63+
}
6164
end
6265
end
6366
end
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# approvals.bash v0.2.7
2+
#
3+
# Interactive approval testing for Bash.
4+
# https://github.com/DannyBen/approvals.bash
5+
approve() {
6+
local expected approval approval_file actual cmd
7+
approvals_dir=${APPROVALS_DIR:=approvals}
8+
9+
cmd=$1
10+
actual=$(eval "$cmd" 2>&1)
11+
last_exit_code=$?
12+
approval=$(printf "%b" "$cmd" | tr -s -c "[:alnum:]" _)
13+
approval_file="$approvals_dir/${2:-"$approval"}"
14+
15+
[[ -d "$approvals_dir" ]] || mkdir "$approvals_dir"
16+
17+
if [[ -f "$approval_file" ]]; then
18+
expected=$(cat "$approval_file")
19+
else
20+
echo "--- [$(blue "new: $cmd")] ---"
21+
printf "%b\n" "$actual"
22+
echo "--- [$(blue "new: $cmd")] ---"
23+
expected="$actual"
24+
user_approval "$cmd" "$actual" "$approval_file"
25+
return
26+
fi
27+
28+
if [[ "$(printf "%b" "$actual")" = "$(printf "%b" "$expected")" ]]; then
29+
green "PASS $cmd"
30+
else
31+
echo "--- [$(blue "diff: $cmd")] ---"
32+
$diff_cmd <(printf "%b" "$expected\n") <(printf "%b" "$actual\n" ) | tail -n +4
33+
echo "--- [$(blue "diff: $cmd")] ---"
34+
user_approval "$cmd" "$actual" "$approval_file"
35+
fi
36+
}
37+
38+
describe() {
39+
cyan "TEST $*"
40+
}
41+
42+
fail() {
43+
red "FAIL $*"
44+
exit 1
45+
}
46+
47+
pass() {
48+
green "PASS $*"
49+
return 0
50+
}
51+
52+
expect_exit_code() {
53+
if [[ $last_exit_code == "$1" ]]; then
54+
pass "exit $last_exit_code"
55+
else
56+
fail "Expected exit code $1, got $last_exit_code"
57+
fi
58+
}
59+
60+
red() { printf "\e[31m%b\e[0m\n" "$*"; }
61+
green() { printf "\e[32m%b\e[0m\n" "$*"; }
62+
blue() { printf "\e[34m%b\e[0m\n" "$*"; }
63+
magenta() { printf "\e[35m%b\e[0m\n" "$*"; }
64+
cyan() { printf "\e[36m%b\e[0m\n" "$*"; }
65+
66+
# Private
67+
68+
user_approval() {
69+
local cmd="$1"
70+
local actual="$2"
71+
local approval_file="$3"
72+
73+
if [[ -v CI || -v GITHUB_ACTIONS ]]; then
74+
fail "$cmd"
75+
fi
76+
77+
echo
78+
printf "[A]pprove? \n"
79+
response=$(bash -c "read -n 1 key; echo \$key")
80+
printf "\r"
81+
if [[ $response =~ [Aa] ]]; then
82+
printf "%b\n" "$actual" > "$approval_file"
83+
pass "$cmd"
84+
else
85+
fail "$cmd"
86+
fi
87+
}
88+
89+
if diff --help | grep -- --color > /dev/null 2>&1; then
90+
diff_cmd="diff --unified --color=always"
91+
else
92+
diff_cmd="diff --unified"
93+
fi

lib/bashly/templates/test/approve

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env bash
2+
# Run this from the root directory
3+
4+
cd ./test || exit
5+
source approvals.bash
6+
PATH="$PATH:../"
7+
8+
# Update me
9+
cli=download
10+
11+
# Tests
12+
describe "root command"
13+
approve "$cli"
14+
approve "$cli --help"
15+
16+
describe "some other command"
17+
approve "$cli other"
18+
approve "$cli other --help"
19+
20+
# ...more tests...

spec/approvals/cli/add/help

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Usage:
77
bashly add colors [--force]
88
bashly add yaml [--force]
99
bashly add validations [--force]
10+
bashly add test [--force]
1011
bashly add comp FORMAT [OUTPUT --force]
1112
bashly add (-h|--help)
1213

@@ -32,6 +33,9 @@ Commands:
3233
validations
3334
Add argument validation functions to the lib directory.
3435

36+
test
37+
Add approval testing.
38+
3539
comp
3640
Generate a bash completions script or function.
3741

spec/approvals/cli/add/test

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
created spec/tmp/test/approvals.bash
2+
created spec/tmp/test/approve
3+
4+
Edit your tests in test/approve and then run:
5+
6+
$ ./test/approve"
7+
8+
Docs: https://github.com/DannyBen/approvals.bash
9+

spec/approvals/cli/generate/usage

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ Usage:
55
bashly add colors [--force]
66
bashly add yaml [--force]
77
bashly add validations [--force]
8+
bashly add test [--force]
89
bashly add comp FORMAT [OUTPUT --force]
910
bashly add (-h|--help)

spec/approvals/library/config-keys

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
- yaml
55
- lib
66
- strings
7+
- test
78
- validations
89
- completions
910
- completions_script

spec/bashly/commands/add_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,20 @@
9999
end
100100
end
101101

102+
context "with test command" do
103+
let(:lib_file) { "#{target_dir}/test/approvals.bash" }
104+
105+
before do
106+
reset_tmp_dir create_src: true
107+
end
108+
109+
it "copies the test folder to the user space" do
110+
expect { subject.run %w[add test] }.to output_approval('cli/add/test')
111+
expect(File).to exist(lib_file)
112+
end
113+
end
114+
115+
102116
context "with colors command" do
103117
let(:lib_file) { "#{source_dir}/lib/colors.sh" }
104118

0 commit comments

Comments
 (0)