Allow the msg argument to fail_json() to be a positional argument

fial_json() requires a message be given to it to inform the end user of
why the module failed.  Prior to this commit, the message had to be a
keyword argument:

    module.fail_json(msg='Failed due to error')

Since this is a required parameter, this commit allows the message to be
given as a positional argument instead:

   module.fail_json('Failed due to an error')
This commit is contained in:
Toshio Kuratomi 2020-04-06 10:47:38 -07:00
parent 148e83f832
commit 6531ba38f8
3 changed files with 21 additions and 5 deletions

View file

@ -0,0 +1,5 @@
minor_features:
- `AnsibleModule.fail_json()` has always required that a message be passed
in which informs the end user why the module failed. In the past this
message had to be passed as the `msg` keyword argument but it can now be
passed as the first positional argument instead.

View file

@ -2041,12 +2041,11 @@ class AnsibleModule(object):
self._return_formatted(kwargs)
sys.exit(0)
def fail_json(self, **kwargs):
def fail_json(self, msg, **kwargs):
''' return from the module, with an error message '''
if 'msg' not in kwargs:
raise AssertionError("implementation error -- msg to explain the error is required")
kwargs['failed'] = True
kwargs['msg'] = msg
# Add traceback if debug or high verbosity and it is missing
# NOTE: Badly named as exception, it really always has been a traceback

View file

@ -55,11 +55,23 @@ class TestAnsibleModuleExitJson:
expected['failed'] = True
assert return_val == expected
@pytest.mark.parametrize('stdin', [{}], indirect=['stdin'])
def test_fail_json_msg_positional(self, am, capfd):
with pytest.raises(SystemExit) as ctx:
am.fail_json('This is the msg')
assert ctx.value.code == 1
out, err = capfd.readouterr()
return_val = json.loads(out)
# Fail_json should add failed=True
assert return_val == {'msg': 'This is the msg', 'failed': True,
'invocation': EMPTY_INVOCATION}
@pytest.mark.parametrize('stdin', [{}], indirect=['stdin'])
def test_fail_json_no_msg(self, am):
with pytest.raises(AssertionError) as ctx:
with pytest.raises(TypeError) as ctx:
am.fail_json()
assert ctx.value.args[0] == "implementation error -- msg to explain the error is required"
assert ctx.value.args[0] == "fail_json() missing 1 required positional argument: 'msg'"
class TestAnsibleModuleExitValuesRemoved: