|
5 | 5 |
|
6 | 6 | """
|
7 | 7 | import codecs
|
| 8 | +import itertools |
8 | 9 | import os
|
9 | 10 | import shutil
|
10 | 11 | import stat
|
|
34 | 35 | build_url_from_netloc,
|
35 | 36 | egg_link_path,
|
36 | 37 | format_size,
|
| 38 | + get_distribution, |
37 | 39 | get_installed_distributions,
|
38 | 40 | get_prog,
|
39 | 41 | hide_url,
|
@@ -192,26 +194,30 @@ def test_noegglink_in_sitepkgs_venv_global(self):
|
192 | 194 | @patch('pip._internal.utils.misc.dist_in_usersite')
|
193 | 195 | @patch('pip._internal.utils.misc.dist_is_local')
|
194 | 196 | @patch('pip._internal.utils.misc.dist_is_editable')
|
195 |
| -class Tests_get_installed_distributions: |
196 |
| - """test util.get_installed_distributions""" |
197 |
| - |
198 |
| - workingset = [ |
199 |
| - Mock(test_name="global"), |
200 |
| - Mock(test_name="editable"), |
201 |
| - Mock(test_name="normal"), |
202 |
| - Mock(test_name="user"), |
203 |
| - ] |
204 |
| - |
205 |
| - workingset_stdlib = [ |
| 197 | +class TestsGetDistributions(object): |
| 198 | + """Test get_installed_distributions() and get_distribution(). |
| 199 | + """ |
| 200 | + class MockWorkingSet(list): |
| 201 | + def require(self, name): |
| 202 | + pass |
| 203 | + |
| 204 | + workingset = MockWorkingSet(( |
| 205 | + Mock(test_name="global", key="global"), |
| 206 | + Mock(test_name="editable", key="editable"), |
| 207 | + Mock(test_name="normal", key="normal"), |
| 208 | + Mock(test_name="user", key="user"), |
| 209 | + )) |
| 210 | + |
| 211 | + workingset_stdlib = MockWorkingSet(( |
206 | 212 | Mock(test_name='normal', key='argparse'),
|
207 | 213 | Mock(test_name='normal', key='wsgiref')
|
208 |
| - ] |
| 214 | + )) |
209 | 215 |
|
210 |
| - workingset_freeze = [ |
| 216 | + workingset_freeze = MockWorkingSet(( |
211 | 217 | Mock(test_name='normal', key='pip'),
|
212 | 218 | Mock(test_name='normal', key='setuptools'),
|
213 | 219 | Mock(test_name='normal', key='distribute')
|
214 |
| - ] |
| 220 | + )) |
215 | 221 |
|
216 | 222 | def dist_is_editable(self, dist):
|
217 | 223 | return dist.test_name == "editable"
|
@@ -287,6 +293,46 @@ def test_freeze_excludes(self, mock_dist_is_editable,
|
287 | 293 | skip=('setuptools', 'pip', 'distribute'))
|
288 | 294 | assert len(dists) == 0
|
289 | 295 |
|
| 296 | + @pytest.mark.parametrize( |
| 297 | + "working_set, req_name", |
| 298 | + itertools.chain( |
| 299 | + itertools.product([workingset], (d.key for d in workingset)), |
| 300 | + itertools.product( |
| 301 | + [workingset_stdlib], (d.key for d in workingset_stdlib), |
| 302 | + ), |
| 303 | + ), |
| 304 | + ) |
| 305 | + def test_get_distribution( |
| 306 | + self, |
| 307 | + mock_dist_is_editable, |
| 308 | + mock_dist_is_local, |
| 309 | + mock_dist_in_usersite, |
| 310 | + working_set, |
| 311 | + req_name, |
| 312 | + ): |
| 313 | + """Ensure get_distribution() finds all kinds of distributions. |
| 314 | + """ |
| 315 | + mock_dist_is_editable.side_effect = self.dist_is_editable |
| 316 | + mock_dist_is_local.side_effect = self.dist_is_local |
| 317 | + mock_dist_in_usersite.side_effect = self.dist_in_usersite |
| 318 | + with patch("pip._vendor.pkg_resources.working_set", working_set): |
| 319 | + dist = get_distribution(req_name) |
| 320 | + assert dist is not None |
| 321 | + assert dist.key == req_name |
| 322 | + |
| 323 | + @patch('pip._vendor.pkg_resources.working_set', workingset) |
| 324 | + def test_get_distribution_nonexist( |
| 325 | + self, |
| 326 | + mock_dist_is_editable, |
| 327 | + mock_dist_is_local, |
| 328 | + mock_dist_in_usersite, |
| 329 | + ): |
| 330 | + mock_dist_is_editable.side_effect = self.dist_is_editable |
| 331 | + mock_dist_is_local.side_effect = self.dist_is_local |
| 332 | + mock_dist_in_usersite.side_effect = self.dist_in_usersite |
| 333 | + dist = get_distribution("non-exist") |
| 334 | + assert dist is None |
| 335 | + |
290 | 336 |
|
291 | 337 | def test_rmtree_errorhandler_nonexistent_directory(tmpdir):
|
292 | 338 | """
|
|
0 commit comments