Skip to content

Commit

Permalink
Merge pull request #225 from wxtim/feature.do_not_allow_--defines_no_…
Browse files Browse the repository at this point in the history
…equal

Raise error if `"="` not in define
  • Loading branch information
hjoliver committed Oct 16, 2023
2 parents 6ca48b2 + c724d8b commit dd24b82
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ ones in. -->
[#250](https://github.com/cylc/cylc-rose/pull/250) - Prevent project
name being manually set to an empty string.

[#225](https://github.com/cylc/cylc-rose/pull/225) - Prevent totally invalid
CLI --defines with no = sign.

[#248](https://github.com/cylc/cylc-rose/pull/248) - Make sure that
rose stem sets variables in `[jinja2:suite.rc]` not `[jinja2]`.

Expand Down
5 changes: 5 additions & 0 deletions cylc/rose/entry_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
dump_rose_log,
get_rose_vars_from_config_node,
identify_templating_section,
invalid_defines_check,
rose_config_exists,
rose_config_tree_loader,
merge_rose_cylc_suite_install_conf,
Expand Down Expand Up @@ -120,6 +121,10 @@ def get_rose_vars(srcdir=None, opts=None):
raise NotARoseSuiteException()
return config

# Check for definitely invalid defines
if opts and hasattr(opts, 'defines'):
invalid_defines_check(opts.defines)

# Load the raw config tree
config_tree = rose_config_tree_loader(srcdir, opts)
deprecation_warnings(config_tree)
Expand Down
44 changes: 40 additions & 4 deletions cylc/rose/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@

from cylc.flow.hostuserutil import get_host
from cylc.flow import LOG
from cylc.flow.exceptions import CylcError
from cylc.flow.flags import cylc7_back_compat
from cylc.rose.jinja2_parser import Parser, patch_jinja2_leading_zeros
from metomi.rose import __version__ as ROSE_VERSION
from metomi.isodatetime.datetimeoper import DateTimeOperator
from metomi.rose.config import ConfigDumper, ConfigNodeDiff, ConfigNode
from metomi.rose.config import ConfigNodeDiff, ConfigNode, ConfigDumper
from metomi.rose.config_processor import ConfigProcessError
from metomi.rose.env import env_var_process, UnboundEnvironmentVariableError

Expand All @@ -46,7 +47,11 @@
ALL_MODES = 'all modes'


class MultipleTemplatingEnginesError(Exception):
class MultipleTemplatingEnginesError(CylcError):
...


class InvalidDefineError(CylcError):
...


Expand Down Expand Up @@ -341,11 +346,42 @@ def merge_rose_cylc_suite_install_conf(old, new):
return old


def invalid_defines_check(defines: List) -> None:
"""Check for defines which do not contain an = and therefore cannot be
valid
Examples:
# A single invalid define:
>>> import pytest
>>> with pytest.raises(InvalidDefineError, match=r'\\* foo'):
... invalid_defines_check(['foo'])
# Two invalid defines and one valid one:
>>> with pytest.raises(
... InvalidDefineError, match=r'\\* foo.*\\n.* \\* bar'
... ):
... invalid_defines_check(['foo', 'bar52', 'baz=442'])
# No invalid defines
>>> invalid_defines_check(['foo=12'])
"""
invalid_defines = []
for define in defines:
if parse_cli_defines(define) is False:
invalid_defines.append(define)
if invalid_defines:
msg = 'Invalid Suite Defines (should contain an =)'
for define in invalid_defines:
msg += f'\n * {define}'
raise InvalidDefineError(msg)


def parse_cli_defines(define: str) -> Union[
bool, Tuple[
bool, str, Tuple[
List[Union[str, Any]],
Union[str, Any],
Union[str, Any]
Union[str, Any],
]
]:
"""Parse a define string.
Expand Down

0 comments on commit dd24b82

Please sign in to comment.