blob: 91b3de85d2ca94bdfb0f985a4bea01e1397a46b7 [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éc1375d52009-02-14 14:35:51 +000027 TempdirManager.setUp(self)
28 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)
Georg Brandlb533e262008-05-25 18:19:30 +000032
33 def test_build_ext(self):
Christian Heimes3e7e0692008-11-25 21:21:32 +000034 global ALREADY_TESTED
Georg Brandlb533e262008-05-25 18:19:30 +000035 xx_c = os.path.join(self.tmp_dir, 'xxmodule.c')
36 xx_ext = Extension('xx', [xx_c])
37 dist = Distribution({'name': 'xx', 'ext_modules': [xx_ext]})
38 dist.package_dir = self.tmp_dir
39 cmd = build_ext(dist)
Thomas Heller84b7f0c2008-05-26 11:51:44 +000040 if os.name == "nt":
41 # On Windows, we must build a debug version iff running
42 # a debug build of Python
43 cmd.debug = sys.executable.endswith("_d.exe")
Georg Brandlb533e262008-05-25 18:19:30 +000044 cmd.build_lib = self.tmp_dir
45 cmd.build_temp = self.tmp_dir
46
47 old_stdout = sys.stdout
48 if not support.verbose:
49 # silence compiler output
50 sys.stdout = StringIO()
51 try:
52 cmd.ensure_finalized()
53 cmd.run()
54 finally:
55 sys.stdout = old_stdout
56
Christian Heimes3e7e0692008-11-25 21:21:32 +000057 if ALREADY_TESTED:
58 return
59 else:
60 ALREADY_TESTED = True
61
Georg Brandlb533e262008-05-25 18:19:30 +000062 import xx
63
64 for attr in ('error', 'foo', 'new', 'roj'):
65 self.assert_(hasattr(xx, attr))
66
67 self.assertEquals(xx.foo(2, 5), 7)
68 self.assertEquals(xx.foo(13,15), 28)
69 self.assertEquals(xx.new().demo(), None)
70 doc = 'This is a template module just for instruction.'
71 self.assertEquals(xx.__doc__, doc)
72 self.assert_(isinstance(xx.Null(), xx.Null))
73 self.assert_(isinstance(xx.Str(), xx.Str))
74
75 def tearDown(self):
76 # Get everything back to normal
77 support.unload('xx')
78 sys.path = self.sys_path
Tarek Ziadéc1375d52009-02-14 14:35:51 +000079 TempdirManager.tearDown(self)
Georg Brandlb533e262008-05-25 18:19:30 +000080
Tarek Ziadé5874ef12009-02-05 22:56:14 +000081 def test_solaris_enable_shared(self):
82 dist = Distribution({'name': 'xx'})
83 cmd = build_ext(dist)
84 old = sys.platform
85
86 sys.platform = 'sunos' # fooling finalize_options
87 from distutils.sysconfig import _config_vars
88 old_var = _config_vars.get('Py_ENABLE_SHARED')
89 _config_vars['Py_ENABLE_SHARED'] = 1
90 try:
91 cmd.ensure_finalized()
92 finally:
93 sys.platform = old
94 if old_var is None:
95 del _config_vars['Py_ENABLE_SHARED']
96 else:
97 _config_vars['Py_ENABLE_SHARED'] = old_var
98
99 # make sur we get some lobrary dirs under solaris
100 self.assert_(len(cmd.library_dirs) > 0)
101
Georg Brandlb533e262008-05-25 18:19:30 +0000102def test_suite():
Neil Schemenauerd8f63bb2009-02-06 21:42:05 +0000103 src = _get_source_filename()
104 if not os.path.exists(src):
Georg Brandlb533e262008-05-25 18:19:30 +0000105 if support.verbose:
Neil Schemenauerd8f63bb2009-02-06 21:42:05 +0000106 print('test_build_ext: Cannot find source code (test'
107 ' must run in python build dir)')
Georg Brandlb533e262008-05-25 18:19:30 +0000108 return unittest.TestSuite()
109 else: return unittest.makeSuite(BuildExtTestCase)
110
111if __name__ == '__main__':
112 support.run_unittest(test_suite())