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 | |
| 14 | class BuildExtTestCase(unittest.TestCase): |
| 15 | def setUp(self): |
| 16 | # Create a simple test environment |
| 17 | # Note that we're making changes to sys.path |
| 18 | self.tmp_dir = tempfile.mkdtemp(prefix="pythontest_") |
| 19 | self.sys_path = sys.path[:] |
| 20 | sys.path.append(self.tmp_dir) |
| 21 | |
| 22 | xx_c = os.path.join(sysconfig.project_base, 'Modules', 'xxmodule.c') |
| 23 | shutil.copy(xx_c, self.tmp_dir) |
| 24 | |
| 25 | def test_build_ext(self): |
| 26 | xx_c = os.path.join(self.tmp_dir, 'xxmodule.c') |
| 27 | xx_ext = Extension('xx', [xx_c]) |
| 28 | dist = Distribution({'name': 'xx', 'ext_modules': [xx_ext]}) |
| 29 | dist.package_dir = self.tmp_dir |
| 30 | cmd = build_ext(dist) |
| 31 | cmd.build_lib = self.tmp_dir |
| 32 | cmd.build_temp = self.tmp_dir |
| 33 | |
| 34 | old_stdout = sys.stdout |
| 35 | if not support.verbose: |
| 36 | # silence compiler output |
| 37 | sys.stdout = StringIO() |
| 38 | try: |
| 39 | cmd.ensure_finalized() |
| 40 | cmd.run() |
| 41 | finally: |
| 42 | sys.stdout = old_stdout |
| 43 | |
| 44 | import xx |
| 45 | |
| 46 | for attr in ('error', 'foo', 'new', 'roj'): |
| 47 | self.assert_(hasattr(xx, attr)) |
| 48 | |
| 49 | self.assertEquals(xx.foo(2, 5), 7) |
| 50 | self.assertEquals(xx.foo(13,15), 28) |
| 51 | self.assertEquals(xx.new().demo(), None) |
| 52 | doc = 'This is a template module just for instruction.' |
| 53 | self.assertEquals(xx.__doc__, doc) |
| 54 | self.assert_(isinstance(xx.Null(), xx.Null)) |
| 55 | self.assert_(isinstance(xx.Str(), xx.Str)) |
| 56 | |
| 57 | def tearDown(self): |
| 58 | # Get everything back to normal |
| 59 | support.unload('xx') |
| 60 | sys.path = self.sys_path |
| 61 | # XXX on Windows the test leaves a directory with xx.pyd in TEMP |
| 62 | shutil.rmtree(self.tmp_dir, False if os.name != "nt" else True) |
| 63 | |
| 64 | def test_suite(): |
| 65 | if not sysconfig.python_build: |
| 66 | if support.verbose: |
| 67 | print('test_build_ext: The test must be run in a python build dir') |
| 68 | return unittest.TestSuite() |
| 69 | else: return unittest.makeSuite(BuildExtTestCase) |
| 70 | |
| 71 | if __name__ == '__main__': |
| 72 | support.run_unittest(test_suite()) |