1
1
import re
2
2
import json
3
3
import pickle
4
- import textwrap
5
4
import unittest
6
5
import warnings
7
6
import importlib .metadata
16
15
Distribution ,
17
16
EntryPoint ,
18
17
PackageNotFoundError ,
18
+ _unique ,
19
19
distributions ,
20
20
entry_points ,
21
21
metadata ,
@@ -51,6 +51,14 @@ def test_package_not_found_mentions_metadata(self):
51
51
def test_new_style_classes (self ):
52
52
self .assertIsInstance (Distribution , type )
53
53
54
+ @fixtures .parameterize (
55
+ dict (name = None ),
56
+ dict (name = '' ),
57
+ )
58
+ def test_invalid_inputs_to_from_name (self , name ):
59
+ with self .assertRaises (Exception ):
60
+ Distribution .from_name (name )
61
+
54
62
55
63
class ImportTests (fixtures .DistInfoPkg , unittest .TestCase ):
56
64
def test_import_nonexistent_module (self ):
@@ -78,48 +86,50 @@ def test_resolve_without_attr(self):
78
86
79
87
class NameNormalizationTests (fixtures .OnSysPath , fixtures .SiteDir , unittest .TestCase ):
80
88
@staticmethod
81
- def pkg_with_dashes ( site_dir ):
89
+ def make_pkg ( name ):
82
90
"""
83
- Create minimal metadata for a package with dashes
84
- in the name (and thus underscores in the filename) .
91
+ Create minimal metadata for a dist-info package with
92
+ the indicated name on the file system .
85
93
"""
86
- metadata_dir = site_dir / 'my_pkg.dist-info'
87
- metadata_dir .mkdir ()
88
- metadata = metadata_dir / 'METADATA'
89
- with metadata .open ('w' , encoding = 'utf-8' ) as strm :
90
- strm .write ('Version: 1.0\n ' )
91
- return 'my-pkg'
94
+ return {
95
+ f'{ name } .dist-info' : {
96
+ 'METADATA' : 'VERSION: 1.0\n ' ,
97
+ },
98
+ }
92
99
93
100
def test_dashes_in_dist_name_found_as_underscores (self ):
94
101
"""
95
102
For a package with a dash in the name, the dist-info metadata
96
103
uses underscores in the name. Ensure the metadata loads.
97
104
"""
98
- pkg_name = self .pkg_with_dashes (self .site_dir )
99
- assert version (pkg_name ) == '1.0'
100
-
101
- @staticmethod
102
- def pkg_with_mixed_case (site_dir ):
103
- """
104
- Create minimal metadata for a package with mixed case
105
- in the name.
106
- """
107
- metadata_dir = site_dir / 'CherryPy.dist-info'
108
- metadata_dir .mkdir ()
109
- metadata = metadata_dir / 'METADATA'
110
- with metadata .open ('w' , encoding = 'utf-8' ) as strm :
111
- strm .write ('Version: 1.0\n ' )
112
- return 'CherryPy'
105
+ fixtures .build_files (self .make_pkg ('my_pkg' ), self .site_dir )
106
+ assert version ('my-pkg' ) == '1.0'
113
107
114
108
def test_dist_name_found_as_any_case (self ):
115
109
"""
116
110
Ensure the metadata loads when queried with any case.
117
111
"""
118
- pkg_name = self .pkg_with_mixed_case (self .site_dir )
112
+ pkg_name = 'CherryPy'
113
+ fixtures .build_files (self .make_pkg (pkg_name ), self .site_dir )
119
114
assert version (pkg_name ) == '1.0'
120
115
assert version (pkg_name .lower ()) == '1.0'
121
116
assert version (pkg_name .upper ()) == '1.0'
122
117
118
+ def test_unique_distributions (self ):
119
+ """
120
+ Two distributions varying only by non-normalized name on
121
+ the file system should resolve as the same.
122
+ """
123
+ fixtures .build_files (self .make_pkg ('abc' ), self .site_dir )
124
+ before = list (_unique (distributions ()))
125
+
126
+ alt_site_dir = self .fixtures .enter_context (fixtures .tempdir ())
127
+ self .fixtures .enter_context (self .add_sys_path (alt_site_dir ))
128
+ fixtures .build_files (self .make_pkg ('ABC' ), alt_site_dir )
129
+ after = list (_unique (distributions ()))
130
+
131
+ assert len (after ) == len (before )
132
+
123
133
124
134
class NonASCIITests (fixtures .OnSysPath , fixtures .SiteDir , unittest .TestCase ):
125
135
@staticmethod
@@ -128,11 +138,12 @@ def pkg_with_non_ascii_description(site_dir):
128
138
Create minimal metadata for a package with non-ASCII in
129
139
the description.
130
140
"""
131
- metadata_dir = site_dir / 'portend.dist-info'
132
- metadata_dir .mkdir ()
133
- metadata = metadata_dir / 'METADATA'
134
- with metadata .open ('w' , encoding = 'utf-8' ) as fp :
135
- fp .write ('Description: pôrˈtend' )
141
+ contents = {
142
+ 'portend.dist-info' : {
143
+ 'METADATA' : 'Description: pôrˈtend' ,
144
+ },
145
+ }
146
+ fixtures .build_files (contents , site_dir )
136
147
return 'portend'
137
148
138
149
@staticmethod
@@ -141,19 +152,15 @@ def pkg_with_non_ascii_description_egg_info(site_dir):
141
152
Create minimal metadata for an egg-info package with
142
153
non-ASCII in the description.
143
154
"""
144
- metadata_dir = site_dir / 'portend.dist-info'
145
- metadata_dir .mkdir ()
146
- metadata = metadata_dir / 'METADATA'
147
- with metadata .open ('w' , encoding = 'utf-8' ) as fp :
148
- fp .write (
149
- textwrap .dedent (
150
- """
155
+ contents = {
156
+ 'portend.dist-info' : {
157
+ 'METADATA' : """
151
158
Name: portend
152
159
153
- pôrˈtend
154
- """
155
- ). strip ()
156
- )
160
+ pôrˈtend""" ,
161
+ },
162
+ }
163
+ fixtures . build_files ( contents , site_dir )
157
164
return 'portend'
158
165
159
166
def test_metadata_loads (self ):
0 commit comments