2023-03-08 22:19:41 +01:00
|
|
|
import collections
|
|
|
|
|
2023-06-06 21:26:19 +02:00
|
|
|
import pytest
|
2019-06-24 01:06:39 +02:00
|
|
|
from flexmock import flexmock
|
|
|
|
|
|
|
|
from borgmatic.commands import arguments as module
|
|
|
|
|
|
|
|
|
2023-06-20 01:18:47 +02:00
|
|
|
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')
|
2019-06-24 01:06:39 +02:00
|
|
|
|
2023-06-20 01:18:47 +02:00
|
|
|
|
|
|
|
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
|
2021-06-18 05:41:44 +02:00
|
|
|
)
|
2019-06-24 01:06:39 +02:00
|
|
|
|
2023-06-20 01:18:47 +02:00
|
|
|
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',)
|
2019-06-24 01:06:39 +02:00
|
|
|
|
|
|
|
|
2023-06-24 23:10:47 +02:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
'arguments, expected',
|
|
|
|
[
|
|
|
|
# A global flag remaining from each parsed action.
|
|
|
|
(
|
|
|
|
(
|
|
|
|
('--latest', 'archive', 'prune', 'extract', 'list', '--test-flag'),
|
|
|
|
('--latest', 'archive', 'check', 'extract', 'list', '--test-flag'),
|
|
|
|
('prune', 'check', 'list', '--test-flag'),
|
|
|
|
('prune', 'check', 'extract', '--test-flag'),
|
|
|
|
),
|
|
|
|
('--test-flag',),
|
|
|
|
),
|
|
|
|
# No global flags remaining.
|
|
|
|
(
|
|
|
|
(
|
|
|
|
('--latest', 'archive', 'prune', 'extract', 'list'),
|
|
|
|
('--latest', 'archive', 'check', 'extract', 'list'),
|
|
|
|
('prune', 'check', 'list'),
|
|
|
|
('prune', 'check', 'extract'),
|
|
|
|
),
|
|
|
|
(),
|
|
|
|
),
|
|
|
|
# Multiple of the same value across global flags.
|
|
|
|
(
|
|
|
|
(
|
|
|
|
('--verbosity', '2', '--syslog-verbosity', '2', '--monitoring-verbosity', '2'),
|
|
|
|
('--verbosity', '2', '--syslog-verbosity', '2', '--monitoring-verbosity', '2'),
|
|
|
|
),
|
|
|
|
('--verbosity', '2', '--syslog-verbosity', '2', '--monitoring-verbosity', '2'),
|
|
|
|
),
|
|
|
|
# Multiple of the same value across action and global flags.
|
|
|
|
(
|
|
|
|
(
|
|
|
|
('list', '--archive', 'test', '--log-file', 'test'),
|
|
|
|
('prune', '--log-file', 'test'),
|
|
|
|
),
|
|
|
|
('--log-file', 'test'),
|
|
|
|
),
|
|
|
|
# No flags.
|
|
|
|
((), ()),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_get_unparsable_arguments_returns_remaining_arguments_that_no_action_can_parse(
|
|
|
|
arguments, expected
|
|
|
|
):
|
|
|
|
assert module.get_unparsable_arguments(arguments) == expected
|
|
|
|
|
|
|
|
|
|
|
|
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_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',)
|
|
|
|
|
|
|
|
|
2023-06-20 01:18:47 +02:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
'arguments, expected',
|
|
|
|
[
|
|
|
|
(
|
|
|
|
(
|
|
|
|
('--latest', 'archive', 'prune', 'extract', 'list', '--test-flag'),
|
|
|
|
('--latest', 'archive', 'check', 'extract', 'list', '--test-flag'),
|
|
|
|
('prune', 'check', 'list', '--test-flag'),
|
|
|
|
('prune', 'check', 'extract', '--test-flag'),
|
|
|
|
),
|
|
|
|
('--test-flag',),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
(
|
|
|
|
('--latest', 'archive', 'prune', 'extract', 'list'),
|
|
|
|
('--latest', 'archive', 'check', 'extract', 'list'),
|
|
|
|
('prune', 'check', 'list'),
|
|
|
|
('prune', 'check', 'extract'),
|
|
|
|
),
|
|
|
|
(),
|
|
|
|
),
|
|
|
|
((), ()),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_get_unparsable_arguments_returns_remaining_arguments_that_no_action_can_parse(
|
|
|
|
arguments, expected
|
|
|
|
):
|
|
|
|
assert module.get_unparsable_arguments(arguments) == expected
|
|
|
|
|
|
|
|
|
|
|
|
def test_parse_arguments_for_actions_consumes_action_arguments_before_action_name():
|
2019-06-24 01:06:39 +02:00
|
|
|
action_namespace = flexmock(foo=True)
|
2023-06-20 01:18:47 +02:00
|
|
|
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}
|
|
|
|
)
|
2023-06-24 23:10:47 +02:00
|
|
|
or ()
|
2023-06-20 01:18:47 +02:00
|
|
|
).and_return(())
|
|
|
|
flexmock(module).should_receive('get_subactions_for_actions').and_return({})
|
|
|
|
action_parsers = {'action': flexmock(), 'other': flexmock()}
|
2023-06-24 23:10:47 +02:00
|
|
|
global_namespace = flexmock()
|
|
|
|
global_parser = flexmock()
|
|
|
|
global_parser.should_receive('parse_known_args').and_return((global_namespace, ()))
|
2023-06-20 01:18:47 +02:00
|
|
|
|
2023-06-24 23:10:47 +02:00
|
|
|
arguments, remaining_action_arguments = module.parse_arguments_for_actions(
|
|
|
|
('--foo', 'true', 'action'), action_parsers, global_parser
|
2023-06-20 01:18:47 +02:00
|
|
|
)
|
|
|
|
|
2023-06-24 23:10:47 +02:00
|
|
|
assert arguments == {'global': global_namespace, 'action': action_namespace}
|
|
|
|
assert remaining_action_arguments == ((), ())
|
2023-06-20 01:18:47 +02:00
|
|
|
|
2019-06-24 01:06:39 +02:00
|
|
|
|
2023-06-20 01:18:47 +02:00
|
|
|
def test_parse_arguments_for_actions_consumes_action_arguments_after_action_name():
|
|
|
|
action_namespace = flexmock(foo=True)
|
2023-06-24 23:10:47 +02:00
|
|
|
remaining = flexmock()
|
2023-06-20 01:18:47 +02:00
|
|
|
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}
|
|
|
|
)
|
2023-06-24 23:10:47 +02:00
|
|
|
or remaining
|
|
|
|
)
|
2023-06-20 01:18:47 +02:00
|
|
|
flexmock(module).should_receive('get_subactions_for_actions').and_return({})
|
|
|
|
action_parsers = {'action': flexmock(), 'other': flexmock()}
|
2023-06-24 23:10:47 +02:00
|
|
|
global_namespace = flexmock()
|
|
|
|
global_parser = flexmock()
|
|
|
|
global_parser.should_receive('parse_known_args').and_return((global_namespace, ()))
|
2023-06-20 01:18:47 +02:00
|
|
|
|
2023-06-24 23:10:47 +02:00
|
|
|
arguments, remaining_action_arguments = module.parse_arguments_for_actions(
|
|
|
|
('action', '--foo', 'true'), action_parsers, global_parser
|
2021-06-18 05:41:44 +02:00
|
|
|
)
|
2019-06-24 01:06:39 +02:00
|
|
|
|
2023-06-24 23:10:47 +02:00
|
|
|
assert arguments == {'global': global_namespace, 'action': action_namespace}
|
|
|
|
assert remaining_action_arguments == (remaining, ())
|
2019-06-24 01:06:39 +02:00
|
|
|
|
|
|
|
|
2023-06-20 01:18:47 +02:00
|
|
|
def test_parse_arguments_for_actions_consumes_action_arguments_with_alias():
|
2019-06-24 01:06:39 +02:00
|
|
|
action_namespace = flexmock(foo=True)
|
2023-06-24 23:10:47 +02:00
|
|
|
remaining = flexmock()
|
2023-06-20 01:18:47 +02:00
|
|
|
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}
|
|
|
|
)
|
2023-06-24 23:10:47 +02:00
|
|
|
or remaining
|
|
|
|
)
|
2023-06-20 01:18:47 +02:00
|
|
|
flexmock(module).should_receive('get_subactions_for_actions').and_return({})
|
|
|
|
action_parsers = {
|
|
|
|
'action': flexmock(),
|
|
|
|
'-a': flexmock(),
|
2021-06-18 05:41:44 +02:00
|
|
|
'other': flexmock(),
|
|
|
|
'-o': flexmock(),
|
|
|
|
}
|
2023-06-24 23:10:47 +02:00
|
|
|
global_namespace = flexmock()
|
|
|
|
global_parser = flexmock()
|
|
|
|
global_parser.should_receive('parse_known_args').and_return((global_namespace, ()))
|
2023-06-20 01:18:47 +02:00
|
|
|
flexmock(module).ACTION_ALIASES = {'action': ['-a'], 'other': ['-o']}
|
2019-06-24 01:06:39 +02:00
|
|
|
|
2023-06-24 23:10:47 +02:00
|
|
|
arguments, remaining_action_arguments = module.parse_arguments_for_actions(
|
|
|
|
('-a', '--foo', 'true'), action_parsers, global_parser
|
2021-06-18 05:41:44 +02:00
|
|
|
)
|
2019-06-24 01:06:39 +02:00
|
|
|
|
2023-06-24 23:10:47 +02:00
|
|
|
assert arguments == {'global': global_namespace, 'action': action_namespace}
|
|
|
|
assert remaining_action_arguments == (remaining, ())
|
2019-06-24 01:06:39 +02:00
|
|
|
|
|
|
|
|
2023-06-20 01:18:47 +02:00
|
|
|
def test_parse_arguments_for_actions_consumes_multiple_action_arguments():
|
2019-06-24 01:06:39 +02:00
|
|
|
action_namespace = flexmock(foo=True)
|
|
|
|
other_namespace = flexmock(bar=3)
|
2023-06-20 01:18:47 +02:00
|
|
|
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}
|
|
|
|
)
|
2023-06-24 23:10:47 +02:00
|
|
|
or ()
|
2023-06-20 01:18:47 +02:00
|
|
|
).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(),
|
2021-06-18 05:41:44 +02:00
|
|
|
}
|
2023-06-24 23:10:47 +02:00
|
|
|
global_namespace = flexmock()
|
|
|
|
global_parser = flexmock()
|
|
|
|
global_parser.should_receive('parse_known_args').and_return((global_namespace, ()))
|
2019-06-24 01:06:39 +02:00
|
|
|
|
2023-06-24 23:10:47 +02:00
|
|
|
arguments, remaining_action_arguments = module.parse_arguments_for_actions(
|
|
|
|
('action', '--foo', 'true', 'other', '--bar', '3'), action_parsers, global_parser
|
2019-06-24 01:06:39 +02:00
|
|
|
)
|
|
|
|
|
2023-06-24 23:10:47 +02:00
|
|
|
assert arguments == {
|
|
|
|
'global': global_namespace,
|
|
|
|
'action': action_namespace,
|
|
|
|
'other': other_namespace,
|
|
|
|
}
|
|
|
|
assert remaining_action_arguments == ((), (), ())
|
2019-06-24 01:06:39 +02:00
|
|
|
|
|
|
|
|
2023-06-20 01:18:47 +02:00
|
|
|
def test_parse_arguments_for_actions_respects_command_line_action_ordering():
|
2023-03-08 22:19:41 +01:00
|
|
|
other_namespace = flexmock()
|
|
|
|
action_namespace = flexmock(foo=True)
|
2023-06-20 01:18:47 +02:00
|
|
|
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}
|
|
|
|
)
|
2023-06-24 23:10:47 +02:00
|
|
|
or ()
|
2023-06-20 01:18:47 +02:00
|
|
|
).and_return(('action',)).and_return(('other', '--foo', 'true'))
|
|
|
|
flexmock(module).should_receive('get_subactions_for_actions').and_return({})
|
|
|
|
action_parsers = {
|
|
|
|
'action': flexmock(),
|
|
|
|
'other': flexmock(),
|
2023-03-08 22:19:41 +01:00
|
|
|
}
|
2023-06-24 23:10:47 +02:00
|
|
|
global_namespace = flexmock()
|
|
|
|
global_parser = flexmock()
|
|
|
|
global_parser.should_receive('parse_known_args').and_return((global_namespace, ()))
|
2023-03-08 22:19:41 +01:00
|
|
|
|
2023-06-24 23:10:47 +02:00
|
|
|
arguments, remaining_action_arguments = module.parse_arguments_for_actions(
|
|
|
|
('other', '--foo', 'true', 'action'), action_parsers, global_parser
|
2023-03-08 22:19:41 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
assert arguments == collections.OrderedDict(
|
2023-06-24 23:10:47 +02:00
|
|
|
[('other', other_namespace), ('action', action_namespace), ('global', global_namespace)]
|
2023-03-08 22:19:41 +01:00
|
|
|
)
|
2023-06-24 23:10:47 +02:00
|
|
|
assert remaining_action_arguments == ((), (), ())
|
2023-03-08 22:19:41 +01:00
|
|
|
|
|
|
|
|
2023-06-20 01:18:47 +02:00
|
|
|
def test_parse_arguments_for_actions_applies_default_action_parsers():
|
2023-06-24 23:10:47 +02:00
|
|
|
global_namespace = flexmock()
|
2023-06-20 01:18:47 +02:00
|
|
|
namespaces = {
|
2023-06-24 23:10:47 +02:00
|
|
|
'global': global_namespace,
|
2023-06-20 01:18:47 +02:00
|
|
|
'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)}
|
|
|
|
)
|
2023-06-24 23:10:47 +02:00
|
|
|
or ()
|
2023-06-20 01:18:47 +02:00
|
|
|
).and_return(())
|
|
|
|
flexmock(module).should_receive('get_subactions_for_actions').and_return({})
|
|
|
|
action_parsers = {
|
|
|
|
'prune': flexmock(),
|
|
|
|
'compact': flexmock(),
|
|
|
|
'create': flexmock(),
|
|
|
|
'check': flexmock(),
|
2021-06-18 05:41:44 +02:00
|
|
|
'other': flexmock(),
|
|
|
|
}
|
2023-06-24 23:10:47 +02:00
|
|
|
global_parser = flexmock()
|
|
|
|
global_parser.should_receive('parse_known_args').and_return((global_namespace, ()))
|
2019-06-24 01:06:39 +02:00
|
|
|
|
2023-06-24 23:10:47 +02:00
|
|
|
arguments, remaining_action_arguments = module.parse_arguments_for_actions(
|
|
|
|
('--progress'), action_parsers, global_parser
|
2023-06-20 01:18:47 +02:00
|
|
|
)
|
2019-06-24 01:06:39 +02:00
|
|
|
|
2023-06-20 01:18:47 +02:00
|
|
|
assert arguments == namespaces
|
2023-06-24 23:10:47 +02:00
|
|
|
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()
|
|
|
|
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'), ())
|
2019-06-24 01:06:39 +02:00
|
|
|
|
|
|
|
|
2023-06-20 01:18:47 +02:00
|
|
|
def test_parse_arguments_for_actions_passes_through_unknown_arguments_before_action_name():
|
2019-06-24 01:06:39 +02:00
|
|
|
action_namespace = flexmock()
|
2023-06-20 01:18:47 +02:00
|
|
|
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}
|
|
|
|
)
|
2023-06-24 23:10:47 +02:00
|
|
|
or ('--wtf', 'yes')
|
|
|
|
)
|
2023-06-20 01:18:47 +02:00
|
|
|
flexmock(module).should_receive('get_subactions_for_actions').and_return({})
|
|
|
|
action_parsers = {
|
|
|
|
'action': flexmock(),
|
2021-06-18 05:41:44 +02:00
|
|
|
'other': flexmock(),
|
|
|
|
}
|
2023-06-24 23:10:47 +02:00
|
|
|
global_namespace = flexmock()
|
|
|
|
global_parser = flexmock()
|
|
|
|
global_parser.should_receive('parse_known_args').and_return((global_namespace, ()))
|
2019-06-24 01:06:39 +02:00
|
|
|
|
2023-06-24 23:10:47 +02:00
|
|
|
arguments, remaining_action_arguments = module.parse_arguments_for_actions(
|
|
|
|
('--wtf', 'yes', 'action'), action_parsers, global_parser
|
2019-06-24 01:06:39 +02:00
|
|
|
)
|
|
|
|
|
2023-06-24 23:10:47 +02:00
|
|
|
assert arguments == {'global': global_namespace, 'action': action_namespace}
|
|
|
|
assert remaining_action_arguments == (('--wtf', 'yes'), ())
|
2019-09-20 20:43:27 +02:00
|
|
|
|
|
|
|
|
2023-06-20 01:18:47 +02:00
|
|
|
def test_parse_arguments_for_actions_passes_through_unknown_arguments_after_action_name():
|
2019-09-20 20:43:27 +02:00
|
|
|
action_namespace = flexmock()
|
2023-06-20 01:18:47 +02:00
|
|
|
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}
|
|
|
|
)
|
2023-06-24 23:10:47 +02:00
|
|
|
or ('--wtf', 'yes')
|
|
|
|
)
|
2023-06-20 01:18:47 +02:00
|
|
|
flexmock(module).should_receive('get_subactions_for_actions').and_return({})
|
|
|
|
action_parsers = {
|
|
|
|
'action': flexmock(),
|
2021-06-18 05:41:44 +02:00
|
|
|
'other': flexmock(),
|
|
|
|
}
|
2023-06-24 23:10:47 +02:00
|
|
|
global_namespace = flexmock()
|
|
|
|
global_parser = flexmock()
|
|
|
|
global_parser.should_receive('parse_known_args').and_return((global_namespace, ()))
|
2019-09-20 20:43:27 +02:00
|
|
|
|
2023-06-24 23:10:47 +02:00
|
|
|
arguments, remaining_action_arguments = module.parse_arguments_for_actions(
|
|
|
|
('action', '--wtf', 'yes'), action_parsers, global_parser
|
2019-09-20 20:43:27 +02:00
|
|
|
)
|
|
|
|
|
2023-06-24 23:10:47 +02:00
|
|
|
assert arguments == {'global': global_namespace, 'action': action_namespace}
|
|
|
|
assert remaining_action_arguments == (('--wtf', 'yes'), ())
|
2019-09-20 20:43:27 +02:00
|
|
|
|
|
|
|
|
2023-06-20 01:18:47 +02:00
|
|
|
def test_parse_arguments_for_actions_with_borg_action_skips_other_action_parsers():
|
2021-06-18 05:41:44 +02:00
|
|
|
action_namespace = flexmock(options=[])
|
2023-06-20 01:18:47 +02:00
|
|
|
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}
|
|
|
|
)
|
2023-06-24 23:10:47 +02:00
|
|
|
or ()
|
2023-06-20 01:18:47 +02:00
|
|
|
).and_return(())
|
|
|
|
flexmock(module).should_receive('get_subactions_for_actions').and_return({})
|
|
|
|
action_parsers = {
|
|
|
|
'borg': flexmock(),
|
2021-06-18 05:41:44 +02:00
|
|
|
'list': flexmock(),
|
|
|
|
}
|
2023-06-24 23:10:47 +02:00
|
|
|
global_namespace = flexmock()
|
|
|
|
global_parser = flexmock()
|
|
|
|
global_parser.should_receive('parse_known_args').and_return((global_namespace, ()))
|
2019-09-20 20:43:27 +02:00
|
|
|
|
2023-06-24 23:10:47 +02:00
|
|
|
arguments, remaining_action_arguments = module.parse_arguments_for_actions(
|
|
|
|
('borg', 'list'), action_parsers, global_parser
|
2023-06-20 01:18:47 +02:00
|
|
|
)
|
2019-09-20 20:43:27 +02:00
|
|
|
|
2023-06-24 23:10:47 +02:00
|
|
|
assert arguments == {'global': global_namespace, 'borg': action_namespace}
|
|
|
|
assert remaining_action_arguments == ((), ())
|
2023-06-06 21:26:19 +02:00
|
|
|
|
|
|
|
|
2023-06-20 01:18:47 +02:00
|
|
|
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()}
|
2023-06-24 23:10:47 +02:00
|
|
|
global_parser = flexmock()
|
|
|
|
global_parser.should_receive('parse_known_args').and_return((flexmock(), ()))
|
2023-06-06 21:26:19 +02:00
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
2023-06-24 23:10:47 +02:00
|
|
|
module.parse_arguments_for_actions(('config',), action_parsers, global_parser)
|