aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_repository.py
blob: 05328e62fe7be116d1860f61f31494077ac76158 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import pytest
import py.path

from dotfiles.repository import Repository
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_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')))


@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)


@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(repo, home):
    with pytest.raises(NotRootedInHome):
        Repository(repo, home)._dotfile(py.path.local('/tmp/foo'))
    with pytest.raises(TargetIgnored):
        Repository(repo, home,
                   ignore_patterns=['.foo'])._dotfile(home.join('.foo'))
    with pytest.raises(TargetIgnored):
        Repository(repo, home,
                   ignore_patterns=['foo'])._dotfile(home.join('.bar/foo'))
    with pytest.raises(IsDirectory):
        Repository(repo, home)._dotfile(home.ensure_dir('.config'))

    # The home fixture is parametrized, we can only expect InRepository
    # exception when the repository is contained in the home directory.
    if repo.dirname == home.basename:
        with pytest.raises(InRepository):
            Repository(repo, home)._dotfile(repo.join('.foo/bar'))

    Repository(repo, home)._dotfile(home.join('.foo'))


def test_dotfiles(repo, home):
    file = home.join('.baz')
    dir = home.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)])
    assert len(dotfiles) == 5


def test_contents(repo, home):
    assert Repository(repo, home).contents() == []

    target_a = repo.ensure('a')
    target_b = repo.ensure('b/b')
    target_c = repo.ensure('c/c/c')
    contents = Repository(repo, home).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()

    assert len(repo.listdir()) == 0