Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions run/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,24 @@ def __new__(cls, *args, **kwargs):

return obj

def check_returncode(self):
if self.returncode != 0:
raise subprocess.CalledProcessError(
returncode=self.returncode,
cmd=self.command,
)

@property
def returncode(self):
return self.status

@property
def status(self):
self.process.communicate()
return self.process.returncode
if not hasattr(self, '_status'):
self.process.communicate()
self._status = self.process.returncode

return self._status

@property
def stdout(self):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def run(self):
setup(name='subprocess.run',
version=run.__version__,
data_files = [
(get_python_lib(), [pth_file]),
(get_python_lib(prefix=''), [pth_file]),
],
packages=find_packages(),
author='Sebastian Pawluś',
Expand Down
18 changes: 18 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,24 @@ def test_status():
assert run('%s not_existing_directory' % _commands.rm).status != 0


def test_returncode():
assert run(_commands.ls).returncode == 0
# win workaround
assert run('%s not_existing_directory' % _commands.rm).returncode != 0


def test_check_returncode():
assert run(_commands.ls).check_returncode() is None

# win workaround
try:
run('%s not_existing_directory' % _commands.rm).check_returncode()
except subprocess.CalledProcessError:
pass
else:
raise AssertionError('CalledProcessError was not raised')


def test_chain():
command = run('ps aux', 'wc -l', 'wc -c')

Expand Down