Skip to content

Commit fed5e2c

Browse files
updated test for logging in case of diff log file names
Signed-off-by: Tanishq Chaudhary <[email protected]>
1 parent ed087d2 commit fed5e2c

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

launch/test/launch/test_logging.py

+25-10
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def test_output_loggers_bad_configuration(log_dir):
6767
launch.logging.get_output_loggers('some-proc', {'stdout': {'garbage'}})
6868

6969

70-
@pytest.mark.parametrize('config,checks', [
70+
configs = [
7171
('screen', {'stdout': {'screen'}, 'stderr': {'screen'}}),
7272
('log', {'stdout': {'log'}, 'stderr': {'log', 'screen'}}),
7373
('both', {'both': {'log', 'screen'}}),
@@ -88,16 +88,30 @@ def test_output_loggers_bad_configuration(log_dir):
8888
'stderr': {'own_log'}
8989
},
9090
)
91-
])
92-
def test_output_loggers_configuration(capsys, log_dir, config, checks, mock_clean_env):
91+
]
92+
log_file_names = ['custom-log-name.log', None]
93+
params = [
94+
(config, checks, log_file_name)
95+
for (config, checks) in configs
96+
for log_file_name in log_file_names
97+
]
98+
99+
100+
@pytest.mark.parametrize('config,checks,main_log_file_name', params)
101+
def test_output_loggers_configuration(
102+
capsys, log_dir, config, checks, main_log_file_name, mock_clean_env
103+
):
93104
checks = {'stdout': set(), 'stderr': set(), 'both': set(), **checks}
94105
launch.logging.reset()
95106
launch.logging.launch_config.log_dir = log_dir
96-
logger = launch.logging.get_logger('some-proc')
107+
if main_log_file_name is None:
108+
main_log_file_name = launch.logging.launch_config.log_file_name
109+
logger_name = main_log_file_name.removesuffix('.log')
110+
logger = launch.logging.get_logger(logger_name, main_log_file_name)
97111
logger.addHandler(launch.logging.launch_config.get_screen_handler())
98-
logger.addHandler(launch.logging.launch_config.get_log_file_handler())
112+
logger.addHandler(launch.logging.launch_config.get_log_file_handler(main_log_file_name))
99113
logger.setLevel(logging.ERROR)
100-
stdout_logger, stderr_logger = launch.logging.get_output_loggers('some-proc', config)
114+
stdout_logger, stderr_logger = launch.logging.get_output_loggers('some-proc', config, main_log_file_name)
101115

102116
logger.debug('oops')
103117
logger.error('baz')
@@ -106,21 +120,22 @@ def test_output_loggers_configuration(capsys, log_dir, config, checks, mock_clea
106120

107121
capture = capsys.readouterr()
108122
lines = list(reversed(capture.out.splitlines()))
109-
assert '[ERROR] [some-proc]: baz' == lines.pop()
123+
assert f'[ERROR] [{logger_name}]: baz' == lines.pop()
110124
if 'screen' in (checks['stdout'] | checks['both']):
111125
assert 'foo' == lines.pop()
112126
if 'screen' in (checks['stderr'] | checks['both']):
113127
assert 'bar' == lines.pop()
114128
assert 0 == len(lines)
115129
assert 0 == len(capture.err)
116130

117-
launch.logging.launch_config.get_log_file_handler().flush()
118-
main_log_path = launch.logging.launch_config.get_log_file_path()
131+
launch.logging.launch_config.get_log_file_handler(main_log_file_name).flush()
132+
main_log_path = launch.logging.launch_config.get_log_file_path(main_log_file_name)
119133
assert os.path.exists(main_log_path)
120134
assert 0 != os.stat(main_log_path).st_size
121135
with open(main_log_path, 'r') as f:
122136
lines = list(reversed(f.readlines()))
123-
assert re.match(r'[0-9]+\.[0-9]+ \[ERROR\] \[some-proc\]: baz', lines.pop()) is not None
137+
assert re.match(rf'[0-9]+\.[0-9]+ \[ERROR\] \[{logger_name}\]: baz',
138+
lines.pop()) is not None
124139
if 'log' in (checks['stdout'] | checks['both']):
125140
assert re.match(r'[0-9]+\.[0-9]+ foo', lines.pop()) is not None
126141
if 'log' in (checks['stderr'] | checks['both']):

0 commit comments

Comments
 (0)