|
| 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 | +} |
0 commit comments