Skip to content

Commit

Permalink
Removing escape sequence from output in cmd_output function!
Browse files Browse the repository at this point in the history
In console outputs we are getting escape sequences because
of which most of the testcases are failing to use the output
of few commands. This PR removes the escape sequence first
then use the output.This PR fixes multiple testcases.

Signed-off-by: Anushree Mathur <anushree.mathur@linux.vnet.ibm.com>
  • Loading branch information
Anushree-Mathur committed Feb 19, 2025
1 parent 72d2e67 commit 998b79f
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions aexpect/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,8 @@ def _get_aexpect_helper(self, helper_cmd, pass_fds, echo, command):
full_output += output
sub_status = sub.poll()
if sub_status is not None:
raise ExpectProcessTerminatedError(pattern, sub_status, full_output)
raise ExpectProcessTerminatedError(
pattern, sub_status, full_output)
else:
raise ExpectTimeoutError(pattern, full_output)
return sub
Expand Down Expand Up @@ -1188,7 +1189,7 @@ def read_up_to_prompt(self, timeout=60.0, internal_timeout=None,
print_func)[1]

def cmd_output(self, cmd, timeout=60, internal_timeout=None,
print_func=None, safe=False):
print_func=None, safe=False, strip_console_codes=False):
"""
Send a command and return its output.
Expand All @@ -1203,15 +1204,19 @@ def cmd_output(self, cmd, timeout=60, internal_timeout=None,
error messages that make read_up_to_prompt to timeout. Let's
try to be a little more robust and send a carriage return, to
see if we can get to the prompt when safe=True.
:param strip_console_codes: Whether remove the escape sequence from the output
In serial session there are escape sequences present,if it is not
expected while reading the output remove it by passing
serial_console_codes = True
:return: The output of cmd
:raise ShellTimeoutError: Raised if timeout expires
:raise ShellProcessTerminatedError: Raised if the shell process
terminates while waiting for output
:raise ShellError: Raised if an unknown error occurs
"""
if safe:
return self.cmd_output_safe(cmd, timeout)
return self.cmd_output_safe(cmd, timeout, strip_console_codes)
session_tag = f"[{self.output_prefix}] " if self.output_prefix else ""
LOG.debug("%sSending command: %s", session_tag, cmd)
self.read_nonblocking(0, timeout)
Expand All @@ -1223,16 +1228,22 @@ def cmd_output(self, cmd, timeout=60, internal_timeout=None,
raise ShellTimeoutError(cmd, output) from error
except ExpectProcessTerminatedError as error:
output = self.remove_command_echo(error.output, cmd)
raise ShellProcessTerminatedError(cmd, error.status, output) from error
raise ShellProcessTerminatedError(
cmd, error.status, output) from error
except ExpectError as error:
output = self.remove_command_echo(error.output, cmd)
raise ShellError(cmd, output) from error

# Remove the echoed command and the final shell prompt
if strip_console_codes:
# Removing the escape sequence
output_no_escape = astring.strip_console_codes(out)
return self.remove_last_nonempty_line(self.remove_command_echo(output_no_escape,
cmd))
return self.remove_last_nonempty_line(self.remove_command_echo(out,
cmd))

def cmd_output_safe(self, cmd, timeout=60):
def cmd_output_safe(self, cmd, timeout=60, strip_console_codes=False):
"""
Send a command and return its output (serial sessions).
Expand All @@ -1244,6 +1255,10 @@ def cmd_output_safe(self, cmd, timeout=60):
:param cmd: Command to send (must not contain newline characters)
:param timeout: The duration (in seconds) to wait for the prompt to
return
:param strip_console_codes: Whether remove the escape sequence from the output
In serial session there are escape sequences present,if it is not
expected while reading the output remove it by passing
serial_console_codes = True
:return: The output of cmd
:raise ShellTimeoutError: Raised if timeout expires
Expand Down Expand Up @@ -1278,6 +1293,11 @@ def cmd_output_safe(self, cmd, timeout=60):
raise ShellTimeoutError(cmd, out)

# Remove the echoed command and the final shell prompt
if strip_console_codes:
# Removing the escape sequence
output_no_escape = astring.strip_console_codes(out)
return self.remove_last_nonempty_line(self.remove_command_echo(output_no_escape,
cmd))
return self.remove_last_nonempty_line(self.remove_command_echo(out,
cmd))

Expand Down

0 comments on commit 998b79f

Please sign in to comment.