aboutsummaryrefslogtreecommitdiffstats
path: root/tests/utils.py
blob: ecdeb840a436a6b408baf3bb54396b564f3459a9 (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
import os
from dotfiles.utils import compare_path as samefile


def _touch(fname, times=None):
    with open(fname, 'a'):
        os.utime(fname, times)


class HomeDirectory(object):


    def __init__(self, path, repo, contents):
        self.path = path
        self.repo = repo
        if contents:
            self.setup(contents)


    def setup(self, contents):
        repo = os.path.join(self.path, self.repo)
        os.mkdir(repo)

        for link, link_should_exist in contents.items():

            target = os.path.join('%s/%s' % (repo, link[1:]))
            _touch(target)

            if link_should_exist:
                os.symlink(target, os.path.join(self.path, link))

        self.verify(contents)


    def verify(self, contents):
        __tracebackhide__ = True

        for link, link_should_exist in contents.items():

            target = os.path.join(self.path, '%s/%s' % (self.repo, link[1:]))

            if not os.path.exists(target):
                pytest.fail("missing expected repo file \"%s\"" % target)

            link = os.path.join(self.path, link)
            link_exists = os.path.exists(link)

            if link_should_exist:
                if not link_exists:
                    pytest.fail("missing expected symlink \"%s\"" % link)
                if not samefile(link, target):
                    pytest.fail("\"%s\" does not link to \"%s\"" % (link, target))

            elif link_exists:
                pytest.fail("found unexpected symlink \"%s\"" % link)