Skip to content
This repository has been archived by the owner on Jan 14, 2024. It is now read-only.

Commit

Permalink
#55: By default decrease verbosity in sh()
Browse files Browse the repository at this point in the history
  • Loading branch information
blackandred committed Nov 9, 2020
1 parent 3d7c79b commit 47058d7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/rkd/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def __init__(self):
self.has_exited = False


def check_call(command: str, script: Optional[str] = ''):
def check_call(command: str, script_to_show: Optional[str] = ''):
if os.getenv('RKD_COMPAT_SUBPROCESS') == 'true':
subprocess.check_call(command, shell=True)
return
Expand Down Expand Up @@ -88,7 +88,7 @@ def check_call(command: str, script: Optional[str] = ''):

if exit_code > 0:
raise subprocess.CalledProcessError(
exit_code, script if script else command, stderr=out_buffer.get_value(), output=out_buffer.get_value()
exit_code, script_to_show if script_to_show else command, stderr=out_buffer.get_value(), output=out_buffer.get_value()
)


Expand Down
10 changes: 8 additions & 2 deletions src/rkd/taskutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from subprocess import check_output, Popen, DEVNULL, CalledProcessError
from tempfile import NamedTemporaryFile
from abc import ABC as AbstractClass, abstractmethod
from copy import deepcopy
from .api.inputoutput import IO
from .process import check_call

Expand Down Expand Up @@ -64,6 +65,10 @@ def sh(self, cmd: str, capture: bool = False, verbose: bool = False, strict: boo
"""

self.io().debug('sh(%s)' % cmd)
is_debug = self.io().is_log_level_at_least('debug')

# cmd without environment variables
original_cmd = deepcopy(cmd)

cmd = 'export PYTHONUNBUFFERED=1; ' + cmd

Expand Down Expand Up @@ -92,7 +97,8 @@ def sh(self, cmd: str, capture: bool = False, verbose: bool = False, strict: boo
bash_temp_file.write(bash_script.encode('utf-8'))
bash_temp_file.flush()

check_call('bash ' + bash_temp_file.name, script=bash_script)
check_call('bash ' + bash_temp_file.name,
script_to_show=original_cmd if not is_debug else bash_script)

return

Expand Down Expand Up @@ -133,7 +139,7 @@ def py(self, code: str = '', become: str = None, capture: bool = False,
os.putenv('RKD_BIN', self.get_rkd_binary())

if not capture:
check_call(cmd + ' ' + arguments, script=code)
check_call(cmd + ' ' + arguments, script_to_show=code)
os.unlink(py_temp_file.name) if py_temp_file else None
return

Expand Down
31 changes: 30 additions & 1 deletion test/test_taskutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def test_sh_rkd_in_rkd_shows_first_lines_on_error(self):
def test_non_interactive_session_returns_output(self):
"""Checks functionally if process.py is implementing a fall-back for non-interactive sessions
"true |" part enforces the console to be non-interactive, which should cause
'true |' part enforces the console to be non-interactive, which should cause
"termios.error: (25, 'Inappropriate ioctl for device')" that should be handled and interactive mode
should be turned off for stdin
"""
Expand All @@ -167,6 +167,35 @@ def test_non_interactive_session_returns_output(self):

self.assertIn('Strajk Kobiet! Jebac PiS!', out.getvalue())

def test_full_command_is_shown_only_in_debug_output_level(self):
"""Test that sh() will show full bash script only in case, when '-rl debug' is used
:return:
"""

task = InitTask()
task._io = IO()
io = IO()
out = StringIO()

with io.capture_descriptors(stream=out, enable_standard_out=False):
# CASE 1
with self.subTest('NORMAL output level'):
try:
task.sh('python3 -m rkd :sh -c "exit 5"')

except subprocess.CalledProcessError as e:
self.assertIn("Command 'exit 5' returned non-zero exit status 5.", e.output)

# CASE 2
with self.subTest('DEBUG output level'):
try:
task.sh('python3 -m rkd -rl debug :sh -c "exit 5"')

except subprocess.CalledProcessError as e:
self.assertIn("Command '#!/bin/bash -eopipefail \r\nset -euo pipefail; export " +
"PYTHONUNBUFFERED=1; exit 5' returned non-zero exit status 5.", e.output)

def test_dollar_symbols_are_escaped_in_shell_commands(self):
"""Check that in envrionment variable there can be defined a value that contains dollar symbols"""

Expand Down

0 comments on commit 47058d7

Please sign in to comment.