1
|
|
|
# coding: utf8 |
2
|
|
|
|
3
|
|
|
# Copyright 2013-2015 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_label(self): |
80
|
|
|
self.assertEqual(CallSubprocess(["xxx", "yyy"]).label, "xxx yyy") |
81
|
|
|
|
82
|
|
|
self.check_call.assert_not_called() |
83
|
|
|
|
84
|
|
|
def test_pickle(self): |
85
|
|
|
self.assertIsInstance(pickle.dumps(CallSubprocess(["xxx", "yyy"])), bytes) |
86
|
|
|
|
87
|
|
|
def test_simple_call(self): |
88
|
|
|
CallSubprocess(["xxx"]).do_execute() |
89
|
|
|
|
90
|
|
|
self.check_call.assert_called_once_with(["xxx"]) |
91
|
|
|
|
92
|
|
|
def test_call_with_several_args(self): |
93
|
|
|
self.check_call.expect(["xxx", "yyy"]) |
94
|
|
|
CallSubprocess(["xxx", "yyy"]).do_execute() |
95
|
|
|
|
96
|
|
|
self.check_call.assert_called_once_with(["xxx", "yyy"]) |
97
|
|
|
|
98
|
|
|
def test_call_with_kwds(self): |
99
|
|
|
CallSubprocess(["xxx", "yyy"], foo="bar").do_execute() |
100
|
|
|
|
101
|
|
|
self.check_call.assert_called_once_with(["xxx", "yyy"], foo="bar") |
102
|
|
|
|
103
|
|
|
def test_called_process_error(self): |
104
|
|
|
self.check_call.side_effect = subprocess.CalledProcessError(1, ["false"], None) |
105
|
|
|
|
106
|
|
|
with self.assertRaises(CalledProcessError) as catcher: |
107
|
|
|
CallSubprocess(["false"]).do_execute() |
108
|
|
|
self.assertEqual(catcher.exception.args, (1, ["false"], None)) |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
class CallSubprocessForRealTestCase(unittest.TestCase): |
112
|
|
|
def test_called_process_error(self): |
113
|
|
|
with self.assertRaises(CompoundException) as catcher: |
114
|
|
|
execute(CallSubprocess(["false"])) |
115
|
|
|
self.assertEqual(catcher.exception.exceptions[0].args, (1, ["false"], None)) |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
class DeleteFileTestCase(PatchingTestCase): |
119
|
|
|
def setUp(self): |
120
|
|
|
self.unlink = self.patch("os.unlink") |
121
|
|
|
|
122
|
|
|
def test_label(self): |
123
|
|
|
self.assertEqual(DeleteFile("xxx").label, "rm xxx") |
124
|
|
|
|
125
|
|
|
self.unlink.assert_not_called() |
126
|
|
|
|
127
|
|
|
def test_pickle(self): |
128
|
|
|
self.assertIsInstance(pickle.dumps(DeleteFile("xxx")), bytes) |
129
|
|
|
|
130
|
|
|
def test_success(self): |
131
|
|
|
DeleteFile("xxx").do_execute() |
132
|
|
|
|
133
|
|
|
self.unlink.assert_called_once_with("xxx") |
134
|
|
|
|
135
|
|
|
def test_file_does_not_exist(self): |
136
|
|
|
self.unlink.side_effect = OSError(errno.ENOENT, "No such file or directory") |
137
|
|
|
|
138
|
|
|
DeleteFile("xxx").do_execute() |
139
|
|
|
|
140
|
|
|
self.unlink.assert_called_once_with("xxx") |
141
|
|
|
|
142
|
|
|
def test_other_failure(self): |
143
|
|
|
self.unlink.side_effect = OSError(-1, "Foobar") |
144
|
|
|
|
145
|
|
|
with self.assertRaises(OSError): |
146
|
|
|
DeleteFile("xxx").do_execute() |
147
|
|
|
|
148
|
|
|
self.unlink.assert_called_once_with("xxx") |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
class CopyFileTestCase(PatchingTestCase): |
152
|
|
|
def setUp(self): |
153
|
|
|
self.copy = self.patch("shutil.copy") |
154
|
|
|
|
155
|
|
|
def test_label(self): |
156
|
|
|
self.assertEqual(CopyFile("from", "to").label, "cp from to") |
157
|
|
|
|
158
|
|
|
self.copy.assert_not_called() |
159
|
|
|
|
160
|
|
|
def test_pickle(self): |
161
|
|
|
self.assertIsInstance(pickle.dumps(CopyFile("from", "to")), bytes) |
162
|
|
|
|
163
|
|
|
def test_success(self): |
164
|
|
|
CopyFile("from", "to").do_execute() |
165
|
|
|
|
166
|
|
|
self.copy.assert_called_once_with("from", "to") |
167
|
|
|
|
168
|
|
|
def test_failure(self): |
169
|
|
|
self.copy.side_effect = OSError(-1, "Foobar") |
170
|
|
|
|
171
|
|
|
with self.assertRaises(OSError): |
172
|
|
|
CopyFile("from", "to").do_execute() |
173
|
|
|
|
174
|
|
|
self.copy.assert_called_once_with("from", "to") |
175
|
|
|
|
176
|
|
|
|
177
|
|
|
class TouchFileTestCase(PatchingTestCase): |
178
|
|
|
def setUp(self): |
179
|
|
|
self.open = self.patch("ActionTree.stock.open", new=unittest.mock.mock_open(), create=True) |
180
|
|
|
self.utime = self.patch("os.utime") |
181
|
|
|
|
182
|
|
|
def test_label(self): |
183
|
|
|
self.assertEqual(TouchFile("xxx").label, "touch xxx") |
184
|
|
|
|
185
|
|
|
self.open.assert_not_called() |
186
|
|
|
self.utime.assert_not_called() |
187
|
|
|
|
188
|
|
|
def test_pickle(self): |
189
|
|
|
self.assertIsInstance(pickle.dumps(TouchFile("xxx")), bytes) |
190
|
|
|
|
191
|
|
|
def test_success(self): |
192
|
|
|
TouchFile("xxx").do_execute() |
193
|
|
|
|
194
|
|
|
self.open.assert_called_once_with("xxx", "ab") |
195
|
|
|
self.open().close.assert_called_once_with() |
196
|
|
|
self.utime.assert_called_once_with("xxx", None) |
197
|
|
|
|
198
|
|
|
|
199
|
|
|
class NullActionTestCase(unittest.TestCase): |
200
|
|
|
def test_label(self): |
201
|
|
|
self.assertIsNone(NullAction().label) |
202
|
|
|
|
203
|
|
|
def test_pickle(self): |
204
|
|
|
self.assertIsInstance(pickle.dumps(NullAction()), bytes) |
205
|
|
|
|
206
|
|
|
def test(self): |
207
|
|
|
NullAction().do_execute() |
208
|
|
|
|
209
|
|
|
|
210
|
|
|
class SleepTestCase(PatchingTestCase): |
211
|
|
|
def setUp(self): |
212
|
|
|
self.sleep = self.patch("time.sleep") |
213
|
|
|
|
214
|
|
|
def test_label(self): |
215
|
|
|
self.assertEqual(Sleep(1).label, "sleep 1") |
216
|
|
|
|
217
|
|
|
self.sleep.assert_not_called() |
218
|
|
|
|
219
|
|
|
def test_pickle(self): |
220
|
|
|
self.assertIsInstance(pickle.dumps(Sleep(1)), bytes) |
221
|
|
|
|
222
|
|
|
def test(self): |
223
|
|
|
Sleep(1).do_execute() |
224
|
|
|
|
225
|
|
|
self.sleep.assert_called_once_with(1) |
226
|
|
|
|