Skip to content

Commit 6ee6660

Browse files
Chen Lihuiclalancette
andauthored
to open expected outpout file with an encoding parameter (#717)
* 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]>
1 parent 3e73d2f commit 6ee6660

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

launch_testing/launch_testing/tools/output.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import os
1616
import re
17+
from typing import Optional
1718

1819
from osrf_pycommon.terminal_color import remove_ansi_escape_sequences
1920

@@ -64,21 +65,22 @@ def _filter(output):
6465
return _filter
6566

6667

67-
def expected_output_from_file(path):
68+
def expected_output_from_file(path: str, encoding: Optional[str] = None):
6869
"""
6970
Get expected output lines from a file.
7071
7172
:param path: path w/o extension of either a .txt file containing the lines
7273
to be matched or a .regex file containing patterns to be searched for.
74+
:param encoding: the character encoding to be used when opening the file.
7375
"""
7476
literal_file = path + '.txt'
7577
if os.path.isfile(literal_file):
76-
with open(literal_file, 'r') as f:
78+
with open(literal_file, 'r', encoding=encoding) as f:
7779
return f.read().splitlines()
7880

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

8486
raise RuntimeError('could not find output check file: {}'.format(path))

launch_testing/test/launch_testing/test_tools.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import os
1516
import re
1617

1718
import launch.actions
@@ -22,6 +23,7 @@
2223

2324
from launch_testing.tools import basic_output_filter
2425
from launch_testing.tools import expect_output
26+
from launch_testing.tools import expected_output_from_file
2527
from launch_testing.tools import ProcessProxy
2628

2729

@@ -95,6 +97,38 @@ def test_expect_output():
9597
)
9698

9799

100+
def test_expected_output_from_file():
101+
# to test txt file
102+
output_text = 'test\btest'
103+
name = 'test_tools_expected_output_from_txt_file'
104+
# use the default encoding parameter
105+
expected_output = expected_output_from_file(
106+
path=os.path.join(os.path.dirname(__file__), name)
107+
)
108+
assert not expect_output(expected_lines=expected_output, text=output_text)
109+
# use the encoding parameter with 'unicode_escape'
110+
expected_output = expected_output_from_file(
111+
path=os.path.join(os.path.dirname(__file__), name),
112+
encoding='unicode_escape'
113+
)
114+
assert expect_output(expected_lines=expected_output, text=output_text)
115+
116+
# to test regex file
117+
output_text = 'test\btestaaaaa'
118+
name = 'test_tools_expected_output_from_regex_file'
119+
# use the default encoding parameter
120+
expected_output = expected_output_from_file(
121+
path=os.path.join(os.path.dirname(__file__), name)
122+
)
123+
assert not expect_output(expected_lines=expected_output, text=output_text)
124+
# use the encoding parameter with 'unicode_escape'
125+
expected_output = expected_output_from_file(
126+
path=os.path.join(os.path.dirname(__file__), name),
127+
encoding='unicode_escape'
128+
)
129+
assert expect_output(expected_lines=expected_output, text=output_text)
130+
131+
98132
def test_process_proxy():
99133
proc_output = launch_testing.io_handler.ActiveIoHandler()
100134
proc_info = launch_testing.proc_info_handler.ActiveProcInfoHandler()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test\btesta{5}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test\btest

0 commit comments

Comments
 (0)