Skip to content

Commit a728b61

Browse files
Merge pull request #6511 from hjoliver/fix-cat-log-traceback
cat-log list-dir mode: fail gracefully
2 parents 99acbe9 + 0d00d48 commit a728b61

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

changes.d/6511.fix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cat-log command list-dir mode: fail gracefully if directory not found.

cylc/flow/scripts/cat_log.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,17 @@ def view_log(
261261
print(os.path.dirname(logpath))
262262
return 0
263263
if mode == 'list-dir':
264-
for entry in sorted(os.listdir(os.path.dirname(logpath))):
264+
dirname = os.path.dirname(logpath)
265+
if not os.path.exists(dirname):
266+
sys.stderr.write(f"Directory not found: {dirname}\n")
267+
return 1
268+
for entry in sorted(os.listdir(dirname)):
265269
print(entry)
266270
return 0
267271
if not os.path.exists(logpath) and batchview_cmd is None:
268272
# Note: batchview_cmd may not need to have access to logpath, so don't
269273
# test for existence of path if it is set.
270-
sys.stderr.write('file not found: %s\n' % logpath)
274+
sys.stderr.write('File not found: %s\n' % logpath)
271275
return 1
272276
if prepend_path:
273277
from cylc.flow.hostuserutil import get_host

tests/integration/scripts/test_cat_log.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,22 @@ def test_bad_workflow2(run_dir, brokendir, capsys):
9292
BAD_NAME
9393
)
9494
msg = (
95-
f'file not found: {run_dir}'
95+
f'File not found: {run_dir}'
9696
'/NONEXISTENTWORKFLOWNAME/log/j\n')
9797
assert capsys.readouterr().err == msg
98+
99+
100+
def test_bad_task_dir(run_dir, brokendir, capsys):
101+
"""Check a non existent job log dir in a valid workflow results in error.
102+
"""
103+
parser = cat_log_gop()
104+
with pytest.raises(SystemExit, match='1'):
105+
cat_log(
106+
parser,
107+
Options(parser)(mode='list-dir'),
108+
BAD_NAME + "//1/foo"
109+
)
110+
msg = (
111+
f'Directory not found: {run_dir}'
112+
'/NONEXISTENTWORKFLOWNAME/log/job/1/foo/NN\n')
113+
assert capsys.readouterr().err == msg

0 commit comments

Comments
 (0)