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