Skip to content

Commit 0678c70

Browse files
committed
chore(test): convert project_open suite to bats and run it in ci
1 parent 746477b commit 0678c70

9 files changed

Lines changed: 221 additions & 154 deletions

File tree

.github/workflows/lint.yml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,18 @@ jobs:
1414
uses: azohra/shell-linter@latest
1515
with:
1616
# zsh completion functions (shell/completions) use zsh-only syntax
17-
# that ShellCheck cannot parse; ShellCheck has no zsh dialect.
18-
exclude-paths: "shell/completions"
17+
# ShellCheck cannot parse; tests/ holds .bats files (non-bash @test
18+
# syntax) and third-party BATS submodules. ShellCheck skips both.
19+
exclude-paths: "shell/completions,tests"
20+
21+
test:
22+
runs-on: ubuntu-latest
23+
steps:
24+
- uses: actions/checkout@v3
25+
with:
26+
submodules: recursive
27+
- name: Run BATS tests
28+
run: ./tests/bats/bin/bats tests/*.bats
1929

2030
lua:
2131
runs-on: ubuntu-latest

.gitmodules

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[submodule "tests/bats"]
2+
path = tests/bats
3+
url = https://github.com/bats-core/bats-core.git
4+
[submodule "tests/test_helper/bats-support"]
5+
path = tests/test_helper/bats-support
6+
url = https://github.com/bats-core/bats-support.git
7+
[submodule "tests/test_helper/bats-assert"]
8+
path = tests/test_helper/bats-assert
9+
url = https://github.com/bats-core/bats-assert.git
10+
[submodule "tests/test_helper/bats-file"]
11+
path = tests/test_helper/bats-file
12+
url = https://github.com/bats-core/bats-file.git

tests/bats

Submodule bats added at 5a7db7a

tests/project_open.bats

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
#!/usr/bin/env bash
2+
# Tests for project_open discovery/cache/resolution (shell/aliases).
3+
# shellcheck disable=SC2030,SC2031 # BATS runs each test in a subshell by design
4+
5+
load test_helper
6+
7+
# tmux is irrelevant here; stub it to a no-op so sourcing/aliases stay quiet.
8+
tmux() { return 0; }
9+
10+
setup() {
11+
ROOT="$(temp_make)"
12+
export PROJECT_OPEN_ROOT="${ROOT}"
13+
XDG_CACHE_HOME="$(temp_make)"
14+
export XDG_CACHE_HOME
15+
16+
# Projects sit at <root>/<category>/<project> and always hold a .git.
17+
# wordpress projects carry a .git alongside their bedrock/site app dir;
18+
# vcpkg is a nested submodule (deeper) that must be excluded from scans.
19+
mkdir -p \
20+
"${ROOT}/mods/HoldFast/.git" \
21+
"${ROOT}/mods/HoldFast/lib" \
22+
"${ROOT}/mods/HoldFast/lib/vcpkg/.git" \
23+
"${ROOT}/misc/khuey/.git" \
24+
"${ROOT}/wordpress/acme/.git" \
25+
"${ROOT}/wordpress/acme/site" \
26+
"${ROOT}/wordpress/beta/.git" \
27+
"${ROOT}/wordpress/beta/bedrock"
28+
29+
# shell/aliases sources ${HOME}/.dotfiles/shell/os.sh, so point HOME at a
30+
# temp dir whose .dotfiles symlinks to this repo. That lets the suite run
31+
# from any checkout path, not only ~/.dotfiles.
32+
TEST_HOME="$(temp_make)"
33+
ln -s "$(cd "${DOTFILES_DIR}" && pwd)" "${TEST_HOME}/.dotfiles"
34+
export HOME="${TEST_HOME}"
35+
36+
# shellcheck source=/dev/null
37+
source "${HOME}/.dotfiles/shell/aliases"
38+
}
39+
40+
teardown() {
41+
temp_del "${ROOT}"
42+
temp_del "${XDG_CACHE_HOME}"
43+
temp_del "${TEST_HOME}"
44+
}
45+
46+
@test "scan finds category/project dirs and excludes nested submodules" {
47+
local out
48+
out="$(_project_open_scan | sed "s#^${ROOT}/##" | LC_ALL=C sort | tr '\n' ',')"
49+
assert_equal "${out}" "misc/khuey,mods/HoldFast,wordpress/acme,wordpress/beta,"
50+
}
51+
52+
@test "po_refresh creates the cache file" {
53+
po_refresh
54+
assert_file_exist "$(_project_open_cache_file)"
55+
}
56+
57+
@test "build is a no-op when the cache already exists" {
58+
po_refresh
59+
mkdir -p "${ROOT}/mods/NewMod/.git"
60+
_project_open_build_cache
61+
run grep -q '/NewMod$' "$(_project_open_cache_file)"
62+
assert_failure
63+
}
64+
65+
@test "po_refresh rebuilds and picks up new projects" {
66+
po_refresh
67+
mkdir -p "${ROOT}/mods/NewMod/.git"
68+
po_refresh
69+
run grep -q '/NewMod$' "$(_project_open_cache_file)"
70+
assert_success
71+
}
72+
73+
@test "read_cache is empty when the cache is missing" {
74+
rm -f "$(_project_open_cache_file)"
75+
run _project_open_read_cache
76+
assert_output ''
77+
}
78+
79+
@test "resolve falls back to a scan when the cache is missing" {
80+
rm -f "$(_project_open_cache_file)"
81+
run _project_open_resolve HoldFast
82+
assert_output "${ROOT}/mods/HoldFast"
83+
}
84+
85+
@test "resolve cache_only returns nothing when the cache is missing" {
86+
rm -f "$(_project_open_cache_file)"
87+
run _project_open_resolve HoldFast cache_only
88+
assert_output ''
89+
}
90+
91+
@test "no cache is built when the root is missing" {
92+
export PROJECT_OPEN_ROOT="${ROOT}/missing"
93+
local missing_cache
94+
missing_cache="$(_project_open_cache_file)"
95+
run _project_open_build_cache
96+
assert_file_not_exist "${missing_cache}"
97+
}
98+
99+
@test "resolve maps a project name to its path" {
100+
run _project_open_resolve HoldFast
101+
assert_output "${ROOT}/mods/HoldFast"
102+
}
103+
104+
@test "resolve maps a wordpress project to its path" {
105+
run _project_open_resolve acme
106+
assert_output "${ROOT}/wordpress/acme"
107+
}
108+
109+
@test "resolve of an unknown name is empty" {
110+
run _project_open_resolve nope
111+
assert_output ''
112+
}
113+
114+
@test "projects lists project basenames" {
115+
po_refresh
116+
local out
117+
out="$(_project_open_projects | LC_ALL=C sort | tr '\n' ',')"
118+
assert_equal "${out}" "HoldFast,acme,beta,khuey,"
119+
}
120+
121+
@test "targets lists a project's subdirs" {
122+
po_refresh
123+
run _project_open_targets HoldFast
124+
assert_output "lib"
125+
}
126+
127+
@test "targets of a childless project is empty" {
128+
po_refresh
129+
run _project_open_targets khuey
130+
assert_output ''
131+
}
132+
133+
@test "targets of an unknown project is empty" {
134+
po_refresh
135+
run _project_open_targets nope
136+
assert_output ''
137+
}
138+
139+
@test "no-arg project_open cds to the root" {
140+
po_refresh
141+
local dir
142+
dir="$(cd / && project_open >/dev/null 2>&1 && pwd)"
143+
assert_equal "${dir}" "${ROOT}"
144+
}
145+
146+
@test "project_open <name> cds into a plain project" {
147+
po_refresh
148+
local dir
149+
dir="$(cd / && project_open HoldFast >/dev/null 2>&1 && pwd)"
150+
assert_equal "${dir}" "${ROOT}/mods/HoldFast"
151+
}
152+
153+
@test "project_open descends into bedrock" {
154+
po_refresh
155+
local dir
156+
dir="$(cd / && project_open beta >/dev/null 2>&1 && pwd)"
157+
assert_equal "${dir}" "${ROOT}/wordpress/beta/bedrock"
158+
}
159+
160+
@test "project_open descends into site" {
161+
po_refresh
162+
local dir
163+
dir="$(cd / && project_open acme >/dev/null 2>&1 && pwd)"
164+
assert_equal "${dir}" "${ROOT}/wordpress/acme/site"
165+
}
166+
167+
@test "project_open <name> <subdir> cds into the subdir" {
168+
po_refresh
169+
local dir
170+
dir="$(cd / && project_open HoldFast lib >/dev/null 2>&1 && pwd)"
171+
assert_equal "${dir}" "${ROOT}/mods/HoldFast/lib"
172+
}
173+
174+
@test "project_open of an unknown project returns 1" {
175+
po_refresh
176+
local rc=0
177+
(cd / && project_open nope >/dev/null 2>&1) || rc=$?
178+
assert_equal "${rc}" "1"
179+
}

tests/project_open_test.sh

Lines changed: 0 additions & 152 deletions
This file was deleted.

tests/test_helper.bash

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
# Common test helper for BATS tests.
3+
4+
# GitHub Actions sets TERM=dumb, which makes tput error; force a sane value.
5+
if [[ -z "${TERM:-}" || "${TERM}" == "dumb" ]]; then
6+
export TERM="xterm"
7+
fi
8+
9+
load 'test_helper/bats-support/load'
10+
load 'test_helper/bats-assert/load'
11+
load 'test_helper/bats-file/load'
12+
13+
# Repo root (tests/ lives directly under it).
14+
DOTFILES_DIR="${BATS_TEST_DIRNAME}/.."

tests/test_helper/bats-assert

Submodule bats-assert added at 697471b

tests/test_helper/bats-file

Submodule bats-file added at 6bee58b

tests/test_helper/bats-support

Submodule bats-support added at 0954abb

0 commit comments

Comments
 (0)