generated from tweag/project
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
check: Add function for finding import names provided by local packages
This adds find_import_names_from_package_name() which will use importlib.metadata to look up a package name (e.g. a dependency name) in the current Python environment, and attempt to find the corresponding import names that this package provides. If the package does not exist in the local environment, or if importlib.metadata is not able to find any provided import names, we return `None`, and expect the caller to fall back to some other mechanism to map the package name into import name(s).
- Loading branch information
Showing
4 changed files
with
79 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
"""Test the mapping of dependency names to import names.""" | ||
|
||
import pytest | ||
|
||
from fawltydeps.check import find_import_names_from_package_name | ||
|
||
# TODO: These tests are not fully isolated, i.e. they do not control the | ||
# virtualenv in which they run. For now, we assume that we are running in an | ||
# environment where at least these packages are available: | ||
# - setuptools (exposes multiple import names, including pkg_resources) | ||
# - pip (exposes a single import name: pip) | ||
# - isort (exposes no top_level.txt, but 'isort' import name can be inferred) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"dep_name,expect_import_names", | ||
[ | ||
pytest.param( | ||
"NOT_A_PACKAGE", | ||
None, | ||
id="missing_package__returns_None", | ||
), | ||
pytest.param( | ||
"isort", | ||
["isort"], | ||
id="package_exposes_nothing__can_still_infer_import_name", | ||
), | ||
pytest.param( | ||
"pip", | ||
["pip"], | ||
id="package_exposes_one_entry__returns_entry", | ||
), | ||
pytest.param( | ||
"setuptools", | ||
["_distutils_hack", "pkg_resources", "setuptools"], | ||
id="package_exposes_many_entries__returns_all_entries", | ||
), | ||
], | ||
) | ||
def test_find_import_names_from_package_name(dep_name, expect_import_names): | ||
assert find_import_names_from_package_name(dep_name) == expect_import_names |