test_arguments.py 15 KB

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