-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Cargo hack for checking all features properly in qa * StdCapture context manager python, panic_on_err_async macro, bash runner CmdOut contains attempted commands and a pretty formatting method of those commands to see what caused a failure, plus CmdOut is attached to BashErr errors coming out too
- Loading branch information
Showing
19 changed files
with
374 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import io | ||
import sys | ||
import typing as tp | ||
|
||
|
||
class StdCapture(list): | ||
r"""Capture stdout/stderr for the duration of a with block. | ||
(e.g. print statements) | ||
Example: | ||
```python | ||
with StdCapture(stderr=True) as out: # By default only captures stdout | ||
print('hello') | ||
sys.stderr.write('world') | ||
print(out) # ['hello', 'world'] | ||
``` | ||
""" | ||
|
||
_stderr_capture: bool | ||
_stdout: "tp.TextIO" | ||
_stderr: "tp.TextIO" | ||
_buf: "io.StringIO" | ||
_out: "list[str]" | ||
|
||
def __init__(self, stderr: bool = False): | ||
"""Creation of new capturer. | ||
By default only stdout is captured, stderr=True enables capturing stderr too. | ||
""" | ||
# Prep all instance vars: | ||
self.stderr_capture = stderr | ||
self._out = [] | ||
self._stdout = sys.stdout | ||
self._stderr = sys.stderr | ||
self._buf = io.StringIO() | ||
|
||
def __enter__(self) -> list[str]: | ||
"""Entering the capturing context.""" | ||
# Overwrite sinks which are being captured with the buffer: | ||
sys.stdout = self._buf | ||
if self.stderr_capture: | ||
sys.stderr = self._buf | ||
|
||
return self._out | ||
|
||
def __exit__(self, *args): # type: ignore | ||
"""On context exit.""" | ||
# First reset the global streams: | ||
sys.stdout = self._stdout | ||
sys.stderr = self._stderr | ||
|
||
self._out.extend(self._buf.getvalue().splitlines()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import sys | ||
|
||
from bitbazaar.misc import StdCapture | ||
|
||
|
||
def test_std_capture(): | ||
orig_stdout = sys.stdout | ||
orig_stderr = sys.stderr | ||
|
||
# Confirm the example works: | ||
with StdCapture(stderr=True) as out: # By default only captures stdout | ||
print("hello") | ||
sys.stderr.write("world") | ||
assert out == ["hello", "world"] | ||
|
||
# Confirm no stderr captured if not requested: | ||
with StdCapture() as out: | ||
sys.stderr.write("world") | ||
assert out == [] | ||
|
||
# Confirm returned to originals: | ||
assert sys.stdout is orig_stdout | ||
assert sys.stderr is orig_stderr | ||
|
||
# Confirm would also return if error occurred inside block: | ||
try: | ||
with StdCapture(stderr=True): | ||
raise ValueError("error") | ||
except ValueError: | ||
pass | ||
|
||
assert sys.stdout is orig_stdout | ||
assert sys.stderr is orig_stderr |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.