From 2ff5bf0f2195851d2558f5aab72acd977e0f44ae Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Tue, 8 Dec 2020 00:01:33 +0100 Subject: [PATCH] Fix processing of add_file_common_args=True when argument_spec is not specified as kwarg. (#72334) (#72361) (cherry picked from commit 233e7beb5bcd791e9f40700864e90163b7e3c92b) --- ...test-validate-modules-file-common-args.yml | 2 ++ .../validate_modules/module_args.py | 24 +++++++++---------- 2 files changed, 14 insertions(+), 12 deletions(-) create mode 100644 changelogs/fragments/ansible-test-validate-modules-file-common-args.yml diff --git a/changelogs/fragments/ansible-test-validate-modules-file-common-args.yml b/changelogs/fragments/ansible-test-validate-modules-file-common-args.yml new file mode 100644 index 00000000000..34b06020923 --- /dev/null +++ b/changelogs/fragments/ansible-test-validate-modules-file-common-args.yml @@ -0,0 +1,2 @@ +bugfixes: +- "ansible-test validate-modules - when a module uses ``add_file_common_args=True`` and does not use a keyword argument for ``argument_spec`` in ``AnsibleModule()``, the common file arguments were not considered added during validation (https://github.com/ansible/ansible/pull/72334)." diff --git a/test/lib/ansible_test/_data/sanity/validate-modules/validate_modules/module_args.py b/test/lib/ansible_test/_data/sanity/validate-modules/validate_modules/module_args.py index a2005c5746a..ac0252914b7 100644 --- a/test/lib/ansible_test/_data/sanity/validate-modules/validate_modules/module_args.py +++ b/test/lib/ansible_test/_data/sanity/validate-modules/validate_modules/module_args.py @@ -146,19 +146,19 @@ def get_py_argument_spec(filename, collection): raise AnsibleModuleNotInitialized() try: - try: - # for ping kwargs == {'argument_spec':{'data':{'type':'str','default':'pong'}}, 'supports_check_mode':True} + # for ping kwargs == {'argument_spec':{'data':{'type':'str','default':'pong'}}, 'supports_check_mode':True} + if 'argument_spec' in fake.kwargs: argument_spec = fake.kwargs['argument_spec'] - # If add_file_common_args is truish, add options from FILE_COMMON_ARGUMENTS when not present. - # This is the only modification to argument_spec done by AnsibleModule itself, and which is - # not caught by setup_env's AnsibleModule replacement - if fake.kwargs.get('add_file_common_args'): - for k, v in FILE_COMMON_ARGUMENTS.items(): - if k not in argument_spec: - argument_spec[k] = v - return argument_spec, fake.args, fake.kwargs - except KeyError: - return fake.args[0], fake.args, fake.kwargs + else: + argument_spec = fake.args[0] + # If add_file_common_args is truish, add options from FILE_COMMON_ARGUMENTS when not present. + # This is the only modification to argument_spec done by AnsibleModule itself, and which is + # not caught by setup_env's AnsibleModule replacement + if fake.kwargs.get('add_file_common_args'): + for k, v in FILE_COMMON_ARGUMENTS.items(): + if k not in argument_spec: + argument_spec[k] = v + return argument_spec, fake.args, fake.kwargs except (TypeError, IndexError): return {}, (), {}