diff options
author | Matt Davis <nitzmahone@users.noreply.github.com> | 2020-09-14 09:14:23 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-14 09:14:23 -0700 |
commit | e813b0151c3b4baec8368fc0d099e7f19be22144 (patch) | |
tree | 0b63287f5e5f5844e8a4cbb3c95e5c265d0f10d0 /test/units/utils | |
parent | 74a103d65587de299ef0d17187f65dca41067e1b (diff) | |
download | ansible-e813b0151c3b4baec8368fc0d099e7f19be22144.tar.gz |
fix coverage output from synthetic packages (#71727)
* fix coverage output from synthetic packages
* synthetic packages (eg, implicit collection packages without `__init__.py`) were always created at runtime with empty string source, which was compiled to a code object and exec'd during the package load. When run with code coverage, it created a bogus coverage entry (since the `__synthetic__`-suffixed `__file__` entry didn't exist on disk).
* modified collection loader `get_code` to preserve the distinction between `None` (eg synthetic package) and empty string (eg empty `__init__.py`) values from `get_source`, and to return `None` when the source is `None`. This allows the package loader to skip `exec`ing things that truly have no source file on disk, thus not creating bogus coverage entries, while preserving behavior and coverage reporting for empty package inits that actually exist.
* add unit test
Diffstat (limited to 'test/units/utils')
-rw-r--r-- | test/units/utils/collection_loader/test_collection_loader.py | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/test/units/utils/collection_loader/test_collection_loader.py b/test/units/utils/collection_loader/test_collection_loader.py index 496dc5416e..6488188c08 100644 --- a/test/units/utils/collection_loader/test_collection_loader.py +++ b/test/units/utils/collection_loader/test_collection_loader.py @@ -594,6 +594,22 @@ def test_bogus_imports(): import_module(bogus_import) +def test_empty_vs_no_code(): + finder = get_default_finder() + reset_collections_loader_state(finder) + + from ansible_collections.testns import testcoll # synthetic package with no code on disk + from ansible_collections.testns.testcoll.plugins import module_utils # real package with empty code file + + # ensure synthetic packages have no code object at all (prevent bogus coverage entries) + assert testcoll.__loader__.get_source(testcoll.__name__) is None + assert testcoll.__loader__.get_code(testcoll.__name__) is None + + # ensure empty package inits do have a code object + assert module_utils.__loader__.get_source(module_utils.__name__) == b'' + assert module_utils.__loader__.get_code(module_utils.__name__) is not None + + def test_finder_playbook_paths(): finder = get_default_finder() reset_collections_loader_state(finder) |