Package list python-procrunner / d6213fe
Create a new return object for run() function Markus Gerstel 2 years ago
3 changed file(s) with 77 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
55 ------------------
66
77 * Support file system path objects (PEP-519) in arguments
8 * Change the return object to make it similar to
9 subprocess.CompletedProcess, introduced with Python 3.5+
810
911 0.9.1 (2019-02-22)
1012 ------------------
330330 return command
331331
332332
333 if sys.version_info < (3, 5):
334
335 class _ReturnObjectParent(object):
336 def check_returncode(self):
337 if self.returncode:
338 raise Exception(
339 "Call %r resulted in non-zero exit code %r"
340 % (self.args, self.returncode)
341 )
342
343
344 else:
345 _ReturnObjectParent = subprocess.CompletedProcess
346
347
348 class ReturnObject(dict, _ReturnObjectParent):
349 def __init__(self, *arg, **kw):
350 super(ReturnObject, self).__init__(*arg, **kw)
351 self.args = self["command"]
352 self.returncode = self["exitcode"]
353 self.stdout = self["stdout"]
354 self.stderr = self["stderr"]
355
356
333357 def run(
334358 command,
335359 timeout=None,
253253 callback.assert_not_called()
254254 aggregator.flush()
255255 callback.assert_called_once_with("morestuff")
256
257
258 def test_return_object_semantics():
259 ro = procrunner.ReturnObject(
260 {
261 "command": mock.sentinel.command,
262 "exitcode": 0,
263 "stdout": mock.sentinel.stdout,
264 "stderr": mock.sentinel.stderr,
265 }
266 )
267 assert ro["command"] == mock.sentinel.command
268 assert ro.args == mock.sentinel.command
269 assert ro["exitcode"] == 0
270 assert ro.returncode == 0
271 assert ro["stdout"] == mock.sentinel.stdout
272 assert ro.stdout == mock.sentinel.stdout
273 assert ro["stderr"] == mock.sentinel.stderr
274 assert ro.stderr == mock.sentinel.stderr
275
276 with pytest.raises(KeyError):
277 ro["unknownkey"]
278 ro.update({"unknownkey": mock.sentinel.key})
279 assert ro["unknownkey"] == mock.sentinel.key
280
281
282 def test_return_object_check_function_passes_on_success():
283 ro = procrunner.ReturnObject(
284 {
285 "command": mock.sentinel.command,
286 "exitcode": 0,
287 "stdout": mock.sentinel.stdout,
288 "stderr": mock.sentinel.stderr,
289 }
290 )
291 ro.check_returncode()
292
293
294 def test_return_object_check_function_raises_on_error():
295 ro = procrunner.ReturnObject(
296 {
297 "command": mock.sentinel.command,
298 "exitcode": 1,
299 "stdout": mock.sentinel.stdout,
300 "stderr": mock.sentinel.stderr,
301 }
302 )
303 with pytest.raises(Exception) as e:
304 ro.check_returncode()
305 assert repr(mock.sentinel.command) in str(e.value)
306 assert "1" in str(e.value)