]>
wolfpit.net Git - tool/Arch-pacman/.git/blob - test/scripts/parseopts_test.sh
3 declare -i testcount
=0 pass
=0 fail
=0 total
=25
5 # source the library function
6 lib
=${1:-${PMTEST_SCRIPTLIB_DIR}parseopts.sh}
7 if [[ -z $lib || ! -f $lib ]]; then
8 printf "Bail out! parseopts library ($lib) could not be located\n"
13 if ! type -t parseopts
>/dev
/null
; then
14 printf 'Bail out! parseopts function not found\n'
18 # borrow opts from makepkg
19 OPT_SHORT
="AcdefFghiLmop:rRsV"
20 OPT_LONG
=('allsource' 'asroot' 'ignorearch' 'check' 'clean:' 'cleanall' 'nodeps'
21 'noextract' 'force' 'forcever:' 'geninteg' 'help' 'holdver'
22 'install' 'key:' 'log' 'nocolor' 'nobuild' 'nocheck' 'noprepare' 'nosign' 'pkg:' 'rmdeps'
23 'repackage' 'skipinteg' 'sign' 'source' 'syncdeps' 'version' 'config:'
24 'noconfirm' 'noprogressbar')
27 local result
=$1 tokencount
=$2; shift 2
30 parseopts
"$OPT_SHORT" "${OPT_LONG[@]}" -- "$@" 2>/dev
/null
31 test_result
"$result" "$tokencount" "$*" "${OPTRET[@]}"
36 local result
=$1 tokencount
=$2 input
=$3; shift 3
38 if [[ $result = "$*" ]] && (( tokencount
== $# )); then
40 printf 'ok %d - %s\n' "$testcount" "$input"
42 printf 'not ok %d - %s\n' "$testcount" "$input"
43 printf '# [TEST %3s]: FAIL\n' "$testcount"
44 printf '# input: %s\n' "$input"
45 printf '# output: %s (%s tokens)\n' "$*" "$#"
46 printf '# expected: %s (%s tokens)\n' "$result" "$tokencount"
53 printf '# All %s tests successful\n\n' "$testcount"
56 printf '# %s of %s tests failed\n\n' "$fail" "$testcount"
62 printf '# Beginning parseopts tests\n'
66 # usage: parse <expected result> <token count> test-params...
67 # a failed parse will match only the end of options marker '--'
73 parse
'-s -r --' 3 -s -r
75 # short options, no spaces
76 parse
'-s -r --' 3 -sr
78 # short opt missing an opt arg
81 # short opt with an opt arg
82 parse
'-p PKGBUILD -L --' 4 -p PKGBUILD
-L
84 # short opt with an opt arg, no space
85 parse
'-p PKGBUILD --' 3 -pPKGBUILD
87 # valid shortopts as a long opt
90 # long opt with no optarg
91 parse
'--log --' 2 --log
93 # long opt with missing optarg
94 parse
'--' 1 -sr --pkg
96 # long opt with optarg
97 parse
'--pkg foo --' 3 --pkg foo
99 # long opt with optarg with whitespace
100 parse
'--pkg foo bar -- baz' 4 --pkg "foo bar" baz
102 # long opt with optarg with =
103 parse
'--pkg foo=bar -- baz' 4 --pkg foo
=bar baz
105 # long opt with explicit optarg
106 parse
'--pkg bar -- foo baz' 5 foo
--pkg=bar baz
108 # long opt with explicit optarg, with whitespace
109 parse
'--pkg foo bar -- baz' 4 baz
--pkg="foo bar"
111 # long opt with explicit optarg that doesn't take optarg
112 parse
'--' 1 --force=always
-s
114 # long opt with explicit optarg with =
115 parse
'--pkg foo=bar --' 3 --pkg=foo
=bar
117 # explicit end of options with options after
118 parse
'-s -r -- foo bar baz' 6 -s -r -- foo bar baz
120 # non-option parameters mixed in with options
121 parse
'-s -r -- foo baz' 5 -s foo baz
-r
123 # optarg with whitespace
124 parse
'-p foo bar -s --' 4 -p'foo bar' -s
126 # non-option parameter with whitespace
127 parse
'-i -- foo bar' 3 -i 'foo bar'
129 # successful stem match (opt has no arg)
130 parse
'--nocolor --' 2 --nocol
132 # successful stem match (opt has arg)
133 parse
'--config foo --' 3 --conf foo
138 # exact match on a possible stem (--force & --forcever)
139 parse
'--force --' 2 --force
141 # exact match on possible stem (opt has optarg)
142 parse
'--clean foo --' 3 --clean=foo