Skip to content

Commit

Permalink
Refactor config options checking a bit.
Browse files Browse the repository at this point in the history
  • Loading branch information
hjoliver committed Jan 27, 2022
1 parent 030cd94 commit ae211d3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 115 deletions.
50 changes: 18 additions & 32 deletions cylc/flow/scripts/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,45 +137,33 @@ def get_config_file_hierarchy(workflow_id: Optional[str] = None) -> List[str]:
return filepaths


def options_are_valid(options: 'Values', wid: Optional[str] = None) -> None:
"""Check parser for options that should not be allowed.
"""
if (
options.print_platform_names and
options.print_platforms
):
raise UserInputError(
'--platform-names is not compatible with --platforms'
)
if (
wid is not None
and (options.print_platform_names or options.print_platforms)
):
raise UserInputError(
'--platform-names and --platforms are not compatible '
'with providing a workflow ID.'
)


@cli_function(get_option_parser)
def main(
parser: COP,
options: 'Values',
*ids,
) -> None:
if not ids:
if options.print_platform_names or options.print_platforms:
# Get platform information:
glbl_cfg().platform_dump(
options.print_platform_names,
options.print_platforms

if options.print_platform_names and options.print_platforms:
options.print_platform_names = False

if options.print_platform_names or options.print_platforms:
# Get platform information:
if ids:
raise UserInputError(
"Workflow IDs are incompatible with --platform options."
)
return
glbl_cfg().platform_dump(
options.print_platform_names,
options.print_platforms
)
return

elif options.print_hierarchy:
print("\n".join(get_config_file_hierarchy()))
return
if options.print_hierarchy:
print("\n".join(get_config_file_hierarchy()))
return

if not ids:
glbl_cfg().idump(
options.item,
not options.defaults,
Expand All @@ -190,8 +178,6 @@ def main(
constraint='workflows',
)

options_are_valid(options, workflow_id)

if options.print_hierarchy:
print("\n".join(get_config_file_hierarchy(workflow_id)))
return
Expand Down
1 change: 0 additions & 1 deletion tests/functional/cylc-config/09-platforms.t
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ __HEREDOC__

export CYLC_CONF_PATH="${PWD}"


TEST_NAME="${TEST_NAME_BASE}-names"
run_ok "${TEST_NAME}" cylc config --platform-names
cmp_ok "${TEST_NAME}.stdout" <<__HEREDOC__
Expand Down
83 changes: 1 addition & 82 deletions tests/unit/scripts/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@
import pytest
from pytest import param

from cylc.flow.scripts.config import (
get_config_file_hierarchy,
options_are_valid
)
from cylc.flow.scripts.config import get_config_file_hierarchy
from cylc.flow.cfgspec.globalcfg import GlobalConfig


Expand Down Expand Up @@ -203,81 +200,3 @@ def test_cylc_site_conf_path_env_var(
GlobalConfig.get_inst()

assert capload == files


@pytest.mark.parametrize(
'expect, wid_given, p_name_set, p_set',
[
(
{
'log': '--platform-names is not compatible with --platforms',
'result': False
},
'Foo', True, True
),
(
{
'log': (
'--platform-names and --platforms are not compatible '
'with providing a workflow registration.'
),
'result': False
},
'Foo', True, False
),
(
{
'log': None,
'result': True
},
'Foo', False, False
),
(
{
'log': (
'--platform-names and --platforms are not compatible '
'with providing a workflow registration.'
),
'result': False
},
'Foo', False, True
),
(
{
'log': '--platform-names is not compatible with --platforms',
'result': False
},
None, True, True
),
(
{'log': None, 'result': True},
None, True, False
),
(
{'log': None, 'result': True},
None, False, False
),
(
{'log': None, 'result': True},
None, False, True
),
]
)
def test_options_are_valid(
caplog, expect, wid_given, p_name_set, p_set,
):
"""Test that bad sets of options are not allowed."""
# Setup a mock options object:
options = SimpleNamespace(
print_platform_names=p_name_set,
print_platforms=p_set
)

# Run the function under test:
if expect['result'] == True:
# Function does nothing:
assert caplog.records == []
else:
# Function Raises:
with pytest.raises(UserInputError):
options_are_valid(options, wid=wid_given)

0 comments on commit ae211d3

Please sign in to comment.