From 0eadef7f8034dc07d6322b7afb8c51a17472011e Mon Sep 17 00:00:00 2001 From: Jon Bernard Date: Fri, 22 Jan 2016 08:54:19 -0500 Subject: Simplify test fixtures into a single repository --- tests/conftest.py | 13 +++-- tests/test_cli.py | 9 ++-- tests/test_dotfile.py | 130 +++++++++++++++++++++++------------------------ tests/test_repository.py | 114 ++++++++++++++++++++--------------------- 4 files changed, 130 insertions(+), 136 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 7d88ce5..f5beb21 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,15 +1,14 @@ import pytest from click.testing import CliRunner - -@pytest.fixture(scope='function', params=['', 'home']) -def home(request, tmpdir): - return tmpdir.ensure_dir(request.param) +from dotfiles.repository import Repository -@pytest.fixture(scope='function') -def repo(tmpdir): - return tmpdir.ensure_dir('repo') +@pytest.fixture(scope='function', params=['', 'home']) +def repo(request, tmpdir): + path = tmpdir.ensure_dir('repo') + home = tmpdir.ensure_dir(request.param) + return Repository(path, home) @pytest.fixture(scope='function') diff --git a/tests/test_cli.py b/tests/test_cli.py index f531989..086976c 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -4,12 +4,13 @@ from dotfiles.repository import Repository class TestCli(object): - def test_status(self, runner, repo, home, monkeypatch): + def test_status(self, runner, repo, monkeypatch): def repo_init(self, *args, **kwargs): - self.ignore = [] - self.homedir = home - self.repodir = repo.ensure(dir=1) + self.path = repo.path.ensure_dir() + self.homedir = repo.homedir + self.ignore_patterns = repo.ignore_patterns + self.preserve_leading_dot = repo.preserve_leading_dot monkeypatch.setattr(Repository, '__init__', repo_init) diff --git a/tests/test_dotfile.py b/tests/test_dotfile.py index ff19d9d..b2dcd28 100644 --- a/tests/test_dotfile.py +++ b/tests/test_dotfile.py @@ -6,140 +6,138 @@ from dotfiles.exceptions import IsSymlink, NotASymlink from dotfiles.exceptions import TargetExists, TargetMissing, Exists -def test_str(repo, home): - dotfile = Dotfile(home.join('.a'), repo.join('.b')) - assert str(dotfile) == home.join('.a') +def _dotfile(repo, name, target=None): + return Dotfile(repo.homedir.join(name), + repo.path.join(target if target is not None else name)) -def test_short_name(repo, home): - dotfile = Dotfile(home.join('.foo'), repo.join('.foo')) - assert dotfile.name == home.join('.foo') - assert dotfile.short_name(home) == '.foo' +def test_str(repo): + dotfile = _dotfile(repo, '.a', '.b') + assert str(dotfile) == repo.homedir.join('.a') -def test_state_error(repo, home): - dotfile = Dotfile(home.join('.vimrc'), repo.join('.vimrc')) +def test_short_name(repo): + dotfile = _dotfile(repo, '.foo') + assert dotfile.name == repo.homedir.join('.foo') + assert dotfile.short_name(repo.homedir) == '.foo' + + +def test_state_error(repo): + dotfile = _dotfile(repo, '.vimrc') assert dotfile.state == 'error' -def test_state_missing(repo, home): - dotfile = Dotfile(home.join('.vimrc'), repo.ensure('.vimrc')) +def test_state_missing(repo): + dotfile = _dotfile(repo, '.vimrc') + dotfile.target.ensure() assert dotfile.state == 'missing' -def test_state_conflict(repo, home): - dotfile = Dotfile(home.ensure('.vimrc'), repo.ensure('.vimrc')) +def test_state_conflict(repo): + dotfile = _dotfile(repo, '.vimrc') + dotfile.target.ensure() + dotfile.name.ensure() assert dotfile.state == 'conflict' -def test_state_ok(repo, home): - name = home.join('.vimrc') - target = repo.ensure('vimrc') - - dotfile = Dotfile(name, target) - name.mksymlinkto(target) +def test_state_ok(repo): + dotfile = _dotfile(repo, '.vimrc', 'vimrc') + dotfile.target.ensure() + dotfile.name.mksymlinkto(dotfile.target) assert dotfile.state == 'ok' - - name.remove() + dotfile.name.remove() assert dotfile.state == 'missing' @pytest.mark.parametrize('path', ['.foo', '.foo/bar/baz']) -def test_add(repo, home, path): - name = home.ensure(path) - target = repo.ensure(path) - dotfile = Dotfile(name, target) +def test_add(repo, path): + dotfile = _dotfile(repo, path) + dotfile.target.ensure() + dotfile.name.ensure() with pytest.raises(TargetExists): dotfile.add() - target.remove() + dotfile.target.remove() dotfile.add() - assert target.check(file=1, link=0) - assert name.check(file=1, link=1) - assert name.samefile(target) + assert dotfile.target.check(file=1, link=0) + assert dotfile.name.check(file=1, link=1) + assert dotfile.name.samefile(dotfile.target) with pytest.raises(IsSymlink): dotfile.add() - assert target.check(file=1, link=0) - assert name.check(file=1, link=1) - assert name.samefile(target) + assert dotfile.target.check(file=1, link=0) + assert dotfile.name.check(file=1, link=1) + assert dotfile.name.samefile(dotfile.target) @pytest.mark.parametrize('path', ['.foo', '.foo/bar/baz']) -def test_remove(repo, home, path): - name = home.join(path) - target = repo.join(path) - dotfile = Dotfile(name, target) - - py.path.local(name.dirname).ensure_dir() - name.mksymlinkto(target) +def test_remove(repo, path): + dotfile = _dotfile(repo, path) + py.path.local(dotfile.name.dirname).ensure_dir() + dotfile.name.mksymlinkto(dotfile.target) with pytest.raises(TargetMissing): dotfile.remove() - target.ensure() + dotfile.target.ensure() dotfile.remove() - assert target.check(exists=0) - assert name.check(file=1, link=0) + assert dotfile.target.check(exists=0) + assert dotfile.name.check(file=1, link=0) with pytest.raises(NotASymlink): dotfile.remove() - assert target.check(exists=0) - assert name.check(file=1, link=0) + assert dotfile.target.check(exists=0) + assert dotfile.name.check(file=1, link=0) @pytest.mark.parametrize('path', ['.foo', '.foo/bar/baz']) -def test_link(repo, home, path): - name = home.join(path) - target = repo.join(path) - dotfile = Dotfile(name, target) +def test_link(repo, path): + dotfile = _dotfile(repo, path) with pytest.raises(TargetMissing): dotfile.link() - target.ensure() + dotfile.target.ensure() dotfile.link() - assert target.check(file=1, link=0) - assert name.check(file=1, link=1) - assert name.samefile(target) + assert dotfile.target.check(file=1, link=0) + assert dotfile.name.check(file=1, link=1) + assert dotfile.name.samefile(dotfile.target) with pytest.raises(Exists): dotfile.link() - assert target.check(file=1, link=0) - assert name.check(file=1, link=1) - assert name.samefile(target) + assert dotfile.target.check(file=1, link=0) + assert dotfile.name.check(file=1, link=1) + assert dotfile.name.samefile(dotfile.target) @pytest.mark.parametrize('path', ['.foo', '.foo/bar/baz']) -def test_unlink(repo, home, path): - name = home.join(path) - target = repo.join(path) - dotfile = Dotfile(name, target) +def test_unlink(repo, path): + dotfile = _dotfile(repo, path) with pytest.raises(NotASymlink): dotfile.unlink() - py.path.local(name.dirname).ensure_dir() - name.mksymlinkto(target) + py.path.local(dotfile.name.dirname).ensure_dir() + dotfile.name.mksymlinkto(dotfile.target) with pytest.raises(TargetMissing): dotfile.unlink() - target.ensure() + dotfile.target.ensure() dotfile.unlink() - assert target.check(file=1, link=0) - assert name.check(exists=0) + assert dotfile.target.check(file=1, link=0) + assert dotfile.name.check(exists=0) with pytest.raises(NotASymlink): dotfile.unlink() - assert target.check(file=1, link=0) - assert name.check(exists=0) + assert dotfile.target.check(file=1, link=0) + assert dotfile.name.check(exists=0) diff --git a/tests/test_repository.py b/tests/test_repository.py index 05328e6..73d5315 100644 --- a/tests/test_repository.py +++ b/tests/test_repository.py @@ -6,98 +6,94 @@ from dotfiles.exceptions import NotRootedInHome, InRepository, TargetIgnored, \ IsDirectory -def test_repo_create(repo, home): - repo.remove() - assert repo.check(exists=0) - Repository(repo, home) - assert repo.check(exists=1, dir=1) +def test_repo_create(repo): + repo.path.remove() + assert repo.path.check(exists=0) + Repository(repo.path, repo.homedir) + assert repo.path.check(exists=1, dir=1) -def test_str(repo, home): - repo.ensure('.a') - repo.ensure('.b') - repo.ensure('.c') - - r = Repository(repo, home) - - assert str(r) == ( - '%s\n%s\n%s' % (home.join('.a'), - home.join('.b'), - home.join('.c'))) +def test_str(repo): + repo.path.ensure('a') + repo.path.ensure('b') + repo.path.ensure('c') + assert str(repo) == ( + '%s\n%s\n%s' % (repo.homedir.join('.a'), + repo.homedir.join('.b'), + repo.homedir.join('.c'))) @pytest.mark.parametrize('path', ['.foo', '.foo/bar/baz']) -def test_target_to_name(repo, home, path): - r = Repository(repo, home, dot=True) - assert r._target_to_name(repo.join(path)) == home.join(path) - - r = Repository(repo, home, dot=False) - assert r._target_to_name(repo.join(path)) == home.join('.%s' % path) +def test_dotfile_path(repo, path): + repo.preserve_leading_dot = True + assert (repo._dotfile_path(repo.path.join(path)) == + repo.homedir.join(path)) + repo.preserve_leading_dot = False + assert (repo._dotfile_path(repo.path.join(path)) == + repo.homedir.join('.%s' % path)) @pytest.mark.parametrize('path', ['.foo', '.foo/bar/baz']) -def test_name_to_target(repo, home, path): - r = Repository(repo, home, dot=True) - assert r._name_to_target(home.join(path)) == repo.join(path) - - r = Repository(repo, home, dot=False) - assert r._name_to_target(home.join(path)) == repo.join(path[1:]) +def test_dotfile_target(repo, path): + repo.preserve_leading_dot = True + assert (repo._dotfile_target(repo.homedir.join(path)) == + repo.path.join(path)) + repo.preserve_leading_dot = False + assert (repo._dotfile_target(repo.homedir.join(path)) == + repo.path.join(path[1:])) -def test_dotfile(repo, home): +def test_dotfile(repo): with pytest.raises(NotRootedInHome): - Repository(repo, home)._dotfile(py.path.local('/tmp/foo')) + repo._dotfile(py.path.local('/tmp/foo')) with pytest.raises(TargetIgnored): - Repository(repo, home, - ignore_patterns=['.foo'])._dotfile(home.join('.foo')) + repo.ignore_patterns = ['.foo'] + repo.preserve_leading_dot = True + repo._dotfile(py.path.local(repo.homedir.join('.foo'))) with pytest.raises(TargetIgnored): - Repository(repo, home, - ignore_patterns=['foo'])._dotfile(home.join('.bar/foo')) + repo.ignore_patterns = ['foo'] + repo._dotfile(repo.homedir.join('.bar/foo')) with pytest.raises(IsDirectory): - Repository(repo, home)._dotfile(home.ensure_dir('.config')) + repo._dotfile(repo.homedir.ensure_dir('.config')) - # The home fixture is parametrized, we can only expect InRepository + # The repo fixture is parametrized, we can only expect InRepository # exception when the repository is contained in the home directory. - if repo.dirname == home.basename: + if repo.path.dirname == repo.homedir.basename: with pytest.raises(InRepository): - Repository(repo, home)._dotfile(repo.join('.foo/bar')) + repo._dotfile(repo.path.join('.foo/bar')) - Repository(repo, home)._dotfile(home.join('.foo')) + repo._dotfile(repo.homedir.join('.foo')) -def test_dotfiles(repo, home): - file = home.join('.baz') - dir = home.ensure_dir('.dir') +def test_dotfiles(repo): + file = repo.homedir.join('.baz') + dir = repo.homedir.ensure_dir('.dir') dir.ensure('foo/bat') dir.ensure('foo/buz') dir.ensure('bar') dir.ensure('boo') - dotfiles = Repository(repo, home).dotfiles([str(file), str(dir)]) + dotfiles = repo.dotfiles([str(file), str(dir)]) assert len(dotfiles) == 5 -def test_contents(repo, home): - assert Repository(repo, home).contents() == [] +def test_contents(repo): + assert repo.contents() == [] - target_a = repo.ensure('a') - target_b = repo.ensure('b/b') - target_c = repo.ensure('c/c/c') - contents = Repository(repo, home).contents() + target_a = repo.path.ensure('a') + target_b = repo.path.ensure('b/b') + target_c = repo.path.ensure('c/c/c') + contents = repo.contents() assert contents[0].target == target_a assert contents[1].target == target_b assert contents[2].target == target_c -# TODO: Need tests for whatever-dot option - - -def test_prune(repo, home): - repo.ensure_dir('.a/a') - repo.ensure_dir('.b/b/b/b') - repo.ensure_dir('.c/c/c/c/c/c/c/c') - - Repository(repo, home).prune() +def test_prune(repo): + repo.path.ensure_dir('.a/a') + repo.path.ensure_dir('.b/b/b/b') + repo.path.ensure_dir('.c/c/c/c/c/c/c/c') - assert len(repo.listdir()) == 0 + repo.prune() + assert len(repo.path.listdir()) == 0 -- cgit v1.2.3