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