| 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 |
|
| 17 |
import re |
| 18 |
|
| 19 |
import pytest |
| 20 |
|
| 21 |
from cylc.flow.command_validation import ( |
| 22 |
ERR_OPT_FLOW_COMBINE, |
| 23 |
ERR_OPT_FLOW_VAL_INT_NEW_NONE, |
| 24 |
flow_opts, |
| 25 |
is_tasks, |
| 26 |
) |
| 27 |
from cylc.flow.cycling.loader import ISO8601_CYCLING_TYPE |
| 28 |
from cylc.flow.exceptions import InputError |
| 29 |
from cylc.flow.flow_mgr import FLOW_NEW, FLOW_NONE |
| 30 |
from cylc.flow.id import TaskTokens |
| 31 |
|
| 32 |
|
| 33 |
@pytest.mark.parametrize('flow_strs, expected_msg', [ |
| 34 |
([FLOW_NEW, '1'], ERR_OPT_FLOW_COMBINE.format(FLOW_NEW)), |
| 35 |
([FLOW_NONE, '1'], ERR_OPT_FLOW_COMBINE.format(FLOW_NONE)), |
| 36 |
([FLOW_NONE, FLOW_NEW], ERR_OPT_FLOW_COMBINE.format(FLOW_NONE)), |
| 37 |
(['a'], ERR_OPT_FLOW_VAL_INT_NEW_NONE), |
| 38 |
(['1', 'a'], ERR_OPT_FLOW_VAL_INT_NEW_NONE), |
| 39 |
]) |
| 40 |
async def test_trigger_invalid(flow_strs, expected_msg): |
| 41 |
"""Ensure invalid flow values are rejected during command validation.""" |
| 42 |
with pytest.raises(InputError) as exc_info: |
| 43 |
flow_opts(flow_strs, False) |
| 44 |
assert str(exc_info.value) == expected_msg |
| 45 |
|
| 46 |
|
| 47 |
async def test_is_tasks(set_cycling_type): |
| 48 |
set_cycling_type(ISO8601_CYCLING_TYPE) |
| 49 |
|
| 50 |
# tokens should be parsed |
| 51 |
assert is_tasks({'20000101T0000Z/a', '20010101T0000Z/b'}) == { |
| 52 |
TaskTokens('20000101T0000Z', 'a'), |
| 53 |
TaskTokens('20010101T0000Z', 'b'), |
| 54 |
} |
| 55 |
|
| 56 |
# cycle points should be standardised unless they are globs |
| 57 |
assert is_tasks({'2000/a', '20010101/b', '*/c', '[23]000/d'}) == { |
| 58 |
TaskTokens('20000101T0000Z', 'a'), |
| 59 |
TaskTokens('20010101T0000Z', 'b'), |
| 60 |
TaskTokens('*', 'c'), |
| 61 |
TaskTokens('[23]000', 'd'), |
| 62 |
} |
| 63 |
|
| 64 |
# the namespace should default to "root" unless provided |
| 65 |
assert is_tasks({'*', '2000'}) == { |
| 66 |
TaskTokens('*', 'root'), |
| 67 |
TaskTokens('20000101T0000Z', 'root'), |
| 68 |
} |
| 69 |
|
| 70 |
# invalid IDs result in errors |
| 71 |
with pytest.raises(InputError, match='Invalid ID: //, ///, ////'): |
| 72 |
is_tasks({'//', '///', '////', '2000/a'}) # last ID is valid |
| 73 |
|
| 74 |
# invalid cycle points result in errors |
| 75 |
with pytest.raises( |
| 76 |
InputError, |
| 77 |
match=re.escape('Invalid cycle point: (42)/answer, 2000Z, abc'), |
| 78 |
): |
| 79 |
is_tasks({'(42)/answer', '2000Z', 'abc', '2000/a'}) # last ID is valid |
| 80 |
|
| 81 |
# job IDs reuslt in errors |
| 82 |
with pytest.raises( |
| 83 |
InputError, |
| 84 |
match=re.escape('This command does not take job IDs: */b/02, 1/a/01'), |
| 85 |
): |
| 86 |
is_tasks({'1/a/01', '*/b/02'}) |
| 87 |
|
| 88 |
# combinations of errors are reported |
| 89 |
with pytest.raises( |
| 90 |
InputError, |
| 91 |
match=( |
| 92 |
re.escape('Invalid ID: ///, ////') |
| 93 |
+ re.escape('\nInvalid cycle point: 200Z, abc') |
| 94 |
+ re.escape('\nThis command does not take job IDs: */b/02, 1/a/01') |
| 95 |
), |
| 96 |
): |
| 97 |
is_tasks({'///', '////', '200Z', 'abc', '1/a/01', '*/b/02'}) |