test_arguments.py 27 KB

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