blob: 2e47953ee31ff3ab17f8cef7e4a913ba4457a816 [file] [log] [blame]
Georg Brandlb533e262008-05-25 18:19:30 +00001import sys
2import os
Tarek Ziadéd7b5f662009-02-13 16:23:57 +00003import tempfile
Georg Brandlb533e262008-05-25 18:19:30 +00004import shutil
5from io import StringIO
6
7from distutils.core import Extension, Distribution
8from distutils.command.build_ext import build_ext
9from distutils import sysconfig
Tarek Ziadéc1375d52009-02-14 14:35:51 +000010from distutils.tests.support import TempdirManager
Georg Brandlb533e262008-05-25 18:19:30 +000011
12import unittest
13from test import support
14
Christian Heimes3e7e0692008-11-25 21:21:32 +000015# http://bugs.python.org/issue4373
16# Don't load the xx module more than once.
17ALREADY_TESTED = False
18
Neil Schemenauerd8f63bb2009-02-06 21:42:05 +000019def _get_source_filename():
20 srcdir = sysconfig.get_config_var('srcdir')
21 return os.path.join(srcdir, 'Modules', 'xxmodule.c')
22
Tarek Ziadéc1375d52009-02-14 14:35:51 +000023class BuildExtTestCase(TempdirManager, unittest.TestCase):
Georg Brandlb533e262008-05-25 18:19:30 +000024 def setUp(self):
25 # Create a simple test environment
26 # Note that we're making changes to sys.path
Tarek Ziadé38e3d512009-02-27 12:58:56 +000027 super(BuildExtTestCase, self).setUp()
Tarek Ziadéc1375d52009-02-14 14:35:51 +000028 self.tmp_dir = self.mkdtemp()
Georg Brandlb533e262008-05-25 18:19:30 +000029 self.sys_path = sys.path[:]
30 sys.path.append(self.tmp_dir)
Neil Schemenauerd8f63bb2009-02-06 21:42:05 +000031 shutil.copy(_get_source_filename(), self.tmp_dir)
Tarek Ziadé38e3d512009-02-27 12:58:56 +000032 if sys.version > "2.6":
33 import site
34 self.old_user_base = site.USER_BASE
35 site.USER_BASE = self.mkdtemp()
36 from distutils.command import build_ext
37 build_ext.USER_BASE = site.USER_BASE
Georg Brandlb533e262008-05-25 18:19:30 +000038
39 def test_build_ext(self):
Christian Heimes3e7e0692008-11-25 21:21:32 +000040 global ALREADY_TESTED
Georg Brandlb533e262008-05-25 18:19:30 +000041 xx_c = os.path.join(self.tmp_dir, 'xxmodule.c')
42 xx_ext = Extension('xx', [xx_c])
43 dist = Distribution({'name': 'xx', 'ext_modules': [xx_ext]})
44 dist.package_dir = self.tmp_dir
45 cmd = build_ext(dist)
Thomas Heller84b7f0c2008-05-26 11:51:44 +000046 if os.name == "nt":
47 # On Windows, we must build a debug version iff running
48 # a debug build of Python
49 cmd.debug = sys.executable.endswith("_d.exe")
Georg Brandlb533e262008-05-25 18:19:30 +000050 cmd.build_lib = self.tmp_dir
51 cmd.build_temp = self.tmp_dir
52
53 old_stdout = sys.stdout
54 if not support.verbose:
55 # silence compiler output
56 sys.stdout = StringIO()
57 try:
58 cmd.ensure_finalized()
59 cmd.run()
60 finally:
61 sys.stdout = old_stdout
62
Christian Heimes3e7e0692008-11-25 21:21:32 +000063 if ALREADY_TESTED:
64 return
65 else:
66 ALREADY_TESTED = True
67
Georg Brandlb533e262008-05-25 18:19:30 +000068 import xx
69
70 for attr in ('error', 'foo', 'new', 'roj'):
71 self.assert_(hasattr(xx, attr))
72
73 self.assertEquals(xx.foo(2, 5), 7)
74 self.assertEquals(xx.foo(13,15), 28)
75 self.assertEquals(xx.new().demo(), None)
76 doc = 'This is a template module just for instruction.'
77 self.assertEquals(xx.__doc__, doc)
78 self.assert_(isinstance(xx.Null(), xx.Null))
79 self.assert_(isinstance(xx.Str(), xx.Str))
80
81 def tearDown(self):
82 # Get everything back to normal
83 support.unload('xx')
84 sys.path = self.sys_path
Tarek Ziadé38e3d512009-02-27 12:58:56 +000085 if sys.version > "2.6":
86 import site
87 site.USER_BASE = self.old_user_base
88 from distutils.command import build_ext
89 build_ext.USER_BASE = self.old_user_base
90 super(BuildExtTestCase, self).tearDown()
Georg Brandlb533e262008-05-25 18:19:30 +000091
Tarek Ziadé5874ef12009-02-05 22:56:14 +000092 def test_solaris_enable_shared(self):
93 dist = Distribution({'name': 'xx'})
94 cmd = build_ext(dist)
95 old = sys.platform
96
97 sys.platform = 'sunos' # fooling finalize_options
98 from distutils.sysconfig import _config_vars
99 old_var = _config_vars.get('Py_ENABLE_SHARED')
100 _config_vars['Py_ENABLE_SHARED'] = 1
101 try:
102 cmd.ensure_finalized()
103 finally:
104 sys.platform = old
105 if old_var is None:
106 del _config_vars['Py_ENABLE_SHARED']
107 else:
108 _config_vars['Py_ENABLE_SHARED'] = old_var
109
110 # make sur we get some lobrary dirs under solaris
111 self.assert_(len(cmd.library_dirs) > 0)
112
Tarek Ziadé38e3d512009-02-27 12:58:56 +0000113 def test_user_site(self):
114 # site.USER_SITE was introduced in 2.6
115 if sys.version < '2.6':
116 return
117
118 import site
119 dist = Distribution({'name': 'xx'})
120 cmd = build_ext(dist)
121
122 # making sure the suer option is there
123 options = [name for name, short, lable in
124 cmd.user_options]
125 self.assert_('user' in options)
126
127 # setting a value
128 cmd.user = 1
129
130 # setting user based lib and include
131 lib = os.path.join(site.USER_BASE, 'lib')
132 incl = os.path.join(site.USER_BASE, 'include')
133 os.mkdir(lib)
134 os.mkdir(incl)
135
136 # let's run finalize
137 cmd.ensure_finalized()
138
139 # see if include_dirs and library_dirs
140 # were set
141 self.assert_(lib in cmd.library_dirs)
142 self.assert_(incl in cmd.include_dirs)
143
Georg Brandlb533e262008-05-25 18:19:30 +0000144def test_suite():
Neil Schemenauerd8f63bb2009-02-06 21:42:05 +0000145 src = _get_source_filename()
146 if not os.path.exists(src):
Georg Brandlb533e262008-05-25 18:19:30 +0000147 if support.verbose:
Neil Schemenauerd8f63bb2009-02-06 21:42:05 +0000148 print('test_build_ext: Cannot find source code (test'
149 ' must run in python build dir)')
Georg Brandlb533e262008-05-25 18:19:30 +0000150 return unittest.TestSuite()
151 else: return unittest.makeSuite(BuildExtTestCase)
152
153if __name__ == '__main__':
154 support.run_unittest(test_suite())