Skip to content
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

FIX: InteractiveOption Remove double call of normal callback #5064

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions aiida/cmdline/params/options/interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,11 @@ def prompt_callback(self, ctx, param, value):

# If we are here, we are in interactive mode and the parameter is not specified
# We enter the prompt loop
value = self.prompt_loop(ctx, param, value)
else:
# There is a prompt_fn function and returns False (i.e. should not ask for this value
# We then set the value to None
value = None
return self.prompt_loop(ctx, param, value)

# There is a prompt_fn function and returns False (i.e. should not ask for this value
# We then set the value to None
value = None

# And then we call the callback
return self.after_callback(ctx, param, value)
Expand Down
38 changes: 38 additions & 0 deletions tests/cmdline/params/options/test_interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ def validate_positive_number(ctx, param, value): # pylint: disable=unused-argum
raise BadParameter(f'{value} is not a valid positive number')


def validate_positive_number_with_echo(ctx, param, value): # pylint: disable=unused-argument
"""Validate that the number passed to this parameter is a positive number.
Also echos a message to the terminal

:param ctx: the `click.Context`
:param param: the parameter
:param value: the value passed for the parameter
:raises `click.BadParameter`: if the value is not a positive number
"""
click.echo(f'Validating {value}')
if not isinstance(value, (int, float)) or value < 0:
from click import BadParameter
raise BadParameter(f'{value} is not a valid positive number')


class InteractiveOptionTest(unittest.TestCase):
"""Unit tests for InteractiveOption."""

Expand Down Expand Up @@ -95,6 +110,29 @@ def test_callback_prompt_twice(self):
self.assertIn(expected_3, lines[9])
self.assertIn(expected_4, lines[12])

def test_callback_prompt_only_once(self):
"""
scenario: using InteractiveOption with type=float and callback that echos an additional message
behaviour: the callback should be called at most once per prompt
"""
cmd = self.simple_command(type=float, callback=validate_positive_number_with_echo)
runner = CliRunner()
result = runner.invoke(cmd, [], input='string\n-1\n1\n')
self.assertIsNone(result.exception)
expected_1 = 'Error: string is not a valid floating point value'
expected_2 = 'Validating -1.0'
expected_3 = 'Error: -1.0 is not a valid positive number'
expected_4 = 'Validating 1.0'
expected_5 = '1.0'
lines = result.output.split('\n')
#The callback should be called once per prompt
#where type conversion was successful
self.assertEqual(expected_1, lines[3])
self.assertEqual(expected_2, lines[6])
self.assertEqual(expected_3, lines[7])
self.assertEqual(expected_4, lines[10])
self.assertEqual(expected_5, lines[11])

def test_prompt_str(self):
"""
scenario: using InteractiveOption with type=str
Expand Down