Skip to content

Commit

Permalink
Improve --flow error message
Browse files Browse the repository at this point in the history
  • Loading branch information
MetRonnie committed Sep 25, 2024
1 parent 75bd752 commit 48bef4e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 33 deletions.
19 changes: 11 additions & 8 deletions cylc/flow/command_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@


ERR_OPT_FLOW_VAL = "Flow values must be an integer, or 'all', 'new', or 'none'"
ERR_OPT_FLOW_INT = "Multiple flow options must all be integer valued"
ERR_OPT_FLOW_COMBINE = "Cannot combine --flow={0} with other flow values"
ERR_OPT_FLOW_WAIT = (
f"--wait is not compatible with --flow={FLOW_NEW} or --flow={FLOW_NONE}"
)
Expand All @@ -51,32 +51,35 @@ def flow_opts(flows: List[str], flow_wait: bool) -> None:
Bad:
>>> flow_opts(["none", "1"], False)
Traceback (most recent call last):
cylc.flow.exceptions.InputError: ... must all be integer valued
cylc.flow.exceptions.InputError: Cannot combine --flow=none with other
flow values
>>> flow_opts(["cheese", "2"], True)
Traceback (most recent call last):
cylc.flow.exceptions.InputError: ... or 'all', 'new', or 'none'
>>> flow_opts(["new"], True)
Traceback (most recent call last):
cylc.flow.exceptions.InputError: ...
cylc.flow.exceptions.InputError: --wait is not compatible with
--flow=new or --flow=none
"""
if not flows:
return

flows = [val.strip() for val in flows]

for val in flows:
val = val.strip()
if val in [FLOW_NONE, FLOW_NEW, FLOW_ALL]:
if val in {FLOW_NONE, FLOW_NEW, FLOW_ALL}:
if len(flows) != 1:
raise InputError(ERR_OPT_FLOW_INT)
raise InputError(ERR_OPT_FLOW_COMBINE.format(val))
else:
try:
int(val)
except ValueError:
raise InputError(ERR_OPT_FLOW_VAL.format(val))
raise InputError(ERR_OPT_FLOW_VAL)

if flow_wait and flows[0] in [FLOW_NEW, FLOW_NONE]:
if flow_wait and flows[0] in {FLOW_NEW, FLOW_NONE}:
raise InputError(ERR_OPT_FLOW_WAIT)


Expand Down
26 changes: 1 addition & 25 deletions tests/integration/test_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,9 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import logging

from cylc.flow.flow_mgr import FLOW_ALL, FLOW_NEW, FLOW_NONE
from cylc.flow.command_validation import flow_opts
from cylc.flow.exceptions import InputError

import pytest
import time


@pytest.mark.parametrize(
'flow_strs',
(
[FLOW_ALL, '1'],
['1', FLOW_ALL],
[FLOW_NEW, '1'],
[FLOW_NONE, '1'],
['a'],
['1', 'a'],
)
)
async def test_trigger_invalid(mod_one, start, log_filter, flow_strs):
"""Ensure invalid flow values are rejected during command validation."""
async with start(mod_one) as log:
log.clear()
with pytest.raises(InputError):
flow_opts(flow_strs, False)
from cylc.flow.flow_mgr import FLOW_ALL


async def test_trigger_no_flows(one, start, log_filter):
Expand Down
41 changes: 41 additions & 0 deletions tests/unit/test_command_validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# 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/>.

import pytest

from cylc.flow.command_validation import (
ERR_OPT_FLOW_COMBINE,
ERR_OPT_FLOW_VAL,
flow_opts,
)
from cylc.flow.exceptions import InputError
from cylc.flow.flow_mgr import FLOW_ALL, FLOW_NEW, FLOW_NONE


@pytest.mark.parametrize('flow_strs, expected_msg', [
([FLOW_ALL, '1'], ERR_OPT_FLOW_COMBINE.format(FLOW_ALL)),
(['1', FLOW_ALL], ERR_OPT_FLOW_COMBINE.format(FLOW_ALL)),
([FLOW_NEW, '1'], ERR_OPT_FLOW_COMBINE.format(FLOW_NEW)),
([FLOW_NONE, '1'], ERR_OPT_FLOW_COMBINE.format(FLOW_NONE)),
([FLOW_NONE, FLOW_ALL], ERR_OPT_FLOW_COMBINE.format(FLOW_NONE)),
(['a'], ERR_OPT_FLOW_VAL),
(['1', 'a'], ERR_OPT_FLOW_VAL),
])
async def test_trigger_invalid(flow_strs, expected_msg):
"""Ensure invalid flow values are rejected during command validation."""
with pytest.raises(InputError) as exc_info:
flow_opts(flow_strs, False)
assert str(exc_info.value) == expected_msg

0 comments on commit 48bef4e

Please sign in to comment.