| 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 |
from textwrap import dedent |
| 19 |
|
| 20 |
from cylc.flow.exceptions import InputError |
| 21 |
from cylc.flow.parsec.exceptions import Jinja2Error |
| 22 |
|
| 23 |
import pytest |
| 24 |
|
| 25 |
|
| 26 |
@pytest.fixture |
| 27 |
def flow_cylc(tmp_path): |
| 28 |
"""Write a flow.cylc file containing the provided text.""" |
| 29 |
def _inner(text): |
| 30 |
(tmp_path / 'flow.cylc').write_text(dedent(text).strip()) |
| 31 |
return tmp_path |
| 32 |
|
| 33 |
return _inner |
| 34 |
|
| 35 |
|
| 36 |
@pytest.mark.parametrize( |
| 37 |
'line', |
| 38 |
[ |
| 39 |
pytest.param("raise('some error message')", id='raise'), |
| 40 |
pytest.param("assert(False, 'some error message')", id='assert'), |
| 41 |
], |
| 42 |
) |
| 43 |
def test_raise_helper(flow_cylc, validate, line, monkeypatch): |
| 44 |
"""It should raise an error from within Jinja2.""" |
| 45 |
# it should raise a minimal InputError |
| 46 |
# (because assert/raise are used to validate inputs) |
| 47 |
# - intended for users of the workflow |
| 48 |
src_dir = flow_cylc(f''' |
| 49 |
#!Jinja2 |
| 50 |
{{{{ {line} }}}} |
| 51 |
''') |
| 52 |
with pytest.raises( |
| 53 |
InputError, |
| 54 |
match=( |
| 55 |
r'^some error message' |
| 56 |
r'\n\(add --verbose for more context\)$' |
| 57 |
), |
| 58 |
): |
| 59 |
validate(src_dir) |
| 60 |
|
| 61 |
# in verbose mode, it should raise the full error |
| 62 |
# (this includes the Jinja2 context including file/line info) |
| 63 |
# - intended for developers of the workflow |
| 64 |
monkeypatch.setattr('cylc.flow.flags.verbosity', 1) |
| 65 |
with pytest.raises( |
| 66 |
Jinja2Error, |
| 67 |
match=( |
| 68 |
r'^some error message' |
| 69 |
r'\nFile /.*/pytest-.*/test_.*/flow.cylc' |
| 70 |
r'\n #!Jinja2' |
| 71 |
r'\n \{\{ ' + re.escape(line) + r' \}\}' |
| 72 |
r'\t<-- Jinja2AssertionError$' |
| 73 |
) |
| 74 |
): |
| 75 |
validate(src_dir) |