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
Import test: improve docs, add more tests #73600
Conversation
5e66fc4
to
e948477
Compare
ready_for_review |
|
||
try: | ||
import another_library | ||
HAS_ANOTHER_LIBRARY = True |
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.
For readability, it's probably better to have this in the else:
block.
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.
Fixed in 4f9e127.
try: | ||
import another_library | ||
HAS_ANOTHER_LIBRARY = True | ||
except ImportError: | ||
HAS_ANOTHER_LIBRARY = False | ||
|
||
|
||
# Later in module code: | ||
|
||
if not HAS_ANOTHER_LIBRARY: | ||
# Needs: from ansible.module_utils.basic import missing_required_lib | ||
module.fail_json(msg=missing_required_lib('another_library')) | ||
|
||
# Later in plugin code: | ||
|
||
if not HAS_ANOTHER_LIBRARY: | ||
raise AnsibleError('another_library must be installed to use this plugin') |
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 want to add that if the code targets a new enough Python 3 version, the example could preserve more metadata about the exception causes:
try:
import another_library
except ImportError as imp_exc:
another_library_import_exc = imp_exc
else:
another_library_import_exc = None
# Later in module code:
if another_library_import_exc is not None:
# Needs: from ansible.module_utils.basic import missing_required_lib
module.fail_json(msg=missing_required_lib('another_library'))
# Later in plugin code:
if another_library_import_exc is not None:
raise AnsibleError('another_library must be installed to use this plugin') from another_library_import_exc
For older Pythons the example could use six.raise_from()
shim.
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 split up the example into modules vs. plugins, and used traceback
to return the import traceback in module.fail_json()
, and six.raise_from()
for plugins (e74bec0).
msg: "{{ lookup('ns.col.bad') }}" | ||
''' | ||
|
||
RETURN = '''''' |
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.
How about
RETURN = '''''' | |
RETURN = '' |
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 copied that from existing tests in the same collection. I've changed it to ''' # '''
as recommended here: https://docs.ansible.com/ansible/latest/dev_guide/developing_modules_documenting.html#return-block (8d8ecba).
and thank you for the clarification! it helps much |
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.
Added a couple of suggestions for clarity.
test/integration/targets/ansible-test/ansible_collections/ns/col/plugins/lookup/bad.py
Show resolved
Hide resolved
Co-authored-by: Alicia Cozine <879121+acozine@users.noreply.github.com>
Co-authored-by: Alicia Cozine <879121+acozine@users.noreply.github.com>
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.
Thanks for adding the reason for using try/except blocks. I'd move things around a bit just so the comparison of unchecked imports and "all other Python libraries" is easier to follow.
Co-authored-by: Alicia Cozine <879121+acozine@users.noreply.github.com>
@evgeni @webknjaz @mattclay @Andersson007 @acozine thanks for all your reviews, suggestions, and for merging this! |
SUMMARY
Follow-up to #72497. Improves documentation of import sanity test,
Also hopefully improves the situation in #73596, CC @rhysmeister.
ISSUE TYPE
COMPONENT NAME
ansible-test import sanity test