test_arguments.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. import collections
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic.commands import arguments as module
  5. def test_get_subaction_parsers_with_no_subactions_returns_empty_result():
  6. assert module.get_subaction_parsers(flexmock(_subparsers=None)) == {}
  7. def test_get_subaction_parsers_with_subactions_returns_one_entry_per_subaction():
  8. foo_parser = flexmock()
  9. bar_parser = flexmock()
  10. baz_parser = flexmock()
  11. assert module.get_subaction_parsers(
  12. flexmock(
  13. _subparsers=flexmock(
  14. _group_actions=(
  15. flexmock(choices={'foo': foo_parser, 'bar': bar_parser}),
  16. flexmock(choices={'baz': baz_parser}),
  17. )
  18. )
  19. )
  20. ) == {'foo': foo_parser, 'bar': bar_parser, 'baz': baz_parser}
  21. def test_get_subactions_for_actions_with_no_subactions_returns_empty_result():
  22. assert module.get_subactions_for_actions({'action': flexmock(_subparsers=None)}) == {}
  23. def test_get_subactions_for_actions_with_subactions_returns_one_entry_per_action():
  24. assert module.get_subactions_for_actions(
  25. {
  26. 'action': flexmock(
  27. _subparsers=flexmock(
  28. _group_actions=(
  29. flexmock(choices={'foo': flexmock(), 'bar': flexmock()}),
  30. flexmock(choices={'baz': flexmock()}),
  31. )
  32. )
  33. ),
  34. 'other': flexmock(
  35. _subparsers=flexmock(_group_actions=(flexmock(choices={'quux': flexmock()}),))
  36. ),
  37. }
  38. ) == {'action': ('foo', 'bar', 'baz'), 'other': ('quux',)}
  39. def test_omit_values_colliding_with_action_names_drops_action_names_that_have__been_parsed_as_values():
  40. assert module.omit_values_colliding_with_action_names(
  41. ('check', '--only', 'extract', '--some-list', 'borg'),
  42. {'check': flexmock(only='extract', some_list=['borg'])},
  43. ) == ('check', '--only', '--some-list')
  44. def test_parse_and_record_action_arguments_without_action_name_leaves_arguments_untouched():
  45. unparsed_arguments = ('--foo', '--bar')
  46. flexmock(module).should_receive('omit_values_colliding_with_action_names').and_return(
  47. unparsed_arguments
  48. )
  49. assert (
  50. module.parse_and_record_action_arguments(
  51. unparsed_arguments, flexmock(), flexmock(), 'action'
  52. )
  53. == unparsed_arguments
  54. )
  55. def test_parse_and_record_action_arguments_updates_parsed_arguments_and_returns_remaining():
  56. unparsed_arguments = ('action', '--foo', '--bar', '--verbosity', '1')
  57. other_parsed_arguments = flexmock()
  58. parsed_arguments = {'other': other_parsed_arguments}
  59. action_parsed_arguments = flexmock()
  60. flexmock(module).should_receive('omit_values_colliding_with_action_names').and_return(
  61. unparsed_arguments
  62. )
  63. action_parser = flexmock()
  64. flexmock(action_parser).should_receive('parse_known_args').and_return(
  65. action_parsed_arguments, ('action', '--verbosity', '1')
  66. )
  67. assert module.parse_and_record_action_arguments(
  68. unparsed_arguments, parsed_arguments, action_parser, 'action'
  69. ) == ('--verbosity', '1')
  70. assert parsed_arguments == {'other': other_parsed_arguments, 'action': action_parsed_arguments}
  71. def test_parse_and_record_action_arguments_with_alias_updates_canonical_parsed_arguments():
  72. unparsed_arguments = ('action', '--foo', '--bar', '--verbosity', '1')
  73. other_parsed_arguments = flexmock()
  74. parsed_arguments = {'other': other_parsed_arguments}
  75. action_parsed_arguments = flexmock()
  76. flexmock(module).should_receive('omit_values_colliding_with_action_names').and_return(
  77. unparsed_arguments
  78. )
  79. action_parser = flexmock()
  80. flexmock(action_parser).should_receive('parse_known_args').and_return(
  81. action_parsed_arguments, ('action', '--verbosity', '1')
  82. )
  83. assert module.parse_and_record_action_arguments(
  84. unparsed_arguments, parsed_arguments, action_parser, 'action', canonical_name='doit'
  85. ) == ('--verbosity', '1')
  86. assert parsed_arguments == {'other': other_parsed_arguments, 'doit': action_parsed_arguments}
  87. def test_parse_and_record_action_arguments_with_borg_action_consumes_arguments_after_action_name():
  88. unparsed_arguments = ('--verbosity', '1', 'borg', 'list')
  89. parsed_arguments = {}
  90. borg_parsed_arguments = flexmock(options=flexmock())
  91. flexmock(module).should_receive('omit_values_colliding_with_action_names').and_return(
  92. unparsed_arguments
  93. )
  94. borg_parser = flexmock()
  95. flexmock(borg_parser).should_receive('parse_known_args').and_return(
  96. borg_parsed_arguments, ('--verbosity', '1', 'borg', 'list')
  97. )
  98. assert module.parse_and_record_action_arguments(
  99. unparsed_arguments,
  100. parsed_arguments,
  101. borg_parser,
  102. 'borg',
  103. ) == ('--verbosity', '1')
  104. assert parsed_arguments == {'borg': borg_parsed_arguments}
  105. assert borg_parsed_arguments.options == ('list',)
  106. @pytest.mark.parametrize(
  107. 'arguments, expected',
  108. [
  109. (
  110. (
  111. ('--latest', 'archive', 'prune', 'extract', 'list', '--test-flag'),
  112. ('--latest', 'archive', 'check', 'extract', 'list', '--test-flag'),
  113. ('prune', 'check', 'list', '--test-flag'),
  114. ('prune', 'check', 'extract', '--test-flag'),
  115. ),
  116. ('--test-flag',),
  117. ),
  118. (
  119. (
  120. ('--latest', 'archive', 'prune', 'extract', 'list'),
  121. ('--latest', 'archive', 'check', 'extract', 'list'),
  122. ('prune', 'check', 'list'),
  123. ('prune', 'check', 'extract'),
  124. ),
  125. (),
  126. ),
  127. ((), ()),
  128. ],
  129. )
  130. def test_get_unparsable_arguments_returns_remaining_arguments_that_no_action_can_parse(
  131. arguments, expected
  132. ):
  133. assert module.get_unparsable_arguments(arguments) == expected
  134. def test_parse_arguments_for_actions_consumes_action_arguments_before_action_name():
  135. action_namespace = flexmock(foo=True)
  136. flexmock(module).should_receive('get_subaction_parsers').and_return({})
  137. flexmock(module).should_receive('parse_and_record_action_arguments').replace_with(
  138. lambda unparsed, parsed, parser, action, canonical=None: parsed.update(
  139. {action: action_namespace}
  140. )
  141. ).and_return(())
  142. flexmock(module).should_receive('get_subactions_for_actions').and_return({})
  143. flexmock(module).should_receive('get_unparsable_arguments').and_return(())
  144. action_parsers = {'action': flexmock(), 'other': flexmock()}
  145. arguments, remaining_arguments = module.parse_arguments_for_actions(
  146. ('--foo', 'true', 'action'), action_parsers
  147. )
  148. assert arguments == {'action': action_namespace}
  149. assert remaining_arguments == ()
  150. def test_parse_arguments_for_actions_consumes_action_arguments_after_action_name():
  151. action_namespace = flexmock(foo=True)
  152. flexmock(module).should_receive('get_subaction_parsers').and_return({})
  153. flexmock(module).should_receive('parse_and_record_action_arguments').replace_with(
  154. lambda unparsed, parsed, parser, action, canonical=None: parsed.update(
  155. {action: action_namespace}
  156. )
  157. ).and_return(())
  158. flexmock(module).should_receive('get_subactions_for_actions').and_return({})
  159. flexmock(module).should_receive('get_unparsable_arguments').and_return(())
  160. action_parsers = {'action': flexmock(), 'other': flexmock()}
  161. arguments, remaining_arguments = module.parse_arguments_for_actions(
  162. ('action', '--foo', 'true'), action_parsers
  163. )
  164. assert arguments == {'action': action_namespace}
  165. assert remaining_arguments == ()
  166. def test_parse_arguments_for_actions_consumes_action_arguments_with_alias():
  167. action_namespace = flexmock(foo=True)
  168. flexmock(module).should_receive('get_subaction_parsers').and_return({})
  169. flexmock(module).should_receive('parse_and_record_action_arguments').replace_with(
  170. lambda unparsed, parsed, parser, action, canonical=None: parsed.update(
  171. {canonical or action: action_namespace}
  172. )
  173. ).and_return(())
  174. flexmock(module).should_receive('get_subactions_for_actions').and_return({})
  175. flexmock(module).should_receive('get_unparsable_arguments').and_return(())
  176. action_parsers = {
  177. 'action': flexmock(),
  178. '-a': flexmock(),
  179. 'other': flexmock(),
  180. '-o': flexmock(),
  181. }
  182. flexmock(module).ACTION_ALIASES = {'action': ['-a'], 'other': ['-o']}
  183. arguments, remaining_arguments = module.parse_arguments_for_actions(
  184. ('-a', '--foo', 'true'), action_parsers
  185. )
  186. assert arguments == {'action': action_namespace}
  187. assert remaining_arguments == ()
  188. def test_parse_arguments_for_actions_consumes_multiple_action_arguments():
  189. action_namespace = flexmock(foo=True)
  190. other_namespace = flexmock(bar=3)
  191. flexmock(module).should_receive('get_subaction_parsers').and_return({})
  192. flexmock(module).should_receive('parse_and_record_action_arguments').replace_with(
  193. lambda unparsed, parsed, parser, action, canonical=None: parsed.update(
  194. {action: action_namespace if action == 'action' else other_namespace}
  195. )
  196. ).and_return(('other', '--bar', '3')).and_return('action', '--foo', 'true')
  197. flexmock(module).should_receive('get_subactions_for_actions').and_return({})
  198. flexmock(module).should_receive('get_unparsable_arguments').and_return(())
  199. action_parsers = {
  200. 'action': flexmock(),
  201. 'other': flexmock(),
  202. }
  203. arguments, remaining_arguments = module.parse_arguments_for_actions(
  204. ('action', '--foo', 'true', 'other', '--bar', '3'), action_parsers
  205. )
  206. assert arguments == {'action': action_namespace, 'other': other_namespace}
  207. assert remaining_arguments == ()
  208. def test_parse_arguments_for_actions_respects_command_line_action_ordering():
  209. other_namespace = flexmock()
  210. action_namespace = flexmock(foo=True)
  211. flexmock(module).should_receive('get_subaction_parsers').and_return({})
  212. flexmock(module).should_receive('parse_and_record_action_arguments').replace_with(
  213. lambda unparsed, parsed, parser, action, canonical=None: parsed.update(
  214. {action: other_namespace if action == 'other' else action_namespace}
  215. )
  216. ).and_return(('action',)).and_return(('other', '--foo', 'true'))
  217. flexmock(module).should_receive('get_subactions_for_actions').and_return({})
  218. flexmock(module).should_receive('get_unparsable_arguments').and_return(())
  219. action_parsers = {
  220. 'action': flexmock(),
  221. 'other': flexmock(),
  222. }
  223. arguments, remaining_arguments = module.parse_arguments_for_actions(
  224. ('other', '--foo', 'true', 'action'), action_parsers
  225. )
  226. assert arguments == collections.OrderedDict(
  227. [('other', other_namespace), ('action', action_namespace)]
  228. )
  229. assert remaining_arguments == ()
  230. def test_parse_arguments_for_actions_applies_default_action_parsers():
  231. namespaces = {
  232. 'prune': flexmock(),
  233. 'compact': flexmock(),
  234. 'create': flexmock(progress=True),
  235. 'check': flexmock(),
  236. }
  237. flexmock(module).should_receive('get_subaction_parsers').and_return({})
  238. flexmock(module).should_receive('parse_and_record_action_arguments').replace_with(
  239. lambda unparsed, parsed, parser, action, canonical=None: parsed.update(
  240. {action: namespaces.get(action)}
  241. )
  242. ).and_return(())
  243. flexmock(module).should_receive('get_subactions_for_actions').and_return({})
  244. flexmock(module).should_receive('get_unparsable_arguments').and_return(())
  245. action_parsers = {
  246. 'prune': flexmock(),
  247. 'compact': flexmock(),
  248. 'create': flexmock(),
  249. 'check': flexmock(),
  250. 'other': flexmock(),
  251. }
  252. arguments, remaining_arguments = module.parse_arguments_for_actions(
  253. ('--progress'), action_parsers
  254. )
  255. assert arguments == namespaces
  256. assert remaining_arguments == ()
  257. def test_parse_arguments_for_actions_passes_through_unknown_arguments_before_action_name():
  258. action_namespace = flexmock()
  259. flexmock(module).should_receive('get_subaction_parsers').and_return({})
  260. flexmock(module).should_receive('parse_and_record_action_arguments').replace_with(
  261. lambda unparsed, parsed, parser, action, canonical=None: parsed.update(
  262. {action: action_namespace}
  263. )
  264. ).and_return(('--verbosity', 'lots'))
  265. flexmock(module).should_receive('get_subactions_for_actions').and_return({})
  266. flexmock(module).should_receive('get_unparsable_arguments').and_return(('--verbosity', 'lots'))
  267. action_parsers = {
  268. 'action': flexmock(),
  269. 'other': flexmock(),
  270. }
  271. arguments, remaining_arguments = module.parse_arguments_for_actions(
  272. ('--verbosity', 'lots', 'action'), action_parsers
  273. )
  274. assert arguments == {'action': action_namespace}
  275. assert remaining_arguments == ('--verbosity', 'lots')
  276. def test_parse_arguments_for_actions_passes_through_unknown_arguments_after_action_name():
  277. action_namespace = flexmock()
  278. flexmock(module).should_receive('get_subaction_parsers').and_return({})
  279. flexmock(module).should_receive('parse_and_record_action_arguments').replace_with(
  280. lambda unparsed, parsed, parser, action, canonical=None: parsed.update(
  281. {action: action_namespace}
  282. )
  283. ).and_return(('--verbosity', 'lots'))
  284. flexmock(module).should_receive('get_subactions_for_actions').and_return({})
  285. flexmock(module).should_receive('get_unparsable_arguments').and_return(('--verbosity', 'lots'))
  286. action_parsers = {
  287. 'action': flexmock(),
  288. 'other': flexmock(),
  289. }
  290. arguments, remaining_arguments = module.parse_arguments_for_actions(
  291. ('action', '--verbosity', 'lots'), action_parsers
  292. )
  293. assert arguments == {'action': action_namespace}
  294. assert remaining_arguments == ('--verbosity', 'lots')
  295. def test_parse_arguments_for_actions_with_borg_action_skips_other_action_parsers():
  296. action_namespace = flexmock(options=[])
  297. flexmock(module).should_receive('get_subaction_parsers').and_return({})
  298. flexmock(module).should_receive('parse_and_record_action_arguments').replace_with(
  299. lambda unparsed, parsed, parser, action, canonical=None: parsed.update(
  300. {action: action_namespace}
  301. )
  302. ).and_return(())
  303. flexmock(module).should_receive('get_subactions_for_actions').and_return({})
  304. flexmock(module).should_receive('get_unparsable_arguments').and_return(())
  305. action_parsers = {
  306. 'borg': flexmock(),
  307. 'list': flexmock(),
  308. }
  309. arguments, remaining_arguments = module.parse_arguments_for_actions(
  310. ('borg', 'list'), action_parsers
  311. )
  312. assert arguments == {'borg': action_namespace}
  313. assert remaining_arguments == ()
  314. def test_parse_arguments_for_actions_raises_error_when_no_action_is_specified():
  315. flexmock(module).should_receive('get_subaction_parsers').and_return({'bootstrap': [flexmock()]})
  316. flexmock(module).should_receive('parse_and_record_action_arguments').and_return(flexmock())
  317. flexmock(module).should_receive('get_subactions_for_actions').and_return(
  318. {'config': ['bootstrap']}
  319. )
  320. action_parsers = {'config': flexmock()}
  321. with pytest.raises(ValueError):
  322. module.parse_arguments_for_actions(('config',), action_parsers)