-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Removing escape sequence from output in cmd_output function! #142
base: main
Are you sure you want to change the base?
Conversation
In total 2000+ testcases are failing but to give an example I am mentioning the following testcase!
After applying the patch:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the strip_console_codes
should be used at all times as get_output
might be used to interact with any kind of output. IMO the consumers should decide whether they need to strip the console codes or leave them present in the output.
It's true that people interacting with odd outputs might use read_nonblocking
directly to get the full output, but having get_output
seems convenient and some only need to check the output after the execution (eg. tests needing to check whether arrows were used correctly).
I mean if there were thousands of lines where people need to clean the output, I'd consider adding an argument to do so, but doing it out of the box seems like removing useful feature for lower-level usage.
Hello @ldoktor , thanks alot for taking time and reviewing this commit! I understand your point, so do you suggest that we do it locally in the functions where these outputs are being used? Could you please guide me how i can proceed otherwise? Thanks in advance! |
What I meant is to do it explicitly in the test itself: from aexpect.utils.astring import strip_console_codes
...
out = session.cmd_output(...)
out_safe = strip_console_codes(out) But if it proves to be massively used we might consider support for |
Hello @ldoktor , understood your point clearly! Thank you so much. Will make the changes in the testcase itself. |
Hi, I will be able to test and review this towards the end of the week, I hope this is ok and thanks for the contribution! |
Hi @ldoktor @pevogam , I have tried modifying my PR in the following way as suggested by @ldoktor! Many places cmd_output is used so I changed the code here and tried to run the same tescase after applying the strip_console_code=True in the local code where I wanted the O/P to be replaced and it worked totally fine! I would really like to have some suggestions here if I can proceed in this manner! Thanks in advance for your time.
|
Hi @Anushree-Mathur, could you perhaps provide a diff instead so that it is easy to parse what modification entails? |
Hi @pevogam , here is the git diff result:
|
This seems fine by me, provided the code is polished a bit. Mainly it needs to tackle the |
I second that opinion. This change is invasive and should preserve the behavior for anyone not wishing to remove the console codes and likely has their own handling logic down the line.
I think this diff is much more reasonable and perhaps you could push the suggested change for easier review now?
I can definitely do in-line commenting and recommendations once I see this pushed but @Anushree-Mathur it would be great if you take in @ldoktor's recommendation before the push so we can save on the feedback cycles. Also, I wonder what is the difference between this PR and #141? Could we instead close one of them and focus on just one pull request regarding escaping such characters? |
Well the #141 is solely about the last_line_matches, which is mainly used to detect console. It's safer to use filtering there as the whole output is returned while the filtered one is only used for the detection. Note I'd classify the #141 actually as a bugfix as I think we ought to treat the output that way and I'm surprised we're not hitting problems with it too frequently. As for this one it's a convenience for test-writers who could use this feature to get filtered output and I like @Anushree-Mathur kept the default behaviour as is so unless one deliberately opts-in for it they get the same behaviour. |
Even then the changes seem quite related and could have at least be two commits in the same branch like "fix critical problem" followed by "enhance use for others for related problem" but your arguments also make a lot of sense. Alright, in any case still good to see the suggested changes pushed so that we can test them. |
Any updates on this @Anushree-Mathur? |
Hi @ldoktor , apologies for being late! Got loaded with work. Will update this PR by tonight for sure! Thank you so much for the patience. |
Sure, that is understandable. We are considering a release which is why I'm asking. No need to rush things, though... |
Thank you so much for understanding @ldoktor ! |
082f8ae
to
998b79f
Compare
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 <[email protected]>
998b79f
to
79d4aa4
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @Anushree-Mathur, this pull request definitely enhanced the session capabilities of aexpect, I left mostly a few typos to fix and a simplification suggestion.
@@ -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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this change intentional? I think this is incompatible with black linting and doesn't relate to the topic of this pull request.
@@ -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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whether to remove the ... followed by a full stop.
@@ -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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In serial sessions.. with a space after the comma.
: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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This empty line is not needed
@@ -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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar statement to the "black" statement above
@@ -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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same adaptations would be needed here.
# 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)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about a much simpler
out = astring.strip_console_codes(out) if strip_console_codes else out
?
# 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)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar simplification to above
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 [email protected]