aboutsummaryrefslogtreecommitdiffstats
path: root/test_dotfiles.py
blob: e0ebc77d57243ce32e60f1d21c47878065dd11e9 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import py
import pytest
from click.testing import CliRunner

from dotfiles import __version__
from dotfiles import Repository
from dotfiles import Dotfile, unique_suffix
from dotfiles import version


class TestCLI(object):

    def test_version(self):
        runner = CliRunner()
        result = runner.invoke(version)
        assert ('dotfiles version %s\n' % __version__) == result.output

    """
    @pytest.mark.xfail()
    def test_empty_status(tmpdir):
        repo = Repository(tmpdir.join("repo"))
        assert '[no dotfiles found]' == repo.status()

    @pytest.mark.xfail()
    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 = 'conflict'
        expected_status = ("{0:<18} {1}\n"
                           "{0:<18} {1}\n"
                           "{0:<18} {1}".format(name.basename, dotfile_state))

        assert expected_status == repo.status()

    @pytest.mark.xfail()
    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('ok',
                                                '.bashrc',
                                                '.inputrc',
                                                '.vimrc'))

        assert expected_status == repo.status()

    @pytest.mark.xfail()
    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 = '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('error',
                                                dotfile_a.name.basename,
                                                dotfile_c.name.basename))

        assert expected_status == repo.check()
        """


class TestRepository(object):

    def test_repodir_create(self, tmpdir):
        repodir = tmpdir.join('test_create_repo')
        repo = Repository(repodir, tmpdir)

        assert True == repodir.check(exists=0)
        repo.contents()
        assert True == repodir.check(exists=1, dir=1)

    def test_contents_empty(self, tmpdir):
        assert [] == Repository(tmpdir.join('Dotfiles'), tmpdir).contents()

    def test_contents_nonempty(self, tmpdir):
        repodir = tmpdir.ensure('test_create_repo', dir=1)
        target_a = repodir.ensure('a')
        target_b = repodir.ensure('b')
        target_c = repodir.ensure('c')

        contents = Repository(repodir, tmpdir).contents()

        assert target_a == contents[0].target
        assert target_b == contents[1].target
        assert target_c == contents[2].target

    @pytest.mark.xfail(reason='not implemented yet')
    def test_expected_name(self):
        assert 0


class TestDotfile(object):

    def test_unique_suffix_overlap(self):
        (name, target) = unique_suffix(py.path.local('/foo/baz'),
                                       py.path.local('/foo/bar/bat'))
        assert 'baz' == name
        assert 'bar/bat' == target

    @pytest.mark.xfail(reason='this is a bug')
    def test_unique_suffix_no_overlap(self):
        (name, target) = unique_suffix(py.path.local('/a/b/c'),
                                       py.path.local('/d/e/f'))
        assert '/a/b/c' == name
        assert '/d/e/f' == target

    def test_state_error(self, tmpdir):
        repo = tmpdir.ensure("Dotfiles", dir=1)
        name = tmpdir.join(".vimrc")
        target = repo.join("vimrc")

        dotfile = Dotfile(name, target)

        assert 'error' == dotfile.state

    def test_state_missing(self, tmpdir):
        repo = tmpdir.ensure("Dotfiles", dir=1)
        name = tmpdir.join(".vimrc")
        target = repo.ensure("vimrc")

        dotfile = Dotfile(name, target)

        assert 'missing' == dotfile.state

    def test_state_conflict(self, tmpdir):
        repo = tmpdir.ensure("Dotfiles", dir=1)
        name = tmpdir.ensure(".vimrc")
        target = repo.ensure("vimrc")

        dotfile = Dotfile(name, target)

        assert 'conflict' == dotfile.state

    def test_state_ok(self, tmpdir):
        repo = tmpdir.join("Dotfiles", dir=1)
        name = tmpdir.join(".vimrc")
        target = repo.ensure("vimrc")
        dotfile = Dotfile(name, target)

        name.mksymlinkto(target)
        assert 'ok' == dotfile.state

        name.remove()
        assert 'missing' == dotfile.state

    @pytest.mark.parametrize("times", range(1, 4))
    def test_add(self, tmpdir, times):
        repo = tmpdir.ensure("Dotfiles", dir=1)
        name = tmpdir.ensure(".vimrc")
        target = repo.join("vimrc")

        dotfile = Dotfile(name, target)
        dotfile.add()

        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(OSError):
                dotfile.add()
            assert target.check(file=1, link=0)
            assert name.check(file=1, link=1)
            assert name.samefile(target)

    @pytest.mark.parametrize("times", range(1, 4))
    def test_remove(self, tmpdir, times):
        repo = tmpdir.ensure("Dotfiles", dir=1)
        name = tmpdir.join(".vimrc")
        target = repo.ensure("vimrc")

        name.mksymlinkto(target)

        dotfile = Dotfile(name, target)
        dotfile.remove()

        assert False == target.check()
        assert name.check(file=1, link=0)

        for x in range(2, times):
            with pytest.raises(OSError):
                dotfile.remove()
            assert False == target.check()
            assert name.check(file=1, link=0)

    @pytest.mark.parametrize("times", range(1, 4))
    def test_sync(self, tmpdir, times):
        repo = tmpdir.ensure("Dotfiles", dir=1)
        name = tmpdir.join(".vimrc")
        target = repo.ensure("vimrc")

        dotfile = Dotfile(name, target)
        dotfile.sync()

        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(py.error.EEXIST):
                dotfile.sync()
            assert target.check(file=1, link=0)
            assert name.check(file=1, link=1)
            assert name.samefile(target)

    @pytest.mark.xfail(reason='not implemented yet')
    def test_unsync(self):
        assert 0