Skip to content

Commit

Permalink
to open expected outpout file with an encoding parameter (#717)
Browse files Browse the repository at this point in the history
* add an option to read the regex file by escape mode

Signed-off-by: Chen Lihui <[email protected]>
Co-authored-by: Chris Lalancette <[email protected]>
  • Loading branch information
Chen Lihui and clalancette authored Sep 13, 2023
1 parent 3e73d2f commit 6ee6660
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
8 changes: 5 additions & 3 deletions launch_testing/launch_testing/tools/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import os
import re
from typing import Optional

from osrf_pycommon.terminal_color import remove_ansi_escape_sequences

Expand Down Expand Up @@ -64,21 +65,22 @@ def _filter(output):
return _filter


def expected_output_from_file(path):
def expected_output_from_file(path: str, encoding: Optional[str] = None):
"""
Get expected output lines from a file.
:param path: path w/o extension of either a .txt file containing the lines
to be matched or a .regex file containing patterns to be searched for.
:param encoding: the character encoding to be used when opening the file.
"""
literal_file = path + '.txt'
if os.path.isfile(literal_file):
with open(literal_file, 'r') as f:
with open(literal_file, 'r', encoding=encoding) as f:
return f.read().splitlines()

regex_file = path + '.regex'
if os.path.isfile(regex_file):
with open(regex_file, 'r') as f:
with open(regex_file, 'r', encoding=encoding) as f:
return [re.compile(regex) for regex in f.read().splitlines()]

raise RuntimeError('could not find output check file: {}'.format(path))
Expand Down
34 changes: 34 additions & 0 deletions launch_testing/test/launch_testing/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import re

import launch.actions
Expand All @@ -22,6 +23,7 @@

from launch_testing.tools import basic_output_filter
from launch_testing.tools import expect_output
from launch_testing.tools import expected_output_from_file
from launch_testing.tools import ProcessProxy


Expand Down Expand Up @@ -95,6 +97,38 @@ def test_expect_output():
)


def test_expected_output_from_file():
# to test txt file
output_text = 'test\btest'
name = 'test_tools_expected_output_from_txt_file'
# use the default encoding parameter
expected_output = expected_output_from_file(
path=os.path.join(os.path.dirname(__file__), name)
)
assert not expect_output(expected_lines=expected_output, text=output_text)
# use the encoding parameter with 'unicode_escape'
expected_output = expected_output_from_file(
path=os.path.join(os.path.dirname(__file__), name),
encoding='unicode_escape'
)
assert expect_output(expected_lines=expected_output, text=output_text)

# to test regex file
output_text = 'test\btestaaaaa'
name = 'test_tools_expected_output_from_regex_file'
# use the default encoding parameter
expected_output = expected_output_from_file(
path=os.path.join(os.path.dirname(__file__), name)
)
assert not expect_output(expected_lines=expected_output, text=output_text)
# use the encoding parameter with 'unicode_escape'
expected_output = expected_output_from_file(
path=os.path.join(os.path.dirname(__file__), name),
encoding='unicode_escape'
)
assert expect_output(expected_lines=expected_output, text=output_text)


def test_process_proxy():
proc_output = launch_testing.io_handler.ActiveIoHandler()
proc_info = launch_testing.proc_info_handler.ActiveProcInfoHandler()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test\btesta{5}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test\btest

0 comments on commit 6ee6660

Please sign in to comment.