forked from clintmod/macprefs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_utils.py
154 lines (120 loc) · 4.21 KB
/
test_utils.py
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
from subprocess import CalledProcessError
import utils
from mock import patch
import config
@patch('utils.check_output')
def test_execute_shell(check_output_mock):
check_output_mock.side_effect = check_output_func
utils.execute_shell(['asdf'])
# pylint: disable=unused-argument
def check_output_func(command, shell, cwd, stderr):
length = len(command)
assert length > 0
assert command[0] == 'asdf'
return ''
@patch('utils.check_output')
def test_execute_shell_handles_errors(check_output_mock):
check_output_mock.side_effect = check_output_error_func
try:
utils.execute_shell(['expecting error'])
assert False, 'expected CalledProcessError'
except CalledProcessError:
pass
def check_output_error_func(command, shell, cwd, stderr):
raise CalledProcessError(0, command)
@patch('utils.check_output')
def test_execute_shell_handles_verbose(check_output_mock):
utils.execute_shell(['asdf'], False, '.', False, True)
@patch('utils.execute_shell')
def test_copy_dir_works_with_extra_args(execute_shell_mock):
utils.copy_dir('src', 'dest')
execute_shell_mock.assert_called_with(
['rsync', '-a', 'src', 'dest']
)
@patch('utils.execute_shell')
def test_copy_dir(execute_shell_mock):
utils.copy_dir('src', 'dest')
execute_shell_mock.assert_called_with(
['rsync', '-a', 'src', 'dest']
)
@patch('utils.execute_shell')
def test_copy_dir_works_with_sudo(execute_shell_mock):
utils.copy_dir('src', 'dest', with_sudo=True)
execute_shell_mock.assert_called_with(
['sudo', 'rsync', '-a', 'src', 'dest']
)
@patch('utils.execute_shell')
def test_copy_files(execute_shell_mock):
files = ['asdf']
dest = "asdf2"
utils.copy_files(files, dest)
execute_shell_mock.assert_called_with(
['rsync', '-a'] + files + [dest]
)
@patch('utils.execute_shell')
def test_change_owner(execute_shell_mock):
ssh_dir = config.get_ssh_user_dir()
utils.change_owner(ssh_dir, 'clint', True)
execute_shell_mock.assert_called_with(
['sudo', 'chown', '-R', 'clint', ssh_dir]
)
@patch('utils.execute_shell')
def test_change_mode(execute_shell_mock):
ssh_dir = config.get_ssh_user_dir()
utils.change_mode(ssh_dir, 600, True)
execute_shell_mock.assert_called_with(
['sudo', 'chmod', '-R', '600', ssh_dir]
)
@patch('utils.execute_shell')
def test_ensure_subdirs_listable(execute_shell_mock):
ssh_dir = config.get_ssh_user_dir()
utils.ensure_subdirs_listable(config.get_ssh_user_dir())
execute_shell_mock.assert_called_with(
['sudo', 'chmod', '-R', 'a+X', ssh_dir]
)
@patch('utils.ensure_subdirs_listable')
@patch('utils.change_owner')
@patch('utils.change_mode')
def test_ensure_dir_owned_by_user(chmod_mock, chown_mock, listable_mock):
dest = config.get_ssh_user_dir
utils.ensure_dir_owned_by_user(dest, 'clint')
chmod_mock.assert_called_with(dest, '600')
chown_mock.assert_called_with(dest, 'clint')
listable_mock.assert_called_with(dest)
@patch('utils.change_owner_for_files')
@patch('utils.change_mode_for_files')
def test_ensure_files_owned_by_user(mode_mock, owner_mock):
files = ['.no_file']
mode = '622'
user = config.get_user()
utils.ensure_files_owned_by_user(user, files, mode)
mode_mock.assert_called_with(files, mode)
owner_mock.assert_called_with(files, user)
@patch('utils.execute_shell')
def test_change_owner_for_files(shell_mock):
files = ['.no_file']
user = config.get_user()
utils.change_owner_for_files(files, user)
shell_mock.assert_called_with(
['sudo', 'chown', user] + files
)
@patch('utils.execute_shell')
def test_change_mode_for_files(shell_mock):
files = ['.no_file']
mode = '622'
utils.change_mode_for_files(files, mode)
shell_mock.assert_called_with(
['sudo', 'chmod', mode] + files
)
def test_is_none_or_empty_string():
assert utils.is_none_or_empty_string('')
assert utils.is_none_or_empty_string(None)
assert not utils.is_none_or_empty_string('asdf')
@patch("utils.execute_shell")
def test_copy_file(shell_mock):
fle = "asdf"
dest = "wtf"
utils.copy_file(fle, dest)
shell_mock.assert_called_with(
['rsync', '-a', fle, dest]
)