Skip to content

Commit

Permalink
response to review
Browse files Browse the repository at this point in the history
  • Loading branch information
wxtim committed Sep 13, 2022
1 parent 8dd0b47 commit c6952ca
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
31 changes: 21 additions & 10 deletions cylc/flow/scripts/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
In-place mode ("-i, --inplace") writes suggestions into the file as comments.
Commit to version control before using this, in case you want to back out.
Configurations for Cylc lint can also be set in a pyproject.toml file. For
example:
Configurations for Cylc lint can also be set in a pyproject.toml file.
"""
from colorama import Fore
Expand All @@ -46,6 +45,7 @@
from cylc.flow.exceptions import CylcError
from cylc.flow.option_parsers import (
CylcOptionParser as COP,
WORKFLOW_ID_OR_PATH_ARG_DOC
)
from cylc.flow.cfgspec.workflow import upg, SPEC
from cylc.flow.id_cli import parse_id
Expand Down Expand Up @@ -239,7 +239,10 @@ def get_pyproject_toml(dir_):
tomlfile = Path(dir_ / 'pyproject.toml')
tomldata = {}
if tomlfile.is_file():
loadeddata = tomli.loads(tomlfile.read_text())
try:
loadeddata = tomli.loads(tomlfile.read_text())
except Exception as exc: # noqa
raise CylcError('pyproject.toml did not load: ' + exc.args[0])
if 'cylc-lint' in loadeddata:
for key in keys:
tomldata[key] = loadeddata.get('cylc-lint').get(key, [])
Expand Down Expand Up @@ -386,7 +389,7 @@ def get_upgrader_info():
}


def parse_checks(check_args, ignores=None, max_line_len=None):
def parse_checks(check_args, ignores=None, max_line_len=130, reference=False):
"""Collapse metadata in checks dicts.
Args:
Expand All @@ -411,11 +414,19 @@ def parse_checks(check_args, ignores=None, max_line_len=None):
meta.update({'index': index})
if f'{purpose}{index:03d}' not in ignores:
parsedchecks.update({pattern: meta})
# Add max line length check.
if 'S' in purpose and max_line_len:
if 'S' in purpose:
if not max_line_len:
max_line_len = 130
regex = r"^.{" + str(max_line_len) + r"}"
if reference:
msg = (
'line > {max_line_len} characters. Max line length '
'set in pyproject.toml (default 130)'
)
else:
msg = f'line > {max_line_len} characters.'
parsedchecks[re.compile(regex)] = {
'short': f'line > {max_line_len} characters.',
'short': msg,
'url': STYLE_GUIDE + 'line-length-and-continuation',
'index': 8,
'purpose': 'S'
Expand Down Expand Up @@ -565,7 +576,7 @@ def get_reference_text(checks):
def get_option_parser() -> COP:
parser = COP(
COP_DOC,
argdoc=[COP.optional(('WORKFLOW | PATH ...', 'Workflows to lint'))],
argdoc=[COP.optional(WORKFLOW_ID_OR_PATH_ARG_DOC)],
)
parser.add_option(
'--inplace', '-i',
Expand Down Expand Up @@ -617,7 +628,7 @@ def main(parser: COP, options: 'Values', *targets) -> None:
rulesets = ['728', 'style']
else:
rulesets = [options.linter]
print(get_reference_text(parse_checks(rulesets)))
print(get_reference_text(parse_checks(rulesets, reference=True)))
exit(0)

# If target not given assume we are looking at PWD
Expand Down Expand Up @@ -710,4 +721,4 @@ def main(parser: COP, options: 'Values', *targets) -> None:

# NOTE: use += so that this works with __import__
# (docstring needed for `cylc help all` output)
__doc__ += get_reference_rst(parse_checks(['728', 'style']))
__doc__ += get_reference_rst(parse_checks(['728', 'style'], reference=True))
2 changes: 1 addition & 1 deletion tests/functional/cylc-lint/00.lint.t
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ __HERE__
rm etc/global.cylc

TEST_NAME="${TEST_NAME_BASE}.vanilla"
run_ok "${TEST_NAME}" cylc lint
run_ok "${TEST_NAME}" cylc lint .
named_grep_ok "check-for-error-code" "S004" "${TEST_NAME}.stdout"

TEST_NAME="${TEST_NAME_BASE}.pick-a-ruleset"
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/scripts/test_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,3 +472,26 @@ def test_validate_toml_items(input_, error):
def test_merge_cli_with_tomldata(clidata, tomldata, expect):
"""It merges each of the three sections correctly: see function.__doc__"""
assert merge_cli_with_tomldata(clidata, tomldata) == expect


def test_invalid_tomlfile(tmp_path):
"""It fails nicely if pyproject.toml is malformed"""
tomlfile = (tmp_path / 'pyproject.toml')
tomlfile.write_text('foo :{}')
expected_msg = 'pyproject.toml did not load:'
with pytest.raises(CylcError, match=expected_msg):
get_pyproject_toml(tmp_path)


@pytest.mark.parametrize(
'ref, expect',
[
[True, 'line > {max_line_len} characters'],
[False, 'line > 130 characters']
]
)
def test_parse_checks_reference_mode(ref, expect):
result = parse_checks(['style'], reference=ref)
key = [i for i in result.keys()][-1]
value = result[key]
assert expect in value['short']

0 comments on commit c6952ca

Please sign in to comment.