Skip to content
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

to open expected outpout file with an encoding parameter #717

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions launch_testing/launch_testing/tools/output.py
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
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
@@ -0,0 +1 @@
test\btesta{5}
@@ -0,0 +1 @@
test\btest