1
1
import os
2
2
import sys
3
+ import uuid
3
4
import importlib
5
+ import subprocess
4
6
from textwrap import dedent
5
7
6
8
import pytest
@@ -31,14 +33,14 @@ def _extension_test_package(tmpdir, request, extension_type='c',
31
33
"""Creates a simple test package with an extension module."""
32
34
33
35
test_pkg = tmpdir .mkdir ('test_pkg' )
34
- test_pkg .mkdir ('apyhtest_eva ' ).ensure ('__init__.py' )
36
+ test_pkg .mkdir ('helpers_test_package ' ).ensure ('__init__.py' )
35
37
36
38
# TODO: It might be later worth making this particular test package into a
37
39
# reusable fixture for other build_ext tests
38
40
39
41
if extension_type in ('c' , 'both' ):
40
42
# A minimal C extension for testing
41
- test_pkg .join ('apyhtest_eva ' , 'unit01.c' ).write (dedent ("""\
43
+ test_pkg .join ('helpers_test_package ' , 'unit01.c' ).write (dedent ("""\
42
44
#include <Python.h>
43
45
44
46
static struct PyModuleDef moduledef = {
@@ -56,7 +58,7 @@ def _extension_test_package(tmpdir, request, extension_type='c',
56
58
57
59
if extension_type in ('pyx' , 'both' ):
58
60
# A minimal Cython extension for testing
59
- test_pkg .join ('apyhtest_eva ' , 'unit02.pyx' ).write (dedent ("""\
61
+ test_pkg .join ('helpers_test_package ' , 'unit02.pyx' ).write (dedent ("""\
60
62
print("Hello cruel angel.")
61
63
""" ))
62
64
@@ -70,11 +72,13 @@ def _extension_test_package(tmpdir, request, extension_type='c',
70
72
include_dirs = ['numpy' ] if include_numpy else []
71
73
72
74
extensions_list = [
73
- "Extension('apyhtest_eva.{0}', [join('apyhtest_eva', '{1}')], include_dirs={2})" .format (
75
+ "Extension('helpers_test_package.{0}', "
76
+ "[join('helpers_test_package', '{1}')], "
77
+ "include_dirs={2})" .format (
74
78
os .path .splitext (extension )[0 ], extension , include_dirs )
75
79
for extension in extensions ]
76
80
77
- test_pkg .join ('apyhtest_eva ' , 'setup_package.py' ).write (dedent ("""\
81
+ test_pkg .join ('helpers_test_package ' , 'setup_package.py' ).write (dedent ("""\
78
82
from setuptools import Extension
79
83
from os.path import join
80
84
def get_extensions():
@@ -89,7 +93,7 @@ def get_extensions():
89
93
from extension_helpers import get_extensions
90
94
91
95
setup(
92
- name='apyhtest_eva ',
96
+ name='helpers_test_package ',
93
97
version='0.1',
94
98
packages=find_packages(),
95
99
ext_modules=get_extensions()
@@ -102,7 +106,7 @@ def get_extensions():
102
106
sys .path .insert (0 , '' )
103
107
104
108
def finalize ():
105
- cleanup_import ('apyhtest_eva ' )
109
+ cleanup_import ('helpers_test_package ' )
106
110
107
111
request .addfinalizer (finalize )
108
112
@@ -169,11 +173,107 @@ def test_compiler_module(capsys, c_extension_test_package):
169
173
'--record={0}' .format (install_temp .join ('record.txt' ))])
170
174
171
175
with install_temp .as_cwd ():
172
- import apyhtest_eva
176
+ import helpers_test_package
173
177
174
- # Make sure we imported the apyhtest_eva package from the correct place
175
- dirname = os .path .abspath (os .path .dirname (apyhtest_eva .__file__ ))
176
- assert dirname == str (install_temp .join ('apyhtest_eva ' ))
178
+ # Make sure we imported the helpers_test_package package from the correct place
179
+ dirname = os .path .abspath (os .path .dirname (helpers_test_package .__file__ ))
180
+ assert dirname == str (install_temp .join ('helpers_test_package ' ))
177
181
178
- import apyhtest_eva .compiler_version
179
- assert apyhtest_eva .compiler_version != 'unknown'
182
+ import helpers_test_package .compiler_version
183
+ assert helpers_test_package .compiler_version != 'unknown'
184
+
185
+
186
+ @pytest .mark .parametrize ('use_extension_helpers' , [None , False , True ])
187
+ def test_no_setup_py (tmpdir , use_extension_helpers ):
188
+ """
189
+ Test that makes sure that extension-helpers can be enabled without a
190
+ setup.py file.
191
+ """
192
+
193
+ package_name = 'helpers_test_package_' + str (uuid .uuid4 ()).replace ('-' , '_' )
194
+
195
+ test_pkg = tmpdir .mkdir ('test_pkg' )
196
+ test_pkg .mkdir (package_name ).ensure ('__init__.py' )
197
+
198
+ simple_c = test_pkg .join (package_name , 'simple.c' )
199
+
200
+ simple_c .write (dedent ("""\
201
+ #include <Python.h>
202
+
203
+ static struct PyModuleDef moduledef = {
204
+ PyModuleDef_HEAD_INIT,
205
+ "simple",
206
+ NULL,
207
+ -1,
208
+ NULL
209
+ };
210
+ PyMODINIT_FUNC
211
+ PyInit_simple(void) {
212
+ return PyModule_Create(&moduledef);
213
+ }
214
+ """ ))
215
+
216
+ test_pkg .join (package_name , 'setup_package.py' ).write (dedent (f"""\
217
+ from setuptools import Extension
218
+ from os.path import join
219
+ def get_extensions():
220
+ return [Extension('{ package_name } .simple', [join('{ package_name } ', 'simple.c')])]
221
+ """ ))
222
+
223
+ if use_extension_helpers is None :
224
+ test_pkg .join ('setup.cfg' ).write (dedent (f"""\
225
+ [metadata]
226
+ name = { package_name }
227
+ version = 0.1
228
+
229
+ [options]
230
+ packages = find:
231
+ """ ))
232
+ else :
233
+ test_pkg .join ('setup.cfg' ).write (dedent (f"""\
234
+ [metadata]
235
+ name = { package_name }
236
+ version = 0.1
237
+
238
+ [options]
239
+ packages = find:
240
+
241
+ [extension-helpers]
242
+ use_extension_helpers = { str (use_extension_helpers ).lower ()}
243
+ """ ))
244
+
245
+ test_pkg .join ('pyproject.toml' ).write (dedent ("""\
246
+ [build-system]
247
+ requires = ["setuptools>=43.0.0",
248
+ "wheel"]
249
+ build-backend = 'setuptools.build_meta'
250
+ """ ))
251
+
252
+ install_temp = test_pkg .mkdir ('install_temp' )
253
+
254
+ with test_pkg .as_cwd ():
255
+ # NOTE: we disable build isolation as we need to pick up the current
256
+ # developer version of extension-helpers
257
+ subprocess .call ([sys .executable , '-m' , 'pip' , 'install' , '.' ,
258
+ '--no-build-isolation' ,
259
+ f'--target={ install_temp } ' ])
260
+
261
+ if '' in sys .path :
262
+ sys .path .remove ('' )
263
+
264
+ sys .path .insert (0 , '' )
265
+
266
+ with install_temp .as_cwd ():
267
+
268
+ importlib .import_module (package_name )
269
+
270
+ if use_extension_helpers :
271
+ compiler_version_mod = importlib .import_module (package_name + '.compiler_version' )
272
+ assert compiler_version_mod .compiler != 'unknown'
273
+ else :
274
+ try :
275
+ importlib .import_module (package_name + '.compiler_version' )
276
+ except ImportError :
277
+ pass
278
+ else :
279
+ raise AssertionError (package_name + '.compiler_version should not exist' )
0 commit comments