| 1 |
#!/usr/bin/env python3 |
| 2 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 3 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 4 |
# |
| 5 |
# This program is free software: you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation, either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
|
| 10 |
# This program is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
"""Test check functions in the `cylc lint` CLI Utility.""" |
| 18 |
|
| 19 |
import doctest |
| 20 |
import json |
| 21 |
import pytest |
| 22 |
import re |
| 23 |
|
| 24 |
from cylc.flow.scripts import lint |
| 25 |
|
| 26 |
VARS = re.compile(r'\{(.*)\}') |
| 27 |
|
| 28 |
# Functions in Cylc Lint defined with "check_* |
| 29 |
CHECKERS = [ |
| 30 |
getattr(lint, i) for i in lint.__dir__() if i.startswith('check_')] |
| 31 |
# List of checks defined as checks by Cylc Lint |
| 32 |
ALL_CHECKS = [ |
| 33 |
*lint.MANUAL_DEPRECATIONS.values(), |
| 34 |
*lint.STYLE_CHECKS.values(), |
| 35 |
] |
| 36 |
|
| 37 |
finder = doctest.DocTestFinder() |
| 38 |
|
| 39 |
|
| 40 |
@pytest.mark.parametrize( |
| 41 |
'check', |
| 42 |
# Those checks that have custom checker functions |
| 43 |
# and a short message with variables to insert: |
| 44 |
[ |
| 45 |
pytest.param(c, id=c.get('function').__name__) for c in ALL_CHECKS |
| 46 |
if c.get('function') in CHECKERS |
| 47 |
] |
| 48 |
) |
| 49 |
def test_custom_checker_doctests(check): |
| 50 |
"""All check functions have at least one failure doctest |
| 51 |
|
| 52 |
By forcing each check function to have valid doctests |
| 53 |
for the case that linting has failed we are able to |
| 54 |
check that the function outputs the correct information |
| 55 |
for formatting the short formats. |
| 56 |
""" |
| 57 |
doctests = finder.find(check['function'])[0] |
| 58 |
|
| 59 |
msg = f'{check["function"].__name__}: No failure examples in doctest' |
| 60 |
assert any(i.want for i in doctests.examples if i.want), msg |
| 61 |
|
| 62 |
|
| 63 |
@pytest.mark.parametrize( |
| 64 |
'ref, check', |
| 65 |
# Those checks that have custom checker functions |
| 66 |
# and a short message with variables to insert: |
| 67 |
[ |
| 68 |
(c.get('function'), c) for c in ALL_CHECKS |
| 69 |
if c.get('function') in CHECKERS |
| 70 |
and VARS.findall(c['short']) |
| 71 |
] |
| 72 |
) |
| 73 |
def test_custom_checkers_short_formatters(ref, check): |
| 74 |
"""If a check message has a format string assert that the checker |
| 75 |
function will return a dict to be used in |
| 76 |
``check['short'].format(**kwargs)``, based on doctest output. |
| 77 |
|
| 78 |
ref is useful to allow us to identify the check, even |
| 79 |
though not used in the test. |
| 80 |
""" |
| 81 |
doctests = finder.find(check['function'])[0] |
| 82 |
|
| 83 |
# Filter doctest examples for cases where there is a json parsable |
| 84 |
# want. |
| 85 |
examples = [ |
| 86 |
eg for eg in [ |
| 87 |
json.loads(e.want.replace("'", '"')) |
| 88 |
for e in doctests.examples if e.want |
| 89 |
] |
| 90 |
if eg |
| 91 |
] |
| 92 |
|
| 93 |
# Formatting using the example output changes the check short text: |
| 94 |
for example in examples: |
| 95 |
assert check['short'].format(**example) != check['short'] |