2
0

test_arguments.py 27 KB

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