File: test_remove.py

package info (click to toggle)
dotdrop 1.15.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,812 kB
  • sloc: sh: 13,401; python: 8,186; makefile: 3
file content (133 lines) | stat: -rw-r--r-- 4,097 bytes parent folder | download | duplicates (2)
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
"""
author: deadc0de6 (https://github.com/deadc0de6)
Copyright (c) 2019, deadc0de6
basic unittest for the remove function
"""

import unittest
import os

# local imports
from dotdrop.dotdrop import cmd_remove
from tests.helpers import (clean, create_dir,
                           create_random_file, load_options,
                           get_tempdir, yaml_load, yaml_dump)


class TestRemove(unittest.TestCase):
    """test case"""

    def load_yaml(self, path):
        """Load yaml to dict"""
        self.assertTrue(os.path.exists(path))
        return yaml_load(path)

    def test_remove(self):
        """test the remove command"""

        # dotfiles in dotpath
        dotdrop_home = get_tempdir()
        self.assertTrue(os.path.exists(dotdrop_home))
        self.addCleanup(clean, dotdrop_home)

        dotfilespath = os.path.join(dotdrop_home, 'dotfiles')
        confpath = os.path.join(dotdrop_home, 'config.yaml')
        create_dir(dotfilespath)

        df1, _ = create_random_file(dotfilespath)
        df2, _ = create_random_file(dotfilespath)
        df3, _ = create_random_file(dotfilespath)
        configdic = {
            'config': {
                'dotpath': 'dotfiles',
            },
            'dotfiles': {
                'f_test1': {
                    'src': df1,
                    'dst': '/dev/null'
                },
                'f_test2': {
                    'src': df2,
                    'dst': '/dev/null'
                },
                'f_test3': {
                    'src': df3,
                    'dst': '/tmp/some-fake-path'
                },
            },
            'profiles': {
                'host1': {
                    'dotfiles': ['f_test1', 'f_test2', 'f_test3'],
                },
                'host2': {
                    'dotfiles': ['f_test1'],
                },
                'host3': {
                    'dotfiles': ['f_test2'],
                },
            },
        }

        yaml_dump(configdic, confpath)
        opt = load_options(confpath, 'host1')
        opt.remove_path = ['f_test1']
        opt.remove_iskey = True
        opt.debug = True
        opt.safe = False
        # by key
        cmd_remove(opt)

        # ensure file is deleted
        self.assertFalse(os.path.exists(df1))
        self.assertTrue(os.path.exists(df2))
        self.assertTrue(os.path.exists(df3))

        # load dict
        cont = yaml_load(confpath)

        # ensure not present
        self.assertTrue('f_test1' not in cont['dotfiles'])
        self.assertTrue('f_test1' not in cont['profiles']['host1']['dotfiles'])
        self.assertTrue('host2' not in cont['profiles'])

        # assert rest is intact
        self.assertTrue('f_test2' in cont['dotfiles'].keys())
        self.assertTrue('f_test3' in cont['dotfiles'].keys())
        self.assertTrue('f_test2' in cont['profiles']['host1']['dotfiles'])
        self.assertTrue('f_test3' in cont['profiles']['host1']['dotfiles'])
        self.assertTrue(cont['profiles']['host3']['dotfiles'] == ['f_test2'])

        opt = load_options(confpath, 'host1')
        opt.remove_path = ['/tmp/some-fake-path']
        opt.remove_iskey = False
        opt.debug = True
        opt.safe = False
        # by path
        cmd_remove(opt)

        # ensure file is deleted
        self.assertTrue(os.path.exists(df2))
        self.assertFalse(os.path.exists(df3))

        # load dict
        cont = yaml_load(confpath)

        # ensure not present
        self.assertTrue('f_test3' not in cont['dotfiles'])
        self.assertTrue('f_test3' not in cont['profiles']['host1']['dotfiles'])

        # assert rest is intact
        self.assertTrue('host1' in cont['profiles'].keys())
        self.assertFalse('host2' in cont['profiles'].keys())
        self.assertTrue('host3' in cont['profiles'].keys())
        self.assertTrue(cont['profiles']['host1']['dotfiles'] == ['f_test2'])
        self.assertTrue(cont['profiles']['host3']['dotfiles'] == ['f_test2'])


def main():
    """entry point"""
    unittest.main()


if __name__ == '__main__':
    main()