test_arguments.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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_help_and_no_actions_shows_global_help(capsys):
  53. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  54. with pytest.raises(SystemExit) as exit:
  55. module.parse_arguments('--help')
  56. assert exit.value.code == 0
  57. captured = capsys.readouterr()
  58. assert 'global arguments:' in captured.out
  59. assert 'actions:' in captured.out
  60. def test_parse_arguments_with_help_and_action_shows_action_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('create', '--help')
  64. assert exit.value.code == 0
  65. captured = capsys.readouterr()
  66. assert 'global arguments:' not in captured.out
  67. assert 'actions:' not in captured.out
  68. assert 'create arguments:' in captured.out
  69. def test_parse_arguments_with_prune_action_leaves_other_actions_disabled():
  70. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  71. arguments = module.parse_arguments('prune')
  72. assert 'prune' in arguments
  73. assert 'create' not in arguments
  74. assert 'check' not in arguments
  75. def test_parse_arguments_with_dashed_prune_action_leaves_other_actions_disabled():
  76. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  77. arguments = module.parse_arguments('--prune')
  78. assert 'prune' in arguments
  79. assert 'create' not in arguments
  80. assert 'check' not in arguments
  81. def test_parse_arguments_with_multiple_actions_leaves_other_action_disabled():
  82. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  83. arguments = module.parse_arguments('create', 'check')
  84. assert 'prune' not in arguments
  85. assert 'create' in arguments
  86. assert 'check' in arguments
  87. def test_parse_arguments_with_multiple_dashed_actions_leaves_other_action_disabled():
  88. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  89. arguments = module.parse_arguments('--create', '--check')
  90. assert 'prune' not in arguments
  91. assert 'create' in arguments
  92. assert 'check' in arguments
  93. def test_parse_arguments_with_invalid_arguments_exits():
  94. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  95. with pytest.raises(SystemExit):
  96. module.parse_arguments('--posix-me-harder')
  97. def test_parse_arguments_disallows_deprecated_excludes_option():
  98. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  99. with pytest.raises(ValueError):
  100. module.parse_arguments('--config', 'myconfig', '--excludes', 'myexcludes')
  101. def test_parse_arguments_disallows_encryption_mode_without_init():
  102. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  103. with pytest.raises(SystemExit):
  104. module.parse_arguments('--config', 'myconfig', '--encryption', 'repokey')
  105. def test_parse_arguments_allows_encryption_mode_with_init():
  106. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  107. module.parse_arguments('--config', 'myconfig', 'init', '--encryption', 'repokey')
  108. def test_parse_arguments_allows_encryption_mode_with_dashed_init():
  109. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  110. module.parse_arguments('--config', 'myconfig', '--init', '--encryption', 'repokey')
  111. def test_parse_arguments_requires_encryption_mode_with_init():
  112. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  113. with pytest.raises(SystemExit):
  114. module.parse_arguments('--config', 'myconfig', 'init')
  115. def test_parse_arguments_disallows_append_only_without_init():
  116. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  117. with pytest.raises(SystemExit):
  118. module.parse_arguments('--config', 'myconfig', '--append-only')
  119. def test_parse_arguments_disallows_storage_quota_without_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', '--storage-quota', '5G')
  123. def test_parse_arguments_allows_init_and_prune():
  124. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  125. module.parse_arguments('--config', 'myconfig', 'init', '--encryption', 'repokey', 'prune')
  126. def test_parse_arguments_allows_init_and_create():
  127. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  128. module.parse_arguments('--config', 'myconfig', 'init', '--encryption', 'repokey', 'create')
  129. def test_parse_arguments_disallows_init_and_dry_run():
  130. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  131. with pytest.raises(ValueError):
  132. module.parse_arguments(
  133. '--config', 'myconfig', 'init', '--encryption', 'repokey', '--dry-run'
  134. )
  135. def test_parse_arguments_disallows_repository_without_extract_or_list():
  136. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  137. with pytest.raises(SystemExit):
  138. module.parse_arguments('--config', 'myconfig', '--repository', 'test.borg')
  139. def test_parse_arguments_allows_repository_with_extract():
  140. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  141. module.parse_arguments(
  142. '--config', 'myconfig', 'extract', '--repository', 'test.borg', '--archive', 'test'
  143. )
  144. def test_parse_arguments_allows_repository_with_list():
  145. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  146. module.parse_arguments('--config', 'myconfig', 'list', '--repository', 'test.borg')
  147. def test_parse_arguments_disallows_archive_without_extract_or_list():
  148. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  149. with pytest.raises(SystemExit):
  150. module.parse_arguments('--config', 'myconfig', '--archive', 'test')
  151. def test_parse_arguments_disallows_restore_paths_without_extract():
  152. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  153. with pytest.raises(SystemExit):
  154. module.parse_arguments('--config', 'myconfig', '--restore-path', 'test')
  155. def test_parse_arguments_allows_archive_with_extract():
  156. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  157. module.parse_arguments('--config', 'myconfig', 'extract', '--archive', 'test')
  158. def test_parse_arguments_allows_archive_with_dashed_extract():
  159. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  160. module.parse_arguments('--config', 'myconfig', '--extract', '--archive', 'test')
  161. def test_parse_arguments_allows_archive_with_list():
  162. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  163. module.parse_arguments('--config', 'myconfig', 'list', '--archive', 'test')
  164. def test_parse_arguments_requires_archive_with_extract():
  165. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  166. with pytest.raises(SystemExit):
  167. module.parse_arguments('--config', 'myconfig', 'extract')
  168. def test_parse_arguments_allows_progress_before_create():
  169. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  170. module.parse_arguments('--progress', 'create', 'list')
  171. def test_parse_arguments_allows_progress_after_create():
  172. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  173. module.parse_arguments('create', '--progress', 'list')
  174. def test_parse_arguments_allows_progress_and_extract():
  175. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  176. module.parse_arguments('--progress', 'extract', '--archive', 'test', 'list')
  177. def test_parse_arguments_disallows_progress_without_create():
  178. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  179. with pytest.raises(SystemExit):
  180. module.parse_arguments('--progress', 'list')
  181. def test_parse_arguments_with_stats_and_create_flags_does_not_raise():
  182. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  183. module.parse_arguments('--stats', 'create', 'list')
  184. def test_parse_arguments_with_stats_and_prune_flags_does_not_raise():
  185. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  186. module.parse_arguments('--stats', 'prune', 'list')
  187. def test_parse_arguments_with_stats_flag_but_no_create_or_prune_flag_raises_value_error():
  188. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  189. with pytest.raises(SystemExit):
  190. module.parse_arguments('--stats', 'list')
  191. def test_parse_arguments_with_just_stats_flag_does_not_raise():
  192. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  193. module.parse_arguments('--stats')
  194. def test_parse_arguments_allows_json_with_list_or_info():
  195. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  196. module.parse_arguments('list', '--json')
  197. module.parse_arguments('info', '--json')
  198. def test_parse_arguments_allows_json_with_dashed_info():
  199. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  200. module.parse_arguments('--info', '--json')
  201. def test_parse_arguments_disallows_json_with_both_list_and_info():
  202. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  203. with pytest.raises(ValueError):
  204. module.parse_arguments('list', 'info', '--json')