11import os
22import sys
3+ import uuid
34import importlib
5+ import subprocess
46from textwrap import dedent
57
68import pytest
@@ -31,14 +33,14 @@ def _extension_test_package(tmpdir, request, extension_type='c',
3133 """Creates a simple test package with an extension module."""
3234
3335 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' )
3537
3638 # TODO: It might be later worth making this particular test package into a
3739 # reusable fixture for other build_ext tests
3840
3941 if extension_type in ('c' , 'both' ):
4042 # 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 ("""\
4244 #include <Python.h>
4345
4446 static struct PyModuleDef moduledef = {
@@ -56,7 +58,7 @@ def _extension_test_package(tmpdir, request, extension_type='c',
5658
5759 if extension_type in ('pyx' , 'both' ):
5860 # 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 ("""\
6062 print("Hello cruel angel.")
6163 """ ))
6264
@@ -70,11 +72,13 @@ def _extension_test_package(tmpdir, request, extension_type='c',
7072 include_dirs = ['numpy' ] if include_numpy else []
7173
7274 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 (
7478 os .path .splitext (extension )[0 ], extension , include_dirs )
7579 for extension in extensions ]
7680
77- test_pkg .join ('apyhtest_eva ' , 'setup_package.py' ).write (dedent ("""\
81+ test_pkg .join ('helpers_test_package ' , 'setup_package.py' ).write (dedent ("""\
7882 from setuptools import Extension
7983 from os.path import join
8084 def get_extensions():
@@ -89,7 +93,7 @@ def get_extensions():
8993 from extension_helpers import get_extensions
9094
9195 setup(
92- name='apyhtest_eva ',
96+ name='helpers_test_package ',
9397 version='0.1',
9498 packages=find_packages(),
9599 ext_modules=get_extensions()
@@ -102,7 +106,7 @@ def get_extensions():
102106 sys .path .insert (0 , '' )
103107
104108 def finalize ():
105- cleanup_import ('apyhtest_eva ' )
109+ cleanup_import ('helpers_test_package ' )
106110
107111 request .addfinalizer (finalize )
108112
@@ -169,11 +173,107 @@ def test_compiler_module(capsys, c_extension_test_package):
169173 '--record={0}' .format (install_temp .join ('record.txt' ))])
170174
171175 with install_temp .as_cwd ():
172- import apyhtest_eva
176+ import helpers_test_package
173177
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 ' ))
177181
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