diff options
author | Chris Jerdonek <chris.jerdonek@gmail.com> | 2019-02-22 04:15:07 -0800 |
---|---|---|
committer | Paul Ganssle <paul@ganssle.io> | 2019-04-03 10:37:22 -0400 |
commit | 80ec85c55b1470df6541473f674f41bdc6bc5268 (patch) | |
tree | a6384eb909ed8e16a82e362c967b8cc7e8850f97 /pkg_resources/tests/test_pkg_resources.py | |
parent | 52939bcc8f549f6c8fef4bf76e09a20d0bf62e44 (diff) | |
download | python-setuptools-git-80ec85c55b1470df6541473f674f41bdc6bc5268.tar.gz |
Include file path when Version: missing
Related to pip's github issue pypa/pip#6194.
This has come up in pip's issue tracker (github) multiple times:
- pypa/pip#6177
- pypa/pip#6283
- pypa/pip#6194
Diffstat (limited to 'pkg_resources/tests/test_pkg_resources.py')
-rw-r--r-- | pkg_resources/tests/test_pkg_resources.py | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/pkg_resources/tests/test_pkg_resources.py b/pkg_resources/tests/test_pkg_resources.py index 2c2c9c7f..fb77c685 100644 --- a/pkg_resources/tests/test_pkg_resources.py +++ b/pkg_resources/tests/test_pkg_resources.py @@ -17,6 +17,7 @@ try: except ImportError: import mock +from pkg_resources import DistInfoDistribution, Distribution, EggInfoDistribution from pkg_resources.extern.six.moves import map from pkg_resources.extern.six import text_type, string_types @@ -190,6 +191,89 @@ class TestResourceManager: subprocess.check_call(cmd) +# TODO: remove this in favor of Path.touch() when Python 2 is dropped. +def touch_file(path): + """ + Create an empty file. + """ + with open(path, 'w'): + pass + + +def make_distribution_no_version(tmpdir, basename): + """ + Create a distribution directory with no file containing the version. + """ + # Convert the LocalPath object to a string before joining. + dist_dir = os.path.join(str(tmpdir), basename) + os.mkdir(dist_dir) + # Make the directory non-empty so distributions_from_metadata() + # will detect it and yield it. + touch_file(os.path.join(dist_dir, 'temp.txt')) + + dists = list(pkg_resources.distributions_from_metadata(dist_dir)) + assert len(dists) == 1 + dist, = dists + + return dist, dist_dir + + +@pytest.mark.parametrize( + 'suffix, expected_filename, expected_dist_type', + [ + ('egg-info', 'PKG-INFO', EggInfoDistribution), + ('dist-info', 'METADATA', DistInfoDistribution), + ], +) +def test_distribution_version_missing(tmpdir, suffix, expected_filename, + expected_dist_type): + """ + Test Distribution.version when the "Version" header is missing. + """ + basename = 'foo.{}'.format(suffix) + dist, dist_dir = make_distribution_no_version(tmpdir, basename) + + expected_text = ( + "Missing 'Version:' header and/or {} file at path: " + ).format(expected_filename) + metadata_path = os.path.join(dist_dir, expected_filename) + + # Now check the exception raised when the "version" attribute is accessed. + with pytest.raises(ValueError) as excinfo: + dist.version + + err = str(excinfo) + # Include a string expression after the assert so the full strings + # will be visible for inspection on failure. + assert expected_text in err, str((expected_text, err)) + + # Also check the args passed to the ValueError. + msg, dist = excinfo.value.args + assert expected_text in msg + # Check that the message portion contains the path. + assert metadata_path in msg, str((metadata_path, msg)) + assert type(dist) == expected_dist_type + + +def test_distribution_version_missing_undetected_path(): + """ + Test Distribution.version when the "Version" header is missing and + the path can't be detected. + """ + # Create a Distribution object with no metadata argument, which results + # in an empty metadata provider. + dist = Distribution('/foo') + with pytest.raises(ValueError) as excinfo: + dist.version + + msg, dist = excinfo.value.args + expected = ( + "Missing 'Version:' header and/or PKG-INFO file at path: " + '[could not detect]' + ) + assert msg == expected + + class TestDeepVersionLookupDistutils: @pytest.fixture def env(self, tmpdir): |