|
12 | 12 | import os
|
13 | 13 | import random
|
14 | 14 | import re
|
| 15 | +from subprocess import CalledProcessError |
15 | 16 | import time
|
16 | 17 |
|
17 | 18 | from . import coverage
|
@@ -57,6 +58,30 @@ def assert_raises_message(exc, message, fun, *args, **kwds):
|
57 | 58 | else:
|
58 | 59 | raise AssertionError("No exception raised")
|
59 | 60 |
|
| 61 | +def assert_raises_process_error(returncode, output, fun, *args, **kwds): |
| 62 | + """Execute a process and asserts the process return code and output. |
| 63 | +
|
| 64 | + Calls function `fun` with arguments `args` and `kwds`. Catches a CalledProcessError |
| 65 | + and verifies that the return code and output are as expected. Throws AssertionError if |
| 66 | + no CalledProcessError was raised or if the return code and output are not as expected. |
| 67 | +
|
| 68 | + Args: |
| 69 | + returncode (int): the process return code. |
| 70 | + output (string): [a substring of] the process output. |
| 71 | + fun (function): the function to call. This should execute a process. |
| 72 | + args*: positional arguments for the function. |
| 73 | + kwds**: named arguments for the function. |
| 74 | + """ |
| 75 | + try: |
| 76 | + fun(*args, **kwds) |
| 77 | + except CalledProcessError as e: |
| 78 | + if returncode != e.returncode: |
| 79 | + raise AssertionError("Unexpected returncode %i" % e.returncode) |
| 80 | + if output not in e.output: |
| 81 | + raise AssertionError("Expected substring not found:" + e.output) |
| 82 | + else: |
| 83 | + raise AssertionError("No exception raised") |
| 84 | + |
60 | 85 | def assert_raises_jsonrpc(code, message, fun, *args, **kwds):
|
61 | 86 | """Run an RPC and verify that a specific JSONRPC exception code and message is raised.
|
62 | 87 |
|
|
0 commit comments