Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cylc lint non zero code from warnings #5546

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ for `cylc play` when called by `cylc vip` or `cylc vr`.
-------------------------------------------------------------------------------
## __cylc-8.1.5 (<span actions:bind='release-date'>Awaiting Release</span>)__

### Enhancements

[#5546](https://github.com/cylc/cylc-flow/pull/5546) -
`cylc lint` will provide a non-zero return code if any issues are identified.
This can be overridden using the new `--exit-zero` flag.

### Fixes

[#5228](https://github.com/cylc/cylc-flow/pull/5228) -
Expand Down
22 changes: 20 additions & 2 deletions cylc/flow/scripts/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,17 @@
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.

A non-zero return code will be returned if any issues are identified.
This can be overridden by providing the "--exit-zero" flag.

Configurations for Cylc lint can also be set in a pyproject.toml file.

"""
from colorama import Fore
from optparse import Values
from pathlib import Path
import re
import sys
import tomli
from typing import Generator, Union

Expand Down Expand Up @@ -666,6 +670,13 @@ def get_option_parser() -> COP:
metavar="CODE",
choices=tuple([f'S{i["index"]:03d}' for i in STYLE_CHECKS.values()])
)
parser.add_option(
'--exit-zero',
help='Exit with status code "0" even if there are issues.',
action='store_true',
default=False,
ColemanTom marked this conversation as resolved.
Show resolved Hide resolved
dest='exit_zero'
)

return parser

Expand All @@ -678,7 +689,7 @@ def main(parser: COP, options: 'Values', target=None) -> None:
else:
rulesets = [options.linter]
print(get_reference_text(parse_checks(rulesets, reference=True)))
exit(0)
sys.exit(0)

# If target not given assume we are looking at PWD
if target is None:
Expand Down Expand Up @@ -724,7 +735,9 @@ def main(parser: COP, options: 'Values', target=None) -> None:
'Lint after renaming '
'"suite.rc" to "flow.cylc"'
)
exit(0)
# Exit with an error code if --exit-zero was not set.
# Return codes: sys.exit(True) == 1, sys.exit(False) == 0
sys.exit(not options.exit_zero)
elif not cylc8 and '728' in mergedopts['rulesets']:
check_names = mergedopts['rulesets']
check_names.remove('728')
Expand Down Expand Up @@ -762,6 +775,11 @@ def main(parser: COP, options: 'Values', target=None) -> None:

print(msg)

# Exit with an error code if there were warnings and
# if --exit-zero was not set.
# Return codes: sys.exit(True) == 1, sys.exit(False) == 0
sys.exit(count != 0 and not options.exit_zero)


# NOTE: use += so that this works with __import__
# (docstring needed for `cylc help all` output)
Expand Down
22 changes: 14 additions & 8 deletions tests/functional/cylc-lint/00.lint.t
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#------------------------------------------------------------------------------
# Test workflow installation
. "$(dirname "$0")/test_header"
set_test_number 18
set_test_number 20

cat > flow.cylc <<__HERE__
# This is definitely not an OK flow.cylc file.
Expand All @@ -29,15 +29,15 @@ __HERE__
rm etc/global.cylc

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

TEST_NAME="${TEST_NAME_BASE}.pick-a-ruleset"
run_ok "${TEST_NAME}" cylc lint . -r 728
run_fail "${TEST_NAME}" cylc lint . -r 728
named_grep_ok "check-for-error-code" "U024" "${TEST_NAME}.stdout"

TEST_NAME="${TEST_NAME_BASE}.inplace"
run_ok "${TEST_NAME}" cylc lint . -i
run_fail "${TEST_NAME}" cylc lint . -i
named_grep_ok "check-for-error-code-in-file" "U024" flow.cylc

rm flow.cylc
Expand All @@ -47,12 +47,18 @@ cat > suite.rc <<__HERE__
{{FOO}}
__HERE__

TEST_NAME="${TEST_NAME_BASE}.pick-a-ruleset"
run_ok "${TEST_NAME}" cylc lint . -r 728
TEST_NAME="${TEST_NAME_BASE}.pick-a-ruleset-728"
run_fail "${TEST_NAME}" cylc lint . -r 728
named_grep_ok "do-not-upgrade-check-if-compat-mode" "Lint after renaming" "${TEST_NAME}.stderr"

TEST_NAME="${TEST_NAME_BASE}.pick-a-ruleset2"
run_ok "${TEST_NAME}" cylc lint . -r all
TEST_NAME="${TEST_NAME_BASE}.pick-a-ruleset-728-exit-zero"
run_ok "${TEST_NAME}" cylc lint . -r 728 --exit-zero

TEST_NAME="${TEST_NAME_BASE}.pick-a-ruleset-all"
run_fail "${TEST_NAME}" cylc lint . -r all

TEST_NAME="${TEST_NAME_BASE}.exit-zero"
run_ok "${TEST_NAME}" cylc lint --exit-zero .

rm suite.rc

Expand Down
6 changes: 3 additions & 3 deletions tests/functional/cylc-lint/01.lint-toml.t
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ __HERE__

# Control tests
TEST_NAME="it lints without toml file"
run_ok "${TEST_NAME}" cylc lint
run_fail "${TEST_NAME}" cylc lint
TESTOUT="${TEST_NAME}.stdout"
named_grep_ok "it returns error code" "S004" "${TESTOUT}"
named_grep_ok "it returns error from subdirectory" "niwa.cylc" "${TESTOUT}"
Expand Down Expand Up @@ -75,7 +75,7 @@ __HERE__

# Test that results are different:
TEST_NAME="it_lints_with_toml_file"
run_ok "${TEST_NAME}" cylc lint
run_fail "${TEST_NAME}" cylc lint
TESTOUT="${TEST_NAME}.stdout"
grep_fail "S004" "${TESTOUT}"
grep_fail "niwa.cylc" "${TESTOUT}"
Expand All @@ -93,7 +93,7 @@ cat > flow.cylc <<__HERE__
__HERE__

TEST_NAME="it_fails_if_max-line-length_set"
run_ok "${TEST_NAME}" cylc lint
run_fail "${TEST_NAME}" cylc lint
named_grep_ok "${TEST_NAME}-line-too-long-message" \
"\[S008\] flow.cylc:2: line > 4 characters." \
"${TEST_NAME}.stdout"
Expand Down