-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add shell_run() and exec_in_context() + basic tests
- Loading branch information
1 parent
97fc5c2
commit 8837150
Showing
4 changed files
with
34 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
""" Miscellaneous utility functions """ | ||
import contextlib | ||
import io | ||
import subprocess | ||
|
||
|
||
def shell_run(command_str): | ||
"""Run a shell command and return stdout/stderr""" | ||
out = subprocess.run(command_str, capture_output=True, shell=True, text=True) | ||
return "\n".join([out.stdout, out.stderr]) | ||
|
||
|
||
def exec_in_context(func, *args, **kwargs): | ||
"""Execute a function in a context manager to capture stdout/stderr""" | ||
with contextlib.redirect_stdout(io.StringIO()) as out_f, contextlib.redirect_stderr( | ||
io.StringIO() | ||
) as err_f: | ||
func(*args, **kwargs) | ||
out_combined = "\n".join([out_f.getvalue(), err_f.getvalue()]) | ||
return out_combined |
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 |
---|---|---|
@@ -1,2 +1,7 @@ | ||
def test_example(): | ||
assert True | ||
from ccbr_tools.util import shell_run | ||
|
||
|
||
def test_help_jobby(): | ||
assert "Will take your job(s)... and display their information!" in shell_run( | ||
"jobby -h" | ||
) |
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,5 @@ | ||
from ccbr_tools.util import exec_in_context | ||
|
||
|
||
def test_exec(): | ||
assert exec_in_context(print, "hello", "world") == "hello world\n\n" |