Skip to content

Commit ffe20c0

Browse files
authored
Merge pull request #17 from HS-Teams/codex/add-tests-for-navigation-and-utility-functions
Add Bats tests for directory helpers
2 parents 60b7487 + 8df403f commit ffe20c0

File tree

1 file changed

+205
-0
lines changed

1 file changed

+205
-0
lines changed

tests/hhs-dirs.bats

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
#!/usr/bin/env bats
2+
3+
load test_helper
4+
load "${HHS_FUNCTIONS_DIR}/hhs-dirs.bash"
5+
load_bats_libs
6+
7+
declare -ag __HHS_MSELECT_STUB_RESPONSES=()
8+
declare -g __HHS_MSELECT_STUB_STATUS=""
9+
10+
__hhs_mselect() {
11+
local outfile="$1"
12+
shift 2 || true
13+
local selection=""
14+
if ((${#__HHS_MSELECT_STUB_RESPONSES[@]} > 0)); then
15+
selection="${__HHS_MSELECT_STUB_RESPONSES[0]}"
16+
__HHS_MSELECT_STUB_RESPONSES=("${__HHS_MSELECT_STUB_RESPONSES[@]:1}")
17+
elif (($# > 0)); then
18+
selection="$1"
19+
fi
20+
21+
if [[ -n "${selection}" ]]; then
22+
printf '%s\n' "${selection}" >"${outfile}"
23+
else
24+
: >"${outfile}"
25+
fi
26+
27+
if [[ -n "${__HHS_MSELECT_STUB_STATUS}" ]]; then
28+
return "${__HHS_MSELECT_STUB_STATUS}"
29+
fi
30+
31+
[[ -n "${selection}" ]]
32+
}
33+
34+
setup() {
35+
__HHS_ORIG_PWD="$PWD"
36+
TEST_ROOT="$(mktemp -d)"
37+
export HHS_DIR="${TEST_ROOT}/hhs"
38+
export HOME="${TEST_ROOT}/home"
39+
mkdir -p "${HHS_DIR}" "${HOME}"
40+
export HHS_SAVED_DIRS_FILE="${HHS_DIR}/.saved_dirs"
41+
: >"${HHS_SAVED_DIRS_FILE}"
42+
export OLDIFS="${IFS}"
43+
WORK_DIR="${TEST_ROOT}/workspace"
44+
mkdir -p "${WORK_DIR}"
45+
cd "${WORK_DIR}"
46+
__HHS_MSELECT_STUB_RESPONSES=()
47+
__HHS_MSELECT_STUB_STATUS=""
48+
}
49+
50+
teardown() {
51+
cd "${__HHS_ORIG_PWD}"
52+
rm -rf "${TEST_ROOT}"
53+
__HHS_MSELECT_STUB_RESPONSES=()
54+
__HHS_MSELECT_STUB_STATUS=""
55+
}
56+
57+
@test "change_dir changes into provided directory" {
58+
mkdir -p "alpha"
59+
__hhs_change_dir "${WORK_DIR}/alpha"
60+
local exit_code=$?
61+
assert_equal "${exit_code}" 0
62+
assert_equal "$(pwd)" "${WORK_DIR}/alpha"
63+
assert_equal "${CURPWD}" "${WORK_DIR}/alpha"
64+
[[ -f "${HHS_DIR}/.last_dirs" ]] || fail "expected .last_dirs to be created"
65+
}
66+
67+
@test "change_dir reports missing directories" {
68+
run __hhs_change_dir "${WORK_DIR}/missing"
69+
assert_failure
70+
assert_output --partial "Directory \"${WORK_DIR}/missing\" was not found !"
71+
}
72+
73+
@test "changeback_ndirs navigates backwards multiple levels" {
74+
mkdir -p "one/two/three"
75+
__hhs_change_dir "${WORK_DIR}/one/two/three"
76+
local out_file
77+
out_file="$(mktemp)"
78+
__hhs_changeback_ndirs 2 >"${out_file}"
79+
local exit_code=$?
80+
assert_equal "${exit_code}" 0
81+
assert_equal "$(pwd)" "${WORK_DIR}/one"
82+
assert_equal "${OLDPWD}" "${WORK_DIR}/one/two/three"
83+
run cat "${out_file}"
84+
assert_output --partial "Changed directory backwards by 2 time(s)"
85+
rm -f "${out_file}"
86+
}
87+
88+
@test "changeback_ndirs surfaces seq errors for invalid counts" {
89+
mkdir -p "base/nested"
90+
__hhs_change_dir "${WORK_DIR}/base/nested"
91+
local err_file out_file
92+
err_file="$(mktemp)"
93+
out_file="$(mktemp)"
94+
__hhs_changeback_ndirs invalid >"${out_file}" 2>"${err_file}"
95+
local exit_code=$?
96+
assert_equal "${exit_code}" 0
97+
assert_equal "$(pwd)" "${WORK_DIR}/base/nested"
98+
run cat "${err_file}"
99+
assert_output --partial "invalid floating point argument"
100+
rm -f "${out_file}" "${err_file}"
101+
}
102+
103+
@test "godir cd into provided path directly" {
104+
mkdir -p "direct"
105+
__hhs_godir "${WORK_DIR}/direct"
106+
local exit_code=$?
107+
assert_equal "${exit_code}" 0
108+
assert_equal "$(pwd)" "${WORK_DIR}/direct"
109+
}
110+
111+
@test "godir selects from multiple matches via mselect" {
112+
mkdir -p "search/a/target" "search/b/target"
113+
cd "${WORK_DIR}/search"
114+
__HHS_MSELECT_STUB_RESPONSES=("./b/target")
115+
__hhs_godir "${WORK_DIR}/search" target
116+
local exit_code=$?
117+
assert_equal "${exit_code}" 0
118+
assert_equal "$(pwd)" "${WORK_DIR}/search/b/target"
119+
}
120+
121+
@test "godir reports when directory is missing" {
122+
mkdir -p "search"
123+
cd "${WORK_DIR}/search"
124+
run __hhs_godir "${WORK_DIR}/search" nomatch
125+
assert_failure
126+
assert_output --partial "No matches for directory with name \"nomatch\""
127+
}
128+
129+
@test "mkcd creates dotted path and jumps into it" {
130+
__hhs_mkcd "foo.bar.baz"
131+
local exit_code=$?
132+
assert_equal "${exit_code}" 0
133+
assert_equal "$(pwd)" "${WORK_DIR}/foo/bar/baz"
134+
[[ -d "${WORK_DIR}/foo/bar/baz" ]] || fail "expected directory tree to exist"
135+
}
136+
137+
@test "mkcd fails when path points to existing file" {
138+
touch "conflict"
139+
run __hhs_mkcd conflict
140+
assert_failure
141+
assert_output --partial "cannot create directory"
142+
}
143+
144+
@test "dirs selects saved entries via mselect" {
145+
mkdir -p "first" "second"
146+
__hhs_change_dir "${WORK_DIR}/first"
147+
__hhs_change_dir "${WORK_DIR}/second"
148+
__HHS_MSELECT_STUB_RESPONSES=("${WORK_DIR}/first")
149+
__hhs_dirs
150+
local exit_code=$?
151+
assert_equal "${exit_code}" 0
152+
assert_equal "$(pwd)" "${WORK_DIR}/first"
153+
}
154+
155+
@test "dirs returns failure when selection is cancelled" {
156+
mkdir -p "first" "second"
157+
__hhs_change_dir "${WORK_DIR}/first"
158+
__hhs_change_dir "${WORK_DIR}/second"
159+
__HHS_MSELECT_STUB_RESPONSES=()
160+
__HHS_MSELECT_STUB_STATUS=1
161+
run __hhs_dirs
162+
assert_failure
163+
}
164+
165+
@test "save_dir persists absolute path entries" {
166+
mkdir -p "persist"
167+
run __hhs_save_dir "${WORK_DIR}/persist" workspace
168+
assert_success
169+
assert_output --partial "saved as WORKSPACE"
170+
run cat "${HHS_SAVED_DIRS_FILE}"
171+
assert_output --partial "WORKSPACE=${WORK_DIR}/persist"
172+
}
173+
174+
@test "save_dir warns when directory is absent" {
175+
run __hhs_save_dir "${WORK_DIR}/missing" ghost
176+
assert_success
177+
assert_output --partial "Directory \"${WORK_DIR}/missing\" does not exist"
178+
[[ ! -s "${HHS_SAVED_DIRS_FILE}" ]] || fail "unexpected entry for missing directory"
179+
}
180+
181+
@test "load_dir changes to saved alias" {
182+
mkdir -p "persist"
183+
printf 'WORK=${WORK_DIR}/persist\n' >"${HHS_SAVED_DIRS_FILE}"
184+
__hhs_load_dir WORK
185+
local exit_code=$?
186+
assert_equal "${exit_code}" 0
187+
assert_equal "$(pwd)" "${WORK_DIR}/persist"
188+
}
189+
190+
@test "load_dir uses mselect when no alias is provided" {
191+
mkdir -p "persist" "other"
192+
printf 'WORK=${WORK_DIR}/persist\nOTHER=${WORK_DIR}/other\n' >"${HHS_SAVED_DIRS_FILE}"
193+
__HHS_MSELECT_STUB_RESPONSES=(" WORK=${WORK_DIR}/persist")
194+
__hhs_load_dir
195+
local exit_code=$?
196+
assert_equal "${exit_code}" 0
197+
assert_equal "$(pwd)" "${WORK_DIR}/persist"
198+
}
199+
200+
@test "load_dir warns about missing saved path" {
201+
printf 'GHOST=${WORK_DIR}/ghost\n' >"${HHS_SAVED_DIRS_FILE}"
202+
run __hhs_load_dir GHOST
203+
assert_success
204+
assert_output --partial "Directory \"${WORK_DIR}/ghost\" does not exist"
205+
}

0 commit comments

Comments
 (0)