Skip to content

Commit 0a8bbfa

Browse files
authored
Improve error message for cylc cat-log (#5830)
Improve error message for Cylc cat log * replaced functional with integration tests for cat log error handling.
1 parent da8ab21 commit 0a8bbfa

File tree

3 files changed

+118
-42
lines changed

3 files changed

+118
-42
lines changed

cylc/flow/scripts/cat_log.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,17 @@ def main(
401401
options: 'Values',
402402
*ids,
403403
color: bool = False
404+
):
405+
"""Wrapper around the main script for simpler testing.
406+
"""
407+
_main(parser, options, *ids, color=color)
408+
409+
410+
def _main(
411+
parser: COP,
412+
options: 'Values',
413+
*ids,
414+
color: bool = False
404415
) -> None:
405416
"""Implement cylc cat-log CLI.
406417
@@ -481,10 +492,16 @@ def main(
481492
),
482493
reverse=True
483494
)
484-
try:
485-
log_file_path = Path(logs[rotation_number])
486-
except IndexError:
487-
raise InputError("max rotation %d" % (len(logs) - 1))
495+
if logs:
496+
try:
497+
log_file_path = Path(logs[rotation_number])
498+
except IndexError:
499+
raise InputError(
500+
f"--rotation {rotation_number} invalid "
501+
f"(max value is {len(logs) - 1})"
502+
)
503+
else:
504+
raise InputError('Log file not found.')
488505
else:
489506
log_file_path = Path(log_dir, file_name)
490507

tests/functional/cylc-cat-log/03-bad-workflow.t

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
2+
# Copyright (C) NIWA & British Crown (Met Office) & Contributors.
3+
#
4+
# This program is free software: you can redistribute it and/or modify
5+
# it under the terms of the GNU General Public License as published by
6+
# the Free Software Foundation, either version 3 of the License, or
7+
# (at your option) any later version.
8+
#
9+
# This program is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
# GNU General Public License for more details.
13+
#
14+
# You should have received a copy of the GNU General Public License
15+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
"""Integration test for the cat log script.
17+
"""
18+
19+
import pytest
20+
import re
21+
import shutil
22+
from types import SimpleNamespace
23+
24+
from cylc.flow.exceptions import InputError
25+
from cylc.flow.option_parsers import Options
26+
from cylc.flow.scripts.cat_log import (
27+
_main as cat_log,
28+
get_option_parser as cat_log_gop
29+
)
30+
31+
32+
BAD_NAME = "NONEXISTENTWORKFLOWNAME"
33+
34+
35+
@pytest.fixture
36+
def brokendir(run_dir):
37+
brokendir = (run_dir / BAD_NAME)
38+
brokendir.mkdir(exist_ok=True)
39+
yield brokendir
40+
shutil.rmtree(brokendir)
41+
42+
43+
def test_fail_no_file(flow):
44+
"""It produces a helpful error if there is no workflow log file.
45+
"""
46+
parser = cat_log_gop()
47+
id_ = flow({})
48+
with pytest.raises(InputError, match='Log file not found.'):
49+
cat_log(parser, Options(parser)(), id_)
50+
51+
52+
def test_fail_rotation_out_of_range(flow):
53+
"""It produces a helpful error if rotation number > number of log files.
54+
"""
55+
parser = cat_log_gop()
56+
id_ = flow({})
57+
path = flow.args[1]
58+
name = id_.split('/')[-1]
59+
logpath = (path / name / 'log/scheduler')
60+
logpath.mkdir(parents=True)
61+
(logpath / '01-start-01.log').touch()
62+
63+
with pytest.raises(SystemExit):
64+
cat_log(parser, Options(parser)(rotation_num=0), id_)
65+
66+
msg = r'--rotation 1 invalid \(max value is 0\)'
67+
68+
with pytest.raises(InputError, match=msg):
69+
cat_log(parser, Options(parser)(rotation_num=1), id_)
70+
71+
72+
def test_bad_workflow(run_dir):
73+
"""Test "cylc cat-log" with bad workflow name."""
74+
parser = cat_log_gop()
75+
msg = re.compile(
76+
fr'^Workflow ID not found: {BAD_NAME}'
77+
fr'\n\(Directory not found: {run_dir}/{BAD_NAME}\)$',
78+
re.MULTILINE
79+
)
80+
with pytest.raises(InputError, match=msg):
81+
cat_log(parser, Options(parser)(filename='l'), BAD_NAME)
82+
83+
84+
def test_bad_workflow2(run_dir, brokendir, capsys):
85+
"""Check a non existent file in a valid workflow results in error.
86+
"""
87+
parser = cat_log_gop()
88+
with pytest.raises(SystemExit, match='1'):
89+
cat_log(
90+
parser,
91+
Options(parser)(filename='j'),
92+
BAD_NAME
93+
)
94+
msg = (
95+
f'file not found: {run_dir}'
96+
'/NONEXISTENTWORKFLOWNAME/log/j\n')
97+
assert capsys.readouterr().err == msg

0 commit comments

Comments
 (0)