|
17 | 17 | except ImportError:
|
18 | 18 | import mock
|
19 | 19 |
|
| 20 | +from pkg_resources import DistInfoDistribution, Distribution, EggInfoDistribution |
20 | 21 | from pkg_resources.extern.six.moves import map
|
21 | 22 | from pkg_resources.extern.six import text_type, string_types
|
22 | 23 |
|
@@ -190,6 +191,89 @@ def test_setuptools_not_imported(self):
|
190 | 191 | subprocess.check_call(cmd)
|
191 | 192 |
|
192 | 193 |
|
| 194 | +# TODO: remove this in favor of Path.touch() when Python 2 is dropped. |
| 195 | +def touch_file(path): |
| 196 | + """ |
| 197 | + Create an empty file. |
| 198 | + """ |
| 199 | + with open(path, 'w'): |
| 200 | + pass |
| 201 | + |
| 202 | + |
| 203 | +def make_distribution_no_version(tmpdir, basename): |
| 204 | + """ |
| 205 | + Create a distribution directory with no file containing the version. |
| 206 | + """ |
| 207 | + # Convert the LocalPath object to a string before joining. |
| 208 | + dist_dir = os.path.join(str(tmpdir), basename) |
| 209 | + os.mkdir(dist_dir) |
| 210 | + # Make the directory non-empty so distributions_from_metadata() |
| 211 | + # will detect it and yield it. |
| 212 | + touch_file(os.path.join(dist_dir, 'temp.txt')) |
| 213 | + |
| 214 | + dists = list(pkg_resources.distributions_from_metadata(dist_dir)) |
| 215 | + assert len(dists) == 1 |
| 216 | + dist, = dists |
| 217 | + |
| 218 | + return dist, dist_dir |
| 219 | + |
| 220 | + |
| 221 | +@pytest.mark.parametrize( |
| 222 | + 'suffix, expected_filename, expected_dist_type', |
| 223 | + [ |
| 224 | + ('egg-info', 'PKG-INFO', EggInfoDistribution), |
| 225 | + ('dist-info', 'METADATA', DistInfoDistribution), |
| 226 | + ], |
| 227 | +) |
| 228 | +def test_distribution_version_missing(tmpdir, suffix, expected_filename, |
| 229 | + expected_dist_type): |
| 230 | + """ |
| 231 | + Test Distribution.version when the "Version" header is missing. |
| 232 | + """ |
| 233 | + basename = 'foo.{}'.format(suffix) |
| 234 | + dist, dist_dir = make_distribution_no_version(tmpdir, basename) |
| 235 | + |
| 236 | + expected_text = ( |
| 237 | + "Missing 'Version:' header and/or {} file at path: " |
| 238 | + ).format(expected_filename) |
| 239 | + metadata_path = os.path.join(dist_dir, expected_filename) |
| 240 | + |
| 241 | + # Now check the exception raised when the "version" attribute is accessed. |
| 242 | + with pytest.raises(ValueError) as excinfo: |
| 243 | + dist.version |
| 244 | + |
| 245 | + err = str(excinfo) |
| 246 | + # Include a string expression after the assert so the full strings |
| 247 | + # will be visible for inspection on failure. |
| 248 | + assert expected_text in err, str((expected_text, err)) |
| 249 | + |
| 250 | + # Also check the args passed to the ValueError. |
| 251 | + msg, dist = excinfo.value.args |
| 252 | + assert expected_text in msg |
| 253 | + # Check that the message portion contains the path. |
| 254 | + assert metadata_path in msg, str((metadata_path, msg)) |
| 255 | + assert type(dist) == expected_dist_type |
| 256 | + |
| 257 | + |
| 258 | +def test_distribution_version_missing_undetected_path(): |
| 259 | + """ |
| 260 | + Test Distribution.version when the "Version" header is missing and |
| 261 | + the path can't be detected. |
| 262 | + """ |
| 263 | + # Create a Distribution object with no metadata argument, which results |
| 264 | + # in an empty metadata provider. |
| 265 | + dist = Distribution('/foo') |
| 266 | + with pytest.raises(ValueError) as excinfo: |
| 267 | + dist.version |
| 268 | + |
| 269 | + msg, dist = excinfo.value.args |
| 270 | + expected = ( |
| 271 | + "Missing 'Version:' header and/or PKG-INFO file at path: " |
| 272 | + '[could not detect]' |
| 273 | + ) |
| 274 | + assert msg == expected |
| 275 | + |
| 276 | + |
193 | 277 | class TestDeepVersionLookupDistutils:
|
194 | 278 | @pytest.fixture
|
195 | 279 | def env(self, tmpdir):
|
|
0 commit comments