2
0

test_arguments.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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. {},
  145. '--config',
  146. 'myconfig',
  147. 'init',
  148. '--encryption',
  149. 'repokey',
  150. )
  151. global_arguments = arguments['global']
  152. assert global_arguments.config_paths == ['myconfig']
  153. assert 'repo-create' in arguments
  154. assert arguments['repo-create'].encryption_mode == 'repokey'
  155. def test_parse_arguments_with_action_and_positional_arguments_after_config_path_omits_action_and_arguments():
  156. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  157. arguments = module.parse_arguments({}, '--config', 'myconfig', 'borg', 'key', 'export')
  158. global_arguments = arguments['global']
  159. assert global_arguments.config_paths == ['myconfig']
  160. assert 'borg' in arguments
  161. assert arguments['borg'].options == ['key', 'export']
  162. def test_parse_arguments_with_single_override_parses():
  163. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  164. arguments = module.parse_arguments({}, '--override', 'foo.bar=baz')
  165. global_arguments = arguments['global']
  166. assert global_arguments.overrides == ['foo.bar=baz']
  167. def test_parse_arguments_with_multiple_overrides_flags_parses():
  168. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  169. arguments = module.parse_arguments(
  170. {},
  171. '--override',
  172. 'foo.bar=baz',
  173. '--override',
  174. 'foo.quux=7',
  175. '--override',
  176. 'this.that=8',
  177. )
  178. global_arguments = arguments['global']
  179. assert global_arguments.overrides == ['foo.bar=baz', 'foo.quux=7', 'this.that=8']
  180. def test_parse_arguments_with_list_json_overrides_default():
  181. arguments = module.parse_arguments({}, 'list', '--json')
  182. assert 'list' in arguments
  183. assert arguments['list'].json is True
  184. def test_parse_arguments_with_no_actions_defaults_to_all_actions_enabled():
  185. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  186. arguments = module.parse_arguments({})
  187. assert 'prune' in arguments
  188. assert 'create' in arguments
  189. assert 'check' in arguments
  190. def test_parse_arguments_with_no_actions_passes_argument_to_relevant_actions():
  191. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  192. arguments = module.parse_arguments({}, '--stats', '--list')
  193. assert 'prune' in arguments
  194. assert arguments['prune'].statistics
  195. assert arguments['prune'].list_details
  196. assert 'create' in arguments
  197. assert arguments['create'].statistics
  198. assert arguments['create'].list_details
  199. assert 'check' in arguments
  200. def test_parse_arguments_with_help_and_no_actions_shows_global_help(capsys):
  201. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  202. with pytest.raises(SystemExit) as system_exit:
  203. module.parse_arguments({}, '--help')
  204. assert system_exit.value.code == 0
  205. captured = capsys.readouterr()
  206. assert 'global arguments:' in captured.out
  207. assert 'actions:' in captured.out
  208. def test_parse_arguments_with_help_and_action_shows_action_help(capsys):
  209. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  210. with pytest.raises(SystemExit) as system_exit:
  211. module.parse_arguments({}, 'create', '--help')
  212. assert system_exit.value.code == 0
  213. captured = capsys.readouterr()
  214. assert 'global arguments:' not in captured.out
  215. assert 'actions:' not in captured.out
  216. assert 'create arguments:' in captured.out
  217. def test_parse_arguments_with_action_before_global_options_parses_options():
  218. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  219. arguments = module.parse_arguments({}, 'prune', '--dry-run')
  220. assert 'prune' in arguments
  221. assert arguments['global'].dry_run
  222. def test_parse_arguments_with_global_options_before_action_parses_options():
  223. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  224. arguments = module.parse_arguments({}, '--dry-run', 'prune')
  225. assert 'prune' in arguments
  226. assert arguments['global'].dry_run
  227. def test_parse_arguments_with_prune_action_leaves_other_actions_disabled():
  228. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  229. arguments = module.parse_arguments({}, 'prune')
  230. assert 'prune' in arguments
  231. assert 'create' not in arguments
  232. assert 'check' not in arguments
  233. def test_parse_arguments_with_multiple_actions_leaves_other_action_disabled():
  234. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  235. arguments = module.parse_arguments({}, 'create', 'check')
  236. assert 'prune' not in arguments
  237. assert 'create' in arguments
  238. assert 'check' in arguments
  239. def test_parse_arguments_disallows_invalid_argument():
  240. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  241. with pytest.raises(ValueError):
  242. module.parse_arguments({}, '--posix-me-harder')
  243. def test_parse_arguments_disallows_encryption_mode_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', '--encryption', 'repokey')
  247. def test_parse_arguments_allows_encryption_mode_with_init():
  248. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  249. module.parse_arguments({}, '--config', 'myconfig', 'init', '--encryption', 'repokey')
  250. def test_parse_arguments_disallows_append_only_without_init():
  251. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  252. with pytest.raises(ValueError):
  253. module.parse_arguments({}, '--config', 'myconfig', '--append-only')
  254. def test_parse_arguments_disallows_storage_quota_without_init():
  255. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  256. with pytest.raises(ValueError):
  257. module.parse_arguments({}, '--config', 'myconfig', '--storage-quota', '5G')
  258. def test_parse_arguments_allows_init_and_prune():
  259. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  260. module.parse_arguments({}, '--config', 'myconfig', 'init', '--encryption', 'repokey', 'prune')
  261. def test_parse_arguments_allows_init_and_create():
  262. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  263. module.parse_arguments({}, '--config', 'myconfig', 'init', '--encryption', 'repokey', 'create')
  264. def test_parse_arguments_allows_repository_with_extract():
  265. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  266. module.parse_arguments(
  267. {},
  268. '--config',
  269. 'myconfig',
  270. 'extract',
  271. '--repository',
  272. 'test.borg',
  273. '--archive',
  274. 'test',
  275. )
  276. def test_parse_arguments_allows_repository_with_mount():
  277. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  278. module.parse_arguments(
  279. {},
  280. '--config',
  281. 'myconfig',
  282. 'mount',
  283. '--repository',
  284. 'test.borg',
  285. '--archive',
  286. 'test',
  287. '--mount-point',
  288. '/mnt',
  289. )
  290. def test_parse_arguments_allows_repository_with_list():
  291. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  292. module.parse_arguments({}, '--config', 'myconfig', 'list', '--repository', 'test.borg')
  293. def test_parse_arguments_disallows_archive_unless_action_consumes_it():
  294. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  295. with pytest.raises(ValueError):
  296. module.parse_arguments({}, '--config', 'myconfig', '--archive', 'test')
  297. def test_parse_arguments_disallows_paths_unless_action_consumes_it():
  298. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  299. with pytest.raises(ValueError):
  300. module.parse_arguments({}, '--config', 'myconfig', '--path', 'test')
  301. def test_parse_arguments_disallows_other_actions_with_config_bootstrap():
  302. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  303. with pytest.raises(ValueError):
  304. module.parse_arguments({}, 'config', 'bootstrap', '--repository', 'test.borg', 'list')
  305. def test_parse_arguments_allows_archive_with_extract():
  306. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  307. module.parse_arguments({}, '--config', 'myconfig', 'extract', '--archive', 'test')
  308. def test_parse_arguments_allows_archive_with_mount():
  309. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  310. module.parse_arguments(
  311. {},
  312. '--config',
  313. 'myconfig',
  314. 'mount',
  315. '--archive',
  316. 'test',
  317. '--mount-point',
  318. '/mnt',
  319. )
  320. def test_parse_arguments_allows_archive_with_restore():
  321. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  322. module.parse_arguments({}, '--config', 'myconfig', 'restore', '--archive', 'test')
  323. def test_parse_arguments_allows_archive_with_list():
  324. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  325. module.parse_arguments({}, '--config', 'myconfig', 'list', '--archive', 'test')
  326. def test_parse_arguments_requires_archive_with_extract():
  327. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  328. with pytest.raises(SystemExit):
  329. module.parse_arguments({}, '--config', 'myconfig', 'extract')
  330. def test_parse_arguments_requires_archive_with_restore():
  331. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  332. with pytest.raises(SystemExit):
  333. module.parse_arguments({}, '--config', 'myconfig', 'restore')
  334. def test_parse_arguments_requires_mount_point_with_mount():
  335. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  336. with pytest.raises(SystemExit):
  337. module.parse_arguments({}, '--config', 'myconfig', 'mount', '--archive', 'test')
  338. def test_parse_arguments_requires_mount_point_with_umount():
  339. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  340. with pytest.raises(SystemExit):
  341. module.parse_arguments({}, '--config', 'myconfig', 'umount')
  342. def test_parse_arguments_allows_progress_before_create():
  343. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  344. module.parse_arguments({}, '--progress', 'create', 'list')
  345. def test_parse_arguments_allows_progress_after_create():
  346. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  347. module.parse_arguments({}, 'create', '--progress', 'list')
  348. def test_parse_arguments_allows_progress_and_extract():
  349. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  350. module.parse_arguments({}, '--progress', 'extract', '--archive', 'test', 'list')
  351. def test_parse_arguments_disallows_progress_without_create():
  352. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  353. with pytest.raises(ValueError):
  354. module.parse_arguments({}, '--progress', 'list')
  355. def test_parse_arguments_with_stats_and_create_flags_does_not_raise():
  356. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  357. module.parse_arguments({}, '--stats', 'create', 'list')
  358. def test_parse_arguments_with_stats_and_prune_flags_does_not_raise():
  359. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  360. module.parse_arguments({}, '--stats', 'prune', 'list')
  361. def test_parse_arguments_with_stats_flag_but_no_create_or_prune_flag_raises_value_error():
  362. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  363. with pytest.raises(ValueError):
  364. module.parse_arguments({}, '--stats', 'list')
  365. def test_parse_arguments_with_list_and_create_flags_does_not_raise():
  366. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  367. module.parse_arguments({}, '--list', 'create')
  368. def test_parse_arguments_with_list_and_prune_flags_does_not_raise():
  369. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  370. module.parse_arguments({}, '--list', 'prune')
  371. def test_parse_arguments_with_list_flag_but_no_relevant_action_raises_value_error():
  372. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  373. with pytest.raises(ValueError):
  374. module.parse_arguments({}, '--list', 'repo-create')
  375. def test_parse_arguments_allows_json_with_list_or_info():
  376. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  377. module.parse_arguments({}, 'list', '--json')
  378. module.parse_arguments({}, 'info', '--json')
  379. def test_parse_arguments_disallows_json_with_both_list_and_info():
  380. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  381. with pytest.raises(ValueError):
  382. module.parse_arguments({}, 'list', 'info', '--json')
  383. def test_parse_arguments_disallows_json_with_both_list_and_repo_info():
  384. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  385. with pytest.raises(ValueError):
  386. module.parse_arguments({}, 'list', 'repo-info', '--json')
  387. def test_parse_arguments_disallows_json_with_both_repo_info_and_info():
  388. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  389. with pytest.raises(ValueError):
  390. module.parse_arguments({}, 'repo-info', 'info', '--json')
  391. def test_parse_arguments_disallows_list_with_both_prefix_and_match_archives():
  392. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  393. with pytest.raises(ValueError):
  394. module.parse_arguments({}, 'list', '--prefix', 'foo', '--match-archives', 'sh:*bar')
  395. def test_parse_arguments_disallows_repo_list_with_both_prefix_and_match_archives():
  396. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  397. with pytest.raises(ValueError):
  398. module.parse_arguments({}, 'repo-list', '--prefix', 'foo', '--match-archives', 'sh:*bar')
  399. def test_parse_arguments_disallows_info_with_both_archive_and_match_archives():
  400. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  401. with pytest.raises(ValueError):
  402. module.parse_arguments({}, 'info', '--archive', 'foo', '--match-archives', 'sh:*bar')
  403. def test_parse_arguments_disallows_info_with_both_archive_and_prefix():
  404. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  405. with pytest.raises(ValueError):
  406. module.parse_arguments({}, 'info', '--archive', 'foo', '--prefix', 'bar')
  407. def test_parse_arguments_disallows_info_with_both_prefix_and_match_archives():
  408. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  409. with pytest.raises(ValueError):
  410. module.parse_arguments({}, 'info', '--prefix', 'foo', '--match-archives', 'sh:*bar')
  411. def test_parse_arguments_check_only_extract_does_not_raise_extract_subparser_error():
  412. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  413. module.parse_arguments({}, 'check', '--only', 'extract')
  414. def test_parse_arguments_extract_archive_check_does_not_raise_check_subparser_error():
  415. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  416. module.parse_arguments({}, 'extract', '--archive', 'check')
  417. def test_parse_arguments_extract_with_check_only_extract_does_not_raise():
  418. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  419. module.parse_arguments({}, 'extract', '--archive', 'name', 'check', '--only', 'extract')
  420. def test_parse_arguments_bootstrap_without_config_errors():
  421. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  422. with pytest.raises(ValueError):
  423. module.parse_arguments({}, 'bootstrap')
  424. def test_parse_arguments_config_with_no_subaction_errors():
  425. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  426. with pytest.raises(ValueError):
  427. module.parse_arguments({}, 'config')
  428. def test_parse_arguments_config_with_help_shows_config_help(capsys):
  429. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  430. with pytest.raises(SystemExit) as system_exit:
  431. module.parse_arguments({}, 'config', '--help')
  432. assert system_exit.value.code == 0
  433. captured = capsys.readouterr()
  434. assert 'global arguments:' not in captured.out
  435. assert 'config arguments:' in captured.out
  436. assert 'config sub-actions:' in captured.out
  437. def test_parse_arguments_config_with_subaction_but_missing_flags_errors():
  438. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  439. with pytest.raises(SystemExit) as system_exit:
  440. module.parse_arguments({}, 'config', 'bootstrap')
  441. assert system_exit.value.code == 2
  442. def test_parse_arguments_config_with_subaction_and_help_shows_subaction_help(capsys):
  443. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  444. with pytest.raises(SystemExit) as system_exit:
  445. module.parse_arguments({}, 'config', 'bootstrap', '--help')
  446. assert system_exit.value.code == 0
  447. captured = capsys.readouterr()
  448. assert 'config bootstrap arguments:' in captured.out
  449. def test_parse_arguments_config_with_subaction_and_required_flags_does_not_raise():
  450. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  451. module.parse_arguments({}, 'config', 'bootstrap', '--repository', 'repo.borg')
  452. def test_parse_arguments_config_with_subaction_and_global_flags_at_start_does_not_raise():
  453. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  454. module.parse_arguments({}, '--dry-run', 'config', 'bootstrap', '--repository', 'repo.borg')
  455. def test_parse_arguments_config_with_subaction_and_global_flags_at_end_does_not_raise():
  456. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  457. module.parse_arguments({}, 'config', 'bootstrap', '--repository', 'repo.borg', '--dry-run')
  458. def test_parse_arguments_config_with_subaction_and_explicit_config_file_does_not_raise():
  459. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  460. module.parse_arguments(
  461. {},
  462. 'config',
  463. 'bootstrap',
  464. '--repository',
  465. 'repo.borg',
  466. '--config',
  467. 'test.yaml',
  468. )
  469. def test_parse_arguments_with_borg_action_and_dry_run_raises():
  470. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  471. with pytest.raises(ValueError):
  472. module.parse_arguments({}, '--dry-run', 'borg', 'list')
  473. def test_parse_arguments_with_borg_action_and_no_dry_run_does_not_raise():
  474. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  475. module.parse_arguments({}, 'borg', 'list')
  476. def test_parse_arguments_with_argument_from_schema_does_not_raise():
  477. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  478. module.parse_arguments(
  479. {
  480. 'type': 'object',
  481. 'properties': {'foo': {'type': 'object', 'properties': {'bar': {'type': 'integer'}}}},
  482. },
  483. '--foo.bar',
  484. '3',
  485. )