| 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 requires_subprocess |
| 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 |
|
| 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 |
@requires_subprocess() |
| 39 |
def test_byte_compile(self): |
| 40 |
project_dir, dist = self.create_dist() |
| 41 |
os.chdir(project_dir) |
| 42 |
cmd = install_lib(dist) |
| 43 |
cmd.compile = cmd.optimize = 1 |
| 44 |
|
| 45 |
f = os.path.join(project_dir, 'foo.py') |
| 46 |
self.write_file(f, '# python file') |
| 47 |
cmd.byte_compile([f]) |
| 48 |
pyc_file = importlib.util.cache_from_source('foo.py', optimization='') |
| 49 |
pyc_opt_file = importlib.util.cache_from_source('foo.py', |
| 50 |
optimization=cmd.optimize) |
| 51 |
self.assertTrue(os.path.exists(pyc_file)) |
| 52 |
self.assertTrue(os.path.exists(pyc_opt_file)) |
| 53 |
|
| 54 |
def test_get_outputs(self): |
| 55 |
project_dir, dist = self.create_dist() |
| 56 |
os.chdir(project_dir) |
| 57 |
os.mkdir('spam') |
| 58 |
cmd = install_lib(dist) |
| 59 |
|
| 60 |
|
| 61 |
cmd.compile = cmd.optimize = 1 |
| 62 |
cmd.install_dir = self.mkdtemp() |
| 63 |
f = os.path.join(project_dir, 'spam', '__init__.py') |
| 64 |
self.write_file(f, '# python package') |
| 65 |
cmd.distribution.ext_modules = [Extension('foo', ['xxx'])] |
| 66 |
cmd.distribution.packages = ['spam'] |
| 67 |
cmd.distribution.script_name = 'setup.py' |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
outputs = cmd.get_outputs() |
| 72 |
self.assertEqual(len(outputs), 4, outputs) |
| 73 |
|
| 74 |
def test_get_inputs(self): |
| 75 |
project_dir, dist = self.create_dist() |
| 76 |
os.chdir(project_dir) |
| 77 |
os.mkdir('spam') |
| 78 |
cmd = install_lib(dist) |
| 79 |
|
| 80 |
|
| 81 |
cmd.compile = cmd.optimize = 1 |
| 82 |
cmd.install_dir = self.mkdtemp() |
| 83 |
f = os.path.join(project_dir, 'spam', '__init__.py') |
| 84 |
self.write_file(f, '# python package') |
| 85 |
cmd.distribution.ext_modules = [Extension('foo', ['xxx'])] |
| 86 |
cmd.distribution.packages = ['spam'] |
| 87 |
cmd.distribution.script_name = 'setup.py' |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
inputs = cmd.get_inputs() |
| 92 |
self.assertEqual(len(inputs), 2, inputs) |
| 93 |
|
| 94 |
@requires_subprocess() |
| 95 |
def test_dont_write_bytecode(self): |
| 96 |
|
| 97 |
dist = self.create_dist()[1] |
| 98 |
cmd = install_lib(dist) |
| 99 |
cmd.compile = 1 |
| 100 |
cmd.optimize = 1 |
| 101 |
|
| 102 |
old_dont_write_bytecode = sys.dont_write_bytecode |
| 103 |
sys.dont_write_bytecode = True |
| 104 |
try: |
| 105 |
cmd.byte_compile([]) |
| 106 |
finally: |
| 107 |
sys.dont_write_bytecode = old_dont_write_bytecode |
| 108 |
|
| 109 |
self.assertIn('byte-compiling is disabled', |
| 110 |
self.logs[0][1] % self.logs[0][2]) |
| 111 |
|
| 112 |
|
| 113 |
if __name__ == "__main__": |
| 114 |
unittest.main() |