| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637 | import pytestfrom flexmock import flexmockfrom borgmatic.commands import arguments as moduledef test_parse_arguments_with_no_arguments_uses_defaults():    config_paths = ['default']    flexmock(module.collect).should_receive('get_default_config_paths').and_return(config_paths)    arguments = module.parse_arguments()    global_arguments = arguments['global']    assert global_arguments.config_paths == config_paths    assert global_arguments.verbosity == 0    assert global_arguments.syslog_verbosity == -2    assert global_arguments.log_file_verbosity == 1    assert global_arguments.monitoring_verbosity == 1def test_parse_arguments_with_multiple_config_flags_parses_as_list():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    arguments = module.parse_arguments('--config', 'myconfig', '--config', 'otherconfig')    global_arguments = arguments['global']    assert global_arguments.config_paths == ['myconfig', 'otherconfig']    assert global_arguments.verbosity == 0    assert global_arguments.syslog_verbosity == -2    assert global_arguments.log_file_verbosity == 1    assert global_arguments.monitoring_verbosity == 1def test_parse_arguments_with_action_after_config_path_omits_action():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    arguments = module.parse_arguments('--config', 'myconfig', 'list', '--json')    global_arguments = arguments['global']    assert global_arguments.config_paths == ['myconfig']    assert 'list' in arguments    assert arguments['list'].jsondef test_parse_arguments_with_action_after_config_path_omits_aliased_action():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    arguments = module.parse_arguments('--config', 'myconfig', 'init', '--encryption', 'repokey')    global_arguments = arguments['global']    assert global_arguments.config_paths == ['myconfig']    assert 'rcreate' in arguments    assert arguments['rcreate'].encryption_mode == 'repokey'def test_parse_arguments_with_action_and_positional_arguments_after_config_path_omits_action_and_arguments():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    arguments = module.parse_arguments('--config', 'myconfig', 'borg', 'key', 'export')    global_arguments = arguments['global']    assert global_arguments.config_paths == ['myconfig']    assert 'borg' in arguments    assert arguments['borg'].options == ['key', 'export']def test_parse_arguments_with_verbosity_overrides_default():    config_paths = ['default']    flexmock(module.collect).should_receive('get_default_config_paths').and_return(config_paths)    arguments = module.parse_arguments('--verbosity', '1')    global_arguments = arguments['global']    assert global_arguments.config_paths == config_paths    assert global_arguments.verbosity == 1    assert global_arguments.syslog_verbosity == -2    assert global_arguments.log_file_verbosity == 1    assert global_arguments.monitoring_verbosity == 1def test_parse_arguments_with_syslog_verbosity_overrides_default():    config_paths = ['default']    flexmock(module.collect).should_receive('get_default_config_paths').and_return(config_paths)    arguments = module.parse_arguments('--syslog-verbosity', '2')    global_arguments = arguments['global']    assert global_arguments.config_paths == config_paths    assert global_arguments.verbosity == 0    assert global_arguments.syslog_verbosity == 2    assert global_arguments.log_file_verbosity == 1    assert global_arguments.monitoring_verbosity == 1def test_parse_arguments_with_log_file_verbosity_overrides_default():    config_paths = ['default']    flexmock(module.collect).should_receive('get_default_config_paths').and_return(config_paths)    arguments = module.parse_arguments('--log-file-verbosity', '-1')    global_arguments = arguments['global']    assert global_arguments.config_paths == config_paths    assert global_arguments.verbosity == 0    assert global_arguments.syslog_verbosity == -2    assert global_arguments.log_file_verbosity == -1    assert global_arguments.monitoring_verbosity == 1def test_parse_arguments_with_single_override_parses():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    arguments = module.parse_arguments('--override', 'foo.bar=baz')    global_arguments = arguments['global']    assert global_arguments.overrides == ['foo.bar=baz']def test_parse_arguments_with_multiple_overrides_flags_parses():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    arguments = module.parse_arguments(        '--override', 'foo.bar=baz', '--override', 'foo.quux=7', '--override', 'this.that=8'    )    global_arguments = arguments['global']    assert global_arguments.overrides == ['foo.bar=baz', 'foo.quux=7', 'this.that=8']def test_parse_arguments_with_list_json_overrides_default():    arguments = module.parse_arguments('list', '--json')    assert 'list' in arguments    assert arguments['list'].json is Truedef test_parse_arguments_with_no_actions_defaults_to_all_actions_enabled():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    arguments = module.parse_arguments()    assert 'prune' in arguments    assert 'create' in arguments    assert 'check' in argumentsdef test_parse_arguments_with_no_actions_passes_argument_to_relevant_actions():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    arguments = module.parse_arguments('--stats', '--list')    assert 'prune' in arguments    assert arguments['prune'].stats    assert arguments['prune'].list_archives    assert 'create' in arguments    assert arguments['create'].stats    assert arguments['create'].list_files    assert 'check' in argumentsdef test_parse_arguments_with_help_and_no_actions_shows_global_help(capsys):    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    with pytest.raises(SystemExit) as exit:        module.parse_arguments('--help')    assert exit.value.code == 0    captured = capsys.readouterr()    assert 'global arguments:' in captured.out    assert 'actions:' in captured.outdef test_parse_arguments_with_help_and_action_shows_action_help(capsys):    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    with pytest.raises(SystemExit) as exit:        module.parse_arguments('create', '--help')    assert exit.value.code == 0    captured = capsys.readouterr()    assert 'global arguments:' not in captured.out    assert 'actions:' not in captured.out    assert 'create arguments:' in captured.outdef test_parse_arguments_with_action_before_global_options_parses_options():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    arguments = module.parse_arguments('prune', '--verbosity', '2')    assert 'prune' in arguments    assert arguments['global'].verbosity == 2def test_parse_arguments_with_global_options_before_action_parses_options():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    arguments = module.parse_arguments('--verbosity', '2', 'prune')    assert 'prune' in arguments    assert arguments['global'].verbosity == 2def test_parse_arguments_with_prune_action_leaves_other_actions_disabled():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    arguments = module.parse_arguments('prune')    assert 'prune' in arguments    assert 'create' not in arguments    assert 'check' not in argumentsdef test_parse_arguments_with_multiple_actions_leaves_other_action_disabled():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    arguments = module.parse_arguments('create', 'check')    assert 'prune' not in arguments    assert 'create' in arguments    assert 'check' in argumentsdef test_parse_arguments_disallows_invalid_argument():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    with pytest.raises(ValueError):        module.parse_arguments('--posix-me-harder')def test_parse_arguments_disallows_encryption_mode_without_init():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    with pytest.raises(ValueError):        module.parse_arguments('--config', 'myconfig', '--encryption', 'repokey')def test_parse_arguments_allows_encryption_mode_with_init():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    module.parse_arguments('--config', 'myconfig', 'init', '--encryption', 'repokey')def test_parse_arguments_requires_encryption_mode_with_init():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    with pytest.raises(SystemExit):        module.parse_arguments('--config', 'myconfig', 'init')def test_parse_arguments_disallows_append_only_without_init():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    with pytest.raises(ValueError):        module.parse_arguments('--config', 'myconfig', '--append-only')def test_parse_arguments_disallows_storage_quota_without_init():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    with pytest.raises(ValueError):        module.parse_arguments('--config', 'myconfig', '--storage-quota', '5G')def test_parse_arguments_allows_init_and_prune():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    module.parse_arguments('--config', 'myconfig', 'init', '--encryption', 'repokey', 'prune')def test_parse_arguments_allows_init_and_create():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    module.parse_arguments('--config', 'myconfig', 'init', '--encryption', 'repokey', 'create')def test_parse_arguments_allows_repository_with_extract():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    module.parse_arguments(        '--config', 'myconfig', 'extract', '--repository', 'test.borg', '--archive', 'test'    )def test_parse_arguments_allows_repository_with_mount():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    module.parse_arguments(        '--config',        'myconfig',        'mount',        '--repository',        'test.borg',        '--archive',        'test',        '--mount-point',        '/mnt',    )def test_parse_arguments_allows_repository_with_list():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    module.parse_arguments('--config', 'myconfig', 'list', '--repository', 'test.borg')def test_parse_arguments_disallows_archive_unless_action_consumes_it():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    with pytest.raises(ValueError):        module.parse_arguments('--config', 'myconfig', '--archive', 'test')def test_parse_arguments_disallows_paths_unless_action_consumes_it():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    with pytest.raises(ValueError):        module.parse_arguments('--config', 'myconfig', '--path', 'test')def test_parse_arguments_disallows_other_actions_with_config_bootstrap():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    with pytest.raises(ValueError):        module.parse_arguments('config', 'bootstrap', '--repository', 'test.borg', 'list')def test_parse_arguments_allows_archive_with_extract():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    module.parse_arguments('--config', 'myconfig', 'extract', '--archive', 'test')def test_parse_arguments_allows_archive_with_mount():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    module.parse_arguments(        '--config', 'myconfig', 'mount', '--archive', 'test', '--mount-point', '/mnt'    )def test_parse_arguments_allows_archive_with_restore():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    module.parse_arguments('--config', 'myconfig', 'restore', '--archive', 'test')def test_parse_arguments_allows_archive_with_list():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    module.parse_arguments('--config', 'myconfig', 'list', '--archive', 'test')def test_parse_arguments_requires_archive_with_extract():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    with pytest.raises(SystemExit):        module.parse_arguments('--config', 'myconfig', 'extract')def test_parse_arguments_requires_archive_with_restore():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    with pytest.raises(SystemExit):        module.parse_arguments('--config', 'myconfig', 'restore')def test_parse_arguments_requires_mount_point_with_mount():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    with pytest.raises(SystemExit):        module.parse_arguments('--config', 'myconfig', 'mount', '--archive', 'test')def test_parse_arguments_requires_mount_point_with_umount():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    with pytest.raises(SystemExit):        module.parse_arguments('--config', 'myconfig', 'umount')def test_parse_arguments_allows_progress_before_create():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    module.parse_arguments('--progress', 'create', 'list')def test_parse_arguments_allows_progress_after_create():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    module.parse_arguments('create', '--progress', 'list')def test_parse_arguments_allows_progress_and_extract():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    module.parse_arguments('--progress', 'extract', '--archive', 'test', 'list')def test_parse_arguments_disallows_progress_without_create():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    with pytest.raises(ValueError):        module.parse_arguments('--progress', 'list')def test_parse_arguments_with_stats_and_create_flags_does_not_raise():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    module.parse_arguments('--stats', 'create', 'list')def test_parse_arguments_with_stats_and_prune_flags_does_not_raise():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    module.parse_arguments('--stats', 'prune', 'list')def test_parse_arguments_with_stats_flag_but_no_create_or_prune_flag_raises_value_error():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    with pytest.raises(ValueError):        module.parse_arguments('--stats', 'list')def test_parse_arguments_with_list_and_create_flags_does_not_raise():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    module.parse_arguments('--list', 'create')def test_parse_arguments_with_list_and_prune_flags_does_not_raise():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    module.parse_arguments('--list', 'prune')def test_parse_arguments_with_list_flag_but_no_relevant_action_raises_value_error():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    with pytest.raises(SystemExit):        module.parse_arguments('--list', 'rcreate')def test_parse_arguments_disallows_list_with_progress_for_create_action():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    with pytest.raises(ValueError):        module.parse_arguments('create', '--list', '--progress')def test_parse_arguments_disallows_list_with_json_for_create_action():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    with pytest.raises(ValueError):        module.parse_arguments('create', '--list', '--json')def test_parse_arguments_allows_json_with_list_or_info():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    module.parse_arguments('list', '--json')    module.parse_arguments('info', '--json')def test_parse_arguments_disallows_json_with_both_list_and_info():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    with pytest.raises(ValueError):        module.parse_arguments('list', 'info', '--json')def test_parse_arguments_disallows_json_with_both_list_and_rinfo():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    with pytest.raises(ValueError):        module.parse_arguments('list', 'rinfo', '--json')def test_parse_arguments_disallows_json_with_both_rinfo_and_info():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    with pytest.raises(ValueError):        module.parse_arguments('rinfo', 'info', '--json')def test_parse_arguments_disallows_transfer_with_both_archive_and_match_archives():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    with pytest.raises(ValueError):        module.parse_arguments(            'transfer',            '--source-repository',            'source.borg',            '--archive',            'foo',            '--match-archives',            'sh:*bar',        )def test_parse_arguments_disallows_list_with_both_prefix_and_match_archives():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    with pytest.raises(ValueError):        module.parse_arguments('list', '--prefix', 'foo', '--match-archives', 'sh:*bar')def test_parse_arguments_disallows_rlist_with_both_prefix_and_match_archives():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    with pytest.raises(ValueError):        module.parse_arguments('rlist', '--prefix', 'foo', '--match-archives', 'sh:*bar')def test_parse_arguments_disallows_info_with_both_archive_and_match_archives():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    with pytest.raises(ValueError):        module.parse_arguments('info', '--archive', 'foo', '--match-archives', 'sh:*bar')def test_parse_arguments_disallows_info_with_both_archive_and_prefix():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    with pytest.raises(ValueError):        module.parse_arguments('info', '--archive', 'foo', '--prefix', 'bar')def test_parse_arguments_disallows_info_with_both_prefix_and_match_archives():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    with pytest.raises(ValueError):        module.parse_arguments('info', '--prefix', 'foo', '--match-archives', 'sh:*bar')def test_parse_arguments_check_only_extract_does_not_raise_extract_subparser_error():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    module.parse_arguments('check', '--only', 'extract')def test_parse_arguments_extract_archive_check_does_not_raise_check_subparser_error():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    module.parse_arguments('extract', '--archive', 'check')def test_parse_arguments_extract_with_check_only_extract_does_not_raise():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    module.parse_arguments('extract', '--archive', 'name', 'check', '--only', 'extract')def test_parse_arguments_bootstrap_without_config_errors():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    with pytest.raises(ValueError):        module.parse_arguments('bootstrap')def test_parse_arguments_config_with_no_subaction_errors():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    with pytest.raises(ValueError):        module.parse_arguments('config')def test_parse_arguments_config_with_help_shows_config_help(capsys):    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    with pytest.raises(SystemExit) as exit:        module.parse_arguments('config', '--help')    assert exit.value.code == 0    captured = capsys.readouterr()    assert 'global arguments:' not in captured.out    assert 'config arguments:' in captured.out    assert 'config sub-actions:' in captured.outdef test_parse_arguments_config_with_subaction_but_missing_flags_errors():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    with pytest.raises(SystemExit) as exit:        module.parse_arguments('config', 'bootstrap')    assert exit.value.code == 2def test_parse_arguments_config_with_subaction_and_help_shows_subaction_help(capsys):    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    with pytest.raises(SystemExit) as exit:        module.parse_arguments('config', 'bootstrap', '--help')    assert exit.value.code == 0    captured = capsys.readouterr()    assert 'config bootstrap arguments:' in captured.outdef test_parse_arguments_config_with_subaction_and_required_flags_does_not_raise():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    module.parse_arguments('config', 'bootstrap', '--repository', 'repo.borg')def test_parse_arguments_config_with_subaction_and_global_flags_at_start_does_not_raise():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    module.parse_arguments('--verbosity', '1', 'config', 'bootstrap', '--repository', 'repo.borg')def test_parse_arguments_config_with_subaction_and_global_flags_at_end_does_not_raise():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    module.parse_arguments('config', 'bootstrap', '--repository', 'repo.borg', '--verbosity', '1')def test_parse_arguments_config_with_subaction_and_explicit_config_file_does_not_raise():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    module.parse_arguments(        'config', 'bootstrap', '--repository', 'repo.borg', '--config', 'test.yaml'    )def test_parse_arguments_with_borg_action_and_dry_run_raises():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    with pytest.raises(ValueError):        module.parse_arguments('--dry-run', 'borg', 'list')def test_parse_arguments_with_borg_action_and_no_dry_run_does_not_raise():    flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])    module.parse_arguments('borg', 'list')
 |