test_arguments.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. import argparse
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic.commands import arguments as module
  5. def test_parse_arguments_with_no_arguments_uses_defaults():
  6. config_paths = ['default']
  7. flexmock(module.collect).should_receive('get_default_config_paths').and_return(config_paths)
  8. arguments = module.parse_arguments()
  9. global_arguments = arguments['global']
  10. assert global_arguments.config_paths == config_paths
  11. assert global_arguments.excludes_filename is None
  12. assert global_arguments.verbosity == 0
  13. assert global_arguments.syslog_verbosity == 0
  14. assert global_arguments.log_file_verbosity == 0
  15. def test_parse_arguments_with_multiple_config_paths_parses_as_list():
  16. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  17. arguments = module.parse_arguments('--config', 'myconfig', 'otherconfig')
  18. global_arguments = arguments['global']
  19. assert global_arguments.config_paths == ['myconfig', 'otherconfig']
  20. assert global_arguments.verbosity == 0
  21. assert global_arguments.syslog_verbosity == 0
  22. assert global_arguments.log_file_verbosity == 0
  23. def test_parse_arguments_with_verbosity_overrides_default():
  24. config_paths = ['default']
  25. flexmock(module.collect).should_receive('get_default_config_paths').and_return(config_paths)
  26. arguments = module.parse_arguments('--verbosity', '1')
  27. global_arguments = arguments['global']
  28. assert global_arguments.config_paths == config_paths
  29. assert global_arguments.excludes_filename is None
  30. assert global_arguments.verbosity == 1
  31. assert global_arguments.syslog_verbosity == 0
  32. assert global_arguments.log_file_verbosity == 0
  33. def test_parse_arguments_with_syslog_verbosity_overrides_default():
  34. config_paths = ['default']
  35. flexmock(module.collect).should_receive('get_default_config_paths').and_return(config_paths)
  36. arguments = module.parse_arguments('--syslog-verbosity', '2')
  37. global_arguments = arguments['global']
  38. assert global_arguments.config_paths == config_paths
  39. assert global_arguments.excludes_filename is None
  40. assert global_arguments.verbosity == 0
  41. assert global_arguments.syslog_verbosity == 2
  42. def test_parse_arguments_with_log_file_verbosity_overrides_default():
  43. config_paths = ['default']
  44. flexmock(module.collect).should_receive('get_default_config_paths').and_return(config_paths)
  45. arguments = module.parse_arguments('--log-file-verbosity', '-1')
  46. global_arguments = arguments['global']
  47. assert global_arguments.config_paths == config_paths
  48. assert global_arguments.excludes_filename is None
  49. assert global_arguments.verbosity == 0
  50. assert global_arguments.syslog_verbosity == 0
  51. assert global_arguments.log_file_verbosity == -1
  52. def test_parse_arguments_with_single_override_parses():
  53. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  54. arguments = module.parse_arguments('--override', 'foo.bar=baz')
  55. global_arguments = arguments['global']
  56. assert global_arguments.overrides == ['foo.bar=baz']
  57. def test_parse_arguments_with_multiple_overrides_parses():
  58. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  59. arguments = module.parse_arguments('--override', 'foo.bar=baz', 'foo.quux=7')
  60. global_arguments = arguments['global']
  61. assert global_arguments.overrides == ['foo.bar=baz', 'foo.quux=7']
  62. def test_parse_arguments_with_multiple_overrides_and_flags_parses():
  63. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  64. arguments = module.parse_arguments(
  65. '--override', 'foo.bar=baz', '--override', 'foo.quux=7', 'this.that=8'
  66. )
  67. global_arguments = arguments['global']
  68. assert global_arguments.overrides == ['foo.bar=baz', 'foo.quux=7', 'this.that=8']
  69. def test_parse_arguments_with_list_json_overrides_default():
  70. arguments = module.parse_arguments('list', '--json')
  71. assert 'list' in arguments
  72. assert arguments['list'].json is True
  73. def test_parse_arguments_with_no_actions_defaults_to_all_actions_enabled():
  74. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  75. arguments = module.parse_arguments()
  76. assert 'prune' in arguments
  77. assert 'create' in arguments
  78. assert 'check' in arguments
  79. def test_parse_arguments_with_no_actions_passes_argument_to_relevant_actions():
  80. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  81. arguments = module.parse_arguments('--stats', '--list')
  82. assert 'prune' in arguments
  83. assert arguments['prune'].stats
  84. assert arguments['prune'].list_archives
  85. assert 'create' in arguments
  86. assert arguments['create'].stats
  87. assert arguments['create'].list_files
  88. assert 'check' in arguments
  89. def test_parse_arguments_with_help_and_no_actions_shows_global_help(capsys):
  90. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  91. with pytest.raises(SystemExit) as exit:
  92. module.parse_arguments('--help')
  93. assert exit.value.code == 0
  94. captured = capsys.readouterr()
  95. assert 'global arguments:' in captured.out
  96. assert 'actions:' in captured.out
  97. def test_parse_arguments_with_help_and_action_shows_action_help(capsys):
  98. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  99. with pytest.raises(SystemExit) as exit:
  100. module.parse_arguments('create', '--help')
  101. assert exit.value.code == 0
  102. captured = capsys.readouterr()
  103. assert 'global arguments:' not in captured.out
  104. assert 'actions:' not in captured.out
  105. assert 'create arguments:' in captured.out
  106. def test_parse_arguments_with_action_before_global_options_parses_options():
  107. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  108. arguments = module.parse_arguments('prune', '--verbosity', '2')
  109. assert 'prune' in arguments
  110. assert arguments['global'].verbosity == 2
  111. def test_parse_arguments_with_global_options_before_action_parses_options():
  112. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  113. arguments = module.parse_arguments('--verbosity', '2', 'prune')
  114. assert 'prune' in arguments
  115. assert arguments['global'].verbosity == 2
  116. def test_parse_arguments_with_prune_action_leaves_other_actions_disabled():
  117. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  118. arguments = module.parse_arguments('prune')
  119. assert 'prune' in arguments
  120. assert 'create' not in arguments
  121. assert 'check' not in arguments
  122. def test_parse_arguments_with_multiple_actions_leaves_other_action_disabled():
  123. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  124. arguments = module.parse_arguments('create', 'check')
  125. assert 'prune' not in arguments
  126. assert 'create' in arguments
  127. assert 'check' in arguments
  128. def test_parse_arguments_with_invalid_arguments_exits():
  129. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  130. with pytest.raises(SystemExit):
  131. module.parse_arguments('--posix-me-harder')
  132. def test_parse_arguments_disallows_deprecated_excludes_option():
  133. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  134. with pytest.raises(ValueError):
  135. module.parse_arguments('--config', 'myconfig', '--excludes', 'myexcludes')
  136. def test_parse_arguments_disallows_encryption_mode_without_init():
  137. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  138. with pytest.raises(SystemExit):
  139. module.parse_arguments('--config', 'myconfig', '--encryption', 'repokey')
  140. def test_parse_arguments_allows_encryption_mode_with_init():
  141. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  142. module.parse_arguments('--config', 'myconfig', 'init', '--encryption', 'repokey')
  143. def test_parse_arguments_requires_encryption_mode_with_init():
  144. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  145. with pytest.raises(SystemExit):
  146. module.parse_arguments('--config', 'myconfig', 'init')
  147. def test_parse_arguments_disallows_append_only_without_init():
  148. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  149. with pytest.raises(SystemExit):
  150. module.parse_arguments('--config', 'myconfig', '--append-only')
  151. def test_parse_arguments_disallows_storage_quota_without_init():
  152. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  153. with pytest.raises(SystemExit):
  154. module.parse_arguments('--config', 'myconfig', '--storage-quota', '5G')
  155. def test_parse_arguments_allows_init_and_prune():
  156. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  157. module.parse_arguments('--config', 'myconfig', 'init', '--encryption', 'repokey', 'prune')
  158. def test_parse_arguments_allows_init_and_create():
  159. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  160. module.parse_arguments('--config', 'myconfig', 'init', '--encryption', 'repokey', 'create')
  161. def test_parse_arguments_allows_repository_with_extract():
  162. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  163. module.parse_arguments(
  164. '--config', 'myconfig', 'extract', '--repository', 'test.borg', '--archive', 'test'
  165. )
  166. def test_parse_arguments_allows_repository_with_mount():
  167. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  168. module.parse_arguments(
  169. '--config',
  170. 'myconfig',
  171. 'mount',
  172. '--repository',
  173. 'test.borg',
  174. '--archive',
  175. 'test',
  176. '--mount-point',
  177. '/mnt',
  178. )
  179. def test_parse_arguments_allows_repository_with_list():
  180. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  181. module.parse_arguments('--config', 'myconfig', 'list', '--repository', 'test.borg')
  182. def test_parse_arguments_disallows_archive_unless_action_consumes_it():
  183. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  184. with pytest.raises(SystemExit):
  185. module.parse_arguments('--config', 'myconfig', '--archive', 'test')
  186. def test_parse_arguments_disallows_paths_unless_action_consumes_it():
  187. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  188. with pytest.raises(SystemExit):
  189. module.parse_arguments('--config', 'myconfig', '--path', 'test')
  190. def test_parse_arguments_disallows_other_actions_with_config_bootstrap():
  191. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  192. with pytest.raises(ValueError):
  193. module.parse_arguments('config', 'bootstrap', '--repository', 'test.borg', 'list')
  194. def test_parse_arguments_allows_archive_with_extract():
  195. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  196. module.parse_arguments('--config', 'myconfig', 'extract', '--archive', 'test')
  197. def test_parse_arguments_allows_archive_with_mount():
  198. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  199. module.parse_arguments(
  200. '--config', 'myconfig', 'mount', '--archive', 'test', '--mount-point', '/mnt'
  201. )
  202. def test_parse_arguments_allows_archive_with_restore():
  203. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  204. module.parse_arguments('--config', 'myconfig', 'restore', '--archive', 'test')
  205. def test_parse_arguments_allows_archive_with_list():
  206. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  207. module.parse_arguments('--config', 'myconfig', 'list', '--archive', 'test')
  208. def test_parse_arguments_requires_archive_with_extract():
  209. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  210. with pytest.raises(SystemExit):
  211. module.parse_arguments('--config', 'myconfig', 'extract')
  212. def test_parse_arguments_requires_archive_with_restore():
  213. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  214. with pytest.raises(SystemExit):
  215. module.parse_arguments('--config', 'myconfig', 'restore')
  216. def test_parse_arguments_requires_mount_point_with_mount():
  217. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  218. with pytest.raises(SystemExit):
  219. module.parse_arguments('--config', 'myconfig', 'mount', '--archive', 'test')
  220. def test_parse_arguments_requires_mount_point_with_umount():
  221. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  222. with pytest.raises(SystemExit):
  223. module.parse_arguments('--config', 'myconfig', 'umount')
  224. def test_parse_arguments_allows_progress_before_create():
  225. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  226. module.parse_arguments('--progress', 'create', 'list')
  227. def test_parse_arguments_allows_progress_after_create():
  228. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  229. module.parse_arguments('create', '--progress', 'list')
  230. def test_parse_arguments_allows_progress_and_extract():
  231. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  232. module.parse_arguments('--progress', 'extract', '--archive', 'test', 'list')
  233. def test_parse_arguments_disallows_progress_without_create():
  234. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  235. with pytest.raises(SystemExit):
  236. module.parse_arguments('--progress', 'list')
  237. def test_parse_arguments_with_stats_and_create_flags_does_not_raise():
  238. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  239. module.parse_arguments('--stats', 'create', 'list')
  240. def test_parse_arguments_with_stats_and_prune_flags_does_not_raise():
  241. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  242. module.parse_arguments('--stats', 'prune', 'list')
  243. def test_parse_arguments_with_stats_flag_but_no_create_or_prune_flag_raises_value_error():
  244. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  245. with pytest.raises(SystemExit):
  246. module.parse_arguments('--stats', 'list')
  247. def test_parse_arguments_with_list_and_create_flags_does_not_raise():
  248. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  249. module.parse_arguments('--list', 'create')
  250. def test_parse_arguments_with_list_and_prune_flags_does_not_raise():
  251. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  252. module.parse_arguments('--list', 'prune')
  253. def test_parse_arguments_with_list_flag_but_no_relevant_action_raises_value_error():
  254. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  255. with pytest.raises(SystemExit):
  256. module.parse_arguments('--list', 'rcreate')
  257. def test_parse_arguments_disallows_list_with_progress_for_create_action():
  258. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  259. with pytest.raises(ValueError):
  260. module.parse_arguments('create', '--list', '--progress')
  261. def test_parse_arguments_disallows_list_with_json_for_create_action():
  262. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  263. with pytest.raises(ValueError):
  264. module.parse_arguments('create', '--list', '--json')
  265. def test_parse_arguments_allows_json_with_list_or_info():
  266. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  267. module.parse_arguments('list', '--json')
  268. module.parse_arguments('info', '--json')
  269. def test_parse_arguments_disallows_json_with_both_list_and_info():
  270. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  271. with pytest.raises(ValueError):
  272. module.parse_arguments('list', 'info', '--json')
  273. def test_parse_arguments_disallows_json_with_both_list_and_rinfo():
  274. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  275. with pytest.raises(ValueError):
  276. module.parse_arguments('list', 'rinfo', '--json')
  277. def test_parse_arguments_disallows_json_with_both_rinfo_and_info():
  278. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  279. with pytest.raises(ValueError):
  280. module.parse_arguments('rinfo', 'info', '--json')
  281. def test_parse_arguments_disallows_transfer_with_both_archive_and_match_archives():
  282. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  283. with pytest.raises(ValueError):
  284. module.parse_arguments(
  285. 'transfer',
  286. '--source-repository',
  287. 'source.borg',
  288. '--archive',
  289. 'foo',
  290. '--match-archives',
  291. 'sh:*bar',
  292. )
  293. def test_parse_arguments_disallows_list_with_both_prefix_and_match_archives():
  294. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  295. with pytest.raises(ValueError):
  296. module.parse_arguments('list', '--prefix', 'foo', '--match-archives', 'sh:*bar')
  297. def test_parse_arguments_disallows_rlist_with_both_prefix_and_match_archives():
  298. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  299. with pytest.raises(ValueError):
  300. module.parse_arguments('rlist', '--prefix', 'foo', '--match-archives', 'sh:*bar')
  301. def test_parse_arguments_disallows_info_with_both_archive_and_match_archives():
  302. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  303. with pytest.raises(ValueError):
  304. module.parse_arguments('info', '--archive', 'foo', '--match-archives', 'sh:*bar')
  305. def test_parse_arguments_disallows_info_with_both_archive_and_prefix():
  306. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  307. with pytest.raises(ValueError):
  308. module.parse_arguments('info', '--archive', 'foo', '--prefix', 'bar')
  309. def test_parse_arguments_disallows_info_with_both_prefix_and_match_archives():
  310. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  311. with pytest.raises(ValueError):
  312. module.parse_arguments('info', '--prefix', 'foo', '--match-archives', 'sh:*bar')
  313. def test_parse_arguments_check_only_extract_does_not_raise_extract_subparser_error():
  314. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  315. module.parse_arguments('check', '--only', 'extract')
  316. def test_parse_arguments_extract_archive_check_does_not_raise_check_subparser_error():
  317. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  318. module.parse_arguments('extract', '--archive', 'check')
  319. def test_parse_arguments_extract_with_check_only_extract_does_not_raise():
  320. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  321. module.parse_arguments('extract', '--archive', 'name', 'check', '--only', 'extract')
  322. def test_merging_two_subparser_collections_merges_their_choices():
  323. top_level_parser = argparse.ArgumentParser()
  324. subparsers = top_level_parser.add_subparsers()
  325. subparser1 = subparsers.add_parser('subparser1')
  326. subparser2 = subparsers.add_parser('subparser2')
  327. subsubparsers = subparser2.add_subparsers()
  328. subsubparser1 = subsubparsers.add_parser('subsubparser1')
  329. merged_subparsers = argparse._SubParsersAction(
  330. None, None, metavar=None, dest='merged', parser_class=None
  331. )
  332. for name, subparser in subparsers.choices.items():
  333. merged_subparsers._name_parser_map[name] = subparser
  334. for name, subparser in subsubparsers.choices.items():
  335. merged_subparsers._name_parser_map[name] = subparser
  336. assert merged_subparsers.choices == {
  337. 'subparser1': subparser1,
  338. 'subparser2': subparser2,
  339. 'subsubparser1': subsubparser1,
  340. }