blob: d0dfca00d756f8f79a0cf9717df32a31ab1f0d6f [file] [log] [blame]
Tarek Ziadéc27aca72009-05-10 11:45:41 +00001"""Tests for distutils.command.install_data."""
R. David Murray378c0cf2010-02-24 01:46:21 +00002import sys
Tarek Ziadé36797272010-07-22 12:50:05 +00003import os
Éric Araujo47a45212011-10-08 00:34:13 +02004import imp
Tarek Ziadéc27aca72009-05-10 11:45:41 +00005import unittest
6
7from distutils.command.install_lib import install_lib
8from distutils.extension import Extension
9from distutils.tests import support
10from distutils.errors import DistutilsOptionError
Éric Araujo70ec44a2010-11-06 02:44:43 +000011from test.support import run_unittest
Tarek Ziadéc27aca72009-05-10 11:45:41 +000012
Éric Araujoc465b2f2011-11-03 03:45:33 +010013
Tarek Ziadéc27aca72009-05-10 11:45:41 +000014class InstallLibTestCase(support.TempdirManager,
15 support.LoggingSilencer,
Tarek Ziadé430fb632009-10-18 11:34:51 +000016 support.EnvironGuard,
Tarek Ziadéc27aca72009-05-10 11:45:41 +000017 unittest.TestCase):
18
Tarek Ziadéc27aca72009-05-10 11:45:41 +000019 def test_finalize_options(self):
Éric Araujoc465b2f2011-11-03 03:45:33 +010020 dist = self.create_dist()[1]
Tarek Ziadéc27aca72009-05-10 11:45:41 +000021 cmd = install_lib(dist)
22
23 cmd.finalize_options()
Ezio Melottib3aedd42010-11-20 19:04:17 +000024 self.assertEqual(cmd.compile, 1)
25 self.assertEqual(cmd.optimize, 0)
Tarek Ziadéc27aca72009-05-10 11:45:41 +000026
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()
Ezio Melottib3aedd42010-11-20 19:04:17 +000035 self.assertEqual(cmd.optimize, 2)
Tarek Ziadéc27aca72009-05-10 11:45:41 +000036
Éric Araujo47a45212011-10-08 00:34:13 +020037 @unittest.skipIf(sys.dont_write_bytecode, 'byte-compile disabled')
Tarek Ziadé36797272010-07-22 12:50:05 +000038 def test_byte_compile(self):
Éric Araujoc465b2f2011-11-03 03:45:33 +010039 project_dir, dist = self.create_dist()
40 os.chdir(project_dir)
Tarek Ziadéc27aca72009-05-10 11:45:41 +000041 cmd = install_lib(dist)
42 cmd.compile = cmd.optimize = 1
43
Éric Araujoc465b2f2011-11-03 03:45:33 +010044 f = os.path.join(project_dir, 'foo.py')
Tarek Ziadéc27aca72009-05-10 11:45:41 +000045 self.write_file(f, '# python file')
46 cmd.byte_compile([f])
Éric Araujoc465b2f2011-11-03 03:45:33 +010047 pyc_file = imp.cache_from_source('foo.py', debug_override=True)
Éric Araujo47a45212011-10-08 00:34:13 +020048 pyo_file = imp.cache_from_source('foo.py', debug_override=False)
49 self.assertTrue(os.path.exists(pyc_file))
50 self.assertTrue(os.path.exists(pyo_file))
Tarek Ziadéc27aca72009-05-10 11:45:41 +000051
52 def test_get_outputs(self):
Éric Araujoc465b2f2011-11-03 03:45:33 +010053 project_dir, dist = self.create_dist()
54 os.chdir(project_dir)
55 os.mkdir('spam')
Tarek Ziadéc27aca72009-05-10 11:45:41 +000056 cmd = install_lib(dist)
57
58 # setting up a dist environment
59 cmd.compile = cmd.optimize = 1
Éric Araujoc465b2f2011-11-03 03:45:33 +010060 cmd.install_dir = self.mkdtemp()
61 f = os.path.join(project_dir, 'spam', '__init__.py')
62 self.write_file(f, '# python package')
Tarek Ziadéc27aca72009-05-10 11:45:41 +000063 cmd.distribution.ext_modules = [Extension('foo', ['xxx'])]
Éric Araujoc465b2f2011-11-03 03:45:33 +010064 cmd.distribution.packages = ['spam']
Tarek Ziadéc27aca72009-05-10 11:45:41 +000065 cmd.distribution.script_name = 'setup.py'
66
Éric Araujoc465b2f2011-11-03 03:45:33 +010067 # get_outputs should return 4 elements: spam/__init__.py, .pyc and
68 # .pyo, foo.import-tag-abiflags.so / foo.pyd
69 outputs = cmd.get_outputs()
70 self.assertEqual(len(outputs), 4, outputs)
Tarek Ziadéc27aca72009-05-10 11:45:41 +000071
72 def test_get_inputs(self):
Éric Araujoc465b2f2011-11-03 03:45:33 +010073 project_dir, dist = self.create_dist()
74 os.chdir(project_dir)
75 os.mkdir('spam')
Tarek Ziadéc27aca72009-05-10 11:45:41 +000076 cmd = install_lib(dist)
77
78 # setting up a dist environment
79 cmd.compile = cmd.optimize = 1
Éric Araujoc465b2f2011-11-03 03:45:33 +010080 cmd.install_dir = self.mkdtemp()
81 f = os.path.join(project_dir, 'spam', '__init__.py')
82 self.write_file(f, '# python package')
Tarek Ziadéc27aca72009-05-10 11:45:41 +000083 cmd.distribution.ext_modules = [Extension('foo', ['xxx'])]
Éric Araujoc465b2f2011-11-03 03:45:33 +010084 cmd.distribution.packages = ['spam']
Tarek Ziadéc27aca72009-05-10 11:45:41 +000085 cmd.distribution.script_name = 'setup.py'
86
Éric Araujoc465b2f2011-11-03 03:45:33 +010087 # get_inputs should return 2 elements: spam/__init__.py and
88 # foo.import-tag-abiflags.so / foo.pyd
89 inputs = cmd.get_inputs()
90 self.assertEqual(len(inputs), 2, inputs)
Tarek Ziadéc27aca72009-05-10 11:45:41 +000091
Tarek Ziadé04fe7c02009-10-25 23:08:47 +000092 def test_dont_write_bytecode(self):
93 # makes sure byte_compile is not used
Éric Araujoc465b2f2011-11-03 03:45:33 +010094 dist = self.create_dist()[1]
Tarek Ziadé04fe7c02009-10-25 23:08:47 +000095 cmd = install_lib(dist)
96 cmd.compile = 1
97 cmd.optimize = 1
98
99 old_dont_write_bytecode = sys.dont_write_bytecode
100 sys.dont_write_bytecode = True
101 try:
102 cmd.byte_compile([])
103 finally:
104 sys.dont_write_bytecode = old_dont_write_bytecode
105
Serhiy Storchaka39989152013-11-17 00:17:46 +0200106 self.assertIn('byte-compiling is disabled', self.logs[0][1])
Tarek Ziadéc27aca72009-05-10 11:45:41 +0000107
Éric Araujoc465b2f2011-11-03 03:45:33 +0100108
Tarek Ziadéc27aca72009-05-10 11:45:41 +0000109def test_suite():
110 return unittest.makeSuite(InstallLibTestCase)
111
112if __name__ == "__main__":
Éric Araujo70ec44a2010-11-06 02:44:43 +0000113 run_unittest(test_suite())