Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 1 | import sys |
| 2 | import os |
| 3 | import tempfile |
| 4 | import shutil |
| 5 | from io import StringIO |
| 6 | |
| 7 | from distutils.core import Extension, Distribution |
| 8 | from distutils.command.build_ext import build_ext |
| 9 | from distutils import sysconfig |
| 10 | |
| 11 | import unittest |
| 12 | from test import support |
| 13 | |
Christian Heimes | 3e7e069 | 2008-11-25 21:21:32 +0000 | [diff] [blame^] | 14 | # http://bugs.python.org/issue4373 |
| 15 | # Don't load the xx module more than once. |
| 16 | ALREADY_TESTED = False |
| 17 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 18 | class BuildExtTestCase(unittest.TestCase): |
| 19 | def setUp(self): |
| 20 | # Create a simple test environment |
| 21 | # Note that we're making changes to sys.path |
| 22 | self.tmp_dir = tempfile.mkdtemp(prefix="pythontest_") |
| 23 | self.sys_path = sys.path[:] |
| 24 | sys.path.append(self.tmp_dir) |
| 25 | |
| 26 | xx_c = os.path.join(sysconfig.project_base, 'Modules', 'xxmodule.c') |
| 27 | shutil.copy(xx_c, self.tmp_dir) |
| 28 | |
| 29 | def test_build_ext(self): |
Christian Heimes | 3e7e069 | 2008-11-25 21:21:32 +0000 | [diff] [blame^] | 30 | global ALREADY_TESTED |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 31 | xx_c = os.path.join(self.tmp_dir, 'xxmodule.c') |
| 32 | xx_ext = Extension('xx', [xx_c]) |
| 33 | dist = Distribution({'name': 'xx', 'ext_modules': [xx_ext]}) |
| 34 | dist.package_dir = self.tmp_dir |
| 35 | cmd = build_ext(dist) |
Thomas Heller | 84b7f0c | 2008-05-26 11:51:44 +0000 | [diff] [blame] | 36 | if os.name == "nt": |
| 37 | # On Windows, we must build a debug version iff running |
| 38 | # a debug build of Python |
| 39 | cmd.debug = sys.executable.endswith("_d.exe") |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 40 | cmd.build_lib = self.tmp_dir |
| 41 | cmd.build_temp = self.tmp_dir |
| 42 | |
| 43 | old_stdout = sys.stdout |
| 44 | if not support.verbose: |
| 45 | # silence compiler output |
| 46 | sys.stdout = StringIO() |
| 47 | try: |
| 48 | cmd.ensure_finalized() |
| 49 | cmd.run() |
| 50 | finally: |
| 51 | sys.stdout = old_stdout |
| 52 | |
Christian Heimes | 3e7e069 | 2008-11-25 21:21:32 +0000 | [diff] [blame^] | 53 | if ALREADY_TESTED: |
| 54 | return |
| 55 | else: |
| 56 | ALREADY_TESTED = True |
| 57 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 58 | import xx |
| 59 | |
| 60 | for attr in ('error', 'foo', 'new', 'roj'): |
| 61 | self.assert_(hasattr(xx, attr)) |
| 62 | |
| 63 | self.assertEquals(xx.foo(2, 5), 7) |
| 64 | self.assertEquals(xx.foo(13,15), 28) |
| 65 | self.assertEquals(xx.new().demo(), None) |
| 66 | doc = 'This is a template module just for instruction.' |
| 67 | self.assertEquals(xx.__doc__, doc) |
| 68 | self.assert_(isinstance(xx.Null(), xx.Null)) |
| 69 | self.assert_(isinstance(xx.Str(), xx.Str)) |
| 70 | |
| 71 | def tearDown(self): |
| 72 | # Get everything back to normal |
| 73 | support.unload('xx') |
| 74 | sys.path = self.sys_path |
Hirokazu Yamamoto | 956ffd7 | 2008-09-21 20:52:42 +0000 | [diff] [blame] | 75 | # XXX on Windows the test leaves a directory with xx module in TEMP |
| 76 | shutil.rmtree(self.tmp_dir, os.name == 'nt' or sys.platform == 'cygwin') |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 77 | |
| 78 | def test_suite(): |
| 79 | if not sysconfig.python_build: |
| 80 | if support.verbose: |
| 81 | print('test_build_ext: The test must be run in a python build dir') |
| 82 | return unittest.TestSuite() |
| 83 | else: return unittest.makeSuite(BuildExtTestCase) |
| 84 | |
| 85 | if __name__ == '__main__': |
| 86 | support.run_unittest(test_suite()) |