| 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 |
"""Tests `cylc lint` CLI Utility.""" |
| 18 |
|
| 19 |
from collections import Counter |
| 20 |
import logging |
| 21 |
from pathlib import Path |
| 22 |
from pprint import pformat |
| 23 |
import re |
| 24 |
from textwrap import dedent |
| 25 |
from types import SimpleNamespace |
| 26 |
|
| 27 |
import pytest |
| 28 |
from pytest import param |
| 29 |
|
| 30 |
from cylc.flow.scripts.lint import ( |
| 31 |
LINT_SECTION, |
| 32 |
MANUAL_DEPRECATIONS, |
| 33 |
check_lowercase_family_names, |
| 34 |
get_cylc_files, |
| 35 |
get_pyproject_toml, |
| 36 |
get_reference, |
| 37 |
get_upgrader_info, |
| 38 |
lint, |
| 39 |
_merge_cli_with_tomldata, |
| 40 |
parse_checks, |
| 41 |
validate_toml_items |
| 42 |
) |
| 43 |
from cylc.flow.exceptions import CylcError |
| 44 |
|
| 45 |
STYLE_CHECKS = parse_checks(['style']) |
| 46 |
UPG_CHECKS = parse_checks(['728']) |
| 47 |
|
| 48 |
TEST_FILE = ''' |
| 49 |
[visualization] |
| 50 |
|
| 51 |
[cylc] |
| 52 |
include at start-up = foo |
| 53 |
exclude at start-up = bar |
| 54 |
reset timer = false |
| 55 |
log resolved dependencies = True |
| 56 |
required run mode = False |
| 57 |
health check interval = PT10M |
| 58 |
abort if any task fails = true |
| 59 |
suite definition directory = '/woo' |
| 60 |
disable automatic shutdown = false |
| 61 |
spawn to max active cycle points = false |
| 62 |
[[reference test]] |
| 63 |
allow task failures = true |
| 64 |
[[simulation]] |
| 65 |
disable suite event handlers = true |
| 66 |
[[authentication]] |
| 67 |
[[environment]] |
| 68 |
force run mode = dummy |
| 69 |
[[events]] |
| 70 |
reset inactivity timer = 42 |
| 71 |
abort on stalled = True |
| 72 |
abort on timeout = False |
| 73 |
abort if startup handler fails= True # deliberately not added a space. |
| 74 |
abort if shutdown handler fails= True |
| 75 |
abort if timeout handler fails = True |
| 76 |
abort if stalled handler fails = True |
| 77 |
abort if inactivity handler fails = False |
| 78 |
aborted handler = woo |
| 79 |
stalled handler = bar |
| 80 |
timeout handler = bas |
| 81 |
shutdown handler = qux |
| 82 |
startup handler = now |
| 83 |
inactivity handler = bored |
| 84 |
mail to = [email protected] |
| 85 |
mail from = [email protected] |
| 86 |
mail footer = "Collecting The Rice" |
| 87 |
mail smtp = 123.456.789.10 |
| 88 |
timeout = 30 |
| 89 |
inactivity = 30 |
| 90 |
abort on inactivity = 30 |
| 91 |
[[parameters]] |
| 92 |
[[parameter templates]] |
| 93 |
[[mail]] |
| 94 |
task event mail interval = PT4M # deliberately added lots of spaces. |
| 95 |
|
| 96 |
[scheduling] |
| 97 |
max active cycle points = 5 |
| 98 |
hold after point = 20220101T0000Z |
| 99 |
[[dependencies]] |
| 100 |
[[[R1]]] |
| 101 |
graph = """ |
| 102 |
MyFaM:finish-all => remote => !mash_theme |
| 103 |
a & \\ |
| 104 |
b => c |
| 105 |
c | \\ |
| 106 |
d => e |
| 107 |
""" |
| 108 |
|
| 109 |
[runtime] |
| 110 |
[[root]] |
| 111 |
[[[environment]]] |
| 112 |
CYLC_VERSION={{CYLC_VERSION}} |
| 113 |
ROSE_VERSION = {{ROSE_VERSION }} |
| 114 |
FCM_VERSION = {{ FCM_VERSION }} |
| 115 |
|
| 116 |
[[MyFaM]] |
| 117 |
extra log files = True |
| 118 |
{% from 'cylc.flow' import LOG %} |
| 119 |
pre-script = "echo ${CYLC_SUITE_DEF_PATH}" |
| 120 |
script = {{HELLOWORLD}} |
| 121 |
post-script = "echo ${CYLC_SUITE_INITIAL_CYCLE_TIME}" |
| 122 |
env-script = POINT=$(rose date 2059 --offset P1M) |
| 123 |
[[[suite state polling]]] |
| 124 |
template = and |
| 125 |
[[[remote]]] |
| 126 |
host = parasite |
| 127 |
suite definition directory = '/home/bar' |
| 128 |
[[[job]]] |
| 129 |
batch system = slurm |
| 130 |
shell = fish |
| 131 |
[[[events]]] |
| 132 |
mail retry delays = PT30S |
| 133 |
warning handler = frr.sh |
| 134 |
submission timeout handler = faiuhdf |
| 135 |
submission retry handler = vhbayhrbfgau |
| 136 |
submission failed handler = giaSEHFUIHJ |
| 137 |
failed handler = woo |
| 138 |
execution timeout handler = sdfghjkl |
| 139 |
expired handler = %(suite_uuid)s %(user@host)s |
| 140 |
late handler = dafuhj |
| 141 |
submitted handler = dafuhj |
| 142 |
started handler = dafuhj |
| 143 |
succeeded handler = dafuhj |
| 144 |
custom handler = efrgh |
| 145 |
critical handler = fgjdsfs |
| 146 |
retry handler = dfaiuhfrgpa |
| 147 |
sumbission handler = fas9hrfgaiuph |
| 148 |
|
| 149 |
# Shouldn't object to a comment, unlike the terrible indents below: |
| 150 |
[[bad indent]] |
| 151 |
inherit = MyFaM |
| 152 |
|
| 153 |
[[remote]] |
| 154 |
platform = $(rose host-select parasite) |
| 155 |
script = "cylc nudge" |
| 156 |
post-script = "rose suite-hook" |
| 157 |
|
| 158 |
[meta] |
| 159 |
[[and_another_thing]] |
| 160 |
[[[remote]]] |
| 161 |
host = `rose host-select thingy` |
| 162 |
|
| 163 |
%include foo.cylc |
| 164 |
''' |
| 165 |
|
| 166 |
|
| 167 |
LINT_TEST_FILE = ''' |
| 168 |
\t[scheduler] |
| 169 |
|
| 170 |
[scheduler] |
| 171 |
|
| 172 |
[[dependencies]] |
| 173 |
{% set N = 009 %} |
| 174 |
{% foo %} |
| 175 |
{{foo}} |
| 176 |
# {{quix}} |
| 177 |
R1 = """ |
| 178 |
foo & \\ |
| 179 |
bar => \\ |
| 180 |
baz |
| 181 |
""" |
| 182 |
|
| 183 |
[runtime] |
| 184 |
[[this_is_ok]] |
| 185 |
script = echo "this is incorrectly indented" |
| 186 |
|
| 187 |
[[foo]] |
| 188 |
inherit = hello |
| 189 |
[[[job]]] |
| 190 |
something\t |
| 191 |
[[bar]] |
| 192 |
platform = $(some-script foo) |
| 193 |
[[[directives]]] |
| 194 |
-l walltime = 666 |
| 195 |
[[baz]] |
| 196 |
run mode = skip |
| 197 |
platform = `no backticks` |
| 198 |
[[[skip]]] |
| 199 |
outputs = succeeded, failed |
| 200 |
''' + ( |
| 201 |
'\nscript = the quick brown fox jumps over the lazy dog until it becomes ' |
| 202 |
'clear that this line is longer than the default 130 character limit.' |
| 203 |
) |
| 204 |
|
| 205 |
|
| 206 |
def lint_text(text, checks, ignores=None, modify=False): |
| 207 |
checks = parse_checks(checks, ignores) |
| 208 |
counter = Counter() |
| 209 |
messages = [] |
| 210 |
outlines = list( |
| 211 |
lint( |
| 212 |
Path('flow.cylc'), |
| 213 |
iter(text.splitlines()), |
| 214 |
checks, |
| 215 |
counter, |
| 216 |
modify=modify, |
| 217 |
write=messages.append, |
| 218 |
) |
| 219 |
) |
| 220 |
|
| 221 |
return SimpleNamespace( |
| 222 |
counter=counter, |
| 223 |
messages=messages, |
| 224 |
outlines=outlines |
| 225 |
) |
| 226 |
|
| 227 |
|
| 228 |
def filter_strings(items, contains): |
| 229 |
"""Return only items which contain a given string.""" |
| 230 |
return [ |
| 231 |
message |
| 232 |
for message in items |
| 233 |
if contains in message |
| 234 |
] |
| 235 |
|
| 236 |
|
| 237 |
def assert_contains(items, contains, instances=None): |
| 238 |
"""Pass if at least one item contains a given string.""" |
| 239 |
filtered = filter_strings(items, contains) |
| 240 |
if not filtered: |
| 241 |
raise Exception( |
| 242 |
f'Could not find: "{contains}" in:\n' |
| 243 |
+ pformat(items)) |
| 244 |
elif instances and len(filtered) != instances: |
| 245 |
raise Exception( |
| 246 |
f'Expected "{contains}" to appear {instances} times' |
| 247 |
f', got it {len(filtered)} times.') |
| 248 |
|
| 249 |
|
| 250 |
EXPECT_INSTANCES_OF_ERR = { |
| 251 |
16: 3, |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
@pytest.mark.parametrize( |
| 256 |
# 11 won't be tested because there is no jinja2 shebang |
| 257 |
'number', set(range(1, len(MANUAL_DEPRECATIONS) + 1)) - {11} |
| 258 |
) |
| 259 |
def test_check_cylc_file_7to8(number): |
| 260 |
"""TEST File has one of each manual deprecation;""" |
| 261 |
lint = lint_text(TEST_FILE, ['728']) |
| 262 |
instances = EXPECT_INSTANCES_OF_ERR.get(number, None) |
| 263 |
# U0008 Has been removed, but we don't want to change the numbering |
| 264 |
if number != 8: |
| 265 |
assert_contains(lint.messages, f'[U{number:03d}]', instances) |
| 266 |
|
| 267 |
|
| 268 |
def test_check_cylc_file_7to8_has_shebang(): |
| 269 |
"""Jinja2 code comments will not be added if shebang present""" |
| 270 |
lint = lint_text('#!jinja2\n{{FOO}}', '', '[scheduler]') |
| 271 |
assert not lint.counter |
| 272 |
|
| 273 |
|
| 274 |
def test_check_cylc_file_line_no(): |
| 275 |
"""It prints the correct line numbers""" |
| 276 |
lint = lint_text(TEST_FILE, ['728']) |
| 277 |
# the first message should be for line number 2 (line is a shebang) |
| 278 |
assert ':2:' in lint.messages[0] |
| 279 |
|
| 280 |
|
| 281 |
@pytest.mark.parametrize( |
| 282 |
'line', |
| 283 |
[ |
| 284 |
# lowercase family names are not permitted |
| 285 |
'inherit = g', |
| 286 |
'inherit = FOO, bar', |
| 287 |
'inherit = None, bar', |
| 288 |
'inherit = A, b, C', |
| 289 |
'inherit = "A", "b"', |
| 290 |
"inherit = 'A', 'b'", |
| 291 |
'inherit = FOO_BAr', |
| 292 |
# whitespace & trailing commas |
| 293 |
' inherit = a , ', |
| 294 |
# parameters, templating code should be ignored |
| 295 |
# but any lowercase chars before or after should not |
| 296 |
'inherit = A<x>z', |
| 297 |
'inherit = A{{ x }}z', |
| 298 |
'inherit = N{# #}one', |
| 299 |
'inherit = A@( x )z', |
| 300 |
] |
| 301 |
) |
| 302 |
def test_check_lowercase_family_names__true(line): |
| 303 |
assert check_lowercase_family_names(line) is True |
| 304 |
|
| 305 |
|
| 306 |
@pytest.mark.parametrize( |
| 307 |
'line', |
| 308 |
[ |
| 309 |
# undefined values are ok |
| 310 |
'inherit =', |
| 311 |
'inherit = ', |
| 312 |
# none, None and root are ok |
| 313 |
'inherit = none', |
| 314 |
'inherit = None', |
| 315 |
'inherit = root', |
| 316 |
# whitespace & trailing commas |
| 317 |
'inherit = None,', |
| 318 |
'inherit = None, ', |
| 319 |
' inherit = None , ', |
| 320 |
# uppercase family names are ok |
| 321 |
'inherit = None, FOO, BAR', |
| 322 |
'inherit = FOO', |
| 323 |
'inherit = FOO_BAR_0', |
| 324 |
# parameters should be ignored |
| 325 |
'inherit = A<a>Z', |
| 326 |
'inherit = <a=1, b-1, c+1>', |
| 327 |
# jinja2 should be ignored |
| 328 |
param( |
| 329 |
'inherit = A{{ a }}Z, {% for x in range(5) %}' |
| 330 |
'A{{ x }}, {% endfor %}', |
| 331 |
id='jinja2-long' |
| 332 |
), |
| 333 |
# trailing comments should be ignored |
| 334 |
'inherit = A, B # no, comment', |
| 335 |
'inherit = # a', |
| 336 |
# quotes are ok |
| 337 |
'inherit = "A", "B"', |
| 338 |
"inherit = 'A', 'B'", |
| 339 |
'inherit = "None", B', |
| 340 |
'inherit = <a = 1, b - 1>', |
| 341 |
# one really awkward, but valid example |
| 342 |
param( |
| 343 |
'inherit = none, FOO_BAR_0, "<a - 1>", A<a>Z, A{{a}}Z', |
| 344 |
id='awkward' |
| 345 |
), |
| 346 |
] |
| 347 |
) |
| 348 |
def test_check_lowercase_family_names__false(line): |
| 349 |
assert check_lowercase_family_names(line) is False |
| 350 |
|
| 351 |
|
| 352 |
def test_inherit_lowercase_matches(): |
| 353 |
lint = lint_text('inherit = a', ['style']) |
| 354 |
assert any('S007' in msg for msg in lint.messages) |
| 355 |
|
| 356 |
|
| 357 |
@pytest.mark.parametrize( |
| 358 |
# 8 and 11 won't be tested because there is no jinja2 shebang |
| 359 |
'number', set(range(1, len(STYLE_CHECKS) + 1)) - {8, 11} |
| 360 |
) |
| 361 |
def test_check_cylc_file_lint(number): |
| 362 |
lint = lint_text(LINT_TEST_FILE, ['style']) |
| 363 |
assert_contains(lint.messages, f'S{number:03d}') |
| 364 |
|
| 365 |
|
| 366 |
@pytest.mark.parametrize('code', STYLE_CHECKS.keys()) |
| 367 |
def test_check_exclusions(code): |
| 368 |
"""It does not report any items excluded.""" |
| 369 |
lint = lint_text(LINT_TEST_FILE, ['style'], [code]) |
| 370 |
assert not filter_strings(lint.messages, code) |
| 371 |
|
| 372 |
|
| 373 |
def test_check_cylc_file_jinja2_comments(): |
| 374 |
"""Jinja2 inside a Jinja2 comment should not warn""" |
| 375 |
lint = lint_text('#!jinja2\n{# {{ foo }} #}', ['style']) |
| 376 |
assert not any('S011' in msg for msg in lint.messages) |
| 377 |
|
| 378 |
|
| 379 |
def test_check_cylc_file_jinja2_comments_shell_arithmetic_not_warned(): |
| 380 |
"""Jinja2 after a $((10#$variable)) should not warn""" |
| 381 |
lint = lint_text('#!jinja2\na = b$((10#$foo+5)) {{ BAR }}', ['style']) |
| 382 |
assert not any('S011' in msg for msg in lint.messages) |
| 383 |
|
| 384 |
|
| 385 |
@pytest.mark.parametrize( |
| 386 |
# 11 won't be tested because there is no jinja2 shebang |
| 387 |
'number', set(range(1, len(MANUAL_DEPRECATIONS) + 1)) - {11} |
| 388 |
) |
| 389 |
def test_check_cylc_file_inplace(number): |
| 390 |
lint = lint_text(TEST_FILE, ['728', 'style'], modify=True) |
| 391 |
# U0008 Has been removed, but we don't want to change the numbering |
| 392 |
if number != 8: |
| 393 |
assert_contains(lint.outlines, f'[U{number:03d}]') |
| 394 |
|
| 395 |
|
| 396 |
def test_get_cylc_files_get_all_rcs(tmp_path): |
| 397 |
"""It returns all paths except `log/**`. |
| 398 |
""" |
| 399 |
expect = [('etc', 'foo.rc'), ('bin', 'foo.rc'), ('an_other', 'foo.rc')] |
| 400 |
|
| 401 |
# Create a fake run directory, including the log file which should not |
| 402 |
# be searched: |
| 403 |
dirs = ['etc', 'bin', 'log', 'an_other', 'log/skarloey/'] |
| 404 |
for path in dirs: |
| 405 |
thispath = tmp_path / path |
| 406 |
thispath.mkdir(parents=True) |
| 407 |
(thispath / 'foo.rc').touch() |
| 408 |
|
| 409 |
# Run the test |
| 410 |
result = [(i.parent.name, i.name) for i in get_cylc_files(tmp_path)] |
| 411 |
assert sorted(result) == sorted(expect) |
| 412 |
|
| 413 |
|
| 414 |
def mock_parse_checks(*args, **kwargs): |
| 415 |
return { |
| 416 |
'U042': { |
| 417 |
'short': 'section `[vizualization]` has been removed.', |
| 418 |
'url': 'some url or other', |
| 419 |
'purpose': 'U', |
| 420 |
'rst': 'section ``[vizualization]`` has been removed.', |
| 421 |
'function': re.compile('not a regex') |
| 422 |
}, |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
def test_get_reference_rst(monkeypatch): |
| 427 |
"""It produces a reference file for our linting.""" |
| 428 |
monkeypatch.setattr( |
| 429 |
'cylc.flow.scripts.lint.parse_checks', mock_parse_checks |
| 430 |
) |
| 431 |
ref = get_reference('all', 'rst') |
| 432 |
expect = ( |
| 433 |
'\n7 to 8 upgrades\n---------------\n\n' |
| 434 |
'`U042 <https://cylc.github.io/cylc-doc/stable' |
| 435 |
'/html/7-to-8/some url or other>`_' |
| 436 |
f'\n{"^" * 78}' |
| 437 |
'\nsection ``[vizualization]`` has been ' |
| 438 |
'removed.\n\n\n' |
| 439 |
) |
| 440 |
assert ref == expect |
| 441 |
|
| 442 |
|
| 443 |
def test_get_reference_text(monkeypatch): |
| 444 |
"""It produces a reference file for our linting.""" |
| 445 |
monkeypatch.setattr( |
| 446 |
'cylc.flow.scripts.lint.parse_checks', mock_parse_checks |
| 447 |
) |
| 448 |
ref = get_reference('all', 'text') |
| 449 |
expect = ( |
| 450 |
'\n7 to 8 upgrades\n---------------\n\n' |
| 451 |
'U042:\n section `[vizualization]` has been ' |
| 452 |
'removed.' |
| 453 |
'\n https://cylc.github.io/cylc-doc/stable/html/7-to-8/some' |
| 454 |
' url or other\n\n\n' |
| 455 |
) |
| 456 |
assert ref == expect |
| 457 |
|
| 458 |
|
| 459 |
@pytest.fixture() |
| 460 |
def fixture_get_deprecations(): |
| 461 |
"""Get the deprections list for cylc.flow.cfgspec.workflow""" |
| 462 |
deprecations = get_upgrader_info() |
| 463 |
return deprecations |
| 464 |
|
| 465 |
|
| 466 |
@pytest.mark.parametrize( |
| 467 |
'findme', |
| 468 |
[ |
| 469 |
pytest.param( |
| 470 |
'abort if any task fails =', |
| 471 |
id='Item not available at Cylc 8' |
| 472 |
), |
| 473 |
pytest.param( |
| 474 |
'timeout =', |
| 475 |
id='Item renamed at Cylc 8' |
| 476 |
), |
| 477 |
pytest.param( |
| 478 |
'!execution retry delays', |
| 479 |
id='Item moved, name unchanged at Cylc 8' |
| 480 |
), |
| 481 |
pytest.param( |
| 482 |
'[cylc]', |
| 483 |
id='Section changed at Cylc 8' |
| 484 |
), |
| 485 |
] |
| 486 |
) |
| 487 |
def test_get_upg_info(fixture_get_deprecations, findme): |
| 488 |
"""It correctly scrapes the Cylc upgrader object. |
| 489 |
|
| 490 |
n.b this is just sampling to ensure that the test it getting items. |
| 491 |
""" |
| 492 |
if findme.startswith('!'): |
| 493 |
assert not any( |
| 494 |
i['function'](findme) for i in fixture_get_deprecations.values() |
| 495 |
) |
| 496 |
else: |
| 497 |
assert any( |
| 498 |
i['function'](findme) for i in fixture_get_deprecations.values() |
| 499 |
) is True |
| 500 |
|
| 501 |
|
| 502 |
@pytest.mark.parametrize( |
| 503 |
'settings, expected', |
| 504 |
[ |
| 505 |
param( |
| 506 |
""" |
| 507 |
rulesets = ['style'] |
| 508 |
ignore = ['S004'] |
| 509 |
exclude = ['sites/*.cylc'] |
| 510 |
""", |
| 511 |
{ |
| 512 |
'rulesets': ['style'], |
| 513 |
'ignore': ['S004'], |
| 514 |
'exclude': ['sites/*.cylc'], |
| 515 |
'max-line-length': None, |
| 516 |
}, |
| 517 |
id="returns what we want" |
| 518 |
), |
| 519 |
param( |
| 520 |
""" |
| 521 |
northgate = ['sites/*.cylc'] |
| 522 |
mons-meg = 42 |
| 523 |
""", |
| 524 |
(CylcError, ".*northgate"), |
| 525 |
id="invalid settings fail validation" |
| 526 |
), |
| 527 |
param( |
| 528 |
"max-line-length = 22", |
| 529 |
{ |
| 530 |
'exclude': [], |
| 531 |
'ignore': [], |
| 532 |
'rulesets': [], |
| 533 |
'max-line-length': 22, |
| 534 |
}, |
| 535 |
id='sets max line length' |
| 536 |
) |
| 537 |
] |
| 538 |
) |
| 539 |
def test_get_pyproject_toml(tmp_path, settings, expected): |
| 540 |
"""It returns only the lists we want from the toml file.""" |
| 541 |
tomlcontent = "[tool.cylc.lint]\n" + dedent(settings) |
| 542 |
(tmp_path / 'pyproject.toml').write_text(tomlcontent) |
| 543 |
|
| 544 |
if isinstance(expected, tuple): |
| 545 |
exc, match = expected |
| 546 |
with pytest.raises(exc, match=match): |
| 547 |
get_pyproject_toml(tmp_path) |
| 548 |
else: |
| 549 |
assert get_pyproject_toml(tmp_path) == expected |
| 550 |
|
| 551 |
|
| 552 |
@pytest.mark.parametrize( |
| 553 |
'tomlfile', |
| 554 |
[None, '', '[tool.cylc.lint]', '[cylc-lint]'] |
| 555 |
) |
| 556 |
def test_get_pyproject_toml_returns_blank(tomlfile, tmp_path): |
| 557 |
if tomlfile is not None: |
| 558 |
tfile = (tmp_path / 'pyproject.toml') |
| 559 |
tfile.write_text(tomlfile) |
| 560 |
expect = { |
| 561 |
'exclude': [], 'ignore': [], 'max-line-length': None, 'rulesets': [] |
| 562 |
} |
| 563 |
assert get_pyproject_toml(tmp_path) == expect |
| 564 |
|
| 565 |
|
| 566 |
def test_get_pyproject_toml__depr( |
| 567 |
tmp_path: Path, caplog: pytest.LogCaptureFixture |
| 568 |
): |
| 569 |
"""It warns if the section is deprecated.""" |
| 570 |
file = tmp_path / 'pyproject.toml' |
| 571 |
caplog.set_level(logging.WARNING) |
| 572 |
|
| 573 |
file.write_text(f'[{LINT_SECTION}]\nmax-line-length=14') |
| 574 |
assert get_pyproject_toml(tmp_path)['max-line-length'] == 14 |
| 575 |
assert not caplog.text |
| 576 |
|
| 577 |
file.write_text('[cylc-lint]\nmax-line-length=17') |
| 578 |
assert get_pyproject_toml(tmp_path)['max-line-length'] == 17 |
| 579 |
assert "[cylc-lint] section in pyproject.toml is deprecated" in caplog.text |
| 580 |
|
| 581 |
|
| 582 |
@pytest.mark.parametrize( |
| 583 |
'input_, output', |
| 584 |
[ |
| 585 |
param( |
| 586 |
{'exclude': ['hey', 'there', 'Delilah']}, |
| 587 |
True, |
| 588 |
id='it works' |
| 589 |
), |
| 590 |
param( |
| 591 |
{'foo': ['hey', 'there', 'Delilah', 42]}, |
| 592 |
'allowed', |
| 593 |
id='it fails with illegal section name' |
| 594 |
), |
| 595 |
param( |
| 596 |
{'exclude': 'woo!'}, |
| 597 |
'should be a list, but', |
| 598 |
id='it fails with illegal section type' |
| 599 |
), |
| 600 |
param( |
| 601 |
{'exclude': ['hey', 'there', 'Delilah', 42]}, |
| 602 |
'should be a string', |
| 603 |
id='it fails with illegal value name' |
| 604 |
), |
| 605 |
param( |
| 606 |
{'rulesets': ['hey']}, |
| 607 |
'hey not valid: Rulesets can be', |
| 608 |
id='it fails with illegal ruleset' |
| 609 |
), |
| 610 |
param( |
| 611 |
{'ignore': ['hey']}, |
| 612 |
'hey not valid: Ignore codes', |
| 613 |
id='it fails with illegal ignores' |
| 614 |
), |
| 615 |
param( |
| 616 |
{'ignore': ['R999']}, |
| 617 |
'R999 is a not a known linter code.', |
| 618 |
id='it fails with non-existant checks ignored' |
| 619 |
), |
| 620 |
param( |
| 621 |
{'ignore': ['U008']}, |
| 622 |
'warn', |
| 623 |
id='valid, but deprecated linter code' |
| 624 |
), |
| 625 |
] |
| 626 |
) |
| 627 |
def test_validate_toml_items(input_, output, caplog): |
| 628 |
"""It chucks out the wrong sort of items.""" |
| 629 |
if output not in [True, 'warn']: |
| 630 |
with pytest.raises(CylcError, match=output): |
| 631 |
validate_toml_items(input_) |
| 632 |
elif output is True: |
| 633 |
assert validate_toml_items(input_) is output |
| 634 |
elif output == 'warn': |
| 635 |
assert validate_toml_items(input_) is True |
| 636 |
assert 'U008 is a deprecated linter code' in caplog.messages |
| 637 |
|
| 638 |
|
| 639 |
@pytest.mark.parametrize( |
| 640 |
'clidata, tomldata, expect', |
| 641 |
[ |
| 642 |
param( |
| 643 |
{ |
| 644 |
'rulesets': ['foo', 'bar'], |
| 645 |
'ignore': ['R101'], |
| 646 |
}, |
| 647 |
{ |
| 648 |
'rulesets': ['baz'], |
| 649 |
'ignore': ['R100'], |
| 650 |
'exclude': ['not_me-*.cylc'] |
| 651 |
}, |
| 652 |
{ |
| 653 |
'rulesets': ['foo', 'bar'], |
| 654 |
'ignore': ['R100', 'R101'], |
| 655 |
'exclude': ['not_me-*.cylc'], |
| 656 |
'max-line-length': None |
| 657 |
}, |
| 658 |
id='It works with good path' |
| 659 |
), |
| 660 |
] |
| 661 |
) |
| 662 |
def test_merge_cli_with_tomldata(clidata, tomldata, expect): |
| 663 |
"""It merges each of the three sections correctly: see function.__doc__""" |
| 664 |
assert _merge_cli_with_tomldata(clidata, tomldata) == expect |
| 665 |
|
| 666 |
|
| 667 |
def test_invalid_tomlfile(tmp_path): |
| 668 |
"""It fails nicely if pyproject.toml is malformed""" |
| 669 |
tomlfile = (tmp_path / 'pyproject.toml') |
| 670 |
tomlfile.write_text('foo :{}') |
| 671 |
expected_msg = 'pyproject.toml did not load:' |
| 672 |
with pytest.raises(CylcError, match=expected_msg): |
| 673 |
get_pyproject_toml(tmp_path) |
| 674 |
|
| 675 |
|
| 676 |
@pytest.mark.parametrize( |
| 677 |
'ref, expect', |
| 678 |
[ |
| 679 |
[True, 'line > ``<max_line_len>`` characters'], |
| 680 |
[False, 'line > 42 characters'] |
| 681 |
] |
| 682 |
) |
| 683 |
def test_parse_checks_reference_mode(ref, expect): |
| 684 |
"""Add extra explanation of max line legth setting in reference mode. |
| 685 |
""" |
| 686 |
result = parse_checks(['style'], reference=ref, max_line_len=42) |
| 687 |
value = result['S012'] |
| 688 |
assert expect in value['short'] |
| 689 |
|
| 690 |
|
| 691 |
@pytest.mark.parametrize( |
| 692 |
'spaces, expect', |
| 693 |
( |
| 694 |
(0, 'S002'), |
| 695 |
(1, 'S013'), |
| 696 |
(2, 'S013'), |
| 697 |
(3, 'S013'), |
| 698 |
(4, None), |
| 699 |
(5, 'S013'), |
| 700 |
(6, 'S013'), |
| 701 |
(7, 'S013'), |
| 702 |
(8, None), |
| 703 |
(9, 'S013') |
| 704 |
) |
| 705 |
) |
| 706 |
def test_indents(spaces, expect): |
| 707 |
"""Test different wrong indentations |
| 708 |
|
| 709 |
Parameterization deliberately over-obvious to avoid replicating |
| 710 |
arithmetic logic from code. Dangerously close to re-testing ``%`` |
| 711 |
builtin. |
| 712 |
""" |
| 713 |
result = lint_text( |
| 714 |
f"{' ' * spaces}foo = 42", |
| 715 |
['style'] |
| 716 |
) |
| 717 |
result = ''.join(result.messages) |
| 718 |
if expect: |
| 719 |
assert expect in result |
| 720 |
else: |
| 721 |
assert not result |
| 722 |
|
| 723 |
|
| 724 |
def test_noqa(): |
| 725 |
"""Comments turn of checks. |
| 726 |
|
| 727 |
""" |
| 728 |
output = lint_text( |
| 729 |
'foo = bar#noqa\n' |
| 730 |
'qux = baz # noqa: S002\n' |
| 731 |
'buzz = food # noqa: S007\n' |
| 732 |
'quixotic = foolish # noqa: S007, S992 S002\n', |
| 733 |
['style'] |
| 734 |
) |
| 735 |
assert len(output.messages) == 1 |
| 736 |
assert 'flow.cylc:3' in output.messages[0] |