Skip to content

Commit

Permalink
Merge pull request cylc#5863 from cylc/8.2.x-sync
Browse files Browse the repository at this point in the history
🤖 Merge 8.2.x-sync into master
  • Loading branch information
wxtim authored Dec 6, 2023
2 parents a965b91 + 0a8bbfa commit 265c21d
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 42 deletions.
25 changes: 21 additions & 4 deletions cylc/flow/scripts/cat_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,17 @@ def main(
options: 'Values',
*ids,
color: bool = False
):
"""Wrapper around the main script for simpler testing.
"""
_main(parser, options, *ids, color=color)


def _main(
parser: COP,
options: 'Values',
*ids,
color: bool = False
) -> None:
"""Implement cylc cat-log CLI.
Expand Down Expand Up @@ -481,10 +492,16 @@ def main(
),
reverse=True
)
try:
log_file_path = Path(logs[rotation_number])
except IndexError:
raise InputError("max rotation %d" % (len(logs) - 1))
if logs:
try:
log_file_path = Path(logs[rotation_number])
except IndexError:
raise InputError(
f"--rotation {rotation_number} invalid "
f"(max value is {len(logs) - 1})"
)
else:
raise InputError('Log file not found.')
else:
log_file_path = Path(log_dir, file_name)

Expand Down
38 changes: 0 additions & 38 deletions tests/functional/cylc-cat-log/03-bad-workflow.t

This file was deleted.

97 changes: 97 additions & 0 deletions tests/integration/scripts/test_cat_log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
# Copyright (C) NIWA & British Crown (Met Office) & Contributors.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Integration test for the cat log script.
"""

import pytest
import re
import shutil
from types import SimpleNamespace

from cylc.flow.exceptions import InputError
from cylc.flow.option_parsers import Options
from cylc.flow.scripts.cat_log import (
_main as cat_log,
get_option_parser as cat_log_gop
)


BAD_NAME = "NONEXISTENTWORKFLOWNAME"


@pytest.fixture
def brokendir(run_dir):
brokendir = (run_dir / BAD_NAME)
brokendir.mkdir(exist_ok=True)
yield brokendir
shutil.rmtree(brokendir)


def test_fail_no_file(flow):
"""It produces a helpful error if there is no workflow log file.
"""
parser = cat_log_gop()
id_ = flow({})
with pytest.raises(InputError, match='Log file not found.'):
cat_log(parser, Options(parser)(), id_)


def test_fail_rotation_out_of_range(flow):
"""It produces a helpful error if rotation number > number of log files.
"""
parser = cat_log_gop()
id_ = flow({})
path = flow.args[1]
name = id_.split('/')[-1]
logpath = (path / name / 'log/scheduler')
logpath.mkdir(parents=True)
(logpath / '01-start-01.log').touch()

with pytest.raises(SystemExit):
cat_log(parser, Options(parser)(rotation_num=0), id_)

msg = r'--rotation 1 invalid \(max value is 0\)'

with pytest.raises(InputError, match=msg):
cat_log(parser, Options(parser)(rotation_num=1), id_)


def test_bad_workflow(run_dir):
"""Test "cylc cat-log" with bad workflow name."""
parser = cat_log_gop()
msg = re.compile(
fr'^Workflow ID not found: {BAD_NAME}'
fr'\n\(Directory not found: {run_dir}/{BAD_NAME}\)$',
re.MULTILINE
)
with pytest.raises(InputError, match=msg):
cat_log(parser, Options(parser)(filename='l'), BAD_NAME)


def test_bad_workflow2(run_dir, brokendir, capsys):
"""Check a non existent file in a valid workflow results in error.
"""
parser = cat_log_gop()
with pytest.raises(SystemExit, match='1'):
cat_log(
parser,
Options(parser)(filename='j'),
BAD_NAME
)
msg = (
f'file not found: {run_dir}'
'/NONEXISTENTWORKFLOWNAME/log/j\n')
assert capsys.readouterr().err == msg

0 comments on commit 265c21d

Please sign in to comment.