1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183 |
- import collections
- import pytest
- from flexmock import flexmock
- from borgmatic.commands import arguments as module
- def test_get_subaction_parsers_with_no_subactions_returns_empty_result():
- assert module.get_subaction_parsers(flexmock(_subparsers=None)) == {}
- def test_get_subaction_parsers_with_subactions_returns_one_entry_per_subaction():
- foo_parser = flexmock()
- bar_parser = flexmock()
- baz_parser = flexmock()
- assert module.get_subaction_parsers(
- flexmock(
- _subparsers=flexmock(
- _group_actions=(
- flexmock(choices={'foo': foo_parser, 'bar': bar_parser}),
- flexmock(choices={'baz': baz_parser}),
- )
- )
- )
- ) == {'foo': foo_parser, 'bar': bar_parser, 'baz': baz_parser}
- def test_get_subactions_for_actions_with_no_subactions_returns_empty_result():
- assert module.get_subactions_for_actions({'action': flexmock(_subparsers=None)}) == {}
- def test_get_subactions_for_actions_with_subactions_returns_one_entry_per_action():
- assert module.get_subactions_for_actions(
- {
- 'action': flexmock(
- _subparsers=flexmock(
- _group_actions=(
- flexmock(choices={'foo': flexmock(), 'bar': flexmock()}),
- flexmock(choices={'baz': flexmock()}),
- )
- )
- ),
- 'other': flexmock(
- _subparsers=flexmock(_group_actions=(flexmock(choices={'quux': flexmock()}),))
- ),
- }
- ) == {'action': ('foo', 'bar', 'baz'), 'other': ('quux',)}
- def test_omit_values_colliding_with_action_names_drops_action_names_that_have_been_parsed_as_values():
- assert module.omit_values_colliding_with_action_names(
- ('check', '--only', 'extract', '--some-list', 'borg'),
- {'check': flexmock(only='extract', some_list=['borg'])},
- ) == ('check', '--only', '--some-list')
- def test_omit_values_colliding_twice_with_action_names_drops_action_names_that_have_been_parsed_as_values():
- assert module.omit_values_colliding_with_action_names(
- ('config', 'bootstrap', '--local-path', '--remote-path', 'borg'),
- {'bootstrap': flexmock(local_path='borg', remote_path='borg')},
- ) == ('config', 'bootstrap', '--local-path', '--remote-path')
- def test_parse_and_record_action_arguments_without_action_name_leaves_arguments_untouched():
- unparsed_arguments = ('--foo', '--bar')
- flexmock(module).should_receive('omit_values_colliding_with_action_names').and_return(
- unparsed_arguments
- )
- assert (
- module.parse_and_record_action_arguments(
- unparsed_arguments, flexmock(), flexmock(), 'action'
- )
- == unparsed_arguments
- )
- def test_parse_and_record_action_arguments_updates_parsed_arguments_and_returns_remaining():
- unparsed_arguments = ('action', '--foo', '--bar', '--verbosity', '1')
- other_parsed_arguments = flexmock()
- parsed_arguments = {'other': other_parsed_arguments}
- action_parsed_arguments = flexmock()
- flexmock(module).should_receive('omit_values_colliding_with_action_names').and_return(
- unparsed_arguments
- )
- action_parser = flexmock()
- flexmock(action_parser).should_receive('parse_known_args').and_return(
- action_parsed_arguments, ('action', '--verbosity', '1')
- )
- assert module.parse_and_record_action_arguments(
- unparsed_arguments, parsed_arguments, action_parser, 'action'
- ) == ('--verbosity', '1')
- assert parsed_arguments == {'other': other_parsed_arguments, 'action': action_parsed_arguments}
- def test_parse_and_record_action_arguments_with_alias_updates_canonical_parsed_arguments():
- unparsed_arguments = ('action', '--foo', '--bar', '--verbosity', '1')
- other_parsed_arguments = flexmock()
- parsed_arguments = {'other': other_parsed_arguments}
- action_parsed_arguments = flexmock()
- flexmock(module).should_receive('omit_values_colliding_with_action_names').and_return(
- unparsed_arguments
- )
- action_parser = flexmock()
- flexmock(action_parser).should_receive('parse_known_args').and_return(
- action_parsed_arguments, ('action', '--verbosity', '1')
- )
- assert module.parse_and_record_action_arguments(
- unparsed_arguments, parsed_arguments, action_parser, 'action', canonical_name='doit'
- ) == ('--verbosity', '1')
- assert parsed_arguments == {'other': other_parsed_arguments, 'doit': action_parsed_arguments}
- def test_parse_and_record_action_arguments_with_borg_action_consumes_arguments_after_action_name():
- unparsed_arguments = ('--verbosity', '1', 'borg', 'list')
- parsed_arguments = {}
- borg_parsed_arguments = flexmock(options=flexmock())
- flexmock(module).should_receive('omit_values_colliding_with_action_names').and_return(
- unparsed_arguments
- )
- borg_parser = flexmock()
- flexmock(borg_parser).should_receive('parse_known_args').and_return(
- borg_parsed_arguments, ('--verbosity', '1', 'borg', 'list')
- )
- assert module.parse_and_record_action_arguments(
- unparsed_arguments,
- parsed_arguments,
- borg_parser,
- 'borg',
- ) == ('--verbosity', '1')
- assert parsed_arguments == {'borg': borg_parsed_arguments}
- assert borg_parsed_arguments.options == ('list',)
- @pytest.mark.parametrize(
- 'argument, expected',
- [
- ('--foo', True),
- ('foo', False),
- (33, False),
- ],
- )
- def test_argument_is_flag_only_for_string_starting_with_double_dash(argument, expected):
- assert module.argument_is_flag(argument) == expected
- @pytest.mark.parametrize(
- 'arguments, expected',
- [
- # Ending with a valueless flag.
- (
- ('--foo', '--bar', 33, '--baz'),
- (
- ('--foo',),
- ('--bar', 33),
- ('--baz',),
- ),
- ),
- # Ending with a flag and its corresponding value.
- (
- ('--foo', '--bar', 33, '--baz', '--quux', 'thing'),
- (('--foo',), ('--bar', 33), ('--baz',), ('--quux', 'thing')),
- ),
- # Starting with an action name.
- (
- ('check', '--foo', '--bar', 33, '--baz'),
- (
- ('check',),
- ('--foo',),
- ('--bar', 33),
- ('--baz',),
- ),
- ),
- # Action name that one could mistake for a flag value.
- (('--progress', 'list'), (('--progress',), ('list',))),
- # No arguments.
- ((), ()),
- ],
- )
- def test_group_arguments_with_values_returns_flags_with_corresponding_values(arguments, expected):
- flexmock(module).should_receive('argument_is_flag').with_args('--foo').and_return(True)
- flexmock(module).should_receive('argument_is_flag').with_args('--bar').and_return(True)
- flexmock(module).should_receive('argument_is_flag').with_args('--baz').and_return(True)
- flexmock(module).should_receive('argument_is_flag').with_args('--quux').and_return(True)
- flexmock(module).should_receive('argument_is_flag').with_args('--progress').and_return(True)
- flexmock(module).should_receive('argument_is_flag').with_args(33).and_return(False)
- flexmock(module).should_receive('argument_is_flag').with_args('thing').and_return(False)
- flexmock(module).should_receive('argument_is_flag').with_args('check').and_return(False)
- flexmock(module).should_receive('argument_is_flag').with_args('list').and_return(False)
- assert module.group_arguments_with_values(arguments) == expected
- @pytest.mark.parametrize(
- 'arguments, grouped_arguments, expected',
- [
- # An unparsable flag remaining from each parsed action.
- (
- (
- ('--latest', 'archive', 'prune', 'extract', 'list', '--flag'),
- ('--latest', 'archive', 'check', 'extract', 'list', '--flag'),
- ('prune', 'check', 'list', '--flag'),
- ('prune', 'check', 'extract', '--flag'),
- ),
- (
- (
- ('--latest',),
- ('archive',),
- ('prune',),
- ('extract',),
- ('list',),
- ('--flag',),
- ),
- (
- ('--latest',),
- ('archive',),
- ('check',),
- ('extract',),
- ('list',),
- ('--flag',),
- ),
- (('prune',), ('check',), ('list',), ('--flag',)),
- (('prune',), ('check',), ('extract',), ('--flag',)),
- ),
- ('--flag',),
- ),
- # No unparsable flags remaining.
- (
- (
- ('--archive', 'archive', 'prune', 'extract', 'list'),
- ('--archive', 'archive', 'check', 'extract', 'list'),
- ('prune', 'check', 'list'),
- ('prune', 'check', 'extract'),
- ),
- (
- (
- (
- '--archive',
- 'archive',
- ),
- ('prune',),
- ('extract',),
- ('list',),
- ),
- (
- (
- '--archive',
- 'archive',
- ),
- ('check',),
- ('extract',),
- ('list',),
- ),
- (('prune',), ('check',), ('list',)),
- (('prune',), ('check',), ('extract',)),
- ),
- (),
- ),
- # No unparsable flags remaining, but some values in common.
- (
- (
- ('--verbosity', '5', 'archive', 'prune', 'extract', 'list'),
- ('--last', '5', 'archive', 'check', 'extract', 'list'),
- ('prune', 'check', 'list', '--last', '5'),
- ('prune', 'check', '--verbosity', '5', 'extract'),
- ),
- (
- (('--verbosity', '5'), ('archive',), ('prune',), ('extract',), ('list',)),
- (
- (
- '--last',
- '5',
- ),
- ('archive',),
- ('check',),
- ('extract',),
- ('list',),
- ),
- (('prune',), ('check',), ('list',), ('--last', '5')),
- (
- ('prune',),
- ('check',),
- (
- '--verbosity',
- '5',
- ),
- ('extract',),
- ),
- ),
- (),
- ),
- # No flags.
- ((), (), ()),
- ],
- )
- def test_get_unparsable_arguments_returns_remaining_arguments_that_no_action_can_parse(
- arguments, grouped_arguments, expected
- ):
- for action_arguments, grouped_action_arguments in zip(arguments, grouped_arguments):
- flexmock(module).should_receive('group_arguments_with_values').with_args(
- action_arguments
- ).and_return(grouped_action_arguments)
- assert module.get_unparsable_arguments(arguments) == expected
- def test_parse_arguments_for_actions_consumes_action_arguments_after_action_name():
- action_namespace = flexmock(foo=True)
- remaining = flexmock()
- flexmock(module).should_receive('get_subaction_parsers').and_return({})
- flexmock(module).should_receive('parse_and_record_action_arguments').replace_with(
- lambda unparsed, parsed, parser, action, canonical=None: parsed.update(
- {action: action_namespace}
- )
- or remaining
- )
- flexmock(module).should_receive('get_subactions_for_actions').and_return({})
- action_parsers = {'action': flexmock(), 'other': flexmock()}
- global_namespace = flexmock(config_paths=[])
- global_parser = flexmock()
- global_parser.should_receive('parse_known_args').and_return((global_namespace, ()))
- arguments, remaining_action_arguments = module.parse_arguments_for_actions(
- ('action', '--foo', 'true'), action_parsers, global_parser
- )
- assert arguments == {'global': global_namespace, 'action': action_namespace}
- assert remaining_action_arguments == (remaining, ())
- def test_parse_arguments_for_actions_consumes_action_arguments_with_alias():
- action_namespace = flexmock(foo=True)
- remaining = flexmock()
- flexmock(module).should_receive('get_subaction_parsers').and_return({})
- flexmock(module).should_receive('parse_and_record_action_arguments').replace_with(
- lambda unparsed, parsed, parser, action, canonical=None: parsed.update(
- {canonical or action: action_namespace}
- )
- or remaining
- )
- flexmock(module).should_receive('get_subactions_for_actions').and_return({})
- action_parsers = {
- 'action': flexmock(),
- '-a': flexmock(),
- 'other': flexmock(),
- '-o': flexmock(),
- }
- global_namespace = flexmock(config_paths=[])
- global_parser = flexmock()
- global_parser.should_receive('parse_known_args').and_return((global_namespace, ()))
- flexmock(module).ACTION_ALIASES = {'action': ['-a'], 'other': ['-o']}
- arguments, remaining_action_arguments = module.parse_arguments_for_actions(
- ('-a', '--foo', 'true'), action_parsers, global_parser
- )
- assert arguments == {'global': global_namespace, 'action': action_namespace}
- assert remaining_action_arguments == (remaining, ())
- def test_parse_arguments_for_actions_consumes_multiple_action_arguments():
- action_namespace = flexmock(foo=True)
- other_namespace = flexmock(bar=3)
- flexmock(module).should_receive('get_subaction_parsers').and_return({})
- flexmock(module).should_receive('parse_and_record_action_arguments').replace_with(
- lambda unparsed, parsed, parser, action, canonical=None: parsed.update(
- {action: action_namespace if action == 'action' else other_namespace}
- )
- or ()
- ).and_return(('other', '--bar', '3')).and_return('action', '--foo', 'true')
- flexmock(module).should_receive('get_subactions_for_actions').and_return({})
- action_parsers = {
- 'action': flexmock(),
- 'other': flexmock(),
- }
- global_namespace = flexmock(config_paths=[])
- global_parser = flexmock()
- global_parser.should_receive('parse_known_args').and_return((global_namespace, ()))
- arguments, remaining_action_arguments = module.parse_arguments_for_actions(
- ('action', '--foo', 'true', 'other', '--bar', '3'), action_parsers, global_parser
- )
- assert arguments == {
- 'global': global_namespace,
- 'action': action_namespace,
- 'other': other_namespace,
- }
- assert remaining_action_arguments == ((), (), ())
- def test_parse_arguments_for_actions_respects_command_line_action_ordering():
- other_namespace = flexmock()
- action_namespace = flexmock(foo=True)
- flexmock(module).should_receive('get_subaction_parsers').and_return({})
- flexmock(module).should_receive('parse_and_record_action_arguments').replace_with(
- lambda unparsed, parsed, parser, action, canonical=None: parsed.update(
- {action: other_namespace if action == 'other' else action_namespace}
- )
- or ()
- ).and_return(('action',)).and_return(('other', '--foo', 'true'))
- flexmock(module).should_receive('get_subactions_for_actions').and_return({})
- action_parsers = {
- 'action': flexmock(),
- 'other': flexmock(),
- }
- global_namespace = flexmock(config_paths=[])
- global_parser = flexmock()
- global_parser.should_receive('parse_known_args').and_return((global_namespace, ()))
- arguments, remaining_action_arguments = module.parse_arguments_for_actions(
- ('other', '--foo', 'true', 'action'), action_parsers, global_parser
- )
- assert arguments == collections.OrderedDict(
- [('other', other_namespace), ('action', action_namespace), ('global', global_namespace)]
- )
- assert remaining_action_arguments == ((), (), ())
- def test_parse_arguments_for_actions_applies_default_action_parsers():
- global_namespace = flexmock(config_paths=[])
- namespaces = {
- 'global': global_namespace,
- 'prune': flexmock(),
- 'compact': flexmock(),
- 'create': flexmock(progress=True),
- 'check': flexmock(),
- }
- flexmock(module).should_receive('get_subaction_parsers').and_return({})
- flexmock(module).should_receive('parse_and_record_action_arguments').replace_with(
- lambda unparsed, parsed, parser, action, canonical=None: parsed.update(
- {action: namespaces.get(action)}
- )
- or ()
- ).and_return(())
- flexmock(module).should_receive('get_subactions_for_actions').and_return({})
- action_parsers = {
- 'prune': flexmock(),
- 'compact': flexmock(),
- 'create': flexmock(),
- 'check': flexmock(),
- 'other': flexmock(),
- }
- global_parser = flexmock()
- global_parser.should_receive('parse_known_args').and_return((global_namespace, ()))
- arguments, remaining_action_arguments = module.parse_arguments_for_actions(
- ('--progress'), action_parsers, global_parser
- )
- assert arguments == namespaces
- assert remaining_action_arguments == ((), (), (), (), ())
- def test_parse_arguments_for_actions_consumes_global_arguments():
- action_namespace = flexmock()
- flexmock(module).should_receive('get_subaction_parsers').and_return({})
- flexmock(module).should_receive('parse_and_record_action_arguments').replace_with(
- lambda unparsed, parsed, parser, action, canonical=None: parsed.update(
- {action: action_namespace}
- )
- or ('--verbosity', 'lots')
- )
- flexmock(module).should_receive('get_subactions_for_actions').and_return({})
- action_parsers = {
- 'action': flexmock(),
- 'other': flexmock(),
- }
- global_namespace = flexmock(config_paths=[])
- global_parser = flexmock()
- global_parser.should_receive('parse_known_args').and_return((global_namespace, ()))
- arguments, remaining_action_arguments = module.parse_arguments_for_actions(
- ('action', '--verbosity', 'lots'), action_parsers, global_parser
- )
- assert arguments == {'global': global_namespace, 'action': action_namespace}
- assert remaining_action_arguments == (('--verbosity', 'lots'), ())
- def test_parse_arguments_for_actions_passes_through_unknown_arguments_before_action_name():
- action_namespace = flexmock()
- flexmock(module).should_receive('get_subaction_parsers').and_return({})
- flexmock(module).should_receive('parse_and_record_action_arguments').replace_with(
- lambda unparsed, parsed, parser, action, canonical=None: parsed.update(
- {action: action_namespace}
- )
- or ('--wtf', 'yes')
- )
- flexmock(module).should_receive('get_subactions_for_actions').and_return({})
- action_parsers = {
- 'action': flexmock(),
- 'other': flexmock(),
- }
- global_namespace = flexmock(config_paths=[])
- global_parser = flexmock()
- global_parser.should_receive('parse_known_args').and_return((global_namespace, ()))
- arguments, remaining_action_arguments = module.parse_arguments_for_actions(
- ('--wtf', 'yes', 'action'), action_parsers, global_parser
- )
- assert arguments == {'global': global_namespace, 'action': action_namespace}
- assert remaining_action_arguments == (('--wtf', 'yes'), ())
- def test_parse_arguments_for_actions_passes_through_unknown_arguments_after_action_name():
- action_namespace = flexmock()
- flexmock(module).should_receive('get_subaction_parsers').and_return({})
- flexmock(module).should_receive('parse_and_record_action_arguments').replace_with(
- lambda unparsed, parsed, parser, action, canonical=None: parsed.update(
- {action: action_namespace}
- )
- or ('--wtf', 'yes')
- )
- flexmock(module).should_receive('get_subactions_for_actions').and_return({})
- action_parsers = {
- 'action': flexmock(),
- 'other': flexmock(),
- }
- global_namespace = flexmock(config_paths=[])
- global_parser = flexmock()
- global_parser.should_receive('parse_known_args').and_return((global_namespace, ()))
- arguments, remaining_action_arguments = module.parse_arguments_for_actions(
- ('action', '--wtf', 'yes'), action_parsers, global_parser
- )
- assert arguments == {'global': global_namespace, 'action': action_namespace}
- assert remaining_action_arguments == (('--wtf', 'yes'), ())
- def test_parse_arguments_for_actions_with_borg_action_skips_other_action_parsers():
- action_namespace = flexmock(options=[])
- flexmock(module).should_receive('get_subaction_parsers').and_return({})
- flexmock(module).should_receive('parse_and_record_action_arguments').replace_with(
- lambda unparsed, parsed, parser, action, canonical=None: parsed.update(
- {action: action_namespace}
- )
- or ()
- ).and_return(())
- flexmock(module).should_receive('get_subactions_for_actions').and_return({})
- action_parsers = {
- 'borg': flexmock(),
- 'list': flexmock(),
- }
- global_namespace = flexmock(config_paths=[])
- global_parser = flexmock()
- global_parser.should_receive('parse_known_args').and_return((global_namespace, ()))
- arguments, remaining_action_arguments = module.parse_arguments_for_actions(
- ('borg', 'list'), action_parsers, global_parser
- )
- assert arguments == {'global': global_namespace, 'borg': action_namespace}
- assert remaining_action_arguments == ((), ())
- def test_parse_arguments_for_actions_raises_error_when_no_action_is_specified():
- flexmock(module).should_receive('get_subaction_parsers').and_return({'bootstrap': [flexmock()]})
- flexmock(module).should_receive('parse_and_record_action_arguments').and_return(flexmock())
- flexmock(module).should_receive('get_subactions_for_actions').and_return(
- {'config': ['bootstrap']}
- )
- action_parsers = {'config': flexmock()}
- global_parser = flexmock()
- global_parser.should_receive('parse_known_args').and_return((flexmock(), ()))
- with pytest.raises(ValueError):
- module.parse_arguments_for_actions(('config',), action_parsers, global_parser)
- def test_make_argument_description_without_description_bails():
- assert (
- module.make_argument_description(
- schema={
- 'description': None,
- 'type': 'not yours',
- },
- flag_name='flag',
- )
- is None
- )
- def test_make_argument_description_with_array_adds_example():
- buffer = flexmock()
- buffer.should_receive('getvalue').and_return('[example]')
- flexmock(module.io).should_receive('StringIO').and_return(buffer)
- yaml = flexmock()
- yaml.should_receive('dump')
- flexmock(module.ruamel.yaml).should_receive('YAML').and_return(yaml)
- assert (
- module.make_argument_description(
- schema={
- 'description': 'Thing.',
- 'type': 'array',
- 'example': ['example'],
- },
- flag_name='flag',
- )
- == 'Thing. Example value: "[example]"'
- )
- def test_make_argument_description_with_array_skips_missing_example():
- yaml = flexmock()
- yaml.should_receive('dump').and_return('[example]')
- flexmock(module.ruamel.yaml).should_receive('YAML').and_return(yaml)
- assert (
- module.make_argument_description(
- schema={
- 'description': 'Thing.',
- 'type': 'array',
- },
- flag_name='flag',
- )
- == 'Thing.'
- )
- def test_make_argument_description_with_array_index_in_flag_name_adds_to_description():
- assert 'list element' in module.make_argument_description(
- schema={
- 'description': 'Thing.',
- 'type': 'something',
- },
- flag_name='flag[0]',
- )
- def test_make_argument_description_escapes_percent_character():
- assert (
- module.make_argument_description(
- schema={
- 'description': '% Thing.',
- 'type': 'something',
- },
- flag_name='flag',
- )
- == '%% Thing.'
- )
- def test_add_array_element_arguments_without_array_index_bails():
- arguments_group = flexmock()
- arguments_group.should_receive('add_argument').never()
- module.add_array_element_arguments(
- arguments_group=arguments_group,
- unparsed_arguments=(),
- flag_name='foo',
- )
- def test_add_array_element_arguments_with_help_flag_bails():
- arguments_group = flexmock()
- arguments_group.should_receive('add_argument').never()
- module.add_array_element_arguments(
- arguments_group=arguments_group,
- unparsed_arguments=('--foo', '--help', '--bar'),
- flag_name='foo[0]',
- )
- def test_add_array_element_arguments_without_any_flags_bails():
- arguments_group = flexmock()
- arguments_group.should_receive('add_argument').never()
- module.add_array_element_arguments(
- arguments_group=arguments_group,
- unparsed_arguments=(),
- flag_name='foo[0]',
- )
- # Use this instead of a flexmock because it's not easy to check the type() of a flexmock instance.
- Group_action = collections.namedtuple(
- 'Group_action',
- (
- 'option_strings',
- 'choices',
- 'default',
- 'nargs',
- 'required',
- 'type',
- ),
- defaults=(
- flexmock(),
- flexmock(),
- flexmock(),
- flexmock(),
- flexmock(),
- ),
- )
- def test_add_array_element_arguments_without_array_index_flags_bails():
- arguments_group = flexmock(
- _group_actions=(
- Group_action(
- option_strings=('--foo[0].val',),
- ),
- ),
- _registries={'action': {'store_stuff': Group_action}},
- )
- arguments_group.should_receive('add_argument').never()
- module.add_array_element_arguments(
- arguments_group=arguments_group,
- unparsed_arguments=('--foo', '--bar'),
- flag_name='foo[0].val',
- )
- def test_add_array_element_arguments_with_non_matching_array_index_flags_bails():
- arguments_group = flexmock(
- _group_actions=(
- Group_action(
- option_strings=('--foo[0].val',),
- ),
- ),
- _registries={'action': {'store_stuff': Group_action}},
- )
- arguments_group.should_receive('add_argument').never()
- module.add_array_element_arguments(
- arguments_group=arguments_group,
- unparsed_arguments=('--foo', '--bar[25].val', 'barval'),
- flag_name='foo[0].val',
- )
- def test_add_array_element_arguments_with_identical_array_index_flag_bails():
- arguments_group = flexmock(
- _group_actions=(
- Group_action(
- option_strings=('--foo[0].val',),
- ),
- ),
- _registries={'action': {'store_stuff': Group_action}},
- )
- arguments_group.should_receive('add_argument').never()
- module.add_array_element_arguments(
- arguments_group=arguments_group,
- unparsed_arguments=('--foo[0].val', 'fooval', '--bar'),
- flag_name='foo[0].val',
- )
- def test_add_array_element_arguments_without_action_type_in_registry_bails():
- arguments_group = flexmock(
- _group_actions=(
- Group_action(
- option_strings=('--foo[0].val',),
- choices=flexmock(),
- default=flexmock(),
- nargs=flexmock(),
- required=flexmock(),
- type=flexmock(),
- ),
- ),
- _registries={'action': {'store_stuff': bool}},
- )
- arguments_group.should_receive('add_argument').never()
- module.add_array_element_arguments(
- arguments_group=arguments_group,
- unparsed_arguments=('--foo[25].val', 'fooval', '--bar[1].val', 'barval'),
- flag_name='foo[0].val',
- )
- def test_add_array_element_arguments_adds_arguments_for_array_index_flags():
- arguments_group = flexmock(
- _group_actions=(
- Group_action(
- option_strings=('--foo[0].val',),
- choices=flexmock(),
- default=flexmock(),
- nargs=flexmock(),
- required=flexmock(),
- type=flexmock(),
- ),
- ),
- _registries={'action': {'store_stuff': Group_action}},
- )
- arguments_group.should_receive('add_argument').with_args(
- '--foo[25].val',
- action='store_stuff',
- choices=object,
- default=object,
- dest='foo[25].val',
- nargs=object,
- required=object,
- type=object,
- ).once()
- module.add_array_element_arguments(
- arguments_group=arguments_group,
- unparsed_arguments=('--foo[25].val', 'fooval', '--bar[1].val', 'barval'),
- flag_name='foo[0].val',
- )
- def test_add_array_element_arguments_adds_arguments_for_array_index_flags_with_equals_sign():
- arguments_group = flexmock(
- _group_actions=(
- Group_action(
- option_strings=('--foo[0].val',),
- choices=flexmock(),
- default=flexmock(),
- nargs=flexmock(),
- required=flexmock(),
- type=flexmock(),
- ),
- ),
- _registries={'action': {'store_stuff': Group_action}},
- )
- arguments_group.should_receive('add_argument').with_args(
- '--foo[25].val',
- action='store_stuff',
- choices=object,
- default=object,
- dest='foo[25].val',
- nargs=object,
- required=object,
- type=object,
- ).once()
- module.add_array_element_arguments(
- arguments_group=arguments_group,
- unparsed_arguments=('--foo[25].val=fooval', '--bar[1].val=barval'),
- flag_name='foo[0].val',
- )
- def test_add_arguments_from_schema_with_non_dict_schema_bails():
- arguments_group = flexmock()
- flexmock(module).should_receive('make_argument_description').never()
- flexmock(module.borgmatic.config.schema).should_receive('parse_type').never()
- arguments_group.should_receive('add_argument').never()
- module.add_arguments_from_schema(
- arguments_group=arguments_group, schema='foo', unparsed_arguments=()
- )
- def test_add_arguments_from_schema_with_nested_object_adds_flag_for_each_option():
- arguments_group = flexmock()
- flexmock(module).should_receive('make_argument_description').and_return('help 1').and_return('help 2')
- flexmock(module.borgmatic.config.schema).should_receive('parse_type').and_return(int).and_return(str)
- arguments_group.should_receive('add_argument').with_args(
- '--foo.bar',
- type=int,
- metavar='BAR',
- help='help 1',
- ).once()
- arguments_group.should_receive('add_argument').with_args(
- '--foo.baz',
- type=str,
- metavar='BAZ',
- help='help 2',
- ).once()
- flexmock(module).should_receive('add_array_element_arguments')
- module.add_arguments_from_schema(
- arguments_group=arguments_group,
- schema={
- 'type': 'object',
- 'properties': {
- 'foo': {
- 'type': 'object',
- 'properties': {
- 'bar': {'type': 'integer'},
- 'baz': {'type': 'str'},
- }
- }
- }
- },
- unparsed_arguments=(),
- )
- def test_add_arguments_from_schema_uses_first_non_null_type_from_multi_type_object():
- arguments_group = flexmock()
- flexmock(module).should_receive('make_argument_description').and_return('help 1')
- flexmock(module.borgmatic.config.schema).should_receive('parse_type').and_return(int)
- arguments_group.should_receive('add_argument').with_args(
- '--foo.bar',
- type=int,
- metavar='BAR',
- help='help 1',
- ).once()
- flexmock(module).should_receive('add_array_element_arguments')
- module.add_arguments_from_schema(
- arguments_group=arguments_group,
- schema={
- 'type': 'object',
- 'properties': {
- 'foo': {
- 'type': ['null', 'object', 'boolean'],
- 'properties': {
- 'bar': {'type': 'integer'},
- }
- }
- }
- },
- unparsed_arguments=(),
- )
- def test_add_arguments_from_schema_with_empty_multi_type_raises():
- arguments_group = flexmock()
- flexmock(module).should_receive('make_argument_description').and_return('help 1')
- flexmock(module.borgmatic.config.schema).should_receive('parse_type').and_return(int)
- arguments_group.should_receive('add_argument').never()
- flexmock(module).should_receive('add_array_element_arguments').never()
- with pytest.raises(ValueError):
- module.add_arguments_from_schema(
- arguments_group=arguments_group,
- schema={
- 'type': 'object',
- 'properties': {
- 'foo': {
- 'type': [],
- 'properties': {
- 'bar': {'type': 'integer'},
- }
- }
- }
- },
- unparsed_arguments=(),
- )
- def test_add_arguments_from_schema_with_propertyless_option_does_not_add_flag():
- arguments_group = flexmock()
- flexmock(module).should_receive('make_argument_description').never()
- flexmock(module.borgmatic.config.schema).should_receive('parse_type').never()
- arguments_group.should_receive('add_argument').never()
- flexmock(module).should_receive('add_array_element_arguments').never()
- module.add_arguments_from_schema(
- arguments_group=arguments_group,
- schema={
- 'type': 'object',
- 'properties': {
- 'foo': {
- 'type': 'object',
- }
- }
- },
- unparsed_arguments=(),
- )
- def test_add_arguments_from_schema_with_array_adds_flag():
- arguments_group = flexmock()
- flexmock(module).should_receive('make_argument_description').and_return('help')
- flexmock(module.borgmatic.config.schema).should_receive('parse_type').and_return(str)
- arguments_group.should_receive('add_argument').with_args(
- '--foo',
- type=str,
- metavar='FOO',
- help='help',
- ).once()
- flexmock(module).should_receive('add_array_element_arguments')
- module.add_arguments_from_schema(
- arguments_group=arguments_group,
- schema={
- 'type': 'object',
- 'properties': {
- 'foo': {
- 'type': 'array',
- 'items': {
- 'type': 'integer',
- }
- }
- }
- },
- unparsed_arguments=(),
- )
- def test_add_arguments_from_schema_with_array_and_nested_object_adds_multiple_flags():
- arguments_group = flexmock()
- flexmock(module).should_receive('make_argument_description').and_return('help 1').and_return('help 2')
- flexmock(module.borgmatic.config.schema).should_receive('parse_type').and_return(int).and_return(str)
- arguments_group.should_receive('add_argument').with_args(
- '--foo[0].bar',
- type=int,
- metavar='BAR',
- help='help 1',
- ).once()
- arguments_group.should_receive('add_argument').with_args(
- '--foo',
- type=str,
- metavar='FOO',
- help='help 2',
- ).once()
- flexmock(module).should_receive('add_array_element_arguments')
- flexmock(module).should_receive('add_array_element_arguments').with_args(
- arguments_group=arguments_group,
- unparsed_arguments=(),
- flag_name='foo[0].bar',
- ).once()
- module.add_arguments_from_schema(
- arguments_group=arguments_group,
- schema={
- 'type': 'object',
- 'properties': {
- 'foo': {
- 'type': 'array',
- 'items': {
- 'type': 'object',
- 'properties': {
- 'bar': {
- 'type': 'integer',
- }
- }
- }
- }
- }
- },
- unparsed_arguments=(),
- )
- def test_add_arguments_from_schema_with_default_false_boolean_adds_valueless_flag():
- arguments_group = flexmock()
- flexmock(module).should_receive('make_argument_description').and_return('help')
- flexmock(module.borgmatic.config.schema).should_receive('parse_type').and_return(bool)
- arguments_group.should_receive('add_argument').with_args(
- '--foo',
- action='store_true',
- default=None,
- help='help',
- ).once()
- flexmock(module).should_receive('add_array_element_arguments')
- module.add_arguments_from_schema(
- arguments_group=arguments_group,
- schema={
- 'type': 'object',
- 'properties': {
- 'foo': {
- 'type': 'boolean',
- 'default': False,
- }
- }
- },
- unparsed_arguments=(),
- )
- def test_add_arguments_from_schema_with_default_true_boolean_adds_value_flag():
- arguments_group = flexmock()
- flexmock(module).should_receive('make_argument_description').and_return('help')
- flexmock(module.borgmatic.config.schema).should_receive('parse_type').and_return(bool)
- arguments_group.should_receive('add_argument').with_args(
- '--foo',
- type=bool,
- metavar='FOO',
- help='help',
- ).once()
- flexmock(module).should_receive('add_array_element_arguments')
- module.add_arguments_from_schema(
- arguments_group=arguments_group,
- schema={
- 'type': 'object',
- 'properties': {
- 'foo': {
- 'type': 'boolean',
- 'default': True,
- }
- }
- },
- unparsed_arguments=(),
- )
- def test_add_arguments_from_schema_with_defaultless_boolean_adds_value_flag():
- arguments_group = flexmock()
- flexmock(module).should_receive('make_argument_description').and_return('help')
- flexmock(module.borgmatic.config.schema).should_receive('parse_type').and_return(bool)
- arguments_group.should_receive('add_argument').with_args(
- '--foo',
- type=bool,
- metavar='FOO',
- help='help',
- ).once()
- flexmock(module).should_receive('add_array_element_arguments')
- module.add_arguments_from_schema(
- arguments_group=arguments_group,
- schema={
- 'type': 'object',
- 'properties': {
- 'foo': {
- 'type': 'boolean',
- }
- }
- },
- unparsed_arguments=(),
- )
- def test_add_arguments_from_schema_skips_omitted_flag_name():
- arguments_group = flexmock()
- flexmock(module).should_receive('make_argument_description').and_return('help')
- flexmock(module.borgmatic.config.schema).should_receive('parse_type').and_return(str)
- arguments_group.should_receive('add_argument').with_args(
- '--match-archives',
- type=object,
- metavar=object,
- help=object,
- ).never()
- arguments_group.should_receive('add_argument').with_args(
- '--foo',
- type=str,
- metavar='FOO',
- help='help',
- ).once()
- flexmock(module).should_receive('add_array_element_arguments')
- module.add_arguments_from_schema(
- arguments_group=arguments_group,
- schema={
- 'type': 'object',
- 'properties': {
- 'match_archives': {
- 'type': 'string',
- },
- 'foo': {
- 'type': 'string',
- },
- }
- },
- unparsed_arguments=(),
- )
- def test_add_arguments_from_schema_rewrites_option_name_to_flag_name():
- arguments_group = flexmock()
- flexmock(module).should_receive('make_argument_description').and_return('help')
- flexmock(module.borgmatic.config.schema).should_receive('parse_type').and_return(str)
- arguments_group.should_receive('add_argument').with_args(
- '--foo-and-stuff',
- type=str,
- metavar='FOO_AND_STUFF',
- help='help',
- ).once()
- flexmock(module).should_receive('add_array_element_arguments')
- module.add_arguments_from_schema(
- arguments_group=arguments_group,
- schema={
- 'type': 'object',
- 'properties': {
- 'foo_and_stuff': {
- 'type': 'string',
- },
- }
- },
- unparsed_arguments=(),
- )
|