diff --git a/launch_testing/launch_testing/tools/output.py b/launch_testing/launch_testing/tools/output.py index ff8a23896..c0a674f2d 100644 --- a/launch_testing/launch_testing/tools/output.py +++ b/launch_testing/launch_testing/tools/output.py @@ -14,6 +14,7 @@ import os import re +from typing import Optional from osrf_pycommon.terminal_color import remove_ansi_escape_sequences @@ -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)) diff --git a/launch_testing/test/launch_testing/test_tools.py b/launch_testing/test/launch_testing/test_tools.py index 3084120d4..1aaaf542f 100644 --- a/launch_testing/test/launch_testing/test_tools.py +++ b/launch_testing/test/launch_testing/test_tools.py @@ -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 @@ -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 @@ -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() diff --git a/launch_testing/test/launch_testing/test_tools_expected_output_from_regex_file.regex b/launch_testing/test/launch_testing/test_tools_expected_output_from_regex_file.regex new file mode 100644 index 000000000..eaf72700c --- /dev/null +++ b/launch_testing/test/launch_testing/test_tools_expected_output_from_regex_file.regex @@ -0,0 +1 @@ +test\btesta{5} diff --git a/launch_testing/test/launch_testing/test_tools_expected_output_from_txt_file.txt b/launch_testing/test/launch_testing/test_tools_expected_output_from_txt_file.txt new file mode 100644 index 000000000..60594950f --- /dev/null +++ b/launch_testing/test/launch_testing/test_tools_expected_output_from_txt_file.txt @@ -0,0 +1 @@ +test\btest