From ad1487517467b4866b8e0572a9cf13604dcd0f2a Mon Sep 17 00:00:00 2001 From: Jon Bernard Date: Tue, 19 Jan 2016 09:55:36 -0500 Subject: Finish tests for dotfile class --- tests/test_dotfile.py | 186 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 121 insertions(+), 65 deletions(-) (limited to 'tests/test_dotfile.py') diff --git a/tests/test_dotfile.py b/tests/test_dotfile.py index 0e09654..a913743 100644 --- a/tests/test_dotfile.py +++ b/tests/test_dotfile.py @@ -1,89 +1,145 @@ +import py import pytest from dotfiles.dotfile import Dotfile -from dotfiles.exceptions import IsSymlink +from dotfiles.exceptions import IsSymlink, NotASymlink +from dotfiles.exceptions import TargetExists, TargetMissing, Exists -class TestDotfile(object): +def test_str(repo, home): + dotfile = Dotfile(home.join('.a'), repo.join('.b')) + assert str(dotfile) == home.join('.a') - def test_state_error(self, repo, home): - dotfile = Dotfile(home.join('.vimrc'), repo.join('vimrc')) - assert dotfile.state == 'error' - def test_state_missing(self, repo, home): - dotfile = Dotfile(home.join('.vimrc'), repo.ensure('vimrc')) - assert dotfile.state == 'missing' +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_state_conflict(self, repo, home): - dotfile = Dotfile(home.ensure('.vimrc'), repo.ensure('vimrc')) - assert dotfile.state == 'conflict' - def test_state_ok(self, repo, home): - name = home.join('.vimrc') - target = repo.ensure('vimrc') +def test_state_error(repo, home): + dotfile = Dotfile(home.join('.vimrc'), repo.join('.vimrc')) + assert dotfile.state == 'error' - dotfile = Dotfile(name, target) - name.mksymlinkto(target) - assert dotfile.state == 'ok' - name.remove() - assert dotfile.state == 'missing' +def test_state_missing(repo, home): + dotfile = Dotfile(home.join('.vimrc'), repo.ensure('.vimrc')) + assert dotfile.state == 'missing' - @pytest.mark.parametrize('times', range(1, 4)) - def test_add(self, repo, home, times): - name = home.ensure('.vimrc') - target = repo.join('vimrc') - Dotfile(name, target).add() +def test_state_conflict(repo, home): + dotfile = Dotfile(home.ensure('.vimrc'), repo.ensure('.vimrc')) + assert dotfile.state == 'conflict' - assert target.check(file=1, link=0) - assert name.check(file=1, link=1) - assert name.samefile(target) - for x in range(2, times): - with pytest.raises(IsSymlink): - Dotfile(name, target).add() - assert target.check(file=1, link=0) - assert name.check(file=1, link=1) - assert name.samefile(target) +def test_state_ok(repo, home): + name = home.join('.vimrc') + target = repo.ensure('vimrc') - @pytest.mark.parametrize('times', range(1, 4)) - def test_remove(self, repo, home, times): - name = home.join('.vimrc') - target = repo.ensure('vimrc') + dotfile = Dotfile(name, target) + name.mksymlinkto(target) + assert dotfile.state == 'ok' - name.mksymlinkto(target) - Dotfile(name, target).remove() + name.remove() + assert dotfile.state == 'missing' - assert not target.check() - assert name.check(file=1, link=0) - for x in range(2, times): - with pytest.raises(Exception): - # TODO: verify exception type once those exists - Dotfile(name, target).remove() - assert not target.check() - assert name.check(file=1, link=0) +@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) - @pytest.mark.parametrize('times', range(1, 4)) - def test_link(self, repo, home, times): - name = home.join('.vimrc') - target = repo.ensure('vimrc') + with pytest.raises(TargetExists): + dotfile.add() - Dotfile(name, target).link() + target.remove() + dotfile.add() - assert target.check(file=1, link=0) - assert name.check(file=1, link=1) - assert name.samefile(target) + assert target.check(file=1, link=0) + assert name.check(file=1, link=1) + assert name.samefile(target) - for x in range(2, times): - with pytest.raises(Exception): - # TODO: verify exception type once those exists - Dotfile(name, target).link() - assert target.check(file=1, link=0) - assert name.check(file=1, link=1) - assert name.samefile(target) + with pytest.raises(IsSymlink): + dotfile.add() - @pytest.mark.xfail(reason='TODO') - def test_unlink(self): - assert False + assert target.check(file=1, link=0) + assert name.check(file=1, link=1) + assert name.samefile(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) + + with pytest.raises(TargetMissing): + dotfile.remove() + + target.ensure() + dotfile.remove() + + assert target.check(exists=0) + assert name.check(file=1, link=0) + + with pytest.raises(NotASymlink): + dotfile.remove() + + assert target.check(exists=0) + assert 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) + + with pytest.raises(TargetMissing): + dotfile.link() + + target.ensure() + dotfile.link() + + assert target.check(file=1, link=0) + assert name.check(file=1, link=1) + assert name.samefile(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) + + +@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) + + with pytest.raises(NotASymlink): + dotfile.unlink() + + py.path.local(name.dirname).ensure_dir() + name.mksymlinkto(target) + + with pytest.raises(TargetMissing): + dotfile.unlink() + + target.ensure() + dotfile.unlink() + + assert target.check(file=1, link=0) + assert name.check(exists=0) + + with pytest.raises(NotASymlink): + dotfile.unlink() + + assert target.check(file=1, link=0) + assert name.check(exists=0) -- cgit v1.2.3