Skip to content

Commit 57e15fa

Browse files
authored
feat: 🧪 add Bats tests for cli-tips.sh
- Introduced a new GitHub Actions workflow to run Bats tests on `cli-tips.sh`. - Created a comprehensive Bats test suite to validate functionality, including: * Help options (`-h` and `--help`) * Language-specific tips * Error handling for unknown options and missing files and etc. - Updated VSCode settings to set a formatter for `.bats` files
1 parent b6d91ef commit 57e15fa

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

.github/workflows/test.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Run Bats Tests
2+
3+
# Trigger the workflow on push events to cli-tips.sh
4+
on:
5+
push:
6+
paths:
7+
- "cli-tips.sh"
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
# Checkout the repository
15+
- name: 📥 Checkout Repository
16+
uses: actions/checkout@v3
17+
18+
- name: 🧪 Run Bats Tests
19+
run: npx bats test/cli-tips.bats

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@
55
},
66
"[markdown]": {
77
"editor.defaultFormatter": "esbenp.prettier-vscode"
8+
},
9+
"[bats]": {
10+
"editor.defaultFormatter": "foxundermoon.shell-format"
811
}
912
}

test/cli-tips.bats

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#!/usr/bin/env bats
2+
3+
setup() {
4+
# Create a temporary directory for TIPS_FOLDER
5+
export TIPS_FOLDER="$(mktemp -d)"
6+
7+
# Create sample tips files
8+
echo -e "Tip 1 in English\nTip 2 in English" >"$TIPS_FOLDER/en.txt"
9+
echo -e "Consejo 1 en Español\nConsejo 2 en Español" >"$TIPS_FOLDER/es.txt"
10+
11+
# Path to the script being tested
12+
SCRIPT_PATH="./cli-tips.sh"
13+
}
14+
15+
teardown() {
16+
# Remove the temporary TIPS_FOLDER
17+
rm -rf "$TIPS_FOLDER"
18+
}
19+
20+
@test "Displays help with -h option" {
21+
run "$SCRIPT_PATH" -h
22+
[ "$status" -eq 0 ]
23+
[[ "$output" == *"Usage:"* ]]
24+
}
25+
26+
@test "Displays help with --help option" {
27+
run "$SCRIPT_PATH" --help
28+
[ "$status" -eq 0 ]
29+
[[ "$output" == *"Usage:"* ]]
30+
}
31+
32+
@test "Errors on unknown option" {
33+
run "$SCRIPT_PATH" --unknown
34+
[ "$status" -ne 0 ]
35+
[[ "$output" == *"Unknown option: --unknown"* ]]
36+
}
37+
38+
@test "Prints a tip in default language" {
39+
run "$SCRIPT_PATH"
40+
[ "$status" -eq 0 ]
41+
[[ "$output" == "Tip "* ]]
42+
}
43+
44+
@test "Prints a tip in specified language" {
45+
run "$SCRIPT_PATH" --language es
46+
[ "$status" -eq 0 ]
47+
[[ "$output" == "Consejo "* ]]
48+
}
49+
50+
@test "Filters tips with --about keyword" {
51+
# Add a tip containing the keyword 'git' to the English tips
52+
echo -e "Tip about git" >>"$TIPS_FOLDER/en.txt"
53+
54+
run "$SCRIPT_PATH" --about git
55+
[ "$status" -eq 0 ]
56+
[[ "$output" == "Tip about git" ]]
57+
}
58+
59+
@test "Filters tips with --about keyword and --language option" {
60+
# Add a tip containing the keyword 'git' to the English tips
61+
echo -e "Consejo sobre git" >>"$TIPS_FOLDER/es.txt"
62+
63+
run "$SCRIPT_PATH" --about git --language es
64+
[ "$status" -eq 0 ]
65+
[[ "$output" == "Consejo sobre git" ]]
66+
}
67+
68+
@test "Exits if no matching tips are found" {
69+
run "$SCRIPT_PATH" --about nonexistent
70+
[ "$status" -eq 0 ]
71+
[ -z "$output" ]
72+
}
73+
74+
@test "Uses default language if specified language file is missing" {
75+
rm "$TIPS_FOLDER/es.txt"
76+
run "$SCRIPT_PATH" --language es
77+
[ "$status" -eq 0 ]
78+
[[ "$output" == "Tip "* ]]
79+
}
80+
81+
@test "Errors if no language is specified after --language option" {
82+
run "$SCRIPT_PATH" --language
83+
[ "$status" -ne 0 ]
84+
[[ "$output" == *"Error: No language specified"* ]]
85+
}
86+
87+
@test "Handles missing TIPS_FOLDER gracefully" {
88+
rm -rf "$TIPS_FOLDER"
89+
run "$SCRIPT_PATH"
90+
[ "$status" -ne 0 ]
91+
}
92+
93+
@test "Handles empty tips file gracefully" {
94+
echo -n "" >"$TIPS_FOLDER/en.txt"
95+
run "$SCRIPT_PATH"
96+
[ "$status" -eq 0 ]
97+
[ -z "$output" ]
98+
}

0 commit comments

Comments
 (0)