Skip to content

Commit

Permalink
Merge pull request #229 from nokia/match_end_start_cmd
Browse files Browse the repository at this point in the history
Command slice to match command start
  • Loading branch information
Ernold11 authored Oct 30, 2019
2 parents a7a0eb6 + b156bee commit 0d48706
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
41 changes: 33 additions & 8 deletions moler/cmd/commandtextualgeneric.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ def __init__(self, connection, prompt=None, newline_chars=None, runner=None):
:param newline_chars: new line chars on device (a list).
:param runner: runner to run command.
"""
self._command_string_right_index = 20 # Right index of substring of command_string passed as _cmd_escaped. Set
# 0 to disable functionality of substring.
self._command_string_right_index = 20 # Right (from 0 to this) index of substring of command_string passed
# as _cmd_escaped. Set 0 to disable functionality of substring.
self._command_string_left_index = 20 # Left (from this to the end) index of substring of command_string passed
# as _cmd_escaped. Set 0 to disable functionality of substring.
self.__command_string = None # String representing command on device
self._cmd_escaped = None # Escaped regular expression string with command
super(CommandTextualGeneric, self).__init__(connection=connection, runner=runner)
Expand Down Expand Up @@ -96,10 +98,33 @@ def _build_command_string_escaped(self):
"""
self._cmd_escaped = None
if self.__command_string is not None:
sub_command_string = self.__command_string
if self._command_string_right_index != 0:
sub_command_string = self.__command_string[:self._command_string_right_index]
self._cmd_escaped = re.compile(re.escape(sub_command_string))
if self._command_string_right_index != 0 or self._command_string_left_index != 0:
sub_command_string = self._build_command_string_slice()
else:
sub_command_string = re.escape(self.__command_string)
self._cmd_escaped = re.compile(sub_command_string)

def _build_command_string_slice(self):
"""
Builds slice of command string.
:return: String with regex with command slice.
"""
sub_command_start_string = None
sub_command_finish_string = None
if self._command_string_right_index != 0:
sub_command_start_string = re.escape(self.__command_string[:self._command_string_right_index])
re_sub_command_string = sub_command_start_string
if self._command_string_left_index != 0:
cmd_len = len(self.__command_string)
if self._command_string_left_index >= cmd_len:
sub_command_finish_string = re.escape(self.__command_string)
else:
sub_command_finish_string = re.escape(self.__command_string[:-self._command_string_left_index])
re_sub_command_string = sub_command_finish_string
if sub_command_finish_string and sub_command_start_string:
re_sub_command_string = "{}|{}".format(sub_command_start_string, sub_command_finish_string)
return re_sub_command_string

@property
def _is_done(self):
Expand Down Expand Up @@ -339,12 +364,12 @@ def on_timeout(self):
"break_on_timeout='{}', _last_not_full_line='{}', _re_prompt='{}', do_not_process_after_done='{}', "
"newline_after_command_string='{}', wait_for_prompt_on_exception='{}', _stored_exception='{}', "
"current_ret='{}', _newline_chars='{}', _concatenate_before_command_starts='{}', "
"_command_string_right_index='{}'.").format(
"_command_string_right_index='{}', _command_string_left_index='{}'.").format(
self.__command_string, self._cmd_escaped, self._cmd_output_started, self.ret_required,
self.break_on_timeout, self._last_not_full_line, self._re_prompt, self.do_not_process_after_done,
self.newline_after_command_string, self.wait_for_prompt_on_exception, self._stored_exception,
self.current_ret, self._newline_chars, self._concatenate_before_command_starts,
self._command_string_right_index)
self._command_string_right_index, self._command_string_left_index)
self._log(logging.DEBUG, msg, levels_to_go_up=2)

def has_any_result(self):
Expand Down
13 changes: 13 additions & 0 deletions test/cmd/unix/test_cmd_sudo.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ def test_calling_by_command_object(buffer_connection, command_output_and_expecte
assert result == expected_result


def test_calling_by_command_object_without_slicing(buffer_connection, command_output_and_expected_result):
command_output, expected_result = command_output_and_expected_result
buffer_connection.remote_inject_response([command_output])

cmd_pwd = Pwd(connection=buffer_connection.moler_connection)
cmd_sudo = Sudo(connection=buffer_connection.moler_connection, password="pass", cmd_object=cmd_pwd)
cmd_sudo._command_string_left_index = 0
cmd_sudo._command_string_right_index = 0
assert "sudo pwd" == cmd_sudo.command_string
result = cmd_sudo()
assert result == expected_result


def test_failing_calling_twice_the_same_command_object(buffer_connection, command_output_and_expected_result):
command_output, expected_result = command_output_and_expected_result
buffer_connection.remote_inject_response([command_output])
Expand Down

0 comments on commit 0d48706

Please sign in to comment.