Skip to content

Commit

Permalink
feat: add shell_run() and exec_in_context() + basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kelly-sovacool committed Aug 8, 2024
1 parent 97fc5c2 commit 8837150
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ classifiers = [
requires-python = ">=3.10"
dependencies = [
"biopython",
"pandas",
"Click >= 8.1.3",
"pandas",
"requests"
]

[project.optional-dependencies]
Expand Down
20 changes: 20 additions & 0 deletions src/ccbr_tools/util.py
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
9 changes: 7 additions & 2 deletions tests/test_cli.py
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"
)
5 changes: 5 additions & 0 deletions tests/test_util.py
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"

0 comments on commit 8837150

Please sign in to comment.