1
|
|
|
# coding: utf8 |
2
|
|
|
|
3
|
|
|
# Copyright 2013-2017 Vincent Jacques <[email protected]> |
4
|
|
|
|
5
|
|
|
from __future__ import division, absolute_import, print_function |
6
|
|
|
|
7
|
|
|
import errno |
8
|
|
|
import pickle |
9
|
|
|
import subprocess |
10
|
|
|
import unittest |
11
|
|
|
|
12
|
|
|
from ActionTree.stock import * |
13
|
|
|
from ActionTree import * |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
class PatchingTestCase(unittest.TestCase): |
17
|
|
|
def patch(self, *args, **kwds): |
18
|
|
|
patcher = unittest.mock.patch(*args, **kwds) |
19
|
|
|
patched = patcher.start() |
20
|
|
|
self.addCleanup(patcher.stop) |
21
|
|
|
return patched |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
class CreateDirectoryTestCase(PatchingTestCase): |
25
|
|
|
def setUp(self): |
26
|
|
|
self.makedirs = self.patch("os.makedirs") |
27
|
|
|
self.isdir = self.patch("os.path.isdir") |
28
|
|
|
|
29
|
|
|
def test_label(self): |
30
|
|
|
self.assertEqual(CreateDirectory("xxx").label, "mkdir xxx") |
31
|
|
|
|
32
|
|
|
self.makedirs.assert_not_called() |
33
|
|
|
self.isdir.assert_not_called() |
34
|
|
|
|
35
|
|
|
def test_pickle(self): |
36
|
|
|
self.assertIsInstance(pickle.dumps(CreateDirectory("xxx")), bytes) |
37
|
|
|
|
38
|
|
|
def test_success(self): |
39
|
|
|
self.makedirs.expect("xxx") |
40
|
|
|
|
41
|
|
|
CreateDirectory("xxx").do_execute({}) |
42
|
|
|
|
43
|
|
|
self.makedirs.assert_called_once_with("xxx") |
44
|
|
|
self.isdir.assert_not_called() |
45
|
|
|
|
46
|
|
|
def test_directory_exists(self): |
47
|
|
|
self.makedirs.side_effect = OSError(errno.EEXIST, "File exists") |
48
|
|
|
self.isdir.return_value = True |
49
|
|
|
|
50
|
|
|
CreateDirectory("xxx").do_execute({}) |
51
|
|
|
|
52
|
|
|
self.makedirs.assert_called_once_with("xxx") |
53
|
|
|
self.isdir.assert_called_once_with("xxx") |
54
|
|
|
|
55
|
|
|
def test_file_exists(self): |
56
|
|
|
self.makedirs.side_effect = OSError(errno.EEXIST, "File exists") |
57
|
|
|
self.isdir.return_value = False |
58
|
|
|
|
59
|
|
|
with self.assertRaises(OSError): |
60
|
|
|
CreateDirectory("xxx").do_execute({}) |
61
|
|
|
|
62
|
|
|
self.makedirs.assert_called_once_with("xxx") |
63
|
|
|
self.isdir.assert_called_once_with("xxx") |
64
|
|
|
|
65
|
|
|
def test_other_failure(self): |
66
|
|
|
self.makedirs.side_effect = OSError(-1, "Foobar") |
67
|
|
|
|
68
|
|
|
with self.assertRaises(OSError): |
69
|
|
|
CreateDirectory("xxx").do_execute({}) |
70
|
|
|
|
71
|
|
|
self.makedirs.assert_called_once_with("xxx") |
72
|
|
|
self.isdir.assert_not_called() |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
class CallSubprocessTestCase(PatchingTestCase): |
76
|
|
|
def setUp(self): |
77
|
|
|
self.check_call = self.patch("subprocess.check_call") |
78
|
|
|
|
79
|
|
|
def test_default_label(self): |
80
|
|
|
self.assertEqual(CallSubprocess(["xxx", "yyy"]).label, "xxx yyy") |
81
|
|
|
|
82
|
|
|
def test_label(self): |
83
|
|
|
self.assertEqual(CallSubprocess(["xxx", "yyy"], label="foo").label, "foo") |
84
|
|
|
|
85
|
|
|
def test_accept_failed_dependencies(self): |
86
|
|
|
self.assertTrue(CallSubprocess(["xxx", "yyy"], accept_failed_dependencies=True).accept_failed_dependencies) |
87
|
|
|
|
88
|
|
|
def test_pickle(self): |
89
|
|
|
self.assertIsInstance(pickle.dumps(CallSubprocess(["xxx", "yyy"])), bytes) |
90
|
|
|
|
91
|
|
|
def test_simple_call(self): |
92
|
|
|
CallSubprocess(["xxx"]).do_execute({}) |
93
|
|
|
|
94
|
|
|
self.check_call.assert_called_once_with(["xxx"]) |
95
|
|
|
|
96
|
|
|
def test_call_with_several_args(self): |
97
|
|
|
self.check_call.expect(["xxx", "yyy"]) |
98
|
|
|
CallSubprocess(["xxx", "yyy"]).do_execute({}) |
99
|
|
|
|
100
|
|
|
self.check_call.assert_called_once_with(["xxx", "yyy"]) |
101
|
|
|
|
102
|
|
|
def test_call_with_kwds(self): |
103
|
|
|
CallSubprocess(["xxx", "yyy"], kwargs=dict(foo="bar")).do_execute({}) |
104
|
|
|
|
105
|
|
|
self.check_call.assert_called_once_with(["xxx", "yyy"], foo="bar") |
106
|
|
|
|
107
|
|
|
def test_called_process_error(self): |
108
|
|
|
self.check_call.side_effect = subprocess.CalledProcessError(1, ["false"], None) |
109
|
|
|
|
110
|
|
|
with self.assertRaises(CalledProcessError) as catcher: |
111
|
|
|
CallSubprocess(["false"]).do_execute({}) |
112
|
|
|
self.assertEqual(catcher.exception.args, (1, ["false"], None)) |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
class CallSubprocessForRealTestCase(unittest.TestCase): |
116
|
|
|
def test_called_process_error(self): |
117
|
|
|
with self.assertRaises(CompoundException) as catcher: |
118
|
|
|
execute(CallSubprocess(["false"])) |
119
|
|
|
self.assertEqual(catcher.exception.exceptions[0].args, (1, ["false"], None)) |
120
|
|
|
|
121
|
|
|
|
122
|
|
View Code Duplication |
class DeleteFileTestCase(PatchingTestCase): |
|
|
|
|
123
|
|
|
def setUp(self): |
124
|
|
|
self.unlink = self.patch("os.unlink") |
125
|
|
|
|
126
|
|
|
def test_label(self): |
127
|
|
|
self.assertEqual(DeleteFile("xxx").label, "rm xxx") |
128
|
|
|
|
129
|
|
|
self.unlink.assert_not_called() |
130
|
|
|
|
131
|
|
|
def test_pickle(self): |
132
|
|
|
self.assertIsInstance(pickle.dumps(DeleteFile("xxx")), bytes) |
133
|
|
|
|
134
|
|
|
def test_success(self): |
135
|
|
|
DeleteFile("xxx").do_execute({}) |
136
|
|
|
|
137
|
|
|
self.unlink.assert_called_once_with("xxx") |
138
|
|
|
|
139
|
|
|
def test_file_does_not_exist(self): |
140
|
|
|
self.unlink.side_effect = OSError(errno.ENOENT, "No such file or directory") |
141
|
|
|
|
142
|
|
|
DeleteFile("xxx").do_execute({}) |
143
|
|
|
|
144
|
|
|
self.unlink.assert_called_once_with("xxx") |
145
|
|
|
|
146
|
|
|
def test_other_failure(self): |
147
|
|
|
self.unlink.side_effect = OSError(-1, "Foobar") |
148
|
|
|
|
149
|
|
|
with self.assertRaises(OSError): |
150
|
|
|
DeleteFile("xxx").do_execute({}) |
151
|
|
|
|
152
|
|
|
self.unlink.assert_called_once_with("xxx") |
153
|
|
|
|
154
|
|
|
|
155
|
|
View Code Duplication |
class DeleteDirectoryTestCase(PatchingTestCase): |
|
|
|
|
156
|
|
|
def setUp(self): |
157
|
|
|
self.rmtree = self.patch("shutil.rmtree") |
158
|
|
|
|
159
|
|
|
def test_label(self): |
160
|
|
|
self.assertEqual(DeleteDirectory("xxx").label, "rm -r xxx") |
161
|
|
|
|
162
|
|
|
self.rmtree.assert_not_called() |
163
|
|
|
|
164
|
|
|
def test_pickle(self): |
165
|
|
|
self.assertIsInstance(pickle.dumps(DeleteDirectory("xxx")), bytes) |
166
|
|
|
|
167
|
|
|
def test_success(self): |
168
|
|
|
DeleteDirectory("xxx").do_execute({}) |
169
|
|
|
|
170
|
|
|
self.rmtree.assert_called_once_with("xxx") |
171
|
|
|
|
172
|
|
|
def test_directory_does_not_exist(self): |
173
|
|
|
self.rmtree.side_effect = OSError(errno.ENOENT, "No such file or directory") |
174
|
|
|
|
175
|
|
|
DeleteDirectory("xxx").do_execute({}) |
176
|
|
|
|
177
|
|
|
self.rmtree.assert_called_once_with("xxx") |
178
|
|
|
|
179
|
|
|
def test_other_failure(self): |
180
|
|
|
self.rmtree.side_effect = OSError(-1, "Foobar") |
181
|
|
|
|
182
|
|
|
with self.assertRaises(OSError): |
183
|
|
|
DeleteDirectory("xxx").do_execute({}) |
184
|
|
|
|
185
|
|
|
self.rmtree.assert_called_once_with("xxx") |
186
|
|
|
|
187
|
|
|
|
188
|
|
|
class CopyFileTestCase(PatchingTestCase): |
189
|
|
|
def setUp(self): |
190
|
|
|
self.copy = self.patch("shutil.copy") |
191
|
|
|
|
192
|
|
|
def test_label(self): |
193
|
|
|
self.assertEqual(CopyFile("from", "to").label, "cp from to") |
194
|
|
|
|
195
|
|
|
self.copy.assert_not_called() |
196
|
|
|
|
197
|
|
|
def test_pickle(self): |
198
|
|
|
self.assertIsInstance(pickle.dumps(CopyFile("from", "to")), bytes) |
199
|
|
|
|
200
|
|
|
def test_success(self): |
201
|
|
|
CopyFile("from", "to").do_execute({}) |
202
|
|
|
|
203
|
|
|
self.copy.assert_called_once_with("from", "to") |
204
|
|
|
|
205
|
|
|
def test_failure(self): |
206
|
|
|
self.copy.side_effect = OSError(-1, "Foobar") |
207
|
|
|
|
208
|
|
|
with self.assertRaises(OSError): |
209
|
|
|
CopyFile("from", "to").do_execute({}) |
210
|
|
|
|
211
|
|
|
self.copy.assert_called_once_with("from", "to") |
212
|
|
|
|
213
|
|
|
|
214
|
|
|
class TouchFileTestCase(PatchingTestCase): |
215
|
|
|
def setUp(self): |
216
|
|
|
self.open = self.patch("ActionTree.stock.open", new=unittest.mock.mock_open(), create=True) |
217
|
|
|
self.utime = self.patch("os.utime") |
218
|
|
|
|
219
|
|
|
def test_label(self): |
220
|
|
|
self.assertEqual(TouchFile("xxx").label, "touch xxx") |
221
|
|
|
|
222
|
|
|
self.open.assert_not_called() |
223
|
|
|
self.utime.assert_not_called() |
224
|
|
|
|
225
|
|
|
def test_pickle(self): |
226
|
|
|
self.assertIsInstance(pickle.dumps(TouchFile("xxx")), bytes) |
227
|
|
|
|
228
|
|
|
def test_success(self): |
229
|
|
|
TouchFile("xxx").do_execute({}) |
230
|
|
|
|
231
|
|
|
self.open.assert_called_once_with("xxx", "ab") |
232
|
|
|
self.open().close.assert_called_once_with() |
233
|
|
|
self.utime.assert_called_once_with("xxx", None) |
234
|
|
|
|
235
|
|
|
|
236
|
|
|
class NullActionTestCase(unittest.TestCase): |
237
|
|
|
def test_label(self): |
238
|
|
|
self.assertIsNone(NullAction().label) |
239
|
|
|
|
240
|
|
|
def test_pickle(self): |
241
|
|
|
self.assertIsInstance(pickle.dumps(NullAction()), bytes) |
242
|
|
|
|
243
|
|
|
def test(self): |
244
|
|
|
NullAction().do_execute({}) |
245
|
|
|
|
246
|
|
|
|
247
|
|
|
class SleepTestCase(PatchingTestCase): |
248
|
|
|
def setUp(self): |
249
|
|
|
self.sleep = self.patch("time.sleep") |
250
|
|
|
|
251
|
|
|
def test_label(self): |
252
|
|
|
self.assertEqual(Sleep(1).label, "sleep 1") |
253
|
|
|
|
254
|
|
|
self.sleep.assert_not_called() |
255
|
|
|
|
256
|
|
|
def test_pickle(self): |
257
|
|
|
self.assertIsInstance(pickle.dumps(Sleep(1)), bytes) |
258
|
|
|
|
259
|
|
|
def test(self): |
260
|
|
|
Sleep(1).do_execute({}) |
261
|
|
|
|
262
|
|
|
self.sleep.assert_called_once_with(1) |
263
|
|
|
|