test_arguments.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. # A global flag remaining from each parsed action.
  110. (
  111. (
  112. ('--latest', 'archive', 'prune', 'extract', 'list', '--test-flag'),
  113. ('--latest', 'archive', 'check', 'extract', 'list', '--test-flag'),
  114. ('prune', 'check', 'list', '--test-flag'),
  115. ('prune', 'check', 'extract', '--test-flag'),
  116. ),
  117. ('--test-flag',),
  118. ),
  119. # No global flags remaining.
  120. (
  121. (
  122. ('--latest', 'archive', 'prune', 'extract', 'list'),
  123. ('--latest', 'archive', 'check', 'extract', 'list'),
  124. ('prune', 'check', 'list'),
  125. ('prune', 'check', 'extract'),
  126. ),
  127. (),
  128. ),
  129. # No flags.
  130. ((), ()),
  131. ],
  132. )
  133. def test_get_unparsable_arguments_returns_remaining_arguments_that_no_action_can_parse(
  134. arguments, expected
  135. ):
  136. assert module.get_unparsable_arguments(arguments) == expected
  137. def test_parse_arguments_for_actions_consumes_action_arguments_after_action_name():
  138. action_namespace = flexmock(foo=True)
  139. remaining = flexmock()
  140. flexmock(module).should_receive('get_subaction_parsers').and_return({})
  141. flexmock(module).should_receive('parse_and_record_action_arguments').replace_with(
  142. lambda unparsed, parsed, parser, action, canonical=None: parsed.update(
  143. {action: action_namespace}
  144. )
  145. or remaining
  146. )
  147. flexmock(module).should_receive('get_subactions_for_actions').and_return({})
  148. action_parsers = {'action': flexmock(), 'other': flexmock()}
  149. global_namespace = flexmock(config_paths=[])
  150. global_parser = flexmock()
  151. global_parser.should_receive('parse_known_args').and_return((global_namespace, ()))
  152. arguments, remaining_action_arguments = module.parse_arguments_for_actions(
  153. ('action', '--foo', 'true'), action_parsers, global_parser
  154. )
  155. assert arguments == {'global': global_namespace, 'action': action_namespace}
  156. assert remaining_action_arguments == (remaining, ())
  157. def test_parse_arguments_for_actions_consumes_action_arguments_with_alias():
  158. action_namespace = flexmock(foo=True)
  159. remaining = flexmock()
  160. flexmock(module).should_receive('get_subaction_parsers').and_return({})
  161. flexmock(module).should_receive('parse_and_record_action_arguments').replace_with(
  162. lambda unparsed, parsed, parser, action, canonical=None: parsed.update(
  163. {canonical or action: action_namespace}
  164. )
  165. or remaining
  166. )
  167. flexmock(module).should_receive('get_subactions_for_actions').and_return({})
  168. action_parsers = {
  169. 'action': flexmock(),
  170. '-a': flexmock(),
  171. 'other': flexmock(),
  172. '-o': flexmock(),
  173. }
  174. global_namespace = flexmock(config_paths=[])
  175. global_parser = flexmock()
  176. global_parser.should_receive('parse_known_args').and_return((global_namespace, ()))
  177. flexmock(module).ACTION_ALIASES = {'action': ['-a'], 'other': ['-o']}
  178. arguments, remaining_action_arguments = module.parse_arguments_for_actions(
  179. ('-a', '--foo', 'true'), action_parsers, global_parser
  180. )
  181. assert arguments == {'global': global_namespace, 'action': action_namespace}
  182. assert remaining_action_arguments == (remaining, ())
  183. def test_parse_arguments_for_actions_consumes_multiple_action_arguments():
  184. action_namespace = flexmock(foo=True)
  185. other_namespace = flexmock(bar=3)
  186. flexmock(module).should_receive('get_subaction_parsers').and_return({})
  187. flexmock(module).should_receive('parse_and_record_action_arguments').replace_with(
  188. lambda unparsed, parsed, parser, action, canonical=None: parsed.update(
  189. {action: action_namespace if action == 'action' else other_namespace}
  190. )
  191. or ()
  192. ).and_return(('other', '--bar', '3')).and_return('action', '--foo', 'true')
  193. flexmock(module).should_receive('get_subactions_for_actions').and_return({})
  194. action_parsers = {
  195. 'action': flexmock(),
  196. 'other': flexmock(),
  197. }
  198. global_namespace = flexmock(config_paths=[])
  199. global_parser = flexmock()
  200. global_parser.should_receive('parse_known_args').and_return((global_namespace, ()))
  201. arguments, remaining_action_arguments = module.parse_arguments_for_actions(
  202. ('action', '--foo', 'true', 'other', '--bar', '3'), action_parsers, global_parser
  203. )
  204. assert arguments == {
  205. 'global': global_namespace,
  206. 'action': action_namespace,
  207. 'other': other_namespace,
  208. }
  209. assert remaining_action_arguments == ((), (), ())
  210. def test_parse_arguments_for_actions_respects_command_line_action_ordering():
  211. other_namespace = flexmock()
  212. action_namespace = flexmock(foo=True)
  213. flexmock(module).should_receive('get_subaction_parsers').and_return({})
  214. flexmock(module).should_receive('parse_and_record_action_arguments').replace_with(
  215. lambda unparsed, parsed, parser, action, canonical=None: parsed.update(
  216. {action: other_namespace if action == 'other' else action_namespace}
  217. )
  218. or ()
  219. ).and_return(('action',)).and_return(('other', '--foo', 'true'))
  220. flexmock(module).should_receive('get_subactions_for_actions').and_return({})
  221. action_parsers = {
  222. 'action': flexmock(),
  223. 'other': flexmock(),
  224. }
  225. global_namespace = flexmock(config_paths=[])
  226. global_parser = flexmock()
  227. global_parser.should_receive('parse_known_args').and_return((global_namespace, ()))
  228. arguments, remaining_action_arguments = module.parse_arguments_for_actions(
  229. ('other', '--foo', 'true', 'action'), action_parsers, global_parser
  230. )
  231. assert arguments == collections.OrderedDict(
  232. [('other', other_namespace), ('action', action_namespace), ('global', global_namespace)]
  233. )
  234. assert remaining_action_arguments == ((), (), ())
  235. def test_parse_arguments_for_actions_applies_default_action_parsers():
  236. global_namespace = flexmock(config_paths=[])
  237. namespaces = {
  238. 'global': global_namespace,
  239. 'prune': flexmock(),
  240. 'compact': flexmock(),
  241. 'create': flexmock(progress=True),
  242. 'check': flexmock(),
  243. }
  244. flexmock(module).should_receive('get_subaction_parsers').and_return({})
  245. flexmock(module).should_receive('parse_and_record_action_arguments').replace_with(
  246. lambda unparsed, parsed, parser, action, canonical=None: parsed.update(
  247. {action: namespaces.get(action)}
  248. )
  249. or ()
  250. ).and_return(())
  251. flexmock(module).should_receive('get_subactions_for_actions').and_return({})
  252. action_parsers = {
  253. 'prune': flexmock(),
  254. 'compact': flexmock(),
  255. 'create': flexmock(),
  256. 'check': flexmock(),
  257. 'other': flexmock(),
  258. }
  259. global_parser = flexmock()
  260. global_parser.should_receive('parse_known_args').and_return((global_namespace, ()))
  261. arguments, remaining_action_arguments = module.parse_arguments_for_actions(
  262. ('--progress'), action_parsers, global_parser
  263. )
  264. assert arguments == namespaces
  265. assert remaining_action_arguments == ((), (), (), (), ())
  266. def test_parse_arguments_for_actions_consumes_global_arguments():
  267. action_namespace = flexmock()
  268. flexmock(module).should_receive('get_subaction_parsers').and_return({})
  269. flexmock(module).should_receive('parse_and_record_action_arguments').replace_with(
  270. lambda unparsed, parsed, parser, action, canonical=None: parsed.update(
  271. {action: action_namespace}
  272. )
  273. or ('--verbosity', 'lots')
  274. )
  275. flexmock(module).should_receive('get_subactions_for_actions').and_return({})
  276. action_parsers = {
  277. 'action': flexmock(),
  278. 'other': flexmock(),
  279. }
  280. global_namespace = flexmock(config_paths=[])
  281. global_parser = flexmock()
  282. global_parser.should_receive('parse_known_args').and_return((global_namespace, ()))
  283. arguments, remaining_action_arguments = module.parse_arguments_for_actions(
  284. ('action', '--verbosity', 'lots'), action_parsers, global_parser
  285. )
  286. assert arguments == {'global': global_namespace, 'action': action_namespace}
  287. assert remaining_action_arguments == (('--verbosity', 'lots'), ())
  288. def test_parse_arguments_for_actions_passes_through_unknown_arguments_before_action_name():
  289. action_namespace = flexmock()
  290. flexmock(module).should_receive('get_subaction_parsers').and_return({})
  291. flexmock(module).should_receive('parse_and_record_action_arguments').replace_with(
  292. lambda unparsed, parsed, parser, action, canonical=None: parsed.update(
  293. {action: action_namespace}
  294. )
  295. or ('--wtf', 'yes')
  296. )
  297. flexmock(module).should_receive('get_subactions_for_actions').and_return({})
  298. action_parsers = {
  299. 'action': flexmock(),
  300. 'other': flexmock(),
  301. }
  302. global_namespace = flexmock(config_paths=[])
  303. global_parser = flexmock()
  304. global_parser.should_receive('parse_known_args').and_return((global_namespace, ()))
  305. arguments, remaining_action_arguments = module.parse_arguments_for_actions(
  306. ('--wtf', 'yes', 'action'), action_parsers, global_parser
  307. )
  308. assert arguments == {'global': global_namespace, 'action': action_namespace}
  309. assert remaining_action_arguments == (('--wtf', 'yes'), ())
  310. def test_parse_arguments_for_actions_passes_through_unknown_arguments_after_action_name():
  311. action_namespace = flexmock()
  312. flexmock(module).should_receive('get_subaction_parsers').and_return({})
  313. flexmock(module).should_receive('parse_and_record_action_arguments').replace_with(
  314. lambda unparsed, parsed, parser, action, canonical=None: parsed.update(
  315. {action: action_namespace}
  316. )
  317. or ('--wtf', 'yes')
  318. )
  319. flexmock(module).should_receive('get_subactions_for_actions').and_return({})
  320. action_parsers = {
  321. 'action': flexmock(),
  322. 'other': flexmock(),
  323. }
  324. global_namespace = flexmock(config_paths=[])
  325. global_parser = flexmock()
  326. global_parser.should_receive('parse_known_args').and_return((global_namespace, ()))
  327. arguments, remaining_action_arguments = module.parse_arguments_for_actions(
  328. ('action', '--wtf', 'yes'), action_parsers, global_parser
  329. )
  330. assert arguments == {'global': global_namespace, 'action': action_namespace}
  331. assert remaining_action_arguments == (('--wtf', 'yes'), ())
  332. def test_parse_arguments_for_actions_with_borg_action_skips_other_action_parsers():
  333. action_namespace = flexmock(options=[])
  334. flexmock(module).should_receive('get_subaction_parsers').and_return({})
  335. flexmock(module).should_receive('parse_and_record_action_arguments').replace_with(
  336. lambda unparsed, parsed, parser, action, canonical=None: parsed.update(
  337. {action: action_namespace}
  338. )
  339. or ()
  340. ).and_return(())
  341. flexmock(module).should_receive('get_subactions_for_actions').and_return({})
  342. action_parsers = {
  343. 'borg': flexmock(),
  344. 'list': flexmock(),
  345. }
  346. global_namespace = flexmock(config_paths=[])
  347. global_parser = flexmock()
  348. global_parser.should_receive('parse_known_args').and_return((global_namespace, ()))
  349. arguments, remaining_action_arguments = module.parse_arguments_for_actions(
  350. ('borg', 'list'), action_parsers, global_parser
  351. )
  352. assert arguments == {'global': global_namespace, 'borg': action_namespace}
  353. assert remaining_action_arguments == ((), ())
  354. def test_parse_arguments_for_actions_raises_error_when_no_action_is_specified():
  355. flexmock(module).should_receive('get_subaction_parsers').and_return({'bootstrap': [flexmock()]})
  356. flexmock(module).should_receive('parse_and_record_action_arguments').and_return(flexmock())
  357. flexmock(module).should_receive('get_subactions_for_actions').and_return(
  358. {'config': ['bootstrap']}
  359. )
  360. action_parsers = {'config': flexmock()}
  361. global_parser = flexmock()
  362. global_parser.should_receive('parse_known_args').and_return((flexmock(), ()))
  363. with pytest.raises(ValueError):
  364. module.parse_arguments_for_actions(('config',), action_parsers, global_parser)