(s)add path, mor tests

This commit is contained in:
Corwin Brust 2026-05-20 05:44:16 +00:00
parent 86af044093
commit 41fdb923b3

View file

@ -1,6 +1,10 @@
#!/usr/bin/bash
# C4 functions for the ghostwheel @@&<_f @@
# Copyright (C)2026 Corwin Brust <corwin@brust>
#
# This program is distributed under the terms of the GNU Public
# License version 3 or (at your option) any later version.
#
# GPLv3+: The program is Free Software
#
# This is a bootstrapping file, used in RC et. al.
@ -105,7 +109,7 @@ function c4.f.pargs {
#### c4.f.sargs_Array -
### create array from (quoted) string list
function c4.f.sargs {
local -a rv;
local -a rv;n
local -i i=0;
for arg in $1 ; do
rv+=$arg;
@ -263,16 +267,31 @@ C4_RUN=0 # only run tests which can indicate startup issues
### UTILITY
function c4.s.path {
_s_path_i=
if test -z "$1" || [[ "$1" =~ \. ]] ; then
return;
fi
if test -d "$1" ; then
printf '%s' "$1"
fi
}
## print the first non empty input
function c4.s.first {
for _s_first_i in "$@" ; do
_s_first_i="${_s_first_i##*( )}"
if test -n "$_s_first_i" ; then
printf '%s' "$_s_first_i"
break
fi
done
}
c4.t.b "s" 0 "first" "[ x z] == x" test "x" == '$( c4.s.first "" "$foo" "x" "z" )'
c4.t.b "s" 0 "first" "[] == " test -z '$( c4.s.first )'
c4.t.b "s" 0 "first" "[x] == x" test "x" == '$( c4.s.first x )'
c4.t.b "s" 0 "first" "[ x] == x" test "x" == '$( c4.s.first "" x )'
c4.t.b "s" 0 "first" "[' ' x] == x" test "x" == '$( c4.s.first " " x )'
c4.t.b "s" 0 "first" "[x z] == x" test "x" == '$( c4.s.first x z )'
## Return first non-empty item matching PATTERN from SET
## when NEGATE is ! or NOT return first non-empty, non-
@ -280,15 +299,16 @@ c4.t.b "s" 0 "first" "[ x z] == x" test "x" == '$( c4.s.first "" "$foo" "x" "z"
function c4.s.firstmatch {
_s_firstmatch_ng=
_s_firstmatch_an=
if [[ "$1" =~ "!" ]] || [[ "${1^^}" =~ "NOT" ]] ; then
if [[ "$1" == "!" ]] || [[ "${1^^}" =~ "NOT" ]] ; then
_s_firstmatch_ng=1; shift
fi
if [[ "$1" =~ "^" ]] ; then
if [[ "$1" == "^" ]] ; then
_s_firstmatch_an="C4C4C4C4"; shift
fi
_s_firstmatch_re=$1 ; shift;
for _s_firstmatch_i in "$@" ; do
_s_firstmatch_i="${_s_firstmatch_i##*( )}"
if [[ "$_s_firstmatch_an$_s_firstmatch_i" \
=~ "$_s_firstmatch_an$_s_firstmatch_re" ]] ; then
if test -z "$_s_firstmatch_ng" -a \
@ -305,8 +325,16 @@ function c4.s.firstmatch {
fi
done
}
c4.t.b "s" 0 "firstmatch" "[] == " test -z '$( c4.s.firstmatch )'
c4.t.b "s" 0 "firstmatch" "[x] == " test -z '$( c4.s.firstmatch x )'
c4.t.b "s" 0 "firstmatch" "[ x] == " test "x" == '$( c4.s.firstmatch "" x )'
c4.t.b "s" 0 "firstmatch" "[x x] == x" test "x" == '$( c4.s.firstmatch x "" x )'
c4.t.b "s" 0 "firstmatch" "[x x z] == x" test "x" == '$( c4.s.firstmatch x x z )'
c4.t.b "s" 0 "firstmatch" "[x o x] == x" test "x" == '"$( c4.s.firstmatch "x" "o" "x" )"'
c4.t.b "s" 0 "firstmatch" "[!x x o] == o" test "o" == '"$( c4.s.firstmatch "!" "x" "" "x" "o" )"'
c4.t.b "s" 0 "firstmatch" "[x ox x] == ox" test "ox" == '"$( c4.s.firstmatch "x" "ox" "x" )"'
c4.t.b "s" 0 "firstmatch" "[^x ox x] == x" test "x" == '"$( c4.s.firstmatch "^" "x" "ox" "x" )"'
c4.t.b "s" 0 "firstmatch" "[!o o x] == x" test "x" == '"$( c4.s.firstmatch "!" "o" " " "o" "x" )"'
c4.t.b "s" 0 "firstmatch" "[!o o x] == x" test "x" == '"$( c4.s.firstmatch "!" "o" "o" " " "x" )"'
## give a friendly "did it work?" report
## TODO supress ANSI color-codes when STDOUT isn't a TTY
@ -399,8 +427,9 @@ function c4.s.host {
test -n "$_s_host" && printf '%s' "$(basename "$_s_host")"
}
c4.t.b "s" 0 \
"host" "\$0 == gw14" test \
'"$( c4.s.host /spokes/gw14/storage/c4/ghostwheel.fn.sh )"' == '"gw14"'
"host" "\$0 == gw" \[\[ \
'"$( c4.s.host /spokes/gw16/storage/c4/ghostwheel.fn.sh )"' =~ gw \
\]\]
## c4.fqn [ GW [ DOMAIN ]]
## Output the fully qualfied hostname
@ -421,12 +450,8 @@ function c4.fqn {
fi
}
## Unit Tests:
c4.t.b "s" 0 "fqn" \
"\$0 == gw14" test \
'"$( c4.fqn foo bar )"' == 'foo.bar'
c4.t.b "s" 0 "fqn_hostname" \
"C4.fqn == \`hostname -f\`" test \
'"$( c4.fqn )"' == '"$(hostname -f)"'
c4.t.b "s" 0 "fqn" "[foo bar] == foo.bar" test '"$( c4.fqn foo bar )"' == 'foo.bar'
c4.t.b "s" 0 "fqn_hostname" "C4.fqn == \`hostname -f\`" test '"$( c4.fqn )"' == '"$(hostname -f)"'
# FIXME: fqn seems badly broken
# c4.t.b "s" 0 "fqn_hostname" \
# "C4.fqn == \`hostname -f\`" test \