Skip to content

Commit

Permalink
parsec: catch section/option conflict in validation
Browse files Browse the repository at this point in the history
* Closes #4955
* If an item in the spec is a section but the user specified an option,
  raise an IllegalItemError if it is.
  • Loading branch information
oliver-sanders committed Apr 4, 2023
1 parent c3cda0a commit 5229bea
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ ones in. -->

### Fixes

[5450](https://github.com/cylc/cylc-flow/pull/5450) - Validation will now
fail earlier for cases where a configuration section is set as an option
(e.g. `scheudler = 42`).

[5398](https://github.com/cylc/cylc-flow/pull/5398) - Fix platform from
group selection order bug.

Expand Down
14 changes: 12 additions & 2 deletions cylc/flow/parsec/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,14 @@ def validate(self, cfg_root, spec_root):
else:
speckey = key
specval = spec[speckey]
if isinstance(value, dict) and not specval.is_leaf():

cfg_is_section = isinstance(value, dict)
spec_is_section = not specval.is_leaf()

if cfg_is_section and spec_is_section:
# Item is dict, push to queue
queue.append([value, specval, keys + [key]])
elif value is not None and specval.is_leaf():
if value is not None and not spec_is_section:
# Item is value, coerce according to value type
cfg[key] = self.coercers[specval.vdr](value, keys + [key])
if specval.options:
Expand All @@ -218,6 +222,12 @@ def validate(self, cfg_root, spec_root):
cfg[key] not in voptions):
raise IllegalValueError(
'option', keys + [key], cfg[key])
elif spec_is_section and not cfg_is_section:
raise IllegalItemError(
keys,
key,
msg=f'{key} should be a section not an option',
)

__call__ = validate

Expand Down
38 changes: 38 additions & 0 deletions tests/functional/validate/76-section-as-setting.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env bash
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
# Copyright (C) NIWA & British Crown (Met Office) & Contributors.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#-------------------------------------------------------------------------------
# Test CYLC_TEMPLATE_VARS exported.

. "$(dirname "$0")/test_header"

set_test_number 2

cat > 'flow.cylc' <<__HEREDOC__
meta = 22
[scheduling]
[[graph]]
R1 = foo
[runtime]
[[foo]]
__HEREDOC__

run_fail "${TEST_NAME_BASE}-validate" cylc validate .
grep_ok \
"IllegalItemError:.*meta" \
"${TEST_NAME_BASE}-validate.stderr"

0 comments on commit 5229bea

Please sign in to comment.