mirror of
https://github.com/clap-rs/clap
synced 2024-12-14 14:52:33 +00:00
tests(Exit Codes): adds some formatting and exit code testing
This commit is contained in:
parent
c070bc7c69
commit
a4c6272e38
2 changed files with 78 additions and 71 deletions
|
@ -7,7 +7,7 @@ failed = False
|
|||
|
||||
_ansi = re.compile(r'\x1b[^m]*m')
|
||||
|
||||
_help = '''claptests 0.0.1
|
||||
_help = '''claptests v1.4.8
|
||||
Kevin K. <kbknapp@gmail.com>
|
||||
tests clap library
|
||||
|
||||
|
@ -38,31 +38,33 @@ SUBCOMMANDS:
|
|||
help Prints this message
|
||||
subcmd tests subcommands'''
|
||||
|
||||
_version = "claptests v1.4.8"
|
||||
|
||||
_sc_dym_usage = '''error: The subcommand 'subcm' isn't valid
|
||||
Did you mean 'subcmd' ?
|
||||
\tDid you mean 'subcmd' ?
|
||||
|
||||
If you received this message in error, try re-running with 'claptests -- subcm'
|
||||
|
||||
USAGE:
|
||||
claptests [FLAGS] [OPTIONS] [ARGS] [SUBCOMMAND]
|
||||
\tclaptests [FLAGS] [OPTIONS] [ARGS] [SUBCOMMAND]
|
||||
|
||||
For more information try --help'''
|
||||
|
||||
_arg_dym_usage = '''error: The argument '--optio' isn't valid
|
||||
Did you mean --option ?
|
||||
\tDid you mean --option ?
|
||||
|
||||
USAGE:
|
||||
claptests --option <opt>...
|
||||
\tclaptests --option <opt>...
|
||||
|
||||
For more information try --help'''
|
||||
|
||||
_pv_dym_usage = '''error: 'slo' isn't a valid value for '--Option <option3>'
|
||||
[valid values: fast slow]
|
||||
\t[valid values: fast slow]
|
||||
|
||||
Did you mean 'slow' ?
|
||||
Did you mean 'slow' ?
|
||||
|
||||
USAGE:
|
||||
claptests --Option <option3>
|
||||
\tclaptests --Option <option3>
|
||||
|
||||
For more information try --help'''
|
||||
|
||||
|
@ -76,7 +78,7 @@ For more information try --help'''
|
|||
_excluded_l = '''error: The argument '-f' cannot be used with '-F'
|
||||
|
||||
USAGE:
|
||||
claptests [positional2] -F --long-option-2 <option2>
|
||||
\tclaptests [positional2] -F --long-option-2 <option2>
|
||||
|
||||
For more information try --help'''
|
||||
|
||||
|
@ -231,86 +233,92 @@ For more information try --help'''
|
|||
_mult_vals_2m1 = '''error: The argument '--multvalsmo <one> <two>' requires 2 values, but 1 was provided
|
||||
|
||||
USAGE:
|
||||
claptests --multvalsmo <one> <two>
|
||||
\tclaptests --multvalsmo <one> <two>
|
||||
|
||||
For more information try --help'''
|
||||
|
||||
_bin = './target/release/claptests'
|
||||
|
||||
cmds = {#'help short: ': ['{} -h'.format(_bin), _help],
|
||||
#'help long: ': ['{} --help'.format(_bin), _help],
|
||||
'help subcmd: ': ['{} help'.format(_bin), _help],
|
||||
#'excluded first: ': ['{} -f -F'.format(_bin), _excluded],
|
||||
#'excluded last: ': ['{} -F -f'.format(_bin), _excluded_l],
|
||||
'missing required: ': ['{} -F'.format(_bin), _required],
|
||||
'max_vals too many: ': ['{} --maxvals3 some other value too'.format(_bin), _max_vals_more],
|
||||
'max_vals exact: ': ['{} --maxvals3 some other value'.format(_bin), _exact],
|
||||
'max_vals less: ': ['{} --maxvals3 some other'.format(_bin), _exact],
|
||||
'min_vals more: ': ['{} --minvals2 some other value too'.format(_bin), _exact],
|
||||
'min_vals exact: ': ['{} --minvals2 some value'.format(_bin), _exact],
|
||||
'min_vals too few: ': ['{} --minvals2 some'.format(_bin), _min_vals_few],
|
||||
'mult_vals too many: ': ['{} --multvals some other --multvals some other'.format(_bin), _mult_vals_more],
|
||||
'mult_vals too few: ': ['{} --multvals some'.format(_bin), _mult_vals_few],
|
||||
'mult_vals exact: ': ['{} --multvals some other'.format(_bin), _exact],
|
||||
'mult_valsmo x2: ': ['{} --multvalsmo some other --multvalsmo some other'.format(_bin), _exact],
|
||||
'mult_valsmo x2-1: ': ['{} --multvalsmo some other --multvalsmo some'.format(_bin), _mult_vals_2m1],
|
||||
'mult_valsmo x1: ': ['{} --multvalsmo some other'.format(_bin), _exact],
|
||||
'F2(ss),O(s),P: ': ['{} value -f -f -o some'.format(_bin), _f2op],
|
||||
'arg dym: ': ['{} --optio=foo'.format(_bin), _arg_dym_usage],
|
||||
#'pv dym: ': ['{} --Option slo'.format(_bin), _pv_dym_usage],
|
||||
#'pv dym(=): ': ['{} --Option=slo'.format(_bin), _pv_dym_usage],
|
||||
'O2(ll)P: ': ['{} value --option some --option other'.format(_bin), _o2p],
|
||||
'O2(l=l=)P: ': ['{} value --option=some --option=other'.format(_bin), _o2p],
|
||||
'O2(ss)P: ': ['{} value -o some -o other'.format(_bin), _o2p],
|
||||
'F2(s2),O(s),P: ': ['{} value -ff -o some'.format(_bin), _f2op],
|
||||
'F(s),O(s),P: ': ['{} value -f -o some'.format(_bin), _fop],
|
||||
'F(l),O(l),P: ': ['{} value --flag --option some'.format(_bin), _fop],
|
||||
'F(l),O(l=),P: ': ['{} value --flag --option=some'.format(_bin), _fop],
|
||||
'sc dym: ': ['{} subcm'.format(_bin), _sc_dym_usage],
|
||||
'sc help short: ': ['{} subcmd -h'.format(_bin), _schelp],
|
||||
'sc help long: ': ['{} subcmd --help'.format(_bin), _schelp],
|
||||
'scF(l),O(l),P: ': ['{} subcmd value --flag --option some'.format(_bin), _scfop],
|
||||
'scF(l),O(s),P: ': ['{} subcmd value --flag -o some'.format(_bin), _scfop],
|
||||
'scF(l),O(l=),P: ': ['{} subcmd value --flag --option=some'.format(_bin), _scfop],
|
||||
'scF(s),O(l),P: ': ['{} subcmd value -f --option some'.format(_bin), _scfop],
|
||||
'scF(s),O(s),P: ': ['{} subcmd value -f -o some'.format(_bin), _scfop],
|
||||
'scF(s),O(l=),P: ': ['{} subcmd value -f --option=some'.format(_bin), _scfop],
|
||||
'scF2(s),O(l),P: ': ['{} subcmd value -ff --option some'.format(_bin), _scf2op],
|
||||
'scF2(s),O(s),P: ': ['{} subcmd value -ff -o some'.format(_bin), _scf2op],
|
||||
'scF2(s),O(l=),P: ': ['{} subcmd value -ff --option=some'.format(_bin), _scf2op],
|
||||
'scF2(l2),O(l),P: ': ['{} subcmd value --flag --flag --option some'.format(_bin), _scf2op],
|
||||
'scF2(l2),O(s),P: ': ['{} subcmd value --flag --flag -o some'.format(_bin), _scf2op],
|
||||
'scF2(l2),O(l=),P: ': ['{} subcmd value --flag --flag --option=some'.format(_bin), _scf2op],
|
||||
'scF2(s2),O(l),P: ': ['{} subcmd value -f -f --option some'.format(_bin), _scf2op],
|
||||
'scF2(s2),O(s),P: ': ['{} subcmd value -f -f -o some'.format(_bin), _scf2op],
|
||||
'scF2(s2),O(l=),P: ': ['{} subcmd value -f -f --option=some'.format(_bin), _scf2op]
|
||||
cmds = {'help short: ': ['{} -h'.format(_bin), _help, 0],
|
||||
'help long: ': ['{} --help'.format(_bin), _help, 0],
|
||||
'version short: ': ['{} -V'.format(_bin), _version, 0],
|
||||
'version long: ': ['{} --version'.format(_bin), _version, 0],
|
||||
'help subcmd: ': ['{} help'.format(_bin), _help, 0],
|
||||
'missing required: ': ['{} -F'.format(_bin), _required, 1],
|
||||
'max_vals too many: ': ['{} --maxvals3 some other value too'.format(_bin), _max_vals_more, 0],
|
||||
'max_vals exact: ': ['{} --maxvals3 some other value'.format(_bin), _exact, 0],
|
||||
'max_vals less: ': ['{} --maxvals3 some other'.format(_bin), _exact, 0],
|
||||
'min_vals more: ': ['{} --minvals2 some other value too'.format(_bin), _exact, 0],
|
||||
'min_vals exact: ': ['{} --minvals2 some value'.format(_bin), _exact, 0],
|
||||
'min_vals too few: ': ['{} --minvals2 some'.format(_bin), _min_vals_few, 1],
|
||||
'mult_vals too many: ': ['{} --multvals some other --multvals some other'.format(_bin), _mult_vals_more, 1],
|
||||
'mult_vals too few: ': ['{} --multvals some'.format(_bin), _mult_vals_few, 1],
|
||||
'mult_vals exact: ': ['{} --multvals some other'.format(_bin), _exact, 0],
|
||||
'mult_valsmo x2: ': ['{} --multvalsmo some other --multvalsmo some other'.format(_bin), _exact, 0],
|
||||
'mult_valsmo x2-1: ': ['{} --multvalsmo some other --multvalsmo some'.format(_bin), _mult_vals_2m1, 1],
|
||||
'mult_valsmo x1: ': ['{} --multvalsmo some other'.format(_bin), _exact, 0],
|
||||
'F2(ss),O(s),P: ': ['{} value -f -f -o some'.format(_bin), _f2op, 0],
|
||||
'arg dym: ': ['{} --optio=foo'.format(_bin), _arg_dym_usage, 1],
|
||||
'O2(ll)P: ': ['{} value --option some --option other'.format(_bin), _o2p, 0],
|
||||
'O2(l=l=)P: ': ['{} value --option=some --option=other'.format(_bin), _o2p, 0],
|
||||
'O2(ss)P: ': ['{} value -o some -o other'.format(_bin), _o2p, 0],
|
||||
'F2(s2),O(s),P: ': ['{} value -ff -o some'.format(_bin), _f2op, 0],
|
||||
'F(s),O(s),P: ': ['{} value -f -o some'.format(_bin), _fop, 0],
|
||||
'F(l),O(l),P: ': ['{} value --flag --option some'.format(_bin), _fop, 0],
|
||||
'F(l),O(l=),P: ': ['{} value --flag --option=some'.format(_bin), _fop, 0],
|
||||
'sc dym: ': ['{} subcm'.format(_bin), _sc_dym_usage, 1],
|
||||
'sc help short: ': ['{} subcmd -h'.format(_bin), _schelp, 0],
|
||||
'sc help long: ': ['{} subcmd --help'.format(_bin), _schelp, 0],
|
||||
'scF(l),O(l),P: ': ['{} subcmd value --flag --option some'.format(_bin), _scfop, 0],
|
||||
'scF(l),O(s),P: ': ['{} subcmd value --flag -o some'.format(_bin), _scfop, 0],
|
||||
'scF(l),O(l=),P: ': ['{} subcmd value --flag --option=some'.format(_bin), _scfop, 0],
|
||||
'scF(s),O(l),P: ': ['{} subcmd value -f --option some'.format(_bin), _scfop, 0],
|
||||
'scF(s),O(s),P: ': ['{} subcmd value -f -o some'.format(_bin), _scfop, 0],
|
||||
'scF(s),O(l=),P: ': ['{} subcmd value -f --option=some'.format(_bin), _scfop, 0],
|
||||
'scF2(s),O(l),P: ': ['{} subcmd value -ff --option some'.format(_bin), _scf2op, 0],
|
||||
'scF2(s),O(s),P: ': ['{} subcmd value -ff -o some'.format(_bin), _scf2op, 0],
|
||||
'scF2(s),O(l=),P: ': ['{} subcmd value -ff --option=some'.format(_bin), _scf2op, 0],
|
||||
'scF2(l2),O(l),P: ': ['{} subcmd value --flag --flag --option some'.format(_bin), _scf2op, 0],
|
||||
'scF2(l2),O(s),P: ': ['{} subcmd value --flag --flag -o some'.format(_bin), _scf2op, 0],
|
||||
'scF2(l2),O(l=),P: ': ['{} subcmd value --flag --flag --option=some'.format(_bin), _scf2op, 0],
|
||||
'scF2(s2),O(l),P: ': ['{} subcmd value -f -f --option some'.format(_bin), _scf2op, 0],
|
||||
'scF2(s2),O(s),P: ': ['{} subcmd value -f -f -o some'.format(_bin), _scf2op, 0],
|
||||
'scF2(s2),O(l=),P: ': ['{} subcmd value -f -f --option=some'.format(_bin), _scf2op, 0]
|
||||
}
|
||||
|
||||
def pass_fail(name, check, good):
|
||||
global failed
|
||||
def pass_fail(name, cmd, check, good):
|
||||
sys.stdout.write(name)
|
||||
if check == good:
|
||||
print('Pass')
|
||||
return
|
||||
failed = True
|
||||
print('Fail\n\tShould be: \n{}\n\tBut is: \n{}'.format(good, check))
|
||||
return 0
|
||||
print('Fail')
|
||||
print('\n\n{}\n# Should be:\n$ {}\n{}\n\n{}\n# But is:\n$ {}\n{}\n\n'.format('#'*25, cmd, good, '#'*25, cmd, check))
|
||||
return 1
|
||||
|
||||
|
||||
def main():
|
||||
num_failed = 0
|
||||
total = len(cmds)
|
||||
for cmd, cmd_v in cmds.items():
|
||||
proc = subprocess.Popen(cmd_v[0], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
|
||||
out, err = proc.communicate()
|
||||
out = _ansi.sub('', out.strip())
|
||||
err = _ansi.sub('', err.strip())
|
||||
if out:
|
||||
pass_fail(cmd, out, cmd_v[1])
|
||||
else:
|
||||
pass_fail(cmd, err, cmd_v[1])
|
||||
rc = proc.returncode
|
||||
if rc != cmd_v[-1]:
|
||||
print('{}Fail (Exit Code={}; Should be={})'.format(cmd, rc, cmd_v[-1]))
|
||||
num_failed += 1
|
||||
continue
|
||||
if out and rc == cmd_v[-1]:
|
||||
num_failed += pass_fail(cmd, cmd_v[0], out, cmd_v[1])
|
||||
elif rc == cmd_v[-1]:
|
||||
num_failed += pass_fail(cmd, cmd_v[0], err, cmd_v[1])
|
||||
|
||||
if failed:
|
||||
print('One or more tests failed')
|
||||
print()
|
||||
if num_failed:
|
||||
print('{}/{} tests failed'.format(num_failed, total))
|
||||
return 1
|
||||
print('All tests passed!')
|
||||
print('{}/{} tests passed!'.format(total, total))
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
|
|
|
@ -11,8 +11,7 @@ fn main() {
|
|||
let opt3_vals = ["fast", "slow"];
|
||||
let pos3_vals = ["vi", "emacs"];
|
||||
let matches = App::new("claptests")
|
||||
// Test version from Cargo.toml
|
||||
.version(&crate_version!()[..])
|
||||
.version("v1.4.8")
|
||||
.about("tests clap library")
|
||||
.author("Kevin K. <kbknapp@gmail.com>")
|
||||
.args_from_usage(args)
|
||||
|
|
Loading…
Reference in a new issue