aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_repository.py
blob: c8212e66bae3aadb48e9680c40a97850e65ecc87 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import os
import pytest

from dotfiles.dotfile import Dotfile
from dotfiles.repository import Repository


def test_getters():

    repo = Repository('foo', 'bar')

    assert isinstance(repo.homedir, str)
    assert isinstance(repo.repodir, str)


def test_setters():

    repodir = '/foo/bar'
    homedir = '/fizz/buzz'

    repo = Repository('foo', 'bar')

    repo.repodir = repodir
    repo.homedir = homedir

    assert repodir == repo.repodir
    assert homedir == repo.homedir


def test_custom_paths():

    homedir = '/foo/bar'
    repodir = '/fizz/buzz'

    repo = Repository(repodir, homedir)

    assert homedir == repo.homedir
    assert repodir == repo.repodir


def test_path_expansion():

    repo = Repository('~/foo', '~/bar')

    assert os.path.expanduser('~/foo') == repo.repodir
    assert os.path.expanduser('~/bar') == repo.homedir


def test_create_repo(tmpdir):

    repodir = tmpdir.join("test_create_repo")

    repodir.check(exists=0)
    Repository(repodir)
    repodir.check(exists=1, dir=1)


def test_empty_status(tmpdir):

    repo = Repository(tmpdir.join("repo"))

    assert '[no dotfiles found]' == repo.status()


def test_status_manual(tmpdir, monkeypatch):

    repodir = tmpdir.join("Dotfiles", dir=1)
    target = repodir.ensure("vimrc")
    name = tmpdir.ensure(".vimrc")

    dotfile = Dotfile(name, target)

    repo = Repository(repodir, tmpdir)
    monkeypatch.setattr(Repository, "_load",
                        lambda self: [dotfile, dotfile, dotfile])

    dotfile_state = Dotfile.states['conflict']['text']
    expected_status = ("{0:<18} {1}\n"
                       "{0:<18} {1}\n"
                       "{0:<18} {1}".format(name.basename, dotfile_state))

    assert expected_status == repo.status()


def test_status_discover(tmpdir):

    repodir = tmpdir.ensure("Dotfiles", dir=1)

    tmpdir.join('.bashrc').mksymlinkto(repodir.ensure('bashrc'))
    tmpdir.join('.inputrc').mksymlinkto(repodir.ensure('inputrc'))
    tmpdir.join('.vimrc').mksymlinkto(repodir.ensure('vimrc'))

    repo = Repository(repodir, tmpdir)

    expected_status = ("{1:<18} {0}\n"
                       "{2:<18} {0}\n"
                       "{3:<18} {0}".format(Dotfile.states['ok']['text'],
                                            '.bashrc',
                                            '.inputrc',
                                            '.vimrc'))

    assert expected_status == repo.status()


def test_check(tmpdir, monkeypatch):

    repodir = tmpdir.join('repo')

    dotfile_a = Dotfile(tmpdir.join('.aaa'), repodir.join('aaa'))
    dotfile_b = Dotfile(tmpdir.join('.bbb'), repodir.join('bbb'))
    dotfile_c = Dotfile(tmpdir.join('.ccc'), repodir.join('ccc'))

    dotfile_b.state = Dotfile.states['ok']

    repo = Repository(tmpdir)
    monkeypatch.setattr(Repository, "_load",
                        lambda self: [dotfile_a, dotfile_b, dotfile_c])

    expected_status = ("{1:<18} {0}\n"
                       "{2:<18} {0}".format(Dotfile.states['error']['text'],
                                            dotfile_a.name.basename,
                                            dotfile_c.name.basename))

    assert expected_status == repo.check()


def test_sync():
    with pytest.raises(NotImplementedError):
        Repository('todo').sync()


def test_unsync():
    with pytest.raises(NotImplementedError):
        Repository('todo').unsync()


def test_add():
    with pytest.raises(NotImplementedError):
        Repository('todo').add()


def test_remove():
    with pytest.raises(NotImplementedError):
        Repository('todo').remove()


def test_move():
    with pytest.raises(NotImplementedError):
        Repository('todo').move()