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

Import test: improve docs, add more tests #73600

Merged
merged 11 commits into from Mar 2, 2021

Conversation

felixfontein
Copy link
Contributor

SUMMARY

Follow-up to #72497. Improves documentation of import sanity test,

Also hopefully improves the situation in #73596, CC @rhysmeister.

ISSUE TYPE
  • Docs Pull Request
  • Test Pull Request
COMPONENT NAME

ansible-test import sanity test

@ansibot ansibot added affects_2.11 core_review In order to be merged, this PR must follow the core review workflow. docs This issue/PR relates to or includes documentation. docsite This issue/PR relates to the documentation website. has_issue needs_triage Needs a first human triage before being processed. new_plugin This PR includes a new plugin. support:community This issue/PR relates to code supported by the Ansible community. support:core This issue/PR relates to code supported by the Ansible Engineering Team. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. needs_ci This PR requires CI testing to be performed. Please close and re-open this PR to trigger CI. and removed core_review In order to be merged, this PR must follow the core review workflow. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. needs_ci This PR requires CI testing to be performed. Please close and re-open this PR to trigger CI. labels Feb 14, 2021
@felixfontein
Copy link
Contributor Author

ready_for_review


try:
import another_library
HAS_ANOTHER_LIBRARY = True
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 4f9e127.

Comment on lines 11 to 27
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')
Copy link
Member

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.

Copy link
Contributor Author

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 = ''''''
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about

Suggested change
RETURN = ''''''
RETURN = ''

Copy link
Contributor Author

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).

@Andersson007
Copy link
Contributor

and thank you for the clarification! it helps much

@samdoran samdoran removed the needs_triage Needs a first human triage before being processed. label Feb 17, 2021
@felixfontein
Copy link
Contributor Author

Copy link
Contributor

@acozine acozine left a 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.

Co-authored-by: Alicia Cozine <879121+acozine@users.noreply.github.com>
@ansibot ansibot added needs_ci This PR requires CI testing to be performed. Please close and re-open this PR to trigger CI. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed core_review In order to be merged, this PR must follow the core review workflow. labels Feb 25, 2021
felixfontein and others added 2 commits February 25, 2021 15:15
Co-authored-by: Alicia Cozine <879121+acozine@users.noreply.github.com>
@ansibot ansibot added core_review In order to be merged, this PR must follow the core review workflow. and removed needs_ci This PR requires CI testing to be performed. Please close and re-open this PR to trigger CI. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Feb 25, 2021
Copy link
Contributor

@acozine acozine left a 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.

docs/docsite/rst/dev_guide/testing/sanity/import.rst Outdated Show resolved Hide resolved
Co-authored-by: Alicia Cozine <879121+acozine@users.noreply.github.com>
@ansibot ansibot added needs_ci This PR requires CI testing to be performed. Please close and re-open this PR to trigger CI. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. core_review In order to be merged, this PR must follow the core review workflow. and removed core_review In order to be merged, this PR must follow the core review workflow. needs_ci This PR requires CI testing to be performed. Please close and re-open this PR to trigger CI. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Mar 1, 2021
@mattclay mattclay merged commit 3cc693b into ansible:devel Mar 2, 2021
@felixfontein felixfontein deleted the import-test-docs-tests branch March 2, 2021 06:37
@felixfontein
Copy link
Contributor Author

@evgeni @webknjaz @mattclay @Andersson007 @acozine thanks for all your reviews, suggestions, and for merging this!

@ansible ansible locked and limited conversation to collaborators Mar 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.11 core_review In order to be merged, this PR must follow the core review workflow. docs This issue/PR relates to or includes documentation. docsite This issue/PR relates to the documentation website. has_issue new_plugin This PR includes a new plugin. support:community This issue/PR relates to code supported by the Ansible community. support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants