Skip to content

Commit 15fee64

Browse files
authored
Fix library load ordering and add zsh coverage (#48)
1 parent 5dbd63b commit 15fee64

3 files changed

Lines changed: 26 additions & 12 deletions

File tree

AGENTS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
- Run `shellcheck` on all modified shell scripts.
66
- If tests exist, run them and ensure each test method covers one execution path.
77
- Keep files you touch tidy by fixing typos or minor issues you encounter.
8+
- When sourcing multiple files, load them individually in a deterministic order.

pms.sh

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,30 +71,25 @@ fi
7171
#
7272
# @internal
7373
####
74-
pms_libs="$(
75-
find "$PMS/lib" -maxdepth 1 -type f \( -name '*.sh' -o -name "*.$PMS_SHELL" \)
76-
)"
77-
for library_file in $pms_libs; do
74+
# Load core and auxiliary libraries in a stable order. The while loop avoids
75+
# shell-specific word-splitting issues and guarantees each file is sourced
76+
# individually.
77+
while IFS= read -r library_file; do
7878
# shellcheck disable=SC1090
7979
. "$library_file"
8080
if [ 1 -eq "${PMS_DEBUG:-0}" ]; then
8181
echo "source $library_file"
8282
fi
83-
done
84-
unset pms_libs library_file
83+
done < <(find "$PMS/lib" -maxdepth 1 -type f \( -name '*.sh' -o -name "*.$PMS_SHELL" \) | sort)
8584

8685
if [ -d "$PMS_LOCAL/lib" ]; then
87-
local_libs="$(
88-
find "$PMS_LOCAL/lib" -maxdepth 1 -type f \( -name '*.sh' -o -name "*.$PMS_SHELL" \)
89-
)"
90-
for local_library in $local_libs; do
86+
while IFS= read -r local_library; do
9187
# shellcheck disable=SC1090
9288
. "$local_library"
9389
if [ 1 -eq "${PMS_DEBUG:-0}" ]; then
9490
echo "source $local_library"
9591
fi
96-
done
97-
unset local_libs local_library
92+
done < <(find "$PMS_LOCAL/lib" -maxdepth 1 -type f \( -name '*.sh' -o -name "*.$PMS_SHELL" \) | sort)
9893
fi
9994

10095
_pms_source_file "$HOME/.pms.plugins"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env bats
2+
# shellcheck shell=bash
3+
4+
setup() {
5+
pms_root="$(cd "$BATS_TEST_DIRNAME/.." && pwd)"
6+
export PMS="$pms_root"
7+
export PMS_LOCAL="$BATS_TEST_TMPDIR/local"
8+
mkdir -p "$PMS_LOCAL/plugins"
9+
export PMS_DEBUG=0
10+
export PMS_THEME=default
11+
export HOME="$BATS_TEST_TMPDIR/home"
12+
mkdir -p "$HOME"
13+
}
14+
15+
@test "pms.sh loads libraries when sourced in zsh" {
16+
run zsh -c "source \"$PMS/pms.sh\" zsh >/dev/null && type _pms_source_file >/dev/null"
17+
[ "$status" -eq 0 ]
18+
}

0 commit comments

Comments
 (0)