summaryrefslogtreecommitdiffstats
path: root/test_dotfiles.py
blob: bb545784603c9ca357059bd7d44b5d8edf3f5a66 (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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import shutil
import tempfile
import unittest

from dotfiles import core


def touch(fname, times=None):
    with file(fname, 'a'):
        os.utime(fname, times)


class DotfilesTestCase(unittest.TestCase):

    def setUp(self):
        """Create a temporary home directory."""

        self.home = tempfile.mkdtemp()

        # Create a repository for the tests to use.
        self.repo = os.path.join(self.home, 'Dotfiles')
        os.mkdir(self.repo)

    def tearDown(self):
        """Delete the temporary home directory and its contents."""

        shutil.rmtree(self.home)

    def test_force_sync_directory(self):
        """Test forced sync when the dotfile is a directory.

        I installed the lastpass chrome extension which stores a socket in
        ~/.lastpass. So I added that directory as an external to /tmp and
        attempted a forced sync. An error occurred because sync() calls
        os.remove() as it mistakenly assumes the dotfile is a file and not
        a directory.
        """

        os.mkdir(os.path.join(self.home, '.lastpass'))
        externals = {'.lastpass': '/tmp'}

        dotfiles = core.Dotfiles(home=self.home, repo=self.repo, prefix='',
                                ignore=[], externals=externals)

        dotfiles.sync(force=True)

        self.assertEqual(
                os.path.realpath(os.path.join(self.home, '.lastpass')), '/tmp')

    def test_move_repository(self):
        """Test the move() method for a Dotfiles repository."""

        touch(os.path.join(self.repo, 'bashrc'))

        dotfiles = core.Dotfiles(
                home=self.home, repo=self.repo, prefix='',
                ignore=[], force=True, externals={})

        dotfiles.sync()

        # Make sure sync() did the right thing.
        self.assertEqual(
                os.path.realpath(os.path.join(self.home, '.bashrc')),
                os.path.join(self.repo, 'bashrc'))

        target = os.path.join(self.home, 'MyDotfiles')

        dotfiles.move(target)

        self.assertTrue(os.path.exists(os.path.join(target, 'bashrc')))
        self.assertEqual(
                os.path.realpath(os.path.join(self.home, '.bashrc')),
                os.path.join(target, 'bashrc'))

    def test_sync_unmanaged_directory_symlink(self):
        """Test a forced sync on a directory symlink.

        A bug was reported where a user wanted to replace a dotfile repository
        with an other one. They had a .vim directory in their home directory
        which was obviously also a symbolic link. This caused:

        OSError: Cannot call rmtree on a symbolic link
        """

        # Create a dotfile symlink to some directory
        os.mkdir(os.path.join(self.home, 'vim'))
        os.symlink(os.path.join(self.home, 'vim'),
                   os.path.join(self.home, '.vim'))

        # Create a vim directory in the repository. This will cause the above
        # symlink to be overwritten on sync.
        os.mkdir(os.path.join(self.repo, 'vim'))

        # Make sure the symlink points to the correct location.
        self.assertEqual(
                os.path.realpath(os.path.join(self.home, '.vim')),
                os.path.join(self.home, 'vim'))

        dotfiles = core.Dotfiles(
                home=self.home, repo=self.repo, prefix='',
                ignore=[], externals={})

        dotfiles.sync(force=True)

        # The symlink should now point to the directory in the repository.
        self.assertEqual(
                os.path.realpath(os.path.join(self.home, '.vim')),
                os.path.join(self.repo, 'vim'))


def suite():
    suite = unittest.TestLoader().loadTestsFromTestCase(DotfilesTestCase)
    return suite

if __name__ == '__main__':
    unittest.TextTestRunner().run(suite())