mirror of
https://github.com/samsonjs/vdirsyncer.git
synced 2026-04-27 14:57:41 +00:00
Testsuite refactoring
This commit is contained in:
parent
548b397dbb
commit
c2ec4cd3a3
1 changed files with 71 additions and 102 deletions
|
|
@ -16,6 +16,30 @@ import pytest
|
||||||
import vdirsyncer.cli as cli
|
import vdirsyncer.cli as cli
|
||||||
|
|
||||||
|
|
||||||
|
class _CustomRunner(object):
|
||||||
|
def __init__(self, tmpdir):
|
||||||
|
self.tmpdir = tmpdir
|
||||||
|
self.cfg = tmpdir.join('config')
|
||||||
|
self.runner = CliRunner()
|
||||||
|
|
||||||
|
def invoke(self, args, env=None, **kwargs):
|
||||||
|
env = env or {}
|
||||||
|
env.setdefault('VDIRSYNCER_CONFIG', str(self.cfg))
|
||||||
|
return self.runner.invoke(cli.app, args, env=env, **kwargs)
|
||||||
|
|
||||||
|
def write_with_general(self, data):
|
||||||
|
self.cfg.write(dedent('''
|
||||||
|
[general]
|
||||||
|
status_path = {}/status/
|
||||||
|
''').format(str(self.tmpdir)))
|
||||||
|
self.cfg.write(data, mode='a')
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def runner(tmpdir, monkeypatch):
|
||||||
|
return _CustomRunner(tmpdir)
|
||||||
|
|
||||||
|
|
||||||
def test_load_config(monkeypatch):
|
def test_load_config(monkeypatch):
|
||||||
f = io.StringIO(dedent(u'''
|
f = io.StringIO(dedent(u'''
|
||||||
[general]
|
[general]
|
||||||
|
|
@ -85,12 +109,8 @@ def test_parse_pairs_args():
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def test_simple_run(tmpdir):
|
def test_simple_run(tmpdir, runner):
|
||||||
config_file = tmpdir.join('config')
|
runner.write_with_general(dedent('''
|
||||||
config_file.write(dedent('''
|
|
||||||
[general]
|
|
||||||
status_path = {0}/status/
|
|
||||||
|
|
||||||
[pair my_pair]
|
[pair my_pair]
|
||||||
a = my_a
|
a = my_a
|
||||||
b = my_b
|
b = my_b
|
||||||
|
|
@ -106,22 +126,17 @@ def test_simple_run(tmpdir):
|
||||||
fileext = .txt
|
fileext = .txt
|
||||||
''').format(str(tmpdir)))
|
''').format(str(tmpdir)))
|
||||||
|
|
||||||
runner = CliRunner(env={'VDIRSYNCER_CONFIG': str(config_file)})
|
result = runner.invoke(['sync'])
|
||||||
result = runner.invoke(cli.app, ['sync'])
|
|
||||||
assert not result.exception
|
assert not result.exception
|
||||||
|
|
||||||
tmpdir.join('path_a/haha.txt').write('UID:haha')
|
tmpdir.join('path_a/haha.txt').write('UID:haha')
|
||||||
result = runner.invoke(cli.app, ['sync'])
|
result = runner.invoke(['sync'])
|
||||||
assert 'Copying (uploading) item haha to my_b' in result.output
|
assert 'Copying (uploading) item haha to my_b' in result.output
|
||||||
assert tmpdir.join('path_b/haha.txt').read() == 'UID:haha'
|
assert tmpdir.join('path_b/haha.txt').read() == 'UID:haha'
|
||||||
|
|
||||||
|
|
||||||
def test_empty_storage(tmpdir):
|
def test_empty_storage(tmpdir, runner):
|
||||||
config_file = tmpdir.join('config')
|
runner.write_with_general(dedent('''
|
||||||
config_file.write(dedent('''
|
|
||||||
[general]
|
|
||||||
status_path = {0}/status/
|
|
||||||
|
|
||||||
[pair my_pair]
|
[pair my_pair]
|
||||||
a = my_a
|
a = my_a
|
||||||
b = my_b
|
b = my_b
|
||||||
|
|
@ -137,14 +152,13 @@ def test_empty_storage(tmpdir):
|
||||||
fileext = .txt
|
fileext = .txt
|
||||||
''').format(str(tmpdir)))
|
''').format(str(tmpdir)))
|
||||||
|
|
||||||
runner = CliRunner(env={'VDIRSYNCER_CONFIG': str(config_file)})
|
result = runner.invoke(['sync'])
|
||||||
result = runner.invoke(cli.app, ['sync'])
|
|
||||||
assert not result.exception
|
assert not result.exception
|
||||||
|
|
||||||
tmpdir.join('path_a/haha.txt').write('UID:haha')
|
tmpdir.join('path_a/haha.txt').write('UID:haha')
|
||||||
result = runner.invoke(cli.app, ['sync'])
|
result = runner.invoke(['sync'])
|
||||||
tmpdir.join('path_b/haha.txt').remove()
|
tmpdir.join('path_b/haha.txt').remove()
|
||||||
result = runner.invoke(cli.app, ['sync'])
|
result = runner.invoke(['sync'])
|
||||||
lines = result.output.splitlines()
|
lines = result.output.splitlines()
|
||||||
assert len(lines) == 2
|
assert len(lines) == 2
|
||||||
assert lines[0] == 'Syncing my_pair'
|
assert lines[0] == 'Syncing my_pair'
|
||||||
|
|
@ -153,9 +167,8 @@ def test_empty_storage(tmpdir):
|
||||||
assert result.exception
|
assert result.exception
|
||||||
|
|
||||||
|
|
||||||
def test_missing_general_section(tmpdir):
|
def test_missing_general_section(tmpdir, runner):
|
||||||
config_file = tmpdir.join('config')
|
runner.cfg.write(dedent('''
|
||||||
config_file.write(dedent('''
|
|
||||||
[pair my_pair]
|
[pair my_pair]
|
||||||
a = my_a
|
a = my_a
|
||||||
b = my_b
|
b = my_b
|
||||||
|
|
@ -171,28 +184,18 @@ def test_missing_general_section(tmpdir):
|
||||||
fileext = .txt
|
fileext = .txt
|
||||||
''').format(str(tmpdir)))
|
''').format(str(tmpdir)))
|
||||||
|
|
||||||
runner = CliRunner()
|
result = runner.invoke(['sync'])
|
||||||
result = runner.invoke(
|
|
||||||
cli.app, ['sync'],
|
|
||||||
env={'VDIRSYNCER_CONFIG': str(config_file)}
|
|
||||||
)
|
|
||||||
assert result.exception
|
assert result.exception
|
||||||
assert result.output.startswith('critical:')
|
assert result.output.startswith('critical:')
|
||||||
assert 'unable to find general section' in result.output.lower()
|
assert 'unable to find general section' in result.output.lower()
|
||||||
|
|
||||||
|
|
||||||
def test_wrong_general_section(tmpdir):
|
def test_wrong_general_section(tmpdir, runner):
|
||||||
config_file = tmpdir.join('config')
|
runner.cfg.write(dedent('''
|
||||||
config_file.write(dedent('''
|
|
||||||
[general]
|
[general]
|
||||||
wrong = true
|
wrong = true
|
||||||
'''))
|
'''))
|
||||||
|
result = runner.invoke(['sync'])
|
||||||
runner = CliRunner()
|
|
||||||
result = runner.invoke(
|
|
||||||
cli.app, ['sync'],
|
|
||||||
env={'VDIRSYNCER_CONFIG': str(config_file)}
|
|
||||||
)
|
|
||||||
|
|
||||||
assert result.exception
|
assert result.exception
|
||||||
lines = result.output.splitlines()
|
lines = result.output.splitlines()
|
||||||
|
|
@ -207,10 +210,7 @@ def test_wrong_general_section(tmpdir):
|
||||||
def test_verbosity(tmpdir):
|
def test_verbosity(tmpdir):
|
||||||
runner = CliRunner()
|
runner = CliRunner()
|
||||||
config_file = tmpdir.join('config')
|
config_file = tmpdir.join('config')
|
||||||
config_file.write(dedent('''
|
config_file.write('')
|
||||||
[general]
|
|
||||||
status_path = {0}/status/
|
|
||||||
''').format(str(tmpdir)))
|
|
||||||
|
|
||||||
result = runner.invoke(
|
result = runner.invoke(
|
||||||
cli.app, ['--verbosity=HAHA', 'sync'],
|
cli.app, ['--verbosity=HAHA', 'sync'],
|
||||||
|
|
@ -255,12 +255,8 @@ def test_deprecated_item_status(tmpdir):
|
||||||
str(tmpdir), 'mypair', data_type='items') == data
|
str(tmpdir), 'mypair', data_type='items') == data
|
||||||
|
|
||||||
|
|
||||||
def test_collections_cache_invalidation(tmpdir):
|
def test_collections_cache_invalidation(tmpdir, runner):
|
||||||
cfg = tmpdir.join('config')
|
runner.write_with_general(dedent('''
|
||||||
cfg.write(dedent('''
|
|
||||||
[general]
|
|
||||||
status_path = {0}/status/
|
|
||||||
|
|
||||||
[storage foo]
|
[storage foo]
|
||||||
type = filesystem
|
type = filesystem
|
||||||
path = {0}/foo/
|
path = {0}/foo/
|
||||||
|
|
@ -281,19 +277,14 @@ def test_collections_cache_invalidation(tmpdir):
|
||||||
bar = tmpdir.mkdir('bar')
|
bar = tmpdir.mkdir('bar')
|
||||||
foo.mkdir('a').join('itemone.txt').write('UID:itemone')
|
foo.mkdir('a').join('itemone.txt').write('UID:itemone')
|
||||||
|
|
||||||
runner = CliRunner()
|
result = runner.invoke(['sync'])
|
||||||
result = runner.invoke(cli.app, ['sync'],
|
|
||||||
env={'VDIRSYNCER_CONFIG': str(cfg)})
|
|
||||||
assert not result.exception
|
assert not result.exception
|
||||||
|
|
||||||
rv = bar.join('a').listdir()
|
rv = bar.join('a').listdir()
|
||||||
assert len(rv) == 1
|
assert len(rv) == 1
|
||||||
assert rv[0].basename == 'itemone.txt'
|
assert rv[0].basename == 'itemone.txt'
|
||||||
|
|
||||||
cfg.write(dedent('''
|
runner.write_with_general(dedent('''
|
||||||
[general]
|
|
||||||
status_path = {0}/status/
|
|
||||||
|
|
||||||
[storage foo]
|
[storage foo]
|
||||||
type = filesystem
|
type = filesystem
|
||||||
path = {0}/foo/
|
path = {0}/foo/
|
||||||
|
|
@ -312,8 +303,7 @@ def test_collections_cache_invalidation(tmpdir):
|
||||||
|
|
||||||
tmpdir.join('status').remove()
|
tmpdir.join('status').remove()
|
||||||
bar2 = tmpdir.mkdir('bar2')
|
bar2 = tmpdir.mkdir('bar2')
|
||||||
result = runner.invoke(cli.app, ['sync'],
|
result = runner.invoke(['sync'])
|
||||||
env={'VDIRSYNCER_CONFIG': str(cfg)})
|
|
||||||
assert not result.exception
|
assert not result.exception
|
||||||
|
|
||||||
rv = bar.join('a').listdir()
|
rv = bar.join('a').listdir()
|
||||||
|
|
@ -322,12 +312,8 @@ def test_collections_cache_invalidation(tmpdir):
|
||||||
assert rv[0].basename == rv2[0].basename == 'itemone.txt'
|
assert rv[0].basename == rv2[0].basename == 'itemone.txt'
|
||||||
|
|
||||||
|
|
||||||
def test_invalid_pairs_as_cli_arg(tmpdir):
|
def test_invalid_pairs_as_cli_arg(tmpdir, runner):
|
||||||
cfg = tmpdir.join('config')
|
runner.write_with_general(dedent('''
|
||||||
cfg.write(dedent('''
|
|
||||||
[general]
|
|
||||||
status_path = {0}/status/
|
|
||||||
|
|
||||||
[storage foo]
|
[storage foo]
|
||||||
type = filesystem
|
type = filesystem
|
||||||
path = {0}/foo/
|
path = {0}/foo/
|
||||||
|
|
@ -347,19 +333,13 @@ def test_invalid_pairs_as_cli_arg(tmpdir):
|
||||||
tmpdir.mkdir('foo')
|
tmpdir.mkdir('foo')
|
||||||
tmpdir.mkdir('bar')
|
tmpdir.mkdir('bar')
|
||||||
|
|
||||||
runner = CliRunner()
|
result = runner.invoke(['sync', 'foobar/d'])
|
||||||
result = runner.invoke(cli.app, ['sync', 'foobar/d'],
|
|
||||||
env={'VDIRSYNCER_CONFIG': str(cfg)})
|
|
||||||
assert result.exception
|
assert result.exception
|
||||||
assert 'pair foobar: collection d not found' in result.output.lower()
|
assert 'pair foobar: collection d not found' in result.output.lower()
|
||||||
|
|
||||||
|
|
||||||
def test_discover_command(tmpdir):
|
def test_discover_command(tmpdir, runner):
|
||||||
cfg = tmpdir.join('config')
|
runner.write_with_general(dedent('''
|
||||||
cfg.write(dedent('''
|
|
||||||
[general]
|
|
||||||
status_path = {0}/status/
|
|
||||||
|
|
||||||
[storage foo]
|
[storage foo]
|
||||||
type = filesystem
|
type = filesystem
|
||||||
path = {0}/foo/
|
path = {0}/foo/
|
||||||
|
|
@ -383,9 +363,7 @@ def test_discover_command(tmpdir):
|
||||||
foo.mkdir('b')
|
foo.mkdir('b')
|
||||||
foo.mkdir('c')
|
foo.mkdir('c')
|
||||||
|
|
||||||
runner = CliRunner()
|
result = runner.invoke(['sync'])
|
||||||
result = runner.invoke(cli.app, ['sync'],
|
|
||||||
env={'VDIRSYNCER_CONFIG': str(cfg)})
|
|
||||||
assert not result.exception
|
assert not result.exception
|
||||||
lines = result.output.splitlines()
|
lines = result.output.splitlines()
|
||||||
assert lines[0].startswith('Discovering')
|
assert lines[0].startswith('Discovering')
|
||||||
|
|
@ -394,47 +372,38 @@ def test_discover_command(tmpdir):
|
||||||
assert 'Syncing foobar/c' in lines
|
assert 'Syncing foobar/c' in lines
|
||||||
|
|
||||||
foo.mkdir('d')
|
foo.mkdir('d')
|
||||||
result = runner.invoke(cli.app, ['sync'],
|
result = runner.invoke(['sync'])
|
||||||
env={'VDIRSYNCER_CONFIG': str(cfg)})
|
|
||||||
assert not result.exception
|
assert not result.exception
|
||||||
assert 'Syncing foobar/d' not in result.output
|
assert 'Syncing foobar/d' not in result.output
|
||||||
|
|
||||||
result = runner.invoke(cli.app, ['discover'],
|
result = runner.invoke(['discover'])
|
||||||
env={'VDIRSYNCER_CONFIG': str(cfg)})
|
|
||||||
assert not result.exception
|
assert not result.exception
|
||||||
|
|
||||||
result = runner.invoke(cli.app, ['sync'],
|
result = runner.invoke(['sync'])
|
||||||
env={'VDIRSYNCER_CONFIG': str(cfg)})
|
|
||||||
assert not result.exception
|
assert not result.exception
|
||||||
assert 'Syncing foobar/d' in result.output
|
assert 'Syncing foobar/d' in result.output
|
||||||
|
|
||||||
|
|
||||||
def test_multiple_pairs(tmpdir):
|
def test_multiple_pairs(tmpdir, runner):
|
||||||
cfg_content = [dedent('''
|
def get_cfg():
|
||||||
[general]
|
|
||||||
status_path = {}/status/
|
|
||||||
''').format(str(tmpdir))]
|
|
||||||
for name_a, name_b in ('foo', 'bar'), ('bam', 'baz'):
|
for name_a, name_b in ('foo', 'bar'), ('bam', 'baz'):
|
||||||
cfg_content.append(dedent('''
|
yield dedent('''
|
||||||
[pair {a}{b}]
|
[pair {a}{b}]
|
||||||
a = {a}
|
a = {a}
|
||||||
b = {b}
|
b = {b}
|
||||||
''').format(a=name_a, b=name_b))
|
''').format(a=name_a, b=name_b)
|
||||||
|
|
||||||
for name in name_a, name_b:
|
for name in name_a, name_b:
|
||||||
cfg_content.append(dedent('''
|
yield dedent('''
|
||||||
[storage {name}]
|
[storage {name}]
|
||||||
type = filesystem
|
type = filesystem
|
||||||
path = {base}/{name}/
|
path = {base}/{name}/
|
||||||
fileext = .txt
|
fileext = .txt
|
||||||
''').format(name=name, base=str(tmpdir)))
|
''').format(name=name, base=str(tmpdir))
|
||||||
|
|
||||||
cfg = tmpdir.join('config')
|
runner.write_with_general(''.join(get_cfg()))
|
||||||
cfg.write(''.join(cfg_content))
|
|
||||||
|
|
||||||
runner = CliRunner()
|
result = runner.invoke(['sync'])
|
||||||
result = runner.invoke(cli.app, ['sync'],
|
|
||||||
env={'VDIRSYNCER_CONFIG': str(cfg)})
|
|
||||||
assert sorted(result.output.splitlines()) == [
|
assert sorted(result.output.splitlines()) == [
|
||||||
'Discovering collections for pair bambaz',
|
'Discovering collections for pair bambaz',
|
||||||
'Discovering collections for pair foobar',
|
'Discovering collections for pair foobar',
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue