-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CLI: Fail command if
--config
file contains unknown key (#5939)
Up till now, the `--config` option would accept configuration files with keys that did not correspond to existing options on the command. This would lead to incorrect options to be swallowed silently, which can be surprising to users if they accidentally misspelled an option or used one that didn't exist. Here the callback is updated to check the specified keys in the config and cross-reference them to the params that are defined on the command. If any unknown keys are specified a `BadParameter` exception is raised which displays which keys are not supported. The implementation was originally provided by the `click-config-file` dependency, however, in order to customize it with the desired behavior we had to essentially vendor the entire code, since there was no available hook for the customizations. The code is taken from https://github.com/phha/click_config_file/blob/7b93a20b4c79458987fac116418859f30a16d82a which corresponds to `v0.6.0`. Besides the main changes, the default provider is changed to `yaml_config_file_provider`, the docstrings are reformated and type hints are added.
- Loading branch information
Showing
8 changed files
with
186 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# -*- coding: utf-8 -*- | ||
########################################################################### | ||
# Copyright (c), The AiiDA team. All rights reserved. # | ||
# This file is part of the AiiDA code. # | ||
# # | ||
# The code is hosted on GitHub at https://github.com/aiidateam/aiida-core # | ||
# For further information on the license, see the LICENSE.txt file # | ||
# For further information please visit http://www.aiida.net # | ||
########################################################################### | ||
# pylint: disable=redefined-outer-name | ||
"""Unit tests for the :class:`aiida.cmdline.params.options.config.ConfigOption`.""" | ||
import functools | ||
import tempfile | ||
import textwrap | ||
|
||
import click | ||
import pytest | ||
|
||
from aiida.cmdline.params.options import CONFIG_FILE | ||
|
||
|
||
@pytest.fixture | ||
def run_cli_command(run_cli_command): | ||
"""Override the ``run_cli_command`` fixture to always run with ``use_subprocess=False`` for tests in this module.""" | ||
return functools.partial(run_cli_command, use_subprocess=False) | ||
|
||
|
||
@click.command() | ||
@click.option('--integer', type=int) | ||
@click.option('--boolean', type=bool) | ||
@CONFIG_FILE() | ||
def cmd(integer, boolean): | ||
"""Test command for :class:`aiida.cmdline.params.options.config.ConfigOption`.""" | ||
click.echo(f'Integer: {integer}') | ||
click.echo(f'Boolean: {boolean}') | ||
|
||
|
||
def test_valid(run_cli_command): | ||
"""Test the option for a valid configuration file.""" | ||
with tempfile.NamedTemporaryFile('w+') as handle: | ||
handle.write(textwrap.dedent(""" | ||
integer: 1 | ||
boolean: false | ||
""")) | ||
handle.flush() | ||
|
||
result = run_cli_command(cmd, ['--config', handle.name]) | ||
assert 'Integer: 1' in result.output_lines[0] | ||
assert 'Boolean: False' in result.output_lines[1] | ||
|
||
|
||
def test_invalid_unknown_keys(run_cli_command): | ||
"""Test the option for an invalid configuration file containing unknown keys.""" | ||
with tempfile.NamedTemporaryFile('w+') as handle: | ||
handle.write(textwrap.dedent(""" | ||
integer: 1 | ||
unknown: 2.0 | ||
""")) | ||
handle.flush() | ||
|
||
result = run_cli_command(cmd, ['--config', handle.name], raises=True) | ||
assert "Error: Invalid value for '--config': Invalid configuration file" in result.stderr | ||
assert "the following keys are not supported: {'unknown'}" in result.stderr |