Some helpers to make interacting with the shell easier
Project description
Some helpers to make interacting with the shell easier
Documentation: https://knowsuchagency.github.io/shell-utils
Installation
pipenv install shell-utils
Usage
def test_shell_capture():
"""Test shell output capture."""
string = 'hello'
echo_string = shell(f'echo {string}', capture=True)
assert echo_string.returncode == 0
assert echo_string.stdout.decode().strip() == string
to_stderr = shell(f'echo "{string}" >&2', capture=True)
assert to_stderr.returncode == 0
assert to_stderr.stderr.decode().strip() == string
def test_shell_raises():
"""Test shell raises."""
import subprocess as sp
with pytest.raises(sp.CalledProcessError):
shell('exit 1')
assert shell('exit 1', check=False).returncode == 1
def test_env():
"""Test env context manager."""
import os
import copy
original_env = copy.deepcopy(os.environ)
string = 'world'
with env(hello=string):
assert os.environ['hello'] == string
assert 'hello' not in os.environ
assert os.environ == original_env
def test_path():
"""Test path context manager."""
from pathlib import Path
from tempfile import TemporaryDirectory
import os
original_path = Path(os.environ['PATH'])
with TemporaryDirectory() as temp_dir, path(temp_dir) as new_path:
temp_dir = Path(temp_dir)
assert new_path[-1] == temp_dir.__fspath__()
assert Path(os.environ['PATH']) != original_path
assert Path(os.environ['PATH']) == original_path
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
shell-utils-0.3.1.tar.gz
(8.3 MB
view hashes)
Built Distribution
Close
Hashes for shell_utils-0.3.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ffca9e3e6f47bfaf08c3035ff2ef36aa4d3099aa4e7bf043b901b9b9c67bdd7a |
|
MD5 | 3f43f9851e9c99a592af025a30a50528 |
|
BLAKE2b-256 | 143936d83ec7b9fe14d2a50637215000b515d65b60c95abfee2b55eb558e1a39 |