Tarek Ziadé | c27aca7 | 2009-05-10 11:45:41 +0000 | [diff] [blame] | 1 | """Tests for distutils.command.install_data.""" |
R. David Murray | 378c0cf | 2010-02-24 01:46:21 +0000 | [diff] [blame] | 2 | import sys |
Tarek Ziadé | 3679727 | 2010-07-22 12:50:05 +0000 | [diff] [blame] | 3 | import os |
Éric Araujo | 47a4521 | 2011-10-08 00:34:13 +0200 | [diff] [blame^] | 4 | import imp |
Tarek Ziadé | c27aca7 | 2009-05-10 11:45:41 +0000 | [diff] [blame] | 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 |
Éric Araujo | 70ec44a | 2010-11-06 02:44:43 +0000 | [diff] [blame] | 11 | from test.support import run_unittest |
Tarek Ziadé | c27aca7 | 2009-05-10 11:45:41 +0000 | [diff] [blame] | 12 | |
| 13 | class InstallLibTestCase(support.TempdirManager, |
| 14 | support.LoggingSilencer, |
Tarek Ziadé | 430fb63 | 2009-10-18 11:34:51 +0000 | [diff] [blame] | 15 | support.EnvironGuard, |
Tarek Ziadé | c27aca7 | 2009-05-10 11:45:41 +0000 | [diff] [blame] | 16 | unittest.TestCase): |
| 17 | |
Tarek Ziadé | c27aca7 | 2009-05-10 11:45:41 +0000 | [diff] [blame] | 18 | def test_finalize_options(self): |
| 19 | pkg_dir, dist = self.create_dist() |
| 20 | cmd = install_lib(dist) |
| 21 | |
| 22 | cmd.finalize_options() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 23 | self.assertEqual(cmd.compile, 1) |
| 24 | self.assertEqual(cmd.optimize, 0) |
Tarek Ziadé | c27aca7 | 2009-05-10 11:45:41 +0000 | [diff] [blame] | 25 | |
| 26 | # optimize must be 0, 1, or 2 |
| 27 | cmd.optimize = 'foo' |
| 28 | self.assertRaises(DistutilsOptionError, cmd.finalize_options) |
| 29 | cmd.optimize = '4' |
| 30 | self.assertRaises(DistutilsOptionError, cmd.finalize_options) |
| 31 | |
| 32 | cmd.optimize = '2' |
| 33 | cmd.finalize_options() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 34 | self.assertEqual(cmd.optimize, 2) |
Tarek Ziadé | c27aca7 | 2009-05-10 11:45:41 +0000 | [diff] [blame] | 35 | |
Éric Araujo | 47a4521 | 2011-10-08 00:34:13 +0200 | [diff] [blame^] | 36 | @unittest.skipIf(sys.dont_write_bytecode, 'byte-compile disabled') |
Tarek Ziadé | 3679727 | 2010-07-22 12:50:05 +0000 | [diff] [blame] | 37 | def test_byte_compile(self): |
Tarek Ziadé | c27aca7 | 2009-05-10 11:45:41 +0000 | [diff] [blame] | 38 | pkg_dir, dist = self.create_dist() |
Éric Araujo | 47a4521 | 2011-10-08 00:34:13 +0200 | [diff] [blame^] | 39 | os.chdir(pkg_dir) |
Tarek Ziadé | c27aca7 | 2009-05-10 11:45:41 +0000 | [diff] [blame] | 40 | cmd = install_lib(dist) |
| 41 | cmd.compile = cmd.optimize = 1 |
| 42 | |
| 43 | f = os.path.join(pkg_dir, 'foo.py') |
| 44 | self.write_file(f, '# python file') |
| 45 | cmd.byte_compile([f]) |
Éric Araujo | 47a4521 | 2011-10-08 00:34:13 +0200 | [diff] [blame^] | 46 | pyc_file = imp.cache_from_source('foo.py') |
| 47 | pyo_file = imp.cache_from_source('foo.py', debug_override=False) |
| 48 | self.assertTrue(os.path.exists(pyc_file)) |
| 49 | self.assertTrue(os.path.exists(pyo_file)) |
Tarek Ziadé | c27aca7 | 2009-05-10 11:45:41 +0000 | [diff] [blame] | 50 | |
| 51 | def test_get_outputs(self): |
| 52 | pkg_dir, dist = self.create_dist() |
| 53 | cmd = install_lib(dist) |
| 54 | |
| 55 | # setting up a dist environment |
| 56 | cmd.compile = cmd.optimize = 1 |
| 57 | cmd.install_dir = pkg_dir |
| 58 | f = os.path.join(pkg_dir, 'foo.py') |
| 59 | self.write_file(f, '# python file') |
| 60 | cmd.distribution.py_modules = [pkg_dir] |
| 61 | cmd.distribution.ext_modules = [Extension('foo', ['xxx'])] |
| 62 | cmd.distribution.packages = [pkg_dir] |
| 63 | cmd.distribution.script_name = 'setup.py' |
| 64 | |
| 65 | # get_output should return 4 elements |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 66 | self.assertTrue(len(cmd.get_outputs()) >= 2) |
Tarek Ziadé | c27aca7 | 2009-05-10 11:45:41 +0000 | [diff] [blame] | 67 | |
| 68 | def test_get_inputs(self): |
| 69 | pkg_dir, dist = self.create_dist() |
| 70 | cmd = install_lib(dist) |
| 71 | |
| 72 | # setting up a dist environment |
| 73 | cmd.compile = cmd.optimize = 1 |
| 74 | cmd.install_dir = pkg_dir |
| 75 | f = os.path.join(pkg_dir, 'foo.py') |
| 76 | self.write_file(f, '# python file') |
| 77 | cmd.distribution.py_modules = [pkg_dir] |
| 78 | cmd.distribution.ext_modules = [Extension('foo', ['xxx'])] |
| 79 | cmd.distribution.packages = [pkg_dir] |
| 80 | cmd.distribution.script_name = 'setup.py' |
| 81 | |
| 82 | # get_input should return 2 elements |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 83 | self.assertEqual(len(cmd.get_inputs()), 2) |
Tarek Ziadé | c27aca7 | 2009-05-10 11:45:41 +0000 | [diff] [blame] | 84 | |
Tarek Ziadé | 04fe7c0 | 2009-10-25 23:08:47 +0000 | [diff] [blame] | 85 | def test_dont_write_bytecode(self): |
| 86 | # makes sure byte_compile is not used |
| 87 | pkg_dir, dist = self.create_dist() |
| 88 | cmd = install_lib(dist) |
| 89 | cmd.compile = 1 |
| 90 | cmd.optimize = 1 |
| 91 | |
| 92 | old_dont_write_bytecode = sys.dont_write_bytecode |
| 93 | sys.dont_write_bytecode = True |
| 94 | try: |
| 95 | cmd.byte_compile([]) |
| 96 | finally: |
| 97 | sys.dont_write_bytecode = old_dont_write_bytecode |
| 98 | |
| 99 | self.assertTrue('byte-compiling is disabled' in self.logs[0][1]) |
Tarek Ziadé | c27aca7 | 2009-05-10 11:45:41 +0000 | [diff] [blame] | 100 | |
| 101 | def test_suite(): |
| 102 | return unittest.makeSuite(InstallLibTestCase) |
| 103 | |
| 104 | if __name__ == "__main__": |
Éric Araujo | 70ec44a | 2010-11-06 02:44:43 +0000 | [diff] [blame] | 105 | run_unittest(test_suite()) |