2
2
from configparser import ConfigParser
3
3
from inspect import cleandoc
4
4
5
+ import jaraco .path
5
6
import pytest
6
7
import tomli_w
7
8
from path import Path
82
83
83
84
84
85
def create_example (path , pkg_root ):
85
- pyproject = path / "pyproject.toml"
86
-
87
- files = [
88
- f"{ pkg_root } /pkg/__init__.py" ,
89
- "_files/file.txt" ,
90
- ]
91
- if pkg_root != "." : # flat-layout will raise error for multi-package dist
92
- # Ensure namespaces are discovered
93
- files .append (f"{ pkg_root } /other/nested/__init__.py" )
86
+ files = {
87
+ "pyproject.toml" : EXAMPLE ,
88
+ "README.md" : "hello world" ,
89
+ "_files" : {
90
+ "file.txt" : "" ,
91
+ },
92
+ }
93
+ packages = {
94
+ "pkg" : {
95
+ "__init__.py" : "" ,
96
+ "mod.py" : "class CustomSdist: pass" ,
97
+ "__version__.py" : "VERSION = (3, 10)" ,
98
+ "__main__.py" : "def exec(): print('hello')" ,
99
+ },
100
+ }
101
+
102
+ assert pkg_root # Meta-test: cannot be empty string.
94
103
95
- for file in files :
96
- (path / file ).parent .mkdir (exist_ok = True , parents = True )
97
- (path / file ).touch ()
104
+ if pkg_root == "." :
105
+ files = {** files , ** packages }
106
+ # skip other files: flat-layout will raise error for multi-package dist
107
+ else :
108
+ # Use this opportunity to ensure namespaces are discovered
109
+ files [pkg_root ] = {** packages , "other" : {"nested" : {"__init__.py" : "" }}}
98
110
99
- pyproject .write_text (EXAMPLE )
100
- (path / "README.md" ).write_text ("hello world" )
101
- (path / f"{ pkg_root } /pkg/mod.py" ).write_text ("class CustomSdist: pass" )
102
- (path / f"{ pkg_root } /pkg/__version__.py" ).write_text ("VERSION = (3, 10)" )
103
- (path / f"{ pkg_root } /pkg/__main__.py" ).write_text ("def exec(): print('hello')" )
111
+ jaraco .path .build (files , prefix = path )
104
112
105
113
106
114
def verify_example (config , path , pkg_root ):
@@ -174,7 +182,7 @@ class TestEntryPoints:
174
182
def write_entry_points (self , tmp_path ):
175
183
entry_points = ConfigParser ()
176
184
entry_points .read_dict (ENTRY_POINTS )
177
- with open (tmp_path / "entry-points.txt" , "w" ) as f :
185
+ with open (tmp_path / "entry-points.txt" , "w" , encoding = "utf-8" ) as f :
178
186
entry_points .write (f )
179
187
180
188
def pyproject (self , dynamic = None ):
@@ -208,11 +216,13 @@ def test_dynamic(self, tmp_path):
208
216
# Let's create a project example that has dynamic classifiers
209
217
# coming from a txt file.
210
218
create_example (tmp_path , "src" )
211
- classifiers = """\
212
- Framework :: Flask
213
- Programming Language :: Haskell
214
- """
215
- (tmp_path / "classifiers.txt" ).write_text (cleandoc (classifiers ))
219
+ classifiers = cleandoc (
220
+ """
221
+ Framework :: Flask
222
+ Programming Language :: Haskell
223
+ """
224
+ )
225
+ (tmp_path / "classifiers.txt" ).write_text (classifiers , encoding = "utf-8" )
216
226
217
227
pyproject = tmp_path / "pyproject.toml"
218
228
config = read_configuration (pyproject , expand = False )
@@ -240,7 +250,7 @@ def test_dynamic_without_config(self, tmp_path):
240
250
"""
241
251
242
252
pyproject = tmp_path / "pyproject.toml"
243
- pyproject .write_text (cleandoc (config ))
253
+ pyproject .write_text (cleandoc (config ), encoding = "utf-8" )
244
254
with pytest .raises (OptionError , match = "No configuration .* .classifiers." ):
245
255
read_configuration (pyproject )
246
256
@@ -252,7 +262,7 @@ def test_dynamic_readme_from_setup_script_args(self, tmp_path):
252
262
dynamic = ["readme"]
253
263
"""
254
264
pyproject = tmp_path / "pyproject.toml"
255
- pyproject .write_text (cleandoc (config ))
265
+ pyproject .write_text (cleandoc (config ), encoding = "utf-8" )
256
266
dist = Distribution (attrs = {"long_description" : "42" })
257
267
# No error should occur because of missing `readme`
258
268
dist = apply_configuration (dist , pyproject )
@@ -270,7 +280,7 @@ def test_dynamic_without_file(self, tmp_path):
270
280
"""
271
281
272
282
pyproject = tmp_path / "pyproject.toml"
273
- pyproject .write_text (cleandoc (config ))
283
+ pyproject .write_text (cleandoc (config ), encoding = "utf-8" )
274
284
with pytest .warns (UserWarning , match = "File .*classifiers.txt. cannot be found" ):
275
285
expanded = read_configuration (pyproject )
276
286
assert "classifiers" not in expanded ["project" ]
@@ -291,7 +301,7 @@ def test_dynamic_without_file(self, tmp_path):
291
301
)
292
302
def test_ignore_unrelated_config (tmp_path , example ):
293
303
pyproject = tmp_path / "pyproject.toml"
294
- pyproject .write_text (cleandoc (example ))
304
+ pyproject .write_text (cleandoc (example ), encoding = "utf-8" )
295
305
296
306
# Make sure no error is raised due to 3rd party configs in pyproject.toml
297
307
assert read_configuration (pyproject ) is not None
@@ -313,7 +323,7 @@ def test_ignore_unrelated_config(tmp_path, example):
313
323
)
314
324
def test_invalid_example (tmp_path , example , error_msg ):
315
325
pyproject = tmp_path / "pyproject.toml"
316
- pyproject .write_text (cleandoc (example ))
326
+ pyproject .write_text (cleandoc (example ), encoding = "utf-8" )
317
327
318
328
pattern = re .compile (f"invalid pyproject.toml.*{ error_msg } .*" , re .M | re .S )
319
329
with pytest .raises (ValueError , match = pattern ):
@@ -323,7 +333,7 @@ def test_invalid_example(tmp_path, example, error_msg):
323
333
@pytest .mark .parametrize ("config" , ("" , "[tool.something]\n value = 42" ))
324
334
def test_empty (tmp_path , config ):
325
335
pyproject = tmp_path / "pyproject.toml"
326
- pyproject .write_text (config )
336
+ pyproject .write_text (config , encoding = "utf-8" )
327
337
328
338
# Make sure no error is raised
329
339
assert read_configuration (pyproject ) == {}
@@ -335,7 +345,7 @@ def test_include_package_data_by_default(tmp_path, config):
335
345
default.
336
346
"""
337
347
pyproject = tmp_path / "pyproject.toml"
338
- pyproject .write_text (config )
348
+ pyproject .write_text (config , encoding = "utf-8" )
339
349
340
350
config = read_configuration (pyproject )
341
351
assert config ["tool" ]["setuptools" ]["include-package-data" ] is True
@@ -347,10 +357,11 @@ def test_include_package_data_in_setuppy(tmp_path):
347
357
348
358
See https://github.com/pypa/setuptools/issues/3197#issuecomment-1079023889
349
359
"""
350
- pyproject = tmp_path / "pyproject.toml"
351
- pyproject .write_text ("[project]\n name = 'myproj'\n version='42'\n " )
352
- setuppy = tmp_path / "setup.py"
353
- setuppy .write_text ("__import__('setuptools').setup(include_package_data=False)" )
360
+ files = {
361
+ "pyproject.toml" : "[project]\n name = 'myproj'\n version='42'\n " ,
362
+ "setup.py" : "__import__('setuptools').setup(include_package_data=False)" ,
363
+ }
364
+ jaraco .path .build (files , prefix = tmp_path )
354
365
355
366
with Path (tmp_path ):
356
367
dist = distutils .core .run_setup ("setup.py" , {}, stop_after = "config" )
0 commit comments