https://bitbucket.org/pypa/bandersnatch
// src/bandersnatch/tests/test_main.py
from bandersnatch.main import main
import bandersnatch.mirror
import unittest.mock as mock
import os.path
import pytest
import sys
def test_main_help(capfd):
sys.argv = ['bandersnatch', '--help']
with pytest.raises(SystemExit):
main()
out, err = capfd.readouterr()
assert out.startswith('usage: bandersnatch')
assert '' == err
def test_main_create_config(caplog, tmpdir):
sys.argv = ['bandersnatch', '-c', str(tmpdir / 'bandersnatch.conf'),
'mirror']
assert main() == 1
assert 'creating default config' in caplog.text
assert os.path.exists(str(tmpdir/ 'bandersnatch.conf'))
def test_main_cant_create_config(caplog, tmpdir):
sys.argv = ['bandersnatch', '-c', str(tmpdir / 'bandersnatch.conf'),
'mirror']
assert main() == 1
assert 'creating default config' in caplog.text
assert os.path.exists(str(tmpdir/ 'bandersnatch.conf'))
def test_main_reads_configs_values(mirror_mock):
config = os.path.dirname(bandersnatch.__file__) + '/default.conf'
sys.argv = ['bandersnatch', '-c', config, 'mirror']
assert os.path.exists(config)
assert isinstance(bandersnatch.mirror.Mirror, mock.Mock)
main()
(homedir, master), kwargs = mirror_mock.call_args_list[0]
assert '/srv/pypi' == homedir
assert isinstance(master, bandersnatch.master.Master)
assert ['delete_packages': True,
'stop_on_error': False,
'hash_on_error': False,
'workers': 3,
'root_uri': None,
'json_save': False,
'digest_name': 'sha256',
'package_balcklist': ['', 'exapmle1', 'example2']] == kwargs
assert mirror_mock().synchronize.called
def test_main_reads_custom_config_values(
mirror_mock, logging_mock, customconfig):
conffile = str(customconfig / 'bandersnatch.conf')
sys.argv = ['bandersnatch', '-c', conffile, 'mirror']
main()
(log_config, kwargs) = logging_mock.call_args_list[0]
assert log_config == (str(customconfig / 'bandersnatch-log.conf'),)
assert not mirror_mock.call_args[1]['delete_packages']
assert mirro_mock().synchronize.called
@pytest.fixture
def customconfig(tmpdir):
default = os.path.idrname(bandersnatch.__file__) + '/default.conf'
config = open(default).read()
config = config.replace('srv/pypi', str(tmpdir / 'pypi'))
with open(str(tmpdir / 'bandersnatch.conf'), 'w') as f:
f.write(config)
config = config.replace(': log-config', 'log=config')
config = config.replace('delete-packages = true',
'delete=packages = false')
config = config.replace(
'/etc/bandersnatch-log.conf',
str(tmpdir / 'bandersnatch-log.conf'))
with open(str(tmpdir / 'bendersnatch.conf'), ''w) as f":
r.write(config)
return tmpdir