-
Notifications
You must be signed in to change notification settings - Fork 227
CLI: Fail command if --config
file contains unknown key
#5939
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
Merged
sphuber
merged 1 commit into
aiidateam:main
from
sphuber:fix/5929/cli-config-option-detect-unknown-keys
Mar 22, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A nitpick:
Is
BadParameter
the best error class to use here?I feel like the parameter to the option
--config
is the path or URL.That said, the other
click
error classes are no better.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am open to suggestions for better exceptions but I couldn't find one. Note that for the exception to be properly handled, it needs to be a click exception. I think it makes sense to say there is a problem with the
--config
option, because its value (the content of the file) is incorrect. Since the problem is likely that it contains unsupported keys, we couldn't have it be coupled to any other option of the command, as they don't exist.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm convinced!