test_arguments.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. import pytest
  2. from flexmock import flexmock
  3. from borgmatic.commands import arguments as module
  4. def test_make_argument_description_with_object_adds_example():
  5. assert (
  6. module.make_argument_description(
  7. schema={
  8. 'description': 'Thing.',
  9. 'type': 'object',
  10. 'example': {'bar': 'baz'},
  11. },
  12. flag_name='flag',
  13. )
  14. # Apparently different versions of ruamel.yaml serialize this
  15. # differently.
  16. in ('Thing. Example value: "bar: baz"' 'Thing. Example value: "{bar: baz}"')
  17. )
  18. def test_make_argument_description_with_array_adds_example():
  19. assert (
  20. module.make_argument_description(
  21. schema={
  22. 'description': 'Thing.',
  23. 'type': 'array',
  24. 'example': [1, '- foo', {'bar': 'baz'}],
  25. },
  26. flag_name='flag',
  27. )
  28. # Apparently different versions of ruamel.yaml serialize this
  29. # differently.
  30. in (
  31. 'Thing. Example value: "[1, \'- foo\', bar: baz]"'
  32. 'Thing. Example value: "[1, \'- foo\', {bar: baz}]"'
  33. )
  34. )
  35. def test_add_array_element_arguments_adds_arguments_for_array_index_flags():
  36. parser = module.ArgumentParser(allow_abbrev=False, add_help=False)
  37. arguments_group = parser.add_argument_group('arguments')
  38. arguments_group.add_argument(
  39. '--foo[0].val',
  40. action='store_true',
  41. dest='--foo[0].val',
  42. )
  43. flexmock(arguments_group).should_receive('add_argument').with_args(
  44. '--foo[25].val',
  45. action='store_true',
  46. default=False,
  47. dest='foo[25].val',
  48. required=object,
  49. ).once()
  50. module.add_array_element_arguments(
  51. arguments_group=arguments_group,
  52. unparsed_arguments=('--foo[25].val', 'fooval', '--bar[1].val', 'barval'),
  53. flag_name='foo[0].val',
  54. )
  55. def test_add_arguments_from_schema_with_nested_object_adds_flag_for_each_option():
  56. parser = module.ArgumentParser(allow_abbrev=False, add_help=False)
  57. arguments_group = parser.add_argument_group('arguments')
  58. flexmock(arguments_group).should_receive('add_argument').with_args(
  59. '--foo.bar',
  60. type=int,
  61. metavar='BAR',
  62. help='help 1',
  63. ).once()
  64. flexmock(arguments_group).should_receive('add_argument').with_args(
  65. '--foo.baz',
  66. type=str,
  67. metavar='BAZ',
  68. help='help 2',
  69. ).once()
  70. module.add_arguments_from_schema(
  71. arguments_group=arguments_group,
  72. schema={
  73. 'type': 'object',
  74. 'properties': {
  75. 'foo': {
  76. 'type': 'object',
  77. 'properties': {
  78. 'bar': {'type': 'integer', 'description': 'help 1'},
  79. 'baz': {'type': 'string', 'description': 'help 2'},
  80. },
  81. }
  82. },
  83. },
  84. unparsed_arguments=(),
  85. )
  86. def test_add_arguments_from_schema_with_array_and_nested_object_adds_multiple_flags():
  87. parser = module.ArgumentParser(allow_abbrev=False, add_help=False)
  88. arguments_group = parser.add_argument_group('arguments')
  89. flexmock(arguments_group).should_receive('add_argument').with_args(
  90. '--foo[0].bar',
  91. type=int,
  92. metavar='BAR',
  93. help=object,
  94. ).once()
  95. flexmock(arguments_group).should_receive('add_argument').with_args(
  96. '--foo',
  97. type=str,
  98. metavar='FOO',
  99. help='help 2',
  100. ).once()
  101. module.add_arguments_from_schema(
  102. arguments_group=arguments_group,
  103. schema={
  104. 'type': 'object',
  105. 'properties': {
  106. 'foo': {
  107. 'type': 'array',
  108. 'items': {
  109. 'type': 'object',
  110. 'properties': {
  111. 'bar': {
  112. 'type': 'integer',
  113. 'description': 'help 1',
  114. }
  115. },
  116. },
  117. 'description': 'help 2',
  118. }
  119. },
  120. },
  121. unparsed_arguments=(),
  122. )
  123. def test_parse_arguments_with_no_arguments_uses_defaults():
  124. config_paths = ['default']
  125. flexmock(module.collect).should_receive('get_default_config_paths').and_return(config_paths)
  126. arguments = module.parse_arguments({})
  127. global_arguments = arguments['global']
  128. assert global_arguments.config_paths == config_paths
  129. def test_parse_arguments_with_multiple_config_flags_parses_as_list():
  130. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  131. arguments = module.parse_arguments({}, '--config', 'myconfig', '--config', 'otherconfig')
  132. global_arguments = arguments['global']
  133. assert global_arguments.config_paths == ['myconfig', 'otherconfig']
  134. def test_parse_arguments_with_action_after_config_path_omits_action():
  135. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  136. arguments = module.parse_arguments({}, '--config', 'myconfig', 'list', '--json')
  137. global_arguments = arguments['global']
  138. assert global_arguments.config_paths == ['myconfig']
  139. assert 'list' in arguments
  140. assert arguments['list'].json
  141. def test_parse_arguments_with_action_after_config_path_omits_aliased_action():
  142. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  143. arguments = module.parse_arguments(
  144. {}, '--config', 'myconfig', 'init', '--encryption', 'repokey'
  145. )
  146. global_arguments = arguments['global']
  147. assert global_arguments.config_paths == ['myconfig']
  148. assert 'repo-create' in arguments
  149. assert arguments['repo-create'].encryption_mode == 'repokey'
  150. def test_parse_arguments_with_action_and_positional_arguments_after_config_path_omits_action_and_arguments():
  151. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  152. arguments = module.parse_arguments({}, '--config', 'myconfig', 'borg', 'key', 'export')
  153. global_arguments = arguments['global']
  154. assert global_arguments.config_paths == ['myconfig']
  155. assert 'borg' in arguments
  156. assert arguments['borg'].options == ['key', 'export']
  157. def test_parse_arguments_with_single_override_parses():
  158. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  159. arguments = module.parse_arguments({}, '--override', 'foo.bar=baz')
  160. global_arguments = arguments['global']
  161. assert global_arguments.overrides == ['foo.bar=baz']
  162. def test_parse_arguments_with_multiple_overrides_flags_parses():
  163. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  164. arguments = module.parse_arguments(
  165. {}, '--override', 'foo.bar=baz', '--override', 'foo.quux=7', '--override', 'this.that=8'
  166. )
  167. global_arguments = arguments['global']
  168. assert global_arguments.overrides == ['foo.bar=baz', 'foo.quux=7', 'this.that=8']
  169. def test_parse_arguments_with_list_json_overrides_default():
  170. arguments = module.parse_arguments({}, 'list', '--json')
  171. assert 'list' in arguments
  172. assert arguments['list'].json is True
  173. def test_parse_arguments_with_no_actions_defaults_to_all_actions_enabled():
  174. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  175. arguments = module.parse_arguments({})
  176. assert 'prune' in arguments
  177. assert 'create' in arguments
  178. assert 'check' in arguments
  179. def test_parse_arguments_with_no_actions_passes_argument_to_relevant_actions():
  180. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  181. arguments = module.parse_arguments({}, '--stats', '--list')
  182. assert 'prune' in arguments
  183. assert arguments['prune'].statistics
  184. assert arguments['prune'].list_details
  185. assert 'create' in arguments
  186. assert arguments['create'].statistics
  187. assert arguments['create'].list_details
  188. assert 'check' in arguments
  189. def test_parse_arguments_with_help_and_no_actions_shows_global_help(capsys):
  190. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  191. with pytest.raises(SystemExit) as exit:
  192. module.parse_arguments({}, '--help')
  193. assert exit.value.code == 0
  194. captured = capsys.readouterr()
  195. assert 'global arguments:' in captured.out
  196. assert 'actions:' in captured.out
  197. def test_parse_arguments_with_help_and_action_shows_action_help(capsys):
  198. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  199. with pytest.raises(SystemExit) as exit:
  200. module.parse_arguments({}, 'create', '--help')
  201. assert exit.value.code == 0
  202. captured = capsys.readouterr()
  203. assert 'global arguments:' not in captured.out
  204. assert 'actions:' not in captured.out
  205. assert 'create arguments:' in captured.out
  206. def test_parse_arguments_with_action_before_global_options_parses_options():
  207. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  208. arguments = module.parse_arguments({}, 'prune', '--dry-run')
  209. assert 'prune' in arguments
  210. assert arguments['global'].dry_run
  211. def test_parse_arguments_with_global_options_before_action_parses_options():
  212. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  213. arguments = module.parse_arguments({}, '--dry-run', 'prune')
  214. assert 'prune' in arguments
  215. assert arguments['global'].dry_run
  216. def test_parse_arguments_with_prune_action_leaves_other_actions_disabled():
  217. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  218. arguments = module.parse_arguments({}, 'prune')
  219. assert 'prune' in arguments
  220. assert 'create' not in arguments
  221. assert 'check' not in arguments
  222. def test_parse_arguments_with_multiple_actions_leaves_other_action_disabled():
  223. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  224. arguments = module.parse_arguments({}, 'create', 'check')
  225. assert 'prune' not in arguments
  226. assert 'create' in arguments
  227. assert 'check' in arguments
  228. def test_parse_arguments_disallows_invalid_argument():
  229. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  230. with pytest.raises(ValueError):
  231. module.parse_arguments({}, '--posix-me-harder')
  232. def test_parse_arguments_disallows_encryption_mode_without_init():
  233. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  234. with pytest.raises(ValueError):
  235. module.parse_arguments({}, '--config', 'myconfig', '--encryption', 'repokey')
  236. def test_parse_arguments_allows_encryption_mode_with_init():
  237. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  238. module.parse_arguments({}, '--config', 'myconfig', 'init', '--encryption', 'repokey')
  239. def test_parse_arguments_disallows_append_only_without_init():
  240. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  241. with pytest.raises(ValueError):
  242. module.parse_arguments({}, '--config', 'myconfig', '--append-only')
  243. def test_parse_arguments_disallows_storage_quota_without_init():
  244. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  245. with pytest.raises(ValueError):
  246. module.parse_arguments({}, '--config', 'myconfig', '--storage-quota', '5G')
  247. def test_parse_arguments_allows_init_and_prune():
  248. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  249. module.parse_arguments({}, '--config', 'myconfig', 'init', '--encryption', 'repokey', 'prune')
  250. def test_parse_arguments_allows_init_and_create():
  251. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  252. module.parse_arguments({}, '--config', 'myconfig', 'init', '--encryption', 'repokey', 'create')
  253. def test_parse_arguments_allows_repository_with_extract():
  254. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  255. module.parse_arguments(
  256. {}, '--config', 'myconfig', 'extract', '--repository', 'test.borg', '--archive', 'test'
  257. )
  258. def test_parse_arguments_allows_repository_with_mount():
  259. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  260. module.parse_arguments(
  261. {},
  262. '--config',
  263. 'myconfig',
  264. 'mount',
  265. '--repository',
  266. 'test.borg',
  267. '--archive',
  268. 'test',
  269. '--mount-point',
  270. '/mnt',
  271. )
  272. def test_parse_arguments_allows_repository_with_list():
  273. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  274. module.parse_arguments({}, '--config', 'myconfig', 'list', '--repository', 'test.borg')
  275. def test_parse_arguments_disallows_archive_unless_action_consumes_it():
  276. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  277. with pytest.raises(ValueError):
  278. module.parse_arguments({}, '--config', 'myconfig', '--archive', 'test')
  279. def test_parse_arguments_disallows_paths_unless_action_consumes_it():
  280. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  281. with pytest.raises(ValueError):
  282. module.parse_arguments({}, '--config', 'myconfig', '--path', 'test')
  283. def test_parse_arguments_disallows_other_actions_with_config_bootstrap():
  284. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  285. with pytest.raises(ValueError):
  286. module.parse_arguments({}, 'config', 'bootstrap', '--repository', 'test.borg', 'list')
  287. def test_parse_arguments_allows_archive_with_extract():
  288. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  289. module.parse_arguments({}, '--config', 'myconfig', 'extract', '--archive', 'test')
  290. def test_parse_arguments_allows_archive_with_mount():
  291. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  292. module.parse_arguments(
  293. {}, '--config', 'myconfig', 'mount', '--archive', 'test', '--mount-point', '/mnt'
  294. )
  295. def test_parse_arguments_allows_archive_with_restore():
  296. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  297. module.parse_arguments({}, '--config', 'myconfig', 'restore', '--archive', 'test')
  298. def test_parse_arguments_allows_archive_with_list():
  299. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  300. module.parse_arguments({}, '--config', 'myconfig', 'list', '--archive', 'test')
  301. def test_parse_arguments_requires_archive_with_extract():
  302. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  303. with pytest.raises(SystemExit):
  304. module.parse_arguments({}, '--config', 'myconfig', 'extract')
  305. def test_parse_arguments_requires_archive_with_restore():
  306. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  307. with pytest.raises(SystemExit):
  308. module.parse_arguments({}, '--config', 'myconfig', 'restore')
  309. def test_parse_arguments_requires_mount_point_with_mount():
  310. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  311. with pytest.raises(SystemExit):
  312. module.parse_arguments({}, '--config', 'myconfig', 'mount', '--archive', 'test')
  313. def test_parse_arguments_requires_mount_point_with_umount():
  314. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  315. with pytest.raises(SystemExit):
  316. module.parse_arguments({}, '--config', 'myconfig', 'umount')
  317. def test_parse_arguments_allows_progress_before_create():
  318. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  319. module.parse_arguments({}, '--progress', 'create', 'list')
  320. def test_parse_arguments_allows_progress_after_create():
  321. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  322. module.parse_arguments({}, 'create', '--progress', 'list')
  323. def test_parse_arguments_allows_progress_and_extract():
  324. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  325. module.parse_arguments({}, '--progress', 'extract', '--archive', 'test', 'list')
  326. def test_parse_arguments_disallows_progress_without_create():
  327. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  328. with pytest.raises(ValueError):
  329. module.parse_arguments({}, '--progress', 'list')
  330. def test_parse_arguments_with_stats_and_create_flags_does_not_raise():
  331. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  332. module.parse_arguments({}, '--stats', 'create', 'list')
  333. def test_parse_arguments_with_stats_and_prune_flags_does_not_raise():
  334. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  335. module.parse_arguments({}, '--stats', 'prune', 'list')
  336. def test_parse_arguments_with_stats_flag_but_no_create_or_prune_flag_raises_value_error():
  337. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  338. with pytest.raises(ValueError):
  339. module.parse_arguments({}, '--stats', 'list')
  340. def test_parse_arguments_with_list_and_create_flags_does_not_raise():
  341. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  342. module.parse_arguments({}, '--list', 'create')
  343. def test_parse_arguments_with_list_and_prune_flags_does_not_raise():
  344. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  345. module.parse_arguments({}, '--list', 'prune')
  346. def test_parse_arguments_with_list_flag_but_no_relevant_action_raises_value_error():
  347. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  348. with pytest.raises(ValueError):
  349. module.parse_arguments({}, '--list', 'repo-create')
  350. def test_parse_arguments_allows_json_with_list_or_info():
  351. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  352. module.parse_arguments({}, 'list', '--json')
  353. module.parse_arguments({}, 'info', '--json')
  354. def test_parse_arguments_disallows_json_with_both_list_and_info():
  355. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  356. with pytest.raises(ValueError):
  357. module.parse_arguments({}, 'list', 'info', '--json')
  358. def test_parse_arguments_disallows_json_with_both_list_and_repo_info():
  359. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  360. with pytest.raises(ValueError):
  361. module.parse_arguments({}, 'list', 'repo-info', '--json')
  362. def test_parse_arguments_disallows_json_with_both_repo_info_and_info():
  363. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  364. with pytest.raises(ValueError):
  365. module.parse_arguments({}, 'repo-info', 'info', '--json')
  366. def test_parse_arguments_disallows_list_with_both_prefix_and_match_archives():
  367. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  368. with pytest.raises(ValueError):
  369. module.parse_arguments({}, 'list', '--prefix', 'foo', '--match-archives', 'sh:*bar')
  370. def test_parse_arguments_disallows_repo_list_with_both_prefix_and_match_archives():
  371. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  372. with pytest.raises(ValueError):
  373. module.parse_arguments({}, 'repo-list', '--prefix', 'foo', '--match-archives', 'sh:*bar')
  374. def test_parse_arguments_disallows_info_with_both_archive_and_match_archives():
  375. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  376. with pytest.raises(ValueError):
  377. module.parse_arguments({}, 'info', '--archive', 'foo', '--match-archives', 'sh:*bar')
  378. def test_parse_arguments_disallows_info_with_both_archive_and_prefix():
  379. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  380. with pytest.raises(ValueError):
  381. module.parse_arguments({}, 'info', '--archive', 'foo', '--prefix', 'bar')
  382. def test_parse_arguments_disallows_info_with_both_prefix_and_match_archives():
  383. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  384. with pytest.raises(ValueError):
  385. module.parse_arguments({}, 'info', '--prefix', 'foo', '--match-archives', 'sh:*bar')
  386. def test_parse_arguments_check_only_extract_does_not_raise_extract_subparser_error():
  387. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  388. module.parse_arguments({}, 'check', '--only', 'extract')
  389. def test_parse_arguments_extract_archive_check_does_not_raise_check_subparser_error():
  390. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  391. module.parse_arguments({}, 'extract', '--archive', 'check')
  392. def test_parse_arguments_extract_with_check_only_extract_does_not_raise():
  393. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  394. module.parse_arguments({}, 'extract', '--archive', 'name', 'check', '--only', 'extract')
  395. def test_parse_arguments_bootstrap_without_config_errors():
  396. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  397. with pytest.raises(ValueError):
  398. module.parse_arguments({}, 'bootstrap')
  399. def test_parse_arguments_config_with_no_subaction_errors():
  400. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  401. with pytest.raises(ValueError):
  402. module.parse_arguments({}, 'config')
  403. def test_parse_arguments_config_with_help_shows_config_help(capsys):
  404. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  405. with pytest.raises(SystemExit) as exit:
  406. module.parse_arguments({}, 'config', '--help')
  407. assert exit.value.code == 0
  408. captured = capsys.readouterr()
  409. assert 'global arguments:' not in captured.out
  410. assert 'config arguments:' in captured.out
  411. assert 'config sub-actions:' in captured.out
  412. def test_parse_arguments_config_with_subaction_but_missing_flags_errors():
  413. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  414. with pytest.raises(SystemExit) as exit:
  415. module.parse_arguments({}, 'config', 'bootstrap')
  416. assert exit.value.code == 2
  417. def test_parse_arguments_config_with_subaction_and_help_shows_subaction_help(capsys):
  418. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  419. with pytest.raises(SystemExit) as exit:
  420. module.parse_arguments({}, 'config', 'bootstrap', '--help')
  421. assert exit.value.code == 0
  422. captured = capsys.readouterr()
  423. assert 'config bootstrap arguments:' in captured.out
  424. def test_parse_arguments_config_with_subaction_and_required_flags_does_not_raise():
  425. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  426. module.parse_arguments({}, 'config', 'bootstrap', '--repository', 'repo.borg')
  427. def test_parse_arguments_config_with_subaction_and_global_flags_at_start_does_not_raise():
  428. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  429. module.parse_arguments({}, '--dry-run', 'config', 'bootstrap', '--repository', 'repo.borg')
  430. def test_parse_arguments_config_with_subaction_and_global_flags_at_end_does_not_raise():
  431. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  432. module.parse_arguments({}, 'config', 'bootstrap', '--repository', 'repo.borg', '--dry-run')
  433. def test_parse_arguments_config_with_subaction_and_explicit_config_file_does_not_raise():
  434. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  435. module.parse_arguments(
  436. {}, 'config', 'bootstrap', '--repository', 'repo.borg', '--config', 'test.yaml'
  437. )
  438. def test_parse_arguments_with_borg_action_and_dry_run_raises():
  439. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  440. with pytest.raises(ValueError):
  441. module.parse_arguments({}, '--dry-run', 'borg', 'list')
  442. def test_parse_arguments_with_borg_action_and_no_dry_run_does_not_raise():
  443. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  444. module.parse_arguments({}, 'borg', 'list')
  445. def test_parse_arguments_with_argument_from_schema_does_not_raise():
  446. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  447. module.parse_arguments(
  448. {
  449. 'type': 'object',
  450. 'properties': {'foo': {'type': 'object', 'properties': {'bar': {'type': 'integer'}}}},
  451. },
  452. '--foo.bar',
  453. '3',
  454. )