1 """Tests for distutils.command.install_data.""" 2 import os 3 import sys 4 import unittest 5 6 from distutils.command.install_lib import install_lib 7 from distutils.extension import Extension 8 from distutils.tests import support 9 from distutils.errors import DistutilsOptionError 10 from test.test_support import run_unittest 11 12 class InstallLibTestCase(support.TempdirManager, 13 support.LoggingSilencer, 14 support.EnvironGuard, 15 unittest.TestCase): 16 17 def test_finalize_options(self): 18 pkg_dir, dist = self.create_dist() 19 cmd = install_lib(dist) 20 21 cmd.finalize_options() 22 self.assertEqual(cmd.compile, 1) 23 self.assertEqual(cmd.optimize, 0) 24 25 # optimize must be 0, 1, or 2 26 cmd.optimize = 'foo' 27 self.assertRaises(DistutilsOptionError, cmd.finalize_options) 28 cmd.optimize = '4' 29 self.assertRaises(DistutilsOptionError, cmd.finalize_options) 30 31 cmd.optimize = '2' 32 cmd.finalize_options() 33 self.assertEqual(cmd.optimize, 2) 34 35 def _setup_byte_compile(self): 36 pkg_dir, dist = self.create_dist() 37 cmd = install_lib(dist) 38 cmd.compile = cmd.optimize = 1 39 40 f = os.path.join(pkg_dir, 'foo.py') 41 self.write_file(f, '# python file') 42 cmd.byte_compile([f]) 43 return pkg_dir 44 45 @unittest.skipIf(sys.dont_write_bytecode, 'byte-compile not enabled') 46 def test_byte_compile(self): 47 pkg_dir = self._setup_byte_compile() 48 if sys.flags.optimize < 1: 49 self.assertTrue(os.path.exists(os.path.join(pkg_dir, 'foo.pyc'))) 50 else: 51 self.assertTrue(os.path.exists(os.path.join(pkg_dir, 'foo.pyo'))) 52 53 def test_get_outputs(self): 54 pkg_dir, dist = self.create_dist() 55 cmd = install_lib(dist) 56 57 # setting up a dist environment 58 cmd.compile = cmd.optimize = 1 59 cmd.install_dir = pkg_dir 60 f = os.path.join(pkg_dir, 'foo.py') 61 self.write_file(f, '# python file') 62 cmd.distribution.py_modules = [pkg_dir] 63 cmd.distribution.ext_modules = [Extension('foo', ['xxx'])] 64 cmd.distribution.packages = [pkg_dir] 65 cmd.distribution.script_name = 'setup.py' 66 67 # get_output should return 4 elements 68 self.assertTrue(len(cmd.get_outputs()) >= 2) 69 70 def test_get_inputs(self): 71 pkg_dir, dist = self.create_dist() 72 cmd = install_lib(dist) 73 74 # setting up a dist environment 75 cmd.compile = cmd.optimize = 1 76 cmd.install_dir = pkg_dir 77 f = os.path.join(pkg_dir, 'foo.py') 78 self.write_file(f, '# python file') 79 cmd.distribution.py_modules = [pkg_dir] 80 cmd.distribution.ext_modules = [Extension('foo', ['xxx'])] 81 cmd.distribution.packages = [pkg_dir] 82 cmd.distribution.script_name = 'setup.py' 83 84 # get_input should return 2 elements 85 self.assertEqual(len(cmd.get_inputs()), 2) 86 87 def test_dont_write_bytecode(self): 88 # makes sure byte_compile is not used 89 pkg_dir, dist = self.create_dist() 90 cmd = install_lib(dist) 91 cmd.compile = 1 92 cmd.optimize = 1 93 94 old_dont_write_bytecode = sys.dont_write_bytecode 95 sys.dont_write_bytecode = True 96 try: 97 cmd.byte_compile([]) 98 finally: 99 sys.dont_write_bytecode = old_dont_write_bytecode 100 101 self.assertTrue('byte-compiling is disabled' in self.logs[0][1]) 102 103 def test_suite(): 104 return unittest.makeSuite(InstallLibTestCase) 105 106 if __name__ == "__main__": 107 run_unittest(test_suite()) 108