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
|
|
|
|
|
|
|
|
|
|
|
|
def test_parse_subparser_arguments_consumes_subparser_arguments_before_subparser_name():
|
|
|
|
action_namespace = flexmock(foo=True)
|
2021-06-18 05:41:44 +02:00
|
|
|
subparsers = {
|
|
|
|
'action': flexmock(parse_known_args=lambda arguments: (action_namespace, ['action'])),
|
|
|
|
'other': flexmock(),
|
|
|
|
}
|
2019-06-24 01:06:39 +02:00
|
|
|
|
2021-06-18 05:41:44 +02:00
|
|
|
arguments, remaining_arguments = module.parse_subparser_arguments(
|
|
|
|
('--foo', 'true', 'action'), subparsers
|
|
|
|
)
|
2019-06-24 01:06:39 +02:00
|
|
|
|
2019-09-20 20:43:27 +02:00
|
|
|
assert arguments == {'action': action_namespace}
|
2021-06-18 05:41:44 +02:00
|
|
|
assert remaining_arguments == []
|
2019-06-24 01:06:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_parse_subparser_arguments_consumes_subparser_arguments_after_subparser_name():
|
|
|
|
action_namespace = flexmock(foo=True)
|
2021-06-18 05:41:44 +02:00
|
|
|
subparsers = {
|
|
|
|
'action': flexmock(parse_known_args=lambda arguments: (action_namespace, ['action'])),
|
|
|
|
'other': flexmock(),
|
|
|
|
}
|
2019-06-24 01:06:39 +02:00
|
|
|
|
2021-06-18 05:41:44 +02:00
|
|
|
arguments, remaining_arguments = module.parse_subparser_arguments(
|
|
|
|
('action', '--foo', 'true'), subparsers
|
|
|
|
)
|
2019-06-24 01:06:39 +02:00
|
|
|
|
2019-09-20 20:43:27 +02:00
|
|
|
assert arguments == {'action': action_namespace}
|
2021-06-18 05:41:44 +02:00
|
|
|
assert remaining_arguments == []
|
2019-06-24 01:06:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_parse_subparser_arguments_consumes_subparser_arguments_with_alias():
|
|
|
|
action_namespace = flexmock(foo=True)
|
2021-06-18 05:41:44 +02:00
|
|
|
action_subparser = flexmock(parse_known_args=lambda arguments: (action_namespace, ['action']))
|
|
|
|
subparsers = {
|
|
|
|
'action': action_subparser,
|
|
|
|
'-a': action_subparser,
|
|
|
|
'other': flexmock(),
|
|
|
|
'-o': flexmock(),
|
|
|
|
}
|
2019-06-24 01:06:39 +02:00
|
|
|
flexmock(module).SUBPARSER_ALIASES = {'action': ['-a'], 'other': ['-o']}
|
|
|
|
|
2021-06-18 05:41:44 +02:00
|
|
|
arguments, remaining_arguments = module.parse_subparser_arguments(
|
|
|
|
('-a', '--foo', 'true'), subparsers
|
|
|
|
)
|
2019-06-24 01:06:39 +02:00
|
|
|
|
2019-09-20 20:43:27 +02:00
|
|
|
assert arguments == {'action': action_namespace}
|
2021-06-18 05:41:44 +02:00
|
|
|
assert remaining_arguments == []
|
2019-06-24 01:06:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_parse_subparser_arguments_consumes_multiple_subparser_arguments():
|
|
|
|
action_namespace = flexmock(foo=True)
|
|
|
|
other_namespace = flexmock(bar=3)
|
2021-06-18 05:41:44 +02:00
|
|
|
subparsers = {
|
|
|
|
'action': flexmock(
|
|
|
|
parse_known_args=lambda arguments: (action_namespace, ['action', '--bar', '3'])
|
|
|
|
),
|
|
|
|
'other': flexmock(parse_known_args=lambda arguments: (other_namespace, [])),
|
|
|
|
}
|
2019-06-24 01:06:39 +02:00
|
|
|
|
2021-06-18 05:41:44 +02:00
|
|
|
arguments, remaining_arguments = module.parse_subparser_arguments(
|
2019-09-20 20:43:27 +02:00
|
|
|
('action', '--foo', 'true', 'other', '--bar', '3'), subparsers
|
2019-06-24 01:06:39 +02:00
|
|
|
)
|
|
|
|
|
2019-09-20 20:43:27 +02:00
|
|
|
assert arguments == {'action': action_namespace, 'other': other_namespace}
|
2021-06-18 05:41:44 +02:00
|
|
|
assert remaining_arguments == []
|
2019-06-24 01:06:39 +02:00
|
|
|
|
|
|
|
|
2023-03-08 22:19:41 +01:00
|
|
|
def test_parse_subparser_arguments_respects_command_line_action_ordering():
|
|
|
|
other_namespace = flexmock()
|
|
|
|
action_namespace = flexmock(foo=True)
|
|
|
|
subparsers = {
|
|
|
|
'action': flexmock(
|
|
|
|
parse_known_args=lambda arguments: (action_namespace, ['action', '--foo', 'true'])
|
|
|
|
),
|
|
|
|
'other': flexmock(parse_known_args=lambda arguments: (other_namespace, ['other'])),
|
|
|
|
}
|
|
|
|
|
|
|
|
arguments, remaining_arguments = module.parse_subparser_arguments(
|
|
|
|
('other', '--foo', 'true', 'action'), subparsers
|
|
|
|
)
|
|
|
|
|
|
|
|
assert arguments == collections.OrderedDict(
|
|
|
|
[('other', other_namespace), ('action', action_namespace)]
|
|
|
|
)
|
|
|
|
assert remaining_arguments == []
|
|
|
|
|
|
|
|
|
2019-06-24 01:06:39 +02:00
|
|
|
def test_parse_subparser_arguments_applies_default_subparsers():
|
|
|
|
prune_namespace = flexmock()
|
2022-02-09 23:33:12 +01:00
|
|
|
compact_namespace = flexmock()
|
2019-06-24 01:06:39 +02:00
|
|
|
create_namespace = flexmock(progress=True)
|
|
|
|
check_namespace = flexmock()
|
2021-06-18 05:41:44 +02:00
|
|
|
subparsers = {
|
|
|
|
'prune': flexmock(
|
|
|
|
parse_known_args=lambda arguments: (prune_namespace, ['prune', '--progress'])
|
|
|
|
),
|
2022-02-09 23:33:12 +01:00
|
|
|
'compact': flexmock(parse_known_args=lambda arguments: (compact_namespace, [])),
|
2021-06-18 05:41:44 +02:00
|
|
|
'create': flexmock(parse_known_args=lambda arguments: (create_namespace, [])),
|
|
|
|
'check': flexmock(parse_known_args=lambda arguments: (check_namespace, [])),
|
|
|
|
'other': flexmock(),
|
|
|
|
}
|
2019-06-24 01:06:39 +02:00
|
|
|
|
2021-06-18 05:41:44 +02:00
|
|
|
arguments, remaining_arguments = module.parse_subparser_arguments(('--progress'), subparsers)
|
2019-06-24 01:06:39 +02:00
|
|
|
|
|
|
|
assert arguments == {
|
|
|
|
'prune': prune_namespace,
|
2022-02-09 23:33:12 +01:00
|
|
|
'compact': compact_namespace,
|
2019-06-24 01:06:39 +02:00
|
|
|
'create': create_namespace,
|
|
|
|
'check': check_namespace,
|
|
|
|
}
|
2021-06-18 05:41:44 +02:00
|
|
|
assert remaining_arguments == []
|
2019-06-24 01:06:39 +02:00
|
|
|
|
|
|
|
|
2021-06-18 05:41:44 +02:00
|
|
|
def test_parse_subparser_arguments_passes_through_unknown_arguments_before_subparser_name():
|
2019-06-24 01:06:39 +02:00
|
|
|
action_namespace = flexmock()
|
2021-06-18 05:41:44 +02:00
|
|
|
subparsers = {
|
|
|
|
'action': flexmock(
|
|
|
|
parse_known_args=lambda arguments: (action_namespace, ['action', '--verbosity', 'lots'])
|
|
|
|
),
|
|
|
|
'other': flexmock(),
|
|
|
|
}
|
2019-06-24 01:06:39 +02:00
|
|
|
|
2021-06-18 05:41:44 +02:00
|
|
|
arguments, remaining_arguments = module.parse_subparser_arguments(
|
|
|
|
('--verbosity', 'lots', 'action'), subparsers
|
2019-06-24 01:06:39 +02:00
|
|
|
)
|
|
|
|
|
2021-06-18 05:41:44 +02:00
|
|
|
assert arguments == {'action': action_namespace}
|
|
|
|
assert remaining_arguments == ['--verbosity', 'lots']
|
2019-09-20 20:43:27 +02:00
|
|
|
|
|
|
|
|
2021-06-18 05:41:44 +02:00
|
|
|
def test_parse_subparser_arguments_passes_through_unknown_arguments_after_subparser_name():
|
2019-09-20 20:43:27 +02:00
|
|
|
action_namespace = flexmock()
|
2021-06-18 05:41:44 +02:00
|
|
|
subparsers = {
|
|
|
|
'action': flexmock(
|
|
|
|
parse_known_args=lambda arguments: (action_namespace, ['action', '--verbosity', 'lots'])
|
|
|
|
),
|
|
|
|
'other': flexmock(),
|
|
|
|
}
|
2019-09-20 20:43:27 +02:00
|
|
|
|
2021-06-18 05:41:44 +02:00
|
|
|
arguments, remaining_arguments = module.parse_subparser_arguments(
|
|
|
|
('action', '--verbosity', 'lots'), subparsers
|
2019-09-20 20:43:27 +02:00
|
|
|
)
|
|
|
|
|
2021-06-18 05:41:44 +02:00
|
|
|
assert arguments == {'action': action_namespace}
|
|
|
|
assert remaining_arguments == ['--verbosity', 'lots']
|
2019-09-20 20:43:27 +02:00
|
|
|
|
|
|
|
|
2021-06-18 05:41:44 +02:00
|
|
|
def test_parse_subparser_arguments_parses_borg_options_and_skips_other_subparsers():
|
|
|
|
action_namespace = flexmock(options=[])
|
|
|
|
subparsers = {
|
|
|
|
'borg': flexmock(parse_known_args=lambda arguments: (action_namespace, ['borg', 'list'])),
|
|
|
|
'list': flexmock(),
|
|
|
|
}
|
2019-09-20 20:43:27 +02:00
|
|
|
|
2021-06-18 05:41:44 +02:00
|
|
|
arguments, remaining_arguments = module.parse_subparser_arguments(('borg', 'list'), subparsers)
|
2019-09-20 20:43:27 +02:00
|
|
|
|
2021-06-18 05:41:44 +02:00
|
|
|
assert arguments == {'borg': action_namespace}
|
|
|
|
assert arguments['borg'].options == ['list']
|
|
|
|
assert remaining_arguments == []
|
2023-06-06 21:26:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_parse_subparser_arguments_raises_error_when_no_subparser_is_specified():
|
|
|
|
action_namespace = flexmock(options=[])
|
|
|
|
subparsers = {
|
|
|
|
'config': flexmock(parse_known_args=lambda arguments: (action_namespace, ['config'])),
|
|
|
|
}
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
module.parse_subparser_arguments(('config',), subparsers)
|