tiramisu-cmdline-parser/tests/test_readme.py

902 lines
30 KiB
Python
Raw Permalink Normal View History

2018-12-01 09:53:31 +01:00
from io import StringIO
from contextlib import redirect_stdout, redirect_stderr
2019-04-17 19:16:43 +02:00
import pytest
2018-12-01 09:53:31 +01:00
from tiramisu_cmdline_parser import TiramisuCmdlineParser
from tiramisu import IntOption, StrOption, BoolOption, ChoiceOption, \
2024-07-30 11:29:10 +02:00
SymLinkOption, OptionDescription, Config, calc_value, \
Calculation, ParamValue, ParamOption, Params
2019-08-23 16:22:34 +02:00
try:
from tiramisu_api import Config as JsonConfig
2024-07-30 11:29:10 +02:00
#params = ['tiramisu', 'tiramisu-json']
params = ['tiramisu']
2019-08-23 16:22:34 +02:00
except:
params = ['tiramisu']
2024-07-30 11:29:10 +02:00
from .utils import TestHelpFormatter, to_dict
2018-12-01 09:53:31 +01:00
2019-04-17 19:16:43 +02:00
def get_config(json, has_tree=False, default_verbosity=False, add_long=False, add_store_false=False):
2018-12-01 09:53:31 +01:00
choiceoption = ChoiceOption('cmd',
'choice the sub argument',
2019-02-27 07:32:32 +01:00
('str', 'list', 'int', 'none'),
2018-12-01 09:53:31 +01:00
properties=('mandatory',
'positional'))
booloption = BoolOption('verbosity',
'increase output verbosity',
default=default_verbosity)
2018-12-01 09:53:31 +01:00
short_booloption = SymLinkOption('v', booloption)
str_ = StrOption('str',
'string option',
2024-07-30 11:29:10 +02:00
properties=('mandatory',
Calculation(calc_value,
Params(ParamValue('disabled'),
kwargs={'condition': ParamOption(choiceoption),
'reverse_condition': ParamValue(True),
'expected': ParamValue('str')})),
),
)
2018-12-01 09:53:31 +01:00
list_ = StrOption('list',
'list string option',
multi=True,
2024-07-30 11:29:10 +02:00
properties=('mandatory',
Calculation(calc_value,
Params(ParamValue('disabled'),
kwargs={'condition': ParamOption(choiceoption),
'reverse_condition': ParamValue(True),
'expected': ParamValue('list')})),
),
)
2018-12-01 09:53:31 +01:00
int_ = IntOption('int',
'int option',
2024-07-30 11:29:10 +02:00
properties=('mandatory',
Calculation(calc_value,
Params(ParamValue('disabled'),
kwargs={'condition': ParamOption(choiceoption),
'reverse_condition': ParamValue(True),
'expected': ParamValue('int')})),
),
)
root = OptionDescription('root',
'root',
[choiceoption,
booloption,
short_booloption,
str_,
list_,
int_
])
if has_tree:
root = OptionDescription('root',
'root',
[root])
config = Config(root)
2019-02-27 07:32:32 +01:00
config.property.read_write()
2019-04-17 19:16:43 +02:00
if add_store_false:
config.option('verbosity').property.add('storefalse')
if add_long:
config.option('verbosity').property.add('longargument')
if json == 'tiramisu':
return config
jconfig = JsonConfig(config.option.dict())
return jconfig
2018-12-01 09:53:31 +01:00
2019-08-23 16:22:34 +02:00
@pytest.fixture(params=params)
2019-04-17 19:16:43 +02:00
def json(request):
return request.param
def test_readme_help(json):
2019-04-02 21:02:08 +02:00
output = """usage: prog.py [-h] [-v] [-nv] {str,list,int,none}
2018-12-01 09:53:31 +01:00
positional arguments:
2019-02-27 07:32:32 +01:00
{str,list,int,none} choice the sub argument
2018-12-01 09:53:31 +01:00
2024-07-30 11:29:10 +02:00
options:
2019-02-27 07:32:32 +01:00
-h, --help show this help message and exit
2024-07-30 11:29:10 +02:00
-v, --verbosity increase output verbosity (default: False)
2019-04-02 21:02:08 +02:00
-nv, --no-verbosity
2018-12-01 09:53:31 +01:00
"""
2024-07-30 11:29:10 +02:00
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', formatter_class=TestHelpFormatter)
2018-12-01 09:53:31 +01:00
f = StringIO()
with redirect_stdout(f):
parser.print_help()
assert f.getvalue() == output
2019-04-17 19:16:43 +02:00
def test_readme_help_tree(json):
2019-04-02 21:02:08 +02:00
output = """usage: prog.py [-h] [-v] [-nv] {str,list,int,none}
2024-07-30 11:29:10 +02:00
options:
-h, --help show this help message and exit
root:
{str,list,int,none} choice the sub argument
2024-07-30 11:29:10 +02:00
-v, --root.verbosity increase output verbosity (default: False)
2019-04-02 21:02:08 +02:00
-nv, --root.no-verbosity
"""
2024-07-30 11:29:10 +02:00
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', formatter_class=TestHelpFormatter)
f = StringIO()
with redirect_stdout(f):
parser.print_help()
assert f.getvalue() == output
2019-04-17 19:16:43 +02:00
def test_readme_help_tree_flatten(json):
2019-04-02 21:02:08 +02:00
output = """usage: prog.py [-h] [-v] [-nv] {str,list,int,none}
2024-07-30 11:29:10 +02:00
options:
-h, --help show this help message and exit
root:
{str,list,int,none} choice the sub argument
2024-07-30 11:29:10 +02:00
-v, --verbosity increase output verbosity (default: False)
2019-04-02 21:02:08 +02:00
-nv, --no-verbosity
"""
2024-07-30 11:29:10 +02:00
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', fullpath=False, formatter_class=TestHelpFormatter)
f = StringIO()
with redirect_stdout(f):
parser.print_help()
assert f.getvalue() == output
2019-04-17 19:16:43 +02:00
def test_readme_help_modif_positional(json):
output = """usage: prog.py "str" [-h] [-v] [-nv] --str STR {str,list,int,none}
positional arguments:
{str,list,int,none} choice the sub argument
2019-04-02 08:49:36 +02:00
2024-07-30 11:29:10 +02:00
options:
2019-04-02 21:02:08 +02:00
-h, --help show this help message and exit
2024-07-30 11:29:10 +02:00
-v, --verbosity increase output verbosity (default: False)
2019-04-02 21:02:08 +02:00
-nv, --no-verbosity
--str STR string option
2019-04-02 08:49:36 +02:00
"""
2024-07-30 11:29:10 +02:00
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', formatter_class=TestHelpFormatter)
2019-04-02 08:49:36 +02:00
f = StringIO()
with redirect_stdout(f):
try:
parser.parse_args(['str', '--help'])
except SystemExit as err:
assert str(err) == "0"
else:
raise Exception('must raises')
assert f.getvalue() == output
2019-07-30 06:58:16 +02:00
def test_readme_help_modif_positional_remove(json):
output = """usage: prog.py "str" [-h] [-v] [-nv] --str STR
2024-07-30 11:29:10 +02:00
options:
2019-07-30 06:58:16 +02:00
-h, --help show this help message and exit
2024-07-30 11:29:10 +02:00
-v, --verbosity increase output verbosity (default: False)
2019-07-30 06:58:16 +02:00
-nv, --no-verbosity
--str STR string option
"""
2024-07-30 11:29:10 +02:00
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', display_modified_value=False, formatter_class=TestHelpFormatter)
2019-07-30 06:58:16 +02:00
f = StringIO()
with redirect_stdout(f):
try:
parser.parse_args(['str', '--help'])
except SystemExit as err:
assert str(err) == "0"
else:
raise Exception('must raises')
assert f.getvalue() == output
2019-04-17 19:16:43 +02:00
def test_readme_help_modif(json):
2024-07-30 11:29:10 +02:00
output = """usage: prog.py "str" --str "toto" [-h] [-v] [-nv] --str STR {str,list,int,none}
positional arguments:
{str,list,int,none} choice the sub argument
2019-04-02 19:01:17 +02:00
2024-07-30 11:29:10 +02:00
options:
2019-04-02 21:02:08 +02:00
-h, --help show this help message and exit
2024-07-30 11:29:10 +02:00
-v, --verbosity increase output verbosity (default: False)
2019-04-02 21:02:08 +02:00
-nv, --no-verbosity
--str STR string option
2019-04-02 19:01:17 +02:00
"""
2024-07-30 11:29:10 +02:00
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', formatter_class=TestHelpFormatter)
2019-04-02 19:01:17 +02:00
f = StringIO()
with redirect_stdout(f):
try:
parser.parse_args(['str', '--str', 'toto', '--help'])
except SystemExit as err:
assert str(err) == "0"
else:
raise Exception('must raises')
assert f.getvalue() == output
2019-07-30 06:58:16 +02:00
def test_readme_help_modif_remove(json):
output = """usage: prog.py "str" --str "toto" [-h] [-v] [-nv]
2024-07-30 11:29:10 +02:00
options:
2019-07-30 06:58:16 +02:00
-h, --help show this help message and exit
2024-07-30 11:29:10 +02:00
-v, --verbosity increase output verbosity (default: False)
2019-07-30 06:58:16 +02:00
-nv, --no-verbosity
"""
2024-07-30 11:29:10 +02:00
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', display_modified_value=False, formatter_class=TestHelpFormatter)
2019-07-30 06:58:16 +02:00
f = StringIO()
with redirect_stdout(f):
try:
parser.parse_args(['str', '--str', 'toto', '--help'])
except SystemExit as err:
assert str(err) == "0"
else:
raise Exception('must raises')
assert f.getvalue() == output
def test_readme_help_modif_short(json):
output = """usage: prog.py "str" -v [-h] [-v] [-nv] --str STR {str,list,int,none}
positional arguments:
{str,list,int,none} choice the sub argument
2019-04-02 19:01:17 +02:00
2024-07-30 11:29:10 +02:00
options:
-h, --help show this help message and exit
2024-07-30 11:29:10 +02:00
-v, --verbosity increase output verbosity (default: False)
-nv, --no-verbosity
--str STR string option
2019-04-02 19:01:17 +02:00
"""
2024-07-30 11:29:10 +02:00
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', formatter_class=TestHelpFormatter)
2019-04-02 19:01:17 +02:00
f = StringIO()
with redirect_stdout(f):
try:
parser.parse_args(['str', '-v', '--help'])
except SystemExit as err:
assert str(err) == "0"
else:
raise Exception('must raises')
assert f.getvalue() == output
2019-07-30 06:58:16 +02:00
def test_readme_help_modif_short_remove(json):
# FIXME -v -nv ?? pas de description
output = """usage: prog.py "str" -v [-h] [-nv] --str STR
2024-07-30 11:29:10 +02:00
options:
2019-07-30 06:58:16 +02:00
-h, --help show this help message and exit
2024-07-30 11:29:10 +02:00
-nv, --no-verbosity increase output verbosity (default: False)
2019-07-30 06:58:16 +02:00
--str STR string option
"""
2024-07-30 11:29:10 +02:00
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', display_modified_value=False, formatter_class=TestHelpFormatter)
2019-07-30 06:58:16 +02:00
f = StringIO()
with redirect_stdout(f):
try:
parser.parse_args(['str', '-v', '--help'])
except SystemExit as err:
assert str(err) == "0"
else:
raise Exception('must raises')
assert f.getvalue() == output
2019-07-30 08:48:46 +02:00
def test_readme_help_modif_short_no1(json):
output = """usage: prog.py "str" -v [-h] [-v] [-nv] --str STR {str,list,int,none}
positional arguments:
{str,list,int,none} choice the sub argument
2019-04-02 21:02:08 +02:00
2024-07-30 11:29:10 +02:00
options:
-h, --help show this help message and exit
2024-07-30 11:29:10 +02:00
-v, --verbosity increase output verbosity (default: False)
-nv, --no-verbosity
--str STR string option
2019-04-02 21:02:08 +02:00
"""
2024-07-30 11:29:10 +02:00
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', formatter_class=TestHelpFormatter)
2019-04-02 21:02:08 +02:00
f = StringIO()
with redirect_stdout(f):
try:
parser.parse_args(['str', '-nv', '--help'])
except SystemExit as err:
assert str(err) == "0"
else:
raise Exception('must raises')
assert f.getvalue() == output
2019-07-30 06:58:16 +02:00
def test_readme_help_modif_short_no_remove(json):
2019-07-30 08:48:46 +02:00
output = """usage: prog.py "str" -v [-h] [-v] --str STR
2019-07-30 06:58:16 +02:00
2024-07-30 11:29:10 +02:00
options:
2019-07-30 08:48:46 +02:00
-h, --help show this help message and exit
2024-07-30 11:29:10 +02:00
-v, --verbosity increase output verbosity (default: False)
2019-07-30 08:48:46 +02:00
--str STR string option
2019-07-30 06:58:16 +02:00
"""
2024-07-30 11:29:10 +02:00
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', display_modified_value=False, formatter_class=TestHelpFormatter)
2019-07-30 06:58:16 +02:00
f = StringIO()
with redirect_stdout(f):
try:
parser.parse_args(['str', '-nv', '--help'])
except SystemExit as err:
assert str(err) == "0"
else:
raise Exception('must raises')
assert f.getvalue() == output
2019-04-17 19:16:43 +02:00
def test_readme_positional_mandatory(json):
2019-04-02 21:02:08 +02:00
output = """usage: prog.py [-h] [-v] [-nv] {str,list,int,none}
2018-12-01 09:53:31 +01:00
prog.py: error: the following arguments are required: cmd
"""
2024-07-30 11:29:10 +02:00
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', formatter_class=TestHelpFormatter)
2018-12-01 09:53:31 +01:00
f = StringIO()
with redirect_stderr(f):
try:
parser.parse_args([])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output
2019-04-17 19:16:43 +02:00
def test_readme_positional_mandatory_tree(json):
2019-04-02 21:02:08 +02:00
output = """usage: prog.py [-h] [-v] [-nv] {str,list,int,none}
prog.py: error: the following arguments are required: root.cmd
"""
2024-07-30 11:29:10 +02:00
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', formatter_class=TestHelpFormatter)
f = StringIO()
with redirect_stderr(f):
try:
parser.parse_args([])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output
2019-04-17 19:16:43 +02:00
def test_readme_positional_mandatory_tree_flatten(json):
2019-04-02 21:02:08 +02:00
output = """usage: prog.py [-h] [-v] [-nv] {str,list,int,none}
prog.py: error: the following arguments are required: cmd
"""
2024-07-30 11:29:10 +02:00
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', fullpath=False, formatter_class=TestHelpFormatter)
f = StringIO()
with redirect_stderr(f):
try:
parser.parse_args([])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output
2019-04-17 19:16:43 +02:00
def test_readme_mandatory(json):
output = """usage: prog.py "str" [-h] [-v] [-nv] --str STR {str,list,int,none}
2018-12-01 09:53:31 +01:00
prog.py: error: the following arguments are required: --str
"""
2024-07-30 11:29:10 +02:00
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', formatter_class=TestHelpFormatter)
2018-12-01 09:53:31 +01:00
f = StringIO()
with redirect_stderr(f):
try:
parser.parse_args(['str'])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output
2019-07-30 06:58:16 +02:00
def test_readme_mandatory_remove(json):
output = """usage: prog.py "str" [-h] [-v] [-nv] --str STR
prog.py: error: the following arguments are required: --str
"""
2024-07-30 11:29:10 +02:00
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', display_modified_value=False, formatter_class=TestHelpFormatter)
2019-07-30 06:58:16 +02:00
f = StringIO()
with redirect_stderr(f):
try:
parser.parse_args(['str'])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output
2019-04-17 19:16:43 +02:00
def test_readme_mandatory_tree(json):
output = """usage: prog.py "str" [-h] [-v] [-nv] --root.str STR {str,list,int,none}
prog.py: error: the following arguments are required: --root.str
"""
2024-07-30 11:29:10 +02:00
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', formatter_class=TestHelpFormatter)
f = StringIO()
with redirect_stderr(f):
try:
parser.parse_args(['str'])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output
2019-07-30 06:58:16 +02:00
def test_readme_mandatory_tree_remove(json):
output = """usage: prog.py "str" [-h] [-v] [-nv] --root.str STR
prog.py: error: the following arguments are required: --root.str
"""
2024-07-30 11:29:10 +02:00
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', display_modified_value=False, formatter_class=TestHelpFormatter)
2019-07-30 06:58:16 +02:00
f = StringIO()
with redirect_stderr(f):
try:
parser.parse_args(['str'])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output
2019-04-17 19:16:43 +02:00
def test_readme_mandatory_tree_flatten(json):
output = """usage: prog.py "str" [-h] [-v] [-nv] --str STR {str,list,int,none}
prog.py: error: the following arguments are required: --str
"""
2024-07-30 11:29:10 +02:00
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', fullpath=False, formatter_class=TestHelpFormatter)
f = StringIO()
with redirect_stderr(f):
try:
parser.parse_args(['str'])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output
2019-07-30 06:58:16 +02:00
def test_readme_mandatory_tree_flatten_remove(json):
output = """usage: prog.py "str" [-h] [-v] [-nv] --str STR
prog.py: error: the following arguments are required: --str
"""
2024-07-30 11:29:10 +02:00
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', fullpath=False, display_modified_value=False, formatter_class=TestHelpFormatter)
2019-07-30 06:58:16 +02:00
f = StringIO()
with redirect_stderr(f):
try:
parser.parse_args(['str'])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output
2019-04-17 19:16:43 +02:00
def test_readme_cross(json):
output = """usage: prog.py "none" [-h] [-v] [-nv] {str,list,int,none}
2018-12-01 09:53:31 +01:00
prog.py: error: unrecognized arguments: --int
"""
2024-07-30 11:29:10 +02:00
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', formatter_class=TestHelpFormatter)
2018-12-01 09:53:31 +01:00
f = StringIO()
with redirect_stderr(f):
try:
2019-08-23 16:22:34 +02:00
parser.parse_args(['none', '--int', '1'])
2018-12-01 09:53:31 +01:00
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output
2019-07-30 06:58:16 +02:00
def test_readme_cross_remove(json):
output = """usage: prog.py "none" [-h] [-v] [-nv]
prog.py: error: unrecognized arguments: --int
"""
2024-07-30 11:29:10 +02:00
parser = TiramisuCmdlineParser(get_config(json), 'prog.py', display_modified_value=False, formatter_class=TestHelpFormatter)
2019-07-30 06:58:16 +02:00
f = StringIO()
with redirect_stderr(f):
try:
2019-08-23 16:22:34 +02:00
parser.parse_args(['none', '--int', '1'])
2019-07-30 06:58:16 +02:00
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output
2019-04-17 19:16:43 +02:00
def test_readme_cross_tree(json):
output = """usage: prog.py "none" [-h] [-v] [-nv] {str,list,int,none}
2019-08-22 15:58:34 +02:00
prog.py: error: unrecognized arguments: --root.int
"""
2024-07-30 11:29:10 +02:00
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', formatter_class=TestHelpFormatter)
f = StringIO()
with redirect_stderr(f):
try:
2019-08-23 16:22:34 +02:00
parser.parse_args(['none', '--root.int', '1'])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output
2019-07-30 06:58:16 +02:00
def test_readme_cross_tree_remove(json):
output = """usage: prog.py "none" [-h] [-v] [-nv]
2019-08-22 15:58:34 +02:00
prog.py: error: unrecognized arguments: --root.int
2019-07-30 06:58:16 +02:00
"""
2024-07-30 11:29:10 +02:00
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', display_modified_value=False, formatter_class=TestHelpFormatter)
2019-07-30 06:58:16 +02:00
f = StringIO()
with redirect_stderr(f):
try:
2019-08-23 16:22:34 +02:00
parser.parse_args(['none', '--root.int', '1'])
2019-07-30 06:58:16 +02:00
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output
2019-04-17 19:16:43 +02:00
def test_readme_cross_tree_flatten(json):
output = """usage: prog.py "none" [-h] [-v] [-nv] {str,list,int,none}
prog.py: error: unrecognized arguments: --int
"""
2024-07-30 11:29:10 +02:00
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', fullpath=False, formatter_class=TestHelpFormatter)
f = StringIO()
2019-07-30 06:58:16 +02:00
with redirect_stderr(f):
try:
2019-08-23 16:22:34 +02:00
parser.parse_args(['none', '--int', '1'])
2019-07-30 06:58:16 +02:00
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output
def test_readme_cross_tree_flatten_remove(json):
output = """usage: prog.py "none" [-h] [-v] [-nv]
prog.py: error: unrecognized arguments: --int
"""
2024-07-30 11:29:10 +02:00
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', fullpath=False, display_modified_value=False, formatter_class=TestHelpFormatter)
2019-07-30 06:58:16 +02:00
f = StringIO()
with redirect_stderr(f):
try:
2019-08-23 16:22:34 +02:00
parser.parse_args(['none', '--int', '1'])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output
2019-04-17 19:16:43 +02:00
def test_readme_unknown(json):
output = """usage: prog.py [-h] [-v] [-nv] {str,list,int,none}
prog.py: error: argument root.cmd: invalid choice: 'unknown' (choose from 'str', 'list', 'int', 'none')
"""
2024-07-30 11:29:10 +02:00
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', fullpath=False, formatter_class=TestHelpFormatter)
2019-04-17 19:16:43 +02:00
f = StringIO()
with redirect_stderr(f):
try:
parser.parse_args(['unknown'])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output
def test_readme_int(json):
2018-12-01 09:53:31 +01:00
output = {'cmd': 'int',
'int': 3,
'verbosity': False,
'v': False}
2019-04-17 19:16:43 +02:00
config = get_config(json)
2019-02-27 07:32:32 +01:00
parser = TiramisuCmdlineParser(config, 'prog.py')
2018-12-01 09:53:31 +01:00
parser.parse_args(['int', '--int', '3'])
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output
2019-04-17 19:16:43 +02:00
def test_readme_int_tree(json):
output = {'root.cmd': 'int',
'root.int': 3,
'root.verbosity': False,
'root.v': False}
2019-04-17 19:16:43 +02:00
config = get_config(json, True)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['int', '--root.int', '3'])
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output
2019-04-17 19:16:43 +02:00
def test_readme_int_tree_flatten(json):
output = {'root.cmd': 'int',
'root.int': 3,
'root.verbosity': False,
'root.v': False}
2019-04-17 19:16:43 +02:00
config = get_config(json, True)
parser = TiramisuCmdlineParser(config, 'prog.py', fullpath=False)
parser.parse_args(['int', '--int', '3'])
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output
2018-12-01 09:53:31 +01:00
2019-04-17 19:16:43 +02:00
def test_readme_int_verbosity(json):
2018-12-01 09:53:31 +01:00
output = {'cmd': 'int',
'int': 3,
'verbosity': True,
'v': True}
2019-04-17 19:16:43 +02:00
config = get_config(json)
2019-02-27 07:32:32 +01:00
parser = TiramisuCmdlineParser(config, 'prog.py')
2018-12-01 09:53:31 +01:00
parser.parse_args(['int', '--int', '3', '--verbosity'])
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output
2018-12-01 09:53:31 +01:00
2019-04-17 19:16:43 +02:00
def test_readme_int_verbosity_tree(json):
output = {'root.cmd': 'int',
'root.int': 3,
'root.verbosity': True,
'root.v': True}
2019-04-17 19:16:43 +02:00
config = get_config(json, True)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['int', '--root.int', '3', '--root.verbosity'])
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output
2019-04-17 19:16:43 +02:00
def test_readme_int_verbosity_tree_flatten(json):
output = {'root.cmd': 'int',
'root.int': 3,
'root.verbosity': True,
'root.v': True}
2019-04-17 19:16:43 +02:00
config = get_config(json, True)
parser = TiramisuCmdlineParser(config, 'prog.py', fullpath=False)
parser.parse_args(['int', '--int', '3', '--verbosity'])
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output
2019-04-17 19:16:43 +02:00
def test_readme_int_verbosity_short(json):
2018-12-01 09:53:31 +01:00
output = {'cmd': 'int',
'int': 3,
'verbosity': True,
'v': True}
2019-04-17 19:16:43 +02:00
config = get_config(json)
2019-02-27 07:32:32 +01:00
parser = TiramisuCmdlineParser(config, 'prog.py')
2018-12-01 09:53:31 +01:00
parser.parse_args(['int', '--int', '3', '-v'])
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output
2018-12-01 09:53:31 +01:00
2019-04-17 19:16:43 +02:00
def test_readme_int_verbosity_short_store_false(json):
output = {'cmd': 'int',
'int': 3,
'verbosity': None,
'v': True}
2019-04-17 19:16:43 +02:00
config = get_config(json, default_verbosity=None, add_store_false=True)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['int', '--int', '3', '-v'])
output = {'cmd': 'int',
'int': 3,
'verbosity': False,
'v': False}
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output
parser.parse_args(['int', '--int', '3', '-nv'])
output = {'cmd': 'int',
'int': 3,
'verbosity': True,
'v': True}
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output
2019-04-17 19:16:43 +02:00
def test_readme_int_verbosity_short_no(json):
2019-04-02 21:02:08 +02:00
output = {'cmd': 'int',
'int': 3,
'verbosity': False,
'v': False}
2019-04-17 19:16:43 +02:00
config = get_config(json)
2019-04-02 21:02:08 +02:00
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['int', '--int', '3', '-nv'])
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output
2019-04-02 21:02:08 +02:00
2019-04-17 19:16:43 +02:00
def test_readme_int_verbosity_short_tree(json):
output = {'root.cmd': 'int',
'root.int': 3,
'root.verbosity': True,
'root.v': True}
2019-04-17 19:16:43 +02:00
config = get_config(json, True)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['int', '--root.int', '3', '-v'])
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output
2019-04-17 19:16:43 +02:00
def test_readme_int_verbosity_short_tree_flatten(json):
output = {'root.cmd': 'int',
'root.int': 3,
'root.verbosity': True,
'root.v': True}
2019-04-17 19:16:43 +02:00
config = get_config(json, True)
parser = TiramisuCmdlineParser(config, 'prog.py', fullpath=False)
parser.parse_args(['int', '--int', '3', '-v'])
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output
2019-04-17 19:16:43 +02:00
def test_readme_int_verbosity_short_and_not(json):
output = {'cmd': 'int',
'int': 3,
'verbosity': False,
'v': False}
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['int', '--int', '3', '-v', '-nv'])
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output
2019-04-17 19:16:43 +02:00
def test_readme_str(json):
2018-12-01 09:53:31 +01:00
output = {'cmd': 'str',
'str': 'value',
'verbosity': False,
'v': False}
2019-04-17 19:16:43 +02:00
config = get_config(json)
2019-02-27 07:32:32 +01:00
parser = TiramisuCmdlineParser(config, 'prog.py')
2018-12-01 09:53:31 +01:00
parser.parse_args(['str', '--str', 'value'])
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output
2018-12-01 09:53:31 +01:00
2019-04-17 19:16:43 +02:00
def test_readme_str_tree(json):
output = {'root.cmd': 'str',
'root.str': 'value',
'root.verbosity': False,
'root.v': False}
2019-04-17 19:16:43 +02:00
config = get_config(json, True)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['str', '--root.str', 'value'])
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output
2019-04-17 19:16:43 +02:00
def test_readme_str_tree_flatten(json):
output = {'root.cmd': 'str',
'root.str': 'value',
'root.verbosity': False,
'root.v': False}
2019-04-17 19:16:43 +02:00
config = get_config(json, True)
parser = TiramisuCmdlineParser(config, 'prog.py', fullpath=False)
parser.parse_args(['str', '--str', 'value'])
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output
2019-04-17 19:16:43 +02:00
def test_readme_str_int(json):
2018-12-01 09:53:31 +01:00
output = {'cmd': 'str',
'str': '3',
'verbosity': False,
'v': False}
2019-04-17 19:16:43 +02:00
config = get_config(json)
2019-02-27 07:32:32 +01:00
parser = TiramisuCmdlineParser(config, 'prog.py')
2018-12-01 09:53:31 +01:00
parser.parse_args(['str', '--str', '3'])
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output
2018-12-01 09:53:31 +01:00
2019-04-17 19:16:43 +02:00
def test_readme_str_int_tree(json):
output = {'root.cmd': 'str',
'root.str': '3',
'root.verbosity': False,
'root.v': False}
2019-04-17 19:16:43 +02:00
config = get_config(json, True)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['str', '--root.str', '3'])
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output
2019-04-17 19:16:43 +02:00
def test_readme_str_int_tree_flatten(json):
output = {'root.cmd': 'str',
'root.str': '3',
'root.verbosity': False,
'root.v': False}
2019-04-17 19:16:43 +02:00
config = get_config(json, True)
parser = TiramisuCmdlineParser(config, 'prog.py', fullpath=False)
parser.parse_args(['str', '--str', '3'])
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output
2019-05-09 20:12:23 +02:00
def test_readme_list_single(json):
output = {'cmd': 'list',
'list': ['a'],
'verbosity': False,
'v': False}
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['list', '--list', 'a'])
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output
2019-05-09 20:12:23 +02:00
2019-04-17 19:16:43 +02:00
def test_readme_list(json):
2018-12-01 09:53:31 +01:00
output = {'cmd': 'list',
'list': ['a', 'b', 'c'],
'verbosity': False,
'v': False}
2019-04-17 19:16:43 +02:00
config = get_config(json)
2019-02-27 07:32:32 +01:00
parser = TiramisuCmdlineParser(config, 'prog.py')
2018-12-01 09:53:31 +01:00
parser.parse_args(['list', '--list', 'a', 'b', 'c'])
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output
2018-12-01 09:53:31 +01:00
2019-04-17 19:16:43 +02:00
def test_readme_list_tree(json):
output = {'root.cmd': 'list',
'root.list': ['a', 'b', 'c'],
'root.verbosity': False,
'root.v': False}
2019-04-17 19:16:43 +02:00
config = get_config(json, True)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['list', '--root.list', 'a', 'b', 'c'])
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output
2019-04-17 19:16:43 +02:00
def test_readme_list_tree_flatten(json):
output = {'root.cmd': 'list',
'root.list': ['a', 'b', 'c'],
'root.verbosity': False,
'root.v': False}
2019-04-17 19:16:43 +02:00
config = get_config(json, True)
parser = TiramisuCmdlineParser(config, 'prog.py', fullpath=False)
parser.parse_args(['list', '--list', 'a', 'b', 'c'])
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output
2019-04-17 19:16:43 +02:00
def test_readme_list_uniq(json):
2018-12-01 09:53:31 +01:00
output = {'cmd': 'list',
'list': ['a'],
'verbosity': False,
'v': False}
2019-04-17 19:16:43 +02:00
config = get_config(json)
2019-02-27 07:32:32 +01:00
parser = TiramisuCmdlineParser(config, 'prog.py')
2018-12-01 09:53:31 +01:00
parser.parse_args(['list', '--list', 'a'])
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output
2019-04-17 19:16:43 +02:00
def test_readme_list_uniq_tree(json):
output = {'root.cmd': 'list',
'root.list': ['a'],
'root.verbosity': False,
'root.v': False}
2019-04-17 19:16:43 +02:00
config = get_config(json, True)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['list', '--root.list', 'a'])
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output
2019-04-17 19:16:43 +02:00
def test_readme_list_uniq_tree_flatten(json):
output = {'root.cmd': 'list',
'root.list': ['a'],
'root.verbosity': False,
'root.v': False}
2019-04-17 19:16:43 +02:00
config = get_config(json, True)
parser = TiramisuCmdlineParser(config, 'prog.py', fullpath=False)
parser.parse_args(['list', '--list', 'a'])
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output
2019-04-03 07:55:19 +02:00
2019-04-17 19:16:43 +02:00
def test_readme_longargument(json):
2019-04-03 07:55:19 +02:00
output = {'cmd': 'list',
'list': ['a'],
'verbosity': True,
'v': True}
2019-04-17 19:16:43 +02:00
config = get_config(json, add_long=True)
2019-04-03 07:55:19 +02:00
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['list', '--list', 'a', '--v'])
2024-07-30 11:29:10 +02:00
assert to_dict(config.value.get()) == output
2019-08-22 15:58:34 +02:00
def test_readme_unknown_key(json):
output1 = """usage: prog.py [-h] [-v] [-nv] {str,list,int,none}
prog.py: error: unrecognized arguments: --unknown
"""
output2 = """usage: prog.py [-h] [-v] [-nv] {str,list,int,none}
prog.py: error: unrecognized arguments: --root.unknown
"""
2024-07-30 11:29:10 +02:00
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', fullpath=False, formatter_class=TestHelpFormatter)
2019-08-22 15:58:34 +02:00
f = StringIO()
with redirect_stderr(f):
try:
parser.parse_args(['--unknown'])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output1
f = StringIO()
with redirect_stderr(f):
try:
parser.parse_args(['--root.unknown'])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output2