test_arguments.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. import pytest
  2. from flexmock import flexmock
  3. from borgmatic.commands import arguments as module
  4. def test_parse_arguments_with_no_arguments_uses_defaults():
  5. config_paths = ['default']
  6. flexmock(module.collect).should_receive('get_default_config_paths').and_return(config_paths)
  7. arguments = module.parse_arguments()
  8. global_arguments = arguments['global']
  9. assert global_arguments.config_paths == config_paths
  10. assert global_arguments.excludes_filename is None
  11. assert global_arguments.verbosity == 0
  12. assert global_arguments.syslog_verbosity == 0
  13. def test_parse_arguments_with_multiple_config_paths_parses_as_list():
  14. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  15. arguments = module.parse_arguments('--config', 'myconfig', 'otherconfig')
  16. global_arguments = arguments['global']
  17. assert global_arguments.config_paths == ['myconfig', 'otherconfig']
  18. assert global_arguments.verbosity == 0
  19. assert global_arguments.syslog_verbosity == 0
  20. def test_parse_arguments_with_verbosity_overrides_default():
  21. config_paths = ['default']
  22. flexmock(module.collect).should_receive('get_default_config_paths').and_return(config_paths)
  23. arguments = module.parse_arguments('--verbosity', '1')
  24. global_arguments = arguments['global']
  25. assert global_arguments.config_paths == config_paths
  26. assert global_arguments.excludes_filename is None
  27. assert global_arguments.verbosity == 1
  28. assert global_arguments.syslog_verbosity == 0
  29. def test_parse_arguments_with_syslog_verbosity_overrides_default():
  30. config_paths = ['default']
  31. flexmock(module.collect).should_receive('get_default_config_paths').and_return(config_paths)
  32. arguments = module.parse_arguments('--syslog-verbosity', '2')
  33. global_arguments = arguments['global']
  34. assert global_arguments.config_paths == config_paths
  35. assert global_arguments.excludes_filename is None
  36. assert global_arguments.verbosity == 0
  37. assert global_arguments.syslog_verbosity == 2
  38. def test_parse_arguments_with_list_json_overrides_default():
  39. arguments = module.parse_arguments('list', '--json')
  40. assert 'list' in arguments
  41. assert arguments['list'].json is True
  42. def test_parse_arguments_with_dashed_list_json_overrides_default():
  43. arguments = module.parse_arguments('--list', '--json')
  44. assert 'list' in arguments
  45. assert arguments['list'].json is True
  46. def test_parse_arguments_with_no_actions_defaults_to_all_actions_enabled():
  47. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  48. arguments = module.parse_arguments()
  49. assert 'prune' in arguments
  50. assert 'create' in arguments
  51. assert 'check' in arguments
  52. def test_parse_arguments_with_no_actions_passes_argument_to_relevant_actions():
  53. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  54. arguments = module.parse_arguments('--stats')
  55. assert 'prune' in arguments
  56. assert arguments['prune'].stats
  57. assert 'create' in arguments
  58. assert arguments['create'].stats
  59. assert 'check' in arguments
  60. def test_parse_arguments_with_help_and_no_actions_shows_global_help(capsys):
  61. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  62. with pytest.raises(SystemExit) as exit:
  63. module.parse_arguments('--help')
  64. assert exit.value.code == 0
  65. captured = capsys.readouterr()
  66. assert 'global arguments:' in captured.out
  67. assert 'actions:' in captured.out
  68. def test_parse_arguments_with_help_and_action_shows_action_help(capsys):
  69. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  70. with pytest.raises(SystemExit) as exit:
  71. module.parse_arguments('create', '--help')
  72. assert exit.value.code == 0
  73. captured = capsys.readouterr()
  74. assert 'global arguments:' not in captured.out
  75. assert 'actions:' not in captured.out
  76. assert 'create arguments:' in captured.out
  77. def test_parse_arguments_with_prune_action_leaves_other_actions_disabled():
  78. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  79. arguments = module.parse_arguments('prune')
  80. assert 'prune' in arguments
  81. assert 'create' not in arguments
  82. assert 'check' not in arguments
  83. def test_parse_arguments_with_dashed_prune_action_leaves_other_actions_disabled():
  84. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  85. arguments = module.parse_arguments('--prune')
  86. assert 'prune' in arguments
  87. assert 'create' not in arguments
  88. assert 'check' not in arguments
  89. def test_parse_arguments_with_multiple_actions_leaves_other_action_disabled():
  90. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  91. arguments = module.parse_arguments('create', 'check')
  92. assert 'prune' not in arguments
  93. assert 'create' in arguments
  94. assert 'check' in arguments
  95. def test_parse_arguments_with_multiple_dashed_actions_leaves_other_action_disabled():
  96. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  97. arguments = module.parse_arguments('--create', '--check')
  98. assert 'prune' not in arguments
  99. assert 'create' in arguments
  100. assert 'check' in arguments
  101. def test_parse_arguments_with_invalid_arguments_exits():
  102. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  103. with pytest.raises(SystemExit):
  104. module.parse_arguments('--posix-me-harder')
  105. def test_parse_arguments_disallows_deprecated_excludes_option():
  106. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  107. with pytest.raises(ValueError):
  108. module.parse_arguments('--config', 'myconfig', '--excludes', 'myexcludes')
  109. def test_parse_arguments_disallows_encryption_mode_without_init():
  110. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  111. with pytest.raises(SystemExit):
  112. module.parse_arguments('--config', 'myconfig', '--encryption', 'repokey')
  113. def test_parse_arguments_allows_encryption_mode_with_init():
  114. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  115. module.parse_arguments('--config', 'myconfig', 'init', '--encryption', 'repokey')
  116. def test_parse_arguments_allows_encryption_mode_with_dashed_init():
  117. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  118. module.parse_arguments('--config', 'myconfig', '--init', '--encryption', 'repokey')
  119. def test_parse_arguments_requires_encryption_mode_with_init():
  120. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  121. with pytest.raises(SystemExit):
  122. module.parse_arguments('--config', 'myconfig', 'init')
  123. def test_parse_arguments_disallows_append_only_without_init():
  124. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  125. with pytest.raises(SystemExit):
  126. module.parse_arguments('--config', 'myconfig', '--append-only')
  127. def test_parse_arguments_disallows_storage_quota_without_init():
  128. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  129. with pytest.raises(SystemExit):
  130. module.parse_arguments('--config', 'myconfig', '--storage-quota', '5G')
  131. def test_parse_arguments_allows_init_and_prune():
  132. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  133. module.parse_arguments('--config', 'myconfig', 'init', '--encryption', 'repokey', 'prune')
  134. def test_parse_arguments_allows_init_and_create():
  135. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  136. module.parse_arguments('--config', 'myconfig', 'init', '--encryption', 'repokey', 'create')
  137. def test_parse_arguments_disallows_init_and_dry_run():
  138. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  139. with pytest.raises(ValueError):
  140. module.parse_arguments(
  141. '--config', 'myconfig', 'init', '--encryption', 'repokey', '--dry-run'
  142. )
  143. def test_parse_arguments_disallows_glob_archives_with_successful():
  144. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  145. with pytest.raises(ValueError):
  146. module.parse_arguments(
  147. '--config', 'myconfig', 'list', '--glob-archives', '*glob*', '--successful'
  148. )
  149. def test_parse_arguments_disallows_repository_without_extract_or_list():
  150. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  151. with pytest.raises(SystemExit):
  152. module.parse_arguments('--config', 'myconfig', '--repository', 'test.borg')
  153. def test_parse_arguments_allows_repository_with_extract():
  154. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  155. module.parse_arguments(
  156. '--config', 'myconfig', 'extract', '--repository', 'test.borg', '--archive', 'test'
  157. )
  158. def test_parse_arguments_allows_repository_with_list():
  159. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  160. module.parse_arguments('--config', 'myconfig', 'list', '--repository', 'test.borg')
  161. def test_parse_arguments_disallows_archive_without_extract_restore_or_list():
  162. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  163. with pytest.raises(SystemExit):
  164. module.parse_arguments('--config', 'myconfig', '--archive', 'test')
  165. def test_parse_arguments_disallows_paths_without_extract():
  166. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  167. with pytest.raises(SystemExit):
  168. module.parse_arguments('--config', 'myconfig', '--path', 'test')
  169. def test_parse_arguments_allows_archive_with_extract():
  170. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  171. module.parse_arguments('--config', 'myconfig', 'extract', '--archive', 'test')
  172. def test_parse_arguments_allows_archive_with_dashed_extract():
  173. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  174. module.parse_arguments('--config', 'myconfig', '--extract', '--archive', 'test')
  175. def test_parse_arguments_allows_archive_with_restore():
  176. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  177. module.parse_arguments('--config', 'myconfig', 'restore', '--archive', 'test')
  178. def test_parse_arguments_allows_archive_with_dashed_restore():
  179. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  180. module.parse_arguments('--config', 'myconfig', '--restore', '--archive', 'test')
  181. def test_parse_arguments_allows_archive_with_list():
  182. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  183. module.parse_arguments('--config', 'myconfig', 'list', '--archive', 'test')
  184. def test_parse_arguments_requires_archive_with_extract():
  185. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  186. with pytest.raises(SystemExit):
  187. module.parse_arguments('--config', 'myconfig', 'extract')
  188. def test_parse_arguments_requires_archive_with_restore():
  189. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  190. with pytest.raises(SystemExit):
  191. module.parse_arguments('--config', 'myconfig', 'restore')
  192. def test_parse_arguments_allows_progress_before_create():
  193. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  194. module.parse_arguments('--progress', 'create', 'list')
  195. def test_parse_arguments_allows_progress_after_create():
  196. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  197. module.parse_arguments('create', '--progress', 'list')
  198. def test_parse_arguments_allows_progress_and_extract():
  199. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  200. module.parse_arguments('--progress', 'extract', '--archive', 'test', 'list')
  201. def test_parse_arguments_allows_progress_and_restore():
  202. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  203. module.parse_arguments('--progress', 'restore', '--archive', 'test', 'list')
  204. def test_parse_arguments_disallows_progress_without_create():
  205. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  206. with pytest.raises(SystemExit):
  207. module.parse_arguments('--progress', 'list')
  208. def test_parse_arguments_with_stats_and_create_flags_does_not_raise():
  209. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  210. module.parse_arguments('--stats', 'create', 'list')
  211. def test_parse_arguments_with_stats_and_prune_flags_does_not_raise():
  212. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  213. module.parse_arguments('--stats', 'prune', 'list')
  214. def test_parse_arguments_with_stats_flag_but_no_create_or_prune_flag_raises_value_error():
  215. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  216. with pytest.raises(SystemExit):
  217. module.parse_arguments('--stats', 'list')
  218. def test_parse_arguments_allows_json_with_list_or_info():
  219. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  220. module.parse_arguments('list', '--json')
  221. module.parse_arguments('info', '--json')
  222. def test_parse_arguments_allows_json_with_dashed_info():
  223. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  224. module.parse_arguments('--info', '--json')
  225. def test_parse_arguments_disallows_json_with_both_list_and_info():
  226. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  227. with pytest.raises(ValueError):
  228. module.parse_arguments('list', 'info', '--json')
  229. def test_parse_arguments_check_only_extract_does_not_raise_extract_subparser_error():
  230. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  231. module.parse_arguments('check', '--only', 'extract')
  232. def test_parse_arguments_extract_archive_check_does_not_raise_check_subparser_error():
  233. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  234. module.parse_arguments('extract', '--archive', 'check')
  235. def test_parse_arguments_extract_with_check_only_extract_does_not_raise():
  236. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  237. module.parse_arguments('extract', '--archive', 'name', 'check', '--only', 'extract')