Skip to content

Commit 7f3f0cc

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

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

launch/test/launch/test_logging.py

+28-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,33 @@ 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+
else:
110+
launch.logging.launch_config.log_file_name = main_log_file_name
111+
logger_name = main_log_file_name.removesuffix('.log')
112+
logger = launch.logging.get_logger(logger_name)
97113
logger.addHandler(launch.logging.launch_config.get_screen_handler())
98-
logger.addHandler(launch.logging.launch_config.get_log_file_handler())
114+
logger.addHandler(launch.logging.launch_config.get_log_file_handler(main_log_file_name))
99115
logger.setLevel(logging.ERROR)
100-
stdout_logger, stderr_logger = launch.logging.get_output_loggers('some-proc', config)
116+
stdout_logger, stderr_logger = launch.logging.get_output_loggers(
117+
'some-proc', config, main_log_file_name)
101118

102119
logger.debug('oops')
103120
logger.error('baz')
@@ -106,21 +123,22 @@ def test_output_loggers_configuration(capsys, log_dir, config, checks, mock_clea
106123

107124
capture = capsys.readouterr()
108125
lines = list(reversed(capture.out.splitlines()))
109-
assert '[ERROR] [some-proc]: baz' == lines.pop()
126+
assert f'[ERROR] [{logger_name}]: baz' == lines.pop()
110127
if 'screen' in (checks['stdout'] | checks['both']):
111128
assert 'foo' == lines.pop()
112129
if 'screen' in (checks['stderr'] | checks['both']):
113130
assert 'bar' == lines.pop()
114131
assert 0 == len(lines)
115132
assert 0 == len(capture.err)
116133

117-
launch.logging.launch_config.get_log_file_handler().flush()
118-
main_log_path = launch.logging.launch_config.get_log_file_path()
134+
launch.logging.launch_config.get_log_file_handler(main_log_file_name).flush()
135+
main_log_path = launch.logging.launch_config.get_log_file_path(main_log_file_name)
119136
assert os.path.exists(main_log_path)
120137
assert 0 != os.stat(main_log_path).st_size
121138
with open(main_log_path, 'r') as f:
122139
lines = list(reversed(f.readlines()))
123-
assert re.match(r'[0-9]+\.[0-9]+ \[ERROR\] \[some-proc\]: baz', lines.pop()) is not None
140+
assert re.match(rf'[0-9]+\.[0-9]+ \[ERROR\] \[{logger_name}\]: baz',
141+
lines.pop()) is not None
124142
if 'log' in (checks['stdout'] | checks['both']):
125143
assert re.match(r'[0-9]+\.[0-9]+ foo', lines.pop()) is not None
126144
if 'log' in (checks['stderr'] | checks['both']):

0 commit comments

Comments
 (0)