blob: 5e42943c38b7bf1c6ba80df2fa44ec4c28539da5 [file] [log] [blame]
Georg Brandlb533e262008-05-25 18:19:30 +00001import sys
2import os
3import tempfile
4import shutil
5from io import StringIO
6
7from distutils.core import Extension, Distribution
8from distutils.command.build_ext import build_ext
9from distutils import sysconfig
10
11import unittest
12from test import support
13
Christian Heimes3e7e0692008-11-25 21:21:32 +000014# http://bugs.python.org/issue4373
15# Don't load the xx module more than once.
16ALREADY_TESTED = False
17
Neil Schemenauerd8f63bb2009-02-06 21:42:05 +000018def _get_source_filename():
19 srcdir = sysconfig.get_config_var('srcdir')
20 return os.path.join(srcdir, 'Modules', 'xxmodule.c')
21
Georg Brandlb533e262008-05-25 18:19:30 +000022class BuildExtTestCase(unittest.TestCase):
23 def setUp(self):
24 # Create a simple test environment
25 # Note that we're making changes to sys.path
26 self.tmp_dir = tempfile.mkdtemp(prefix="pythontest_")
27 self.sys_path = sys.path[:]
28 sys.path.append(self.tmp_dir)
Neil Schemenauerd8f63bb2009-02-06 21:42:05 +000029 shutil.copy(_get_source_filename(), self.tmp_dir)
Georg Brandlb533e262008-05-25 18:19:30 +000030
31 def test_build_ext(self):
Christian Heimes3e7e0692008-11-25 21:21:32 +000032 global ALREADY_TESTED
Georg Brandlb533e262008-05-25 18:19:30 +000033 xx_c = os.path.join(self.tmp_dir, 'xxmodule.c')
34 xx_ext = Extension('xx', [xx_c])
35 dist = Distribution({'name': 'xx', 'ext_modules': [xx_ext]})
36 dist.package_dir = self.tmp_dir
37 cmd = build_ext(dist)
Thomas Heller84b7f0c2008-05-26 11:51:44 +000038 if os.name == "nt":
39 # On Windows, we must build a debug version iff running
40 # a debug build of Python
41 cmd.debug = sys.executable.endswith("_d.exe")
Georg Brandlb533e262008-05-25 18:19:30 +000042 cmd.build_lib = self.tmp_dir
43 cmd.build_temp = self.tmp_dir
44
45 old_stdout = sys.stdout
46 if not support.verbose:
47 # silence compiler output
48 sys.stdout = StringIO()
49 try:
50 cmd.ensure_finalized()
51 cmd.run()
52 finally:
53 sys.stdout = old_stdout
54
Christian Heimes3e7e0692008-11-25 21:21:32 +000055 if ALREADY_TESTED:
56 return
57 else:
58 ALREADY_TESTED = True
59
Georg Brandlb533e262008-05-25 18:19:30 +000060 import xx
61
62 for attr in ('error', 'foo', 'new', 'roj'):
63 self.assert_(hasattr(xx, attr))
64
65 self.assertEquals(xx.foo(2, 5), 7)
66 self.assertEquals(xx.foo(13,15), 28)
67 self.assertEquals(xx.new().demo(), None)
68 doc = 'This is a template module just for instruction.'
69 self.assertEquals(xx.__doc__, doc)
70 self.assert_(isinstance(xx.Null(), xx.Null))
71 self.assert_(isinstance(xx.Str(), xx.Str))
72
73 def tearDown(self):
74 # Get everything back to normal
75 support.unload('xx')
76 sys.path = self.sys_path
Hirokazu Yamamoto956ffd72008-09-21 20:52:42 +000077 # XXX on Windows the test leaves a directory with xx module in TEMP
78 shutil.rmtree(self.tmp_dir, os.name == 'nt' or sys.platform == 'cygwin')
Georg Brandlb533e262008-05-25 18:19:30 +000079
Tarek Ziadé5874ef12009-02-05 22:56:14 +000080 def test_solaris_enable_shared(self):
81 dist = Distribution({'name': 'xx'})
82 cmd = build_ext(dist)
83 old = sys.platform
84
85 sys.platform = 'sunos' # fooling finalize_options
86 from distutils.sysconfig import _config_vars
87 old_var = _config_vars.get('Py_ENABLE_SHARED')
88 _config_vars['Py_ENABLE_SHARED'] = 1
89 try:
90 cmd.ensure_finalized()
91 finally:
92 sys.platform = old
93 if old_var is None:
94 del _config_vars['Py_ENABLE_SHARED']
95 else:
96 _config_vars['Py_ENABLE_SHARED'] = old_var
97
98 # make sur we get some lobrary dirs under solaris
99 self.assert_(len(cmd.library_dirs) > 0)
100
Georg Brandlb533e262008-05-25 18:19:30 +0000101def test_suite():
Neil Schemenauerd8f63bb2009-02-06 21:42:05 +0000102 src = _get_source_filename()
103 if not os.path.exists(src):
Georg Brandlb533e262008-05-25 18:19:30 +0000104 if support.verbose:
Neil Schemenauerd8f63bb2009-02-06 21:42:05 +0000105 print('test_build_ext: Cannot find source code (test'
106 ' must run in python build dir)')
Georg Brandlb533e262008-05-25 18:19:30 +0000107 return unittest.TestSuite()
108 else: return unittest.makeSuite(BuildExtTestCase)
109
110if __name__ == '__main__':
111 support.run_unittest(test_suite())