Skip to content

Commit

Permalink
Add tests for egg-info package with .files from inaccurate SOURCES.txt
Browse files Browse the repository at this point in the history
As established in previous commits, the SOURCES.txt file is not always
an accurate source of files that are present after a package has been
installed.

One situation where this inaccuracy is problematic is when top_level.txt
is also missing, and packages_distributions() is forced to infer the
provided import names based on Distribution.files. In this situation we
end up with incorrect mappings between import packages and distribution
packages, including import packages that clearly do not exist at all.

For example, a SOURCES.txt that lists setup.py (which is used _when_
installing, but is not available after installation), will see that
setup.py returned from .files, which then will cause
packages_distributions() to claim a mapping from the non-existent
'setup' import name to this distribution.

This commit adds EggInfoPkgSourcesFallback which demostrates such a
scenario, and adds this new class to a couple of relevant tests.
A couple of these tests are currently failing, to demonstrate the
issue at hand. These test failures will be fixed in the next commit.

See the python#115 issue for more details.
  • Loading branch information
jherland committed Mar 11, 2023
1 parent 2efa0d8 commit 8f62dd3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
43 changes: 43 additions & 0 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,49 @@ def setUp(self):
build_files(EggInfoPkgPipInstalledNoModules.files, prefix=self.site_dir)


class EggInfoPkgSourcesFallback(OnSysPath, SiteDir):
files: FilesDef = {
"starved_egg_pkg.egg-info": {
"PKG-INFO": """
Name: starved_egg-pkg
Author: Steven Ma
License: Unknown
Version: 1.0.0
Classifier: Intended Audience :: Developers
Classifier: Topic :: Software Development :: Libraries
Keywords: sample package
Description: Once upon a time
There was an egginfo package
that was installed with pip
""",
# SOURCES.txt is made from the source archive, and contains files
# (setup.py) that are not present after installation.
"SOURCES.txt": """
starved_egg_pkg.py
setup.py
starved_egg_pkg.egg-info/PKG-INFO
starved_egg_pkg.egg-info/SOURCES.txt
starved_egg_pkg.egg-info/requires.txt
""",
# missing installed-files.txt
"requires.txt": """
wheel >= 1.0; python_version >= "2.7"
[test]
pytest
""",
# missing top_level.txt
},
"starved_egg_pkg.py": """
def main():
print("hello world")
""",
}

def setUp(self):
super().setUp()
build_files(EggInfoPkgSourcesFallback.files, prefix=self.site_dir)


class EggInfoFile(OnSysPath, SiteDir):
files: FilesDef = {
"egginfo_file.egg-info": """
Expand Down
2 changes: 2 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def suppress_known_deprecation():
class APITests(
fixtures.EggInfoPkg,
fixtures.EggInfoPkgPipInstalledNoModules,
fixtures.EggInfoPkgSourcesFallback,
fixtures.DistInfoPkg,
fixtures.DistInfoPkgWithDot,
fixtures.EggInfoFile,
Expand Down Expand Up @@ -185,6 +186,7 @@ def test_files_dist_info(self):
def test_files_egg_info(self):
self._test_files(files('egginfo-pkg'))
self._test_files(files('empty_egg-pkg'))
self._test_files(files('starved_egg-pkg'))

def test_version_egg_info_file(self):
self.assertEqual(version('egginfo-file'), '0.1')
Expand Down
7 changes: 7 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ def test_metadata_loads_egg_info(self):
class DiscoveryTests(
fixtures.EggInfoPkg,
fixtures.EggInfoPkgPipInstalledNoModules,
fixtures.EggInfoPkgSourcesFallback,
fixtures.DistInfoPkg,
unittest.TestCase,
):
Expand All @@ -181,6 +182,7 @@ def test_package_discovery(self):
assert all(isinstance(dist, Distribution) for dist in dists)
assert any(dist.metadata['Name'] == 'egginfo-pkg' for dist in dists)
assert any(dist.metadata['Name'] == 'empty_egg-pkg' for dist in dists)
assert any(dist.metadata['Name'] == 'starved_egg-pkg' for dist in dists)
assert any(dist.metadata['Name'] == 'distinfo-pkg' for dist in dists)

def test_invalid_usage(self):
Expand Down Expand Up @@ -312,6 +314,7 @@ def test_packages_distributions_example2(self):
class PackagesDistributionsTest(
fixtures.EggInfoPkg,
fixtures.EggInfoPkgPipInstalledNoModules,
fixtures.EggInfoPkgSourcesFallback,
fixtures.OnSysPath,
fixtures.SiteDir,
unittest.TestCase,
Expand Down Expand Up @@ -353,3 +356,7 @@ def import_names_from_package(package_name):
# empty_egg-pkg should not be associated with any import names
# (top_level.txt is empty, and installed-files.txt has no .py files)
assert import_names_from_package('empty_egg-pkg') == set()

# starved_egg-pkg has one import ('starved_egg_pkg') inferred
# from SOURCES.txt (top_level.txt is missing)
assert import_names_from_package('starved_egg-pkg') == {'starved_egg_pkg'}

0 comments on commit 8f62dd3

Please sign in to comment.