Skip to content

Commit 3d6713b

Browse files
authored
Merge pull request #20 from HS-Teams/codex/add-tests-for-hhs-shell-utils
Add Bats coverage for shell utilities
2 parents 00ae2b6 + c91d553 commit 3d6713b

File tree

1 file changed

+306
-0
lines changed

1 file changed

+306
-0
lines changed

tests/hhs-shell-utils.bats

Lines changed: 306 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,306 @@
1+
#!/usr/bin/env bats
2+
3+
# Script: hhs-shell-utils.bats
4+
# Purpose: __hhs_shell-utils tests.
5+
# Created: Apr 09, 2025
6+
# Author: OpenAI ChatGPT (gpt-5-codex)
7+
# License: Please refer to <https://opensource.org/licenses/MIT>
8+
#
9+
# Copyright (c) 2025, HomeSetup team
10+
11+
load test_helper
12+
load "${HHS_FUNCTIONS_DIR}/hhs-text.bash"
13+
load "${HHS_FUNCTIONS_DIR}/hhs-shell-utils.bash"
14+
load_bats_libs
15+
16+
setup() {
17+
export HHS_SHOPTS_FILE="${BATS_TEST_TMPDIR}/shopt-state.toml"
18+
: >"${HHS_SHOPTS_FILE}"
19+
20+
HHS_HISTORY_OUTPUT=$' 1 placeholder\n'
21+
HHS_TPUT_COLS=80
22+
23+
unset HHS_HAS_git HHS_HAS_brew HHS_HAS_seq HHS_HAS_jot HHS_HAS_tldr
24+
HHS_HAS_git=1
25+
HHS_HAS_brew=1
26+
HHS_HAS_seq=0
27+
HHS_HAS_jot=1
28+
HHS_HAS_tldr=1
29+
30+
declare -gA HHS_SHOPT_STATES=()
31+
HHS_SHOPT_STATES=(
32+
[cdspell]='off'
33+
[globstar]='on'
34+
[histappend]='on'
35+
)
36+
37+
HHS_TOML_SET_WRITES=()
38+
HHS_MSELECT_STATUS=0
39+
HHS_MSELECT_CHOICE=""
40+
HHS_CHSH_STATUS=0
41+
HHS_CHSH_CALL=""
42+
HHS_CLEAR_CALLED=0
43+
HHS_GREP_SHELLS_OUTPUT=""
44+
45+
HHS_DU_LISTING=""
46+
HHS_DU_TOTAL=""
47+
48+
GIT_IS_REPO=1
49+
GIT_REMOTE_OUTPUT=""
50+
GIT_LOG_OUTPUT=""
51+
GIT_BRANCH_OUTPUT=""
52+
GIT_DIFF_OUTPUT=""
53+
}
54+
55+
teardown() {
56+
unset HHS_HAS_git HHS_HAS_brew HHS_HAS_seq HHS_HAS_jot HHS_HAS_tldr
57+
}
58+
59+
__hhs_highlight() {
60+
cat
61+
}
62+
63+
__hhs_has() {
64+
local cmd="$1"
65+
local var="HHS_HAS_${cmd//[^a-zA-Z0-9]/_}"
66+
local override="${!var}"
67+
if [[ -n "${override}" ]]; then
68+
return "${override}"
69+
fi
70+
command -v "${cmd}" >/dev/null 2>&1
71+
}
72+
73+
history() {
74+
printf "%s" "${HHS_HISTORY_OUTPUT}"
75+
}
76+
77+
tput() {
78+
if [[ "$1" == "cols" ]]; then
79+
echo "${HHS_TPUT_COLS}"
80+
return 0
81+
fi
82+
echo 0
83+
}
84+
85+
clear() {
86+
HHS_CLEAR_CALLED=1
87+
}
88+
89+
chsh() {
90+
HHS_CHSH_CALL="$*"
91+
return "${HHS_CHSH_STATUS}"
92+
}
93+
94+
__hhs_mselect() {
95+
local outfile="$1"
96+
shift 2
97+
if [[ "${HHS_MSELECT_STATUS}" -eq 0 && -n "${HHS_MSELECT_CHOICE}" ]]; then
98+
printf "%s\n" "${HHS_MSELECT_CHOICE}" >"${outfile}"
99+
fi
100+
return "${HHS_MSELECT_STATUS}"
101+
}
102+
103+
grep() {
104+
if [[ "$#" -eq 2 && "$1" == '/.*' && "$2" == '/etc/shells' ]]; then
105+
if [[ -n "${HHS_GREP_SHELLS_OUTPUT}" ]]; then
106+
printf "%s\n" "${HHS_GREP_SHELLS_OUTPUT}"
107+
else
108+
printf "%s\n" "/bin/bash" "/bin/sh"
109+
fi
110+
return 0
111+
fi
112+
command grep "$@"
113+
}
114+
115+
shopt() {
116+
if [[ $# -eq 0 ]]; then
117+
for opt in $(printf "%s\n" "${!HHS_SHOPT_STATES[@]}" | sort); do
118+
printf "%s %s\n" "${opt}" "${HHS_SHOPT_STATES[${opt}]}"
119+
done
120+
return 0
121+
fi
122+
123+
case "$1" in
124+
-s)
125+
local opt="$2"
126+
HHS_SHOPT_STATES["${opt}"]='on'
127+
return 0
128+
;;
129+
-u)
130+
local opt="$2"
131+
HHS_SHOPT_STATES["${opt}"]='off'
132+
return 0
133+
;;
134+
-q)
135+
local opt="$2"
136+
[[ "${HHS_SHOPT_STATES[${opt}]}" == 'on' ]]
137+
return $?
138+
;;
139+
-o)
140+
printf "errexit off\n"
141+
return 0
142+
;;
143+
-p)
144+
for opt in $(printf "%s\n" "${!HHS_SHOPT_STATES[@]}" | sort); do
145+
printf "%s %s\n" "${opt}" "${HHS_SHOPT_STATES[${opt}]}"
146+
done
147+
return 0
148+
;;
149+
*)
150+
local opt="$1"
151+
printf "%s %s\n" "${opt}" "${HHS_SHOPT_STATES[${opt}]:-off}"
152+
return 0
153+
;;
154+
esac
155+
}
156+
157+
__hhs_toml_set() {
158+
local file="$1"
159+
local kv="$2"
160+
printf "%s\n" "${kv}" >"${file}"
161+
HHS_TOML_SET_WRITES+=("${kv}")
162+
return 0
163+
}
164+
165+
__hhs() {
166+
printf "__hhs %s\n" "$*"
167+
}
168+
169+
__hhs_about() {
170+
printf "__hhs_about %s\n" "$*"
171+
}
172+
173+
__hhs_errcho() {
174+
printf "%s: %s\n" "$1" "$2" >&2
175+
}
176+
177+
python() {
178+
printf "Python 3.11.0\n"
179+
}
180+
181+
git() {
182+
if [[ "$1" == 'rev-parse' && "$2" == '--is-inside-work-tree' ]]; then
183+
return "${GIT_IS_REPO}"
184+
elif [[ "$1" == 'remote' && "$2" == '-v' ]]; then
185+
printf "%s\n" "${GIT_REMOTE_OUTPUT}"
186+
return 0
187+
elif [[ "$1" == 'log' && "$2" == '--oneline' ]]; then
188+
printf "%s\n" "${GIT_LOG_OUTPUT}"
189+
return 0
190+
elif [[ "$1" == 'rev-parse' && "$2" == '--abbrev-ref' ]]; then
191+
printf "%s\n" "${GIT_BRANCH_OUTPUT}"
192+
return 0
193+
elif [[ "$1" == 'diff' && "$2" == '--shortstat' ]]; then
194+
[[ -n "${GIT_DIFF_OUTPUT}" ]] && printf "%s\n" "${GIT_DIFF_OUTPUT}"
195+
return 0
196+
fi
197+
return 0
198+
}
199+
200+
du() {
201+
if [[ "$1" == '-hk' ]]; then
202+
printf "%s" "${HHS_DU_LISTING}"
203+
return 0
204+
elif [[ "$1" == '-hc' ]]; then
205+
printf "%s" "${HHS_DU_TOTAL}"
206+
return 0
207+
fi
208+
command du "$@"
209+
}
210+
211+
# -- Tests -----------------------------------------------------------------
212+
213+
@test "__hhs_history prints usage when called with help" {
214+
run __hhs_history -h
215+
assert_failure
216+
assert_output --partial "usage: __hhs_history [regex_filter]"
217+
}
218+
219+
@test "__hhs_history collapses duplicates while keeping latest entries" {
220+
HHS_HISTORY_OUTPUT=$' 1 ls\n 2 git status\n 3 ls\n 4 git commit\n'
221+
run __hhs_history
222+
assert_success
223+
assert_line --index 1 --regexp '^ +2 +git status$'
224+
assert_line --index 2 --regexp '^ +3 +ls$'
225+
assert_line --index 3 --regexp '^ +4 +git commit$'
226+
}
227+
228+
@test "__hhs_hist_stats renders aggregate counts and bar chart" {
229+
HHS_HISTORY_OUTPUT=$' 1 2024-01-01 10:00:00 - 0 ls\n 2 2024-01-01 10:05:00 - 0 git status\n 3 2024-01-01 10:06:00 - 0 ls\n 4 2024-01-01 10:07:00 - 0 git commit\n 5 2024-01-01 10:08:00 - 0 ls\n'
230+
HHS_TPUT_COLS=72
231+
run __hhs_hist_stats 2
232+
assert_success
233+
assert_output --partial "Top 2 used commands in history"
234+
assert_output --partial "ls"
235+
assert_output --partial "git"
236+
assert_output --partial "|▄"
237+
}
238+
239+
@test "__hhs_where_am_i delegates to __hhs help when command is available" {
240+
HHS_HAS_sample=0
241+
HHS_HAS_tldr=1
242+
run __hhs_where_am_i sample
243+
assert_success
244+
assert_line --index 0 "__hhs help sample"
245+
}
246+
247+
@test "__hhs_where_am_i prints repository context" {
248+
HHS_HAS_git=0
249+
GIT_IS_REPO=0
250+
GIT_REMOTE_OUTPUT=$'origin git@example.com:repo.git (fetch)'
251+
GIT_LOG_OUTPUT=$'abc123 Fix bug'
252+
GIT_BRANCH_OUTPUT=$'main'
253+
GIT_DIFF_OUTPUT=$' 1 file changed, 2 insertions(+)'
254+
run __hhs_where_am_i
255+
assert_success
256+
assert_output --partial "-=- You are here -=-"
257+
assert_output --partial "Remote repository:"
258+
assert_output --partial "abc123"
259+
assert_output --partial " main"
260+
assert_output --partial "1 file changed"
261+
}
262+
263+
@test "__hhs_shell_select changes default shell when selection succeeds" {
264+
local fake_shell_dir
265+
fake_shell_dir="${BATS_TEST_TMPDIR}/shells"
266+
mkdir -p "${fake_shell_dir}"
267+
local fake_shell="${fake_shell_dir}/fakeshell"
268+
printf '#!/bin/sh\n' >"${fake_shell}"
269+
chmod +x "${fake_shell}"
270+
271+
HHS_GREP_SHELLS_OUTPUT="${fake_shell}"
272+
HHS_MSELECT_CHOICE="${fake_shell}"
273+
HHS_CHSH_STATUS=0
274+
HHS_HAS_brew=1
275+
276+
run __hhs_shell_select
277+
assert_success
278+
assert_output --partial "Your default shell has changed to => '${fake_shell}'"
279+
[ "${HHS_CHSH_CALL}" = "-s ${fake_shell}" ]
280+
[ "${HHS_CLEAR_CALLED}" -eq 1 ]
281+
}
282+
283+
@test "__hhs_shopt persists toggled options" {
284+
HHS_SHOPT_STATES[cdspell]='off'
285+
run __hhs_shopt -s cdspell
286+
assert_success
287+
assert_output --partial "Shell option cdspell set to on"
288+
run cat "${HHS_SHOPTS_FILE}"
289+
assert_output "cdspell=on"
290+
[ "${HHS_SHOPT_STATES[cdspell]}" = 'on' ]
291+
}
292+
293+
@test "__hhs_du summarizes directory usage" {
294+
local dir="${BATS_TEST_TMPDIR}/du-sample"
295+
mkdir -p "${dir}"
296+
HHS_DU_LISTING=$'4096\t./alpha\n2048\t./beta\n1024\t.\n'
297+
HHS_DU_TOTAL=$'6.0M\ttotal\n'
298+
299+
run __hhs_du "${dir}" 2 20
300+
assert_success
301+
assert_output --partial "Top 2 disk usage"
302+
assert_output --partial "alpha"
303+
assert_output --partial "beta"
304+
assert_output --partial "|▄"
305+
assert_output --partial "Total: 6.0M"
306+
}

0 commit comments

Comments
 (0)