2015-05-05 16:16:27 +00:00
|
|
|
#!/usr/bin/env python
|
2015-03-26 18:21:55 +00:00
|
|
|
import sys
|
|
|
|
import subprocess
|
2015-05-05 22:23:27 +00:00
|
|
|
import re
|
2015-11-08 08:48:33 +00:00
|
|
|
import difflib
|
2015-03-26 18:21:55 +00:00
|
|
|
|
|
|
|
failed = False
|
|
|
|
|
2015-05-05 22:23:27 +00:00
|
|
|
_ansi = re.compile(r'\x1b[^m]*m')
|
|
|
|
|
2015-11-03 05:51:43 +00:00
|
|
|
_help = '''claptests v1.4.8
|
2015-04-08 22:01:06 +00:00
|
|
|
Kevin K. <kbknapp@gmail.com>
|
|
|
|
tests clap library
|
|
|
|
|
|
|
|
USAGE:
|
2015-07-16 04:27:38 +00:00
|
|
|
\tclaptests [FLAGS] [OPTIONS] [ARGS] [SUBCOMMAND]
|
2015-04-08 22:01:06 +00:00
|
|
|
|
|
|
|
FLAGS:
|
|
|
|
-f, --flag tests flags
|
|
|
|
-F tests flags with exclusions
|
|
|
|
-h, --help Prints help information
|
2015-06-16 22:06:29 +00:00
|
|
|
-V, --version Prints version information
|
2015-04-08 22:01:06 +00:00
|
|
|
|
|
|
|
OPTIONS:
|
2015-05-05 22:23:27 +00:00
|
|
|
--maxvals3 <maxvals>... Tests 3 max vals
|
|
|
|
--minvals2 <minvals>... Tests 2 min vals
|
|
|
|
--multvals <one> <two> Tests mutliple values, not mult occs
|
|
|
|
--multvalsmo <one> <two> Tests mutliple values, not mult occs
|
|
|
|
-o, --option <opt>... tests options
|
|
|
|
--long-option-2 <option2> tests long options with exclusions
|
2015-07-16 04:27:38 +00:00
|
|
|
-O, --Option <option3> tests options with specific value sets [values: fast slow]
|
2015-04-08 22:01:06 +00:00
|
|
|
|
2015-07-09 13:51:51 +00:00
|
|
|
ARGS:
|
2015-07-16 04:27:38 +00:00
|
|
|
positional tests positionals
|
|
|
|
positional2 tests positionals with exclusions
|
2015-08-01 04:49:08 +00:00
|
|
|
positional3... tests positionals with specific values
|
2015-04-08 22:01:06 +00:00
|
|
|
|
|
|
|
SUBCOMMANDS:
|
|
|
|
help Prints this message
|
|
|
|
subcmd tests subcommands'''
|
|
|
|
|
2015-11-03 05:51:43 +00:00
|
|
|
_version = "claptests v1.4.8"
|
|
|
|
|
2015-05-22 22:17:57 +00:00
|
|
|
_sc_dym_usage = '''error: The subcommand 'subcm' isn't valid
|
2015-11-03 05:51:43 +00:00
|
|
|
\tDid you mean 'subcmd' ?
|
2015-05-05 22:23:27 +00:00
|
|
|
|
|
|
|
If you received this message in error, try re-running with 'claptests -- subcm'
|
|
|
|
|
2015-05-05 09:47:39 +00:00
|
|
|
USAGE:
|
2015-11-03 05:51:43 +00:00
|
|
|
\tclaptests [FLAGS] [OPTIONS] [ARGS] [SUBCOMMAND]
|
2015-05-06 12:40:46 +00:00
|
|
|
|
2015-05-05 09:47:39 +00:00
|
|
|
For more information try --help'''
|
|
|
|
|
2015-05-22 22:17:57 +00:00
|
|
|
_arg_dym_usage = '''error: The argument '--optio' isn't valid
|
2015-11-03 05:51:43 +00:00
|
|
|
\tDid you mean --option ?
|
2015-05-06 12:40:46 +00:00
|
|
|
|
2015-05-05 12:27:40 +00:00
|
|
|
USAGE:
|
2015-11-03 05:51:43 +00:00
|
|
|
\tclaptests --option <opt>...
|
2015-05-06 12:40:46 +00:00
|
|
|
|
2015-05-05 12:27:40 +00:00
|
|
|
For more information try --help'''
|
|
|
|
|
2015-05-22 22:17:57 +00:00
|
|
|
_pv_dym_usage = '''error: 'slo' isn't a valid value for '--Option <option3>'
|
2015-11-03 05:51:43 +00:00
|
|
|
\t[valid values: fast slow]
|
2015-05-22 22:17:57 +00:00
|
|
|
|
2015-11-03 05:51:43 +00:00
|
|
|
Did you mean 'slow' ?
|
2015-05-06 12:40:46 +00:00
|
|
|
|
2015-05-05 15:23:51 +00:00
|
|
|
USAGE:
|
2015-11-03 05:51:43 +00:00
|
|
|
\tclaptests --Option <option3>
|
2015-05-06 12:40:46 +00:00
|
|
|
|
2015-05-05 15:23:51 +00:00
|
|
|
For more information try --help'''
|
|
|
|
|
2015-05-22 22:17:57 +00:00
|
|
|
_excluded = '''error: The argument '--flag' cannot be used with '-F'
|
2015-05-06 12:40:46 +00:00
|
|
|
|
2015-04-30 23:29:27 +00:00
|
|
|
USAGE:
|
2015-05-03 04:33:05 +00:00
|
|
|
\tclaptests [positional2] -F --long-option-2 <option2>
|
2015-05-06 12:40:46 +00:00
|
|
|
|
2015-04-27 04:49:31 +00:00
|
|
|
For more information try --help'''
|
|
|
|
|
2015-10-04 08:43:59 +00:00
|
|
|
_excluded_l = '''error: The argument '-f' cannot be used with '-F'
|
2015-05-06 12:40:46 +00:00
|
|
|
|
2015-04-30 23:29:27 +00:00
|
|
|
USAGE:
|
2015-11-03 05:51:43 +00:00
|
|
|
\tclaptests [positional2] -F --long-option-2 <option2>
|
2015-05-06 12:40:46 +00:00
|
|
|
|
2015-04-08 22:01:06 +00:00
|
|
|
For more information try --help'''
|
|
|
|
|
2015-05-22 22:17:57 +00:00
|
|
|
_required = '''error: The following required arguments were not supplied:
|
2015-10-13 18:30:35 +00:00
|
|
|
\t[positional2]
|
|
|
|
\t--long-option-2 <option2>
|
2015-05-03 04:33:05 +00:00
|
|
|
|
2015-04-30 23:29:27 +00:00
|
|
|
USAGE:
|
2015-05-03 04:33:05 +00:00
|
|
|
\tclaptests [positional2] -F --long-option-2 <option2>
|
2015-05-06 12:40:46 +00:00
|
|
|
|
2015-04-08 22:01:06 +00:00
|
|
|
For more information try --help'''
|
|
|
|
|
|
|
|
_fop = '''flag present 1 times
|
|
|
|
option present 1 times with value: some
|
|
|
|
An option: some
|
|
|
|
positional present with value: value
|
|
|
|
flag2 NOT present
|
|
|
|
option2 maybe present with value of: Nothing
|
|
|
|
positional2 maybe present with value of: Nothing
|
|
|
|
option3 NOT present
|
|
|
|
positional3 NOT present
|
|
|
|
option present 1 times with value: some
|
|
|
|
An option: some
|
|
|
|
positional present with value: value
|
|
|
|
subcmd NOT present'''
|
|
|
|
|
|
|
|
_f2op = '''flag present 2 times
|
|
|
|
option present 1 times with value: some
|
|
|
|
An option: some
|
|
|
|
positional present with value: value
|
|
|
|
flag2 NOT present
|
|
|
|
option2 maybe present with value of: Nothing
|
|
|
|
positional2 maybe present with value of: Nothing
|
|
|
|
option3 NOT present
|
|
|
|
positional3 NOT present
|
|
|
|
option present 1 times with value: some
|
|
|
|
An option: some
|
|
|
|
positional present with value: value
|
|
|
|
subcmd NOT present'''
|
|
|
|
|
|
|
|
_o2p = '''flag NOT present
|
|
|
|
option present 2 times with value: some
|
|
|
|
An option: some
|
|
|
|
An option: other
|
|
|
|
positional present with value: value
|
|
|
|
flag2 NOT present
|
|
|
|
option2 maybe present with value of: Nothing
|
|
|
|
positional2 maybe present with value of: Nothing
|
|
|
|
option3 NOT present
|
|
|
|
positional3 NOT present
|
|
|
|
option present 2 times with value: some
|
|
|
|
An option: some
|
|
|
|
An option: other
|
|
|
|
positional present with value: value
|
|
|
|
subcmd NOT present'''
|
|
|
|
|
|
|
|
_schelp = '''claptests-subcmd 0.1
|
|
|
|
Kevin K. <kbknapp@gmail.com>
|
|
|
|
tests subcommands
|
|
|
|
|
|
|
|
USAGE:
|
2015-07-20 01:50:07 +00:00
|
|
|
\tclaptests subcmd [FLAGS] [OPTIONS] [--] [ARGS]
|
2015-04-08 22:01:06 +00:00
|
|
|
|
|
|
|
FLAGS:
|
|
|
|
-f, --flag tests flags
|
2015-05-22 17:10:34 +00:00
|
|
|
-h, --help Prints help information
|
2015-06-16 22:06:29 +00:00
|
|
|
-V, --version Prints version information
|
2015-04-08 22:01:06 +00:00
|
|
|
|
|
|
|
OPTIONS:
|
2015-07-16 04:27:38 +00:00
|
|
|
-o, --option <scoption>... tests options
|
2015-04-08 22:01:06 +00:00
|
|
|
|
2015-07-09 13:51:51 +00:00
|
|
|
ARGS:
|
2015-07-16 04:27:38 +00:00
|
|
|
scpositional tests positionals'''
|
2015-04-08 22:01:06 +00:00
|
|
|
|
|
|
|
_scfop = '''flag NOT present
|
|
|
|
option NOT present
|
|
|
|
positional NOT present
|
|
|
|
flag2 NOT present
|
|
|
|
option2 maybe present with value of: Nothing
|
|
|
|
positional2 maybe present with value of: Nothing
|
|
|
|
option3 NOT present
|
|
|
|
positional3 NOT present
|
|
|
|
option NOT present
|
|
|
|
positional NOT present
|
|
|
|
subcmd present
|
2015-05-22 17:10:34 +00:00
|
|
|
flag present 1 times
|
2015-04-08 22:01:06 +00:00
|
|
|
scoption present with value: some
|
|
|
|
An scoption: some
|
|
|
|
scpositional present with value: value'''
|
|
|
|
|
|
|
|
_scf2op = '''flag NOT present
|
|
|
|
option NOT present
|
|
|
|
positional NOT present
|
|
|
|
flag2 NOT present
|
|
|
|
option2 maybe present with value of: Nothing
|
|
|
|
positional2 maybe present with value of: Nothing
|
|
|
|
option3 NOT present
|
|
|
|
positional3 NOT present
|
|
|
|
option NOT present
|
|
|
|
positional NOT present
|
|
|
|
subcmd present
|
2015-05-22 17:10:34 +00:00
|
|
|
flag present 2 times
|
2015-04-08 22:01:06 +00:00
|
|
|
scoption present with value: some
|
|
|
|
An scoption: some
|
|
|
|
scpositional present with value: value'''
|
|
|
|
|
2015-03-26 18:21:55 +00:00
|
|
|
_bin = './target/release/claptests'
|
2015-04-08 22:01:06 +00:00
|
|
|
|
2015-11-03 05:51:43 +00:00
|
|
|
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],
|
|
|
|
'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]
|
2015-04-08 22:01:06 +00:00
|
|
|
}
|
2015-03-26 18:21:55 +00:00
|
|
|
|
2015-11-03 05:51:43 +00:00
|
|
|
def pass_fail(name, cmd, check, good):
|
2015-05-05 16:16:27 +00:00
|
|
|
sys.stdout.write(name)
|
2015-04-08 22:01:06 +00:00
|
|
|
if check == good:
|
|
|
|
print('Pass')
|
2015-11-03 05:51:43 +00:00
|
|
|
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))
|
2015-11-08 08:48:33 +00:00
|
|
|
for line in difflib.context_diff(good.splitlines(), check.splitlines(), fromfile="Should Be", tofile="Currently Is", lineterm=""):
|
|
|
|
print(line)
|
|
|
|
print()
|
2015-11-03 05:51:43 +00:00
|
|
|
return 1
|
2015-03-26 18:21:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2015-11-03 05:51:43 +00:00
|
|
|
num_failed = 0
|
|
|
|
total = len(cmds)
|
2015-03-26 18:21:55 +00:00
|
|
|
for cmd, cmd_v in cmds.items():
|
2015-07-09 23:35:28 +00:00
|
|
|
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())
|
2015-11-03 05:51:43 +00:00
|
|
|
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])
|
|
|
|
|
|
|
|
print()
|
|
|
|
if num_failed:
|
|
|
|
print('{}/{} tests failed'.format(num_failed, total))
|
2015-03-26 18:21:55 +00:00
|
|
|
return 1
|
2015-11-03 05:51:43 +00:00
|
|
|
print('{}/{} tests passed!'.format(total, total))
|
2015-03-26 18:21:55 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2015-04-02 22:22:58 +00:00
|
|
|
sys.exit(main())
|