diff options
author | Toshio Kuratomi <a.badger@gmail.com> | 2020-04-06 11:48:05 -0700 |
---|---|---|
committer | Toshio Kuratomi <a.badger@gmail.com> | 2020-04-08 07:56:11 -0700 |
commit | 184f54005695a101d9f4d86d29aaa499dd9ba1ad (patch) | |
tree | bbd3eafe2a7db1ca8ed1d9706131895b0841ba34 /test | |
parent | 6531ba38f88a7c7889180ae3376bbb4df6ed098e (diff) | |
download | ansible-184f54005695a101d9f4d86d29aaa499dd9ba1ad.tar.gz |
Add some more unittests for fail_json
Test that order of msg as a keyword arg doesn't matter
Diffstat (limited to 'test')
-rw-r--r-- | test/units/module_utils/basic/test_exit_json.py | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/test/units/module_utils/basic/test_exit_json.py b/test/units/module_utils/basic/test_exit_json.py index 65fc5b8842..94313765a7 100644 --- a/test/units/module_utils/basic/test_exit_json.py +++ b/test/units/module_utils/basic/test_exit_json.py @@ -7,6 +7,7 @@ from __future__ import (absolute_import, division) __metaclass__ = type import json +import sys import pytest @@ -68,10 +69,30 @@ class TestAnsibleModuleExitJson: 'invocation': EMPTY_INVOCATION} @pytest.mark.parametrize('stdin', [{}], indirect=['stdin']) + def test_fail_json_msg_as_kwarg_after(self, am, capfd): + """Test that msg as a kwarg after other kwargs works""" + with pytest.raises(SystemExit) as ctx: + am.fail_json(arbitrary=42, msg='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, + 'arbitrary': 42, + 'invocation': EMPTY_INVOCATION} + + @pytest.mark.parametrize('stdin', [{}], indirect=['stdin']) def test_fail_json_no_msg(self, am): with pytest.raises(TypeError) as ctx: am.fail_json() - assert ctx.value.args[0] == "fail_json() missing 1 required positional argument: 'msg'" + + if sys.version_info < (3,): + error_msg = "fail_json() takes exactly 2 arguments (1 given)" + else: + error_msg = "fail_json() missing 1 required positional argument: 'msg'" + + assert ctx.value.args[0] == error_msg class TestAnsibleModuleExitValuesRemoved: |