diff --git a/changes.d/6511.fix.md b/changes.d/6511.fix.md new file mode 100644 index 0000000000..f142edeb7a --- /dev/null +++ b/changes.d/6511.fix.md @@ -0,0 +1 @@ +cat-log command list-dir mode: fail gracefully if directory not found. diff --git a/cylc/flow/scripts/cat_log.py b/cylc/flow/scripts/cat_log.py index 688f51ddd3..0c497d9db2 100755 --- a/cylc/flow/scripts/cat_log.py +++ b/cylc/flow/scripts/cat_log.py @@ -261,13 +261,17 @@ def view_log( print(os.path.dirname(logpath)) return 0 if mode == 'list-dir': - for entry in sorted(os.listdir(os.path.dirname(logpath))): + dirname = os.path.dirname(logpath) + if not os.path.exists(dirname): + sys.stderr.write(f"Directory not found: {dirname}\n") + return 1 + for entry in sorted(os.listdir(dirname)): print(entry) return 0 if not os.path.exists(logpath) and batchview_cmd is None: # Note: batchview_cmd may not need to have access to logpath, so don't # test for existence of path if it is set. - sys.stderr.write('file not found: %s\n' % logpath) + sys.stderr.write('File not found: %s\n' % logpath) return 1 if prepend_path: from cylc.flow.hostuserutil import get_host diff --git a/tests/integration/scripts/test_cat_log.py b/tests/integration/scripts/test_cat_log.py index d60a6096d7..520de54d21 100644 --- a/tests/integration/scripts/test_cat_log.py +++ b/tests/integration/scripts/test_cat_log.py @@ -92,6 +92,22 @@ def test_bad_workflow2(run_dir, brokendir, capsys): BAD_NAME ) msg = ( - f'file not found: {run_dir}' + f'File not found: {run_dir}' '/NONEXISTENTWORKFLOWNAME/log/j\n') assert capsys.readouterr().err == msg + + +def test_bad_task_dir(run_dir, brokendir, capsys): + """Check a non existent job log dir in a valid workflow results in error. + """ + parser = cat_log_gop() + with pytest.raises(SystemExit, match='1'): + cat_log( + parser, + Options(parser)(mode='list-dir'), + BAD_NAME + "//1/foo" + ) + msg = ( + f'Directory not found: {run_dir}' + '/NONEXISTENTWORKFLOWNAME/log/job/1/foo/NN\n') + assert capsys.readouterr().err == msg