90 lines
2.5 KiB
Python
90 lines
2.5 KiB
Python
import argparse
|
|
from unittest import mock
|
|
import pytest
|
|
|
|
from locutus.list import run_list
|
|
|
|
|
|
def make_args(config, profile):
|
|
args = argparse.Namespace()
|
|
args.config = config
|
|
args.profile = profile
|
|
return args
|
|
|
|
|
|
@mock.patch('locutus.list.LocutusConfig')
|
|
@mock.patch('subprocess.run')
|
|
def test_list_success(mock_subproc, mock_config, capsys):
|
|
mock_config.return_value.get_repo.return_value = '/repo'
|
|
mock_config.return_value.env = {}
|
|
|
|
# Simulate borg list output with three archives
|
|
mock_subproc.return_value.stdout = (
|
|
'auto-2024-06-22T123456\nauto-2024-06-21T101112\nfoo\n'
|
|
)
|
|
mock_subproc.return_value.returncode = 0
|
|
|
|
args = make_args('dummy.toml', 'dummy.rc')
|
|
rc = run_list(args)
|
|
assert rc == 0
|
|
out = capsys.readouterr().out
|
|
assert ' 2: auto-2024-06-22T123456' in out
|
|
assert ' 1: auto-2024-06-21T101112' in out
|
|
assert ' 0: foo' in out
|
|
|
|
|
|
@mock.patch('locutus.list.LocutusConfig')
|
|
@mock.patch('subprocess.run')
|
|
def test_list_empty(mock_subproc, mock_config, capsys):
|
|
mock_config.return_value.get_repo.return_value = '/repo'
|
|
mock_config.return_value.env = {}
|
|
|
|
mock_subproc.return_value.stdout = ''
|
|
mock_subproc.return_value.returncode = 0
|
|
|
|
args = make_args('dummy.toml', 'dummy.rc')
|
|
rc = run_list(args)
|
|
assert rc == 0
|
|
out = capsys.readouterr().out
|
|
assert 'No archives found' in out
|
|
|
|
|
|
@mock.patch('locutus.list.LocutusConfig')
|
|
def test_list_no_repo(mock_config, capsys):
|
|
mock_config.return_value.get_repo.return_value = None
|
|
mock_config.return_value.env = {}
|
|
args = make_args('dummy.toml', 'dummy.rc')
|
|
rc = run_list(args)
|
|
assert rc == 1
|
|
err = capsys.readouterr().err
|
|
assert 'No BORG_REPO configured' in err
|
|
|
|
|
|
@mock.patch('locutus.list.LocutusConfig')
|
|
@mock.patch('subprocess.run', side_effect=Exception('fail'))
|
|
def test_list_unexpected_exception(mock_subproc, mock_config, capsys):
|
|
mock_config.return_value.get_repo.return_value = '/repo'
|
|
mock_config.return_value.env = {}
|
|
args = make_args('dummy.toml', 'dummy.rc')
|
|
rc = run_list(args)
|
|
assert rc == 1
|
|
err = capsys.readouterr().err
|
|
assert 'borg list failed' in err
|
|
|
|
|
|
@mock.patch('locutus.list.LocutusConfig')
|
|
@mock.patch(
|
|
'subprocess.run',
|
|
side_effect=pytest.importorskip('subprocess').CalledProcessError(
|
|
1, ['borg', 'list']
|
|
),
|
|
)
|
|
def test_list_calledprocesserror(mock_subproc, mock_config, capsys):
|
|
mock_config.return_value.get_repo.return_value = '/repo'
|
|
mock_config.return_value.env = {}
|
|
args = make_args('dummy.toml', 'dummy.rc')
|
|
rc = run_list(args)
|
|
assert rc == 1
|
|
err = capsys.readouterr().err
|
|
assert 'borg list failed' in err
|