diff --git a/tests/tools-help b/tests/tools-help index cb27c288f..cb19063bf 100755 --- a/tests/tools-help +++ b/tests/tools-help @@ -53,3 +53,16 @@ for f in "${tools[@]}"; do || fail "$f --help doesn't follow its header when it grows by a line" done echo "ok - ${#tools[@]} tools print their header comment as --help, however long it grows, and no code" + +# The same --help, output and exit code, run by a relative path from the +# repository's parent and from tools/: a tool that cds to the repository +# first must not look for its header at "$0" from the directory it left +# (#559). +help_from() { (cd -- "$1" && "$2" --help 2>&1; echo "exit $?"); } +repo=$PWD +for f in "${tools[@]}"; do + out=$(help_from "$repo" "$f") + [[ $(help_from "$repo/.." "${repo##*/}/$f") == "$out" ]] || fail "$f --help differs run from the repository's parent" + [[ $(help_from "$repo/tools" "./${f#tools/}") == "$out" ]] || fail "$f --help differs run from tools/" +done +echo "ok - ${#tools[@]} tools print the same --help by a relative path from another directory" diff --git a/tools/lib/usage.sh b/tools/lib/usage.sh index 63299f273..b5cbc6d9f 100644 --- a/tools/lib/usage.sh +++ b/tools/lib/usage.sh @@ -10,17 +10,25 @@ # # source tools/lib/usage.sh # usage_from_header [--raw] FILE [PARAGRAPHS] -# prints FILE's header with each line's "#" and one space after it taken +# prints the header of the tool named FILE, in the tools directory +# above this file, with each line's "#" and one space after it taken # off (--raw keeps them). With PARAGRAPHS, only that many paragraphs of # it (a bare "#" line ends one), for a tool whose help is the first part # of a longer header. +# +# Only FILE's name is used. A tool that cds to the repository first and +# passes "$0" passes a path relative to the directory it left +# (`cd ..; omarchy-mobile/tools/build-packages --help`, #559), while this +# file's own path is still good: the tool has just sourced it from where it +# is now. usage_from_header() { local raw=0 [[ ${1:-} == --raw ]] && { raw=1; shift; } + local name=${1:?usage_from_header FILE [PARAGRAPHS]} awk -v raw="$raw" -v paras="${2:-0}" ' NR <= 2 { next } !/^#/ { exit } /^#[ \t]*$/ && ++blank == paras { exit } - { if (!raw) sub(/^# ?/, ""); print }' "${1:?usage_from_header FILE [PARAGRAPHS]}" + { if (!raw) sub(/^# ?/, ""); print }' "$(dirname -- "${BASH_SOURCE[0]}")/../${name##*/}" }