2009-05-10 08:45:41 -03:00
|
|
|
"""Tests for distutils.command.install_data."""
|
2010-02-23 21:46:21 -04:00
|
|
|
import sys
|
2010-07-22 09:50:05 -03:00
|
|
|
import os
|
2013-06-15 13:59:53 -03:00
|
|
|
import importlib.util
|
2009-05-10 08:45:41 -03:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
from distutils.command.install_lib import install_lib
|
|
|
|
from distutils.extension import Extension
|
|
|
|
from distutils.tests import support
|
|
|
|
from distutils.errors import DistutilsOptionError
|
2010-11-05 23:44:43 -03:00
|
|
|
from test.support import run_unittest
|
2009-05-10 08:45:41 -03:00
|
|
|
|
2011-11-02 23:45:33 -03:00
|
|
|
|
2009-05-10 08:45:41 -03:00
|
|
|
class InstallLibTestCase(support.TempdirManager,
|
|
|
|
support.LoggingSilencer,
|
2009-10-18 08:34:51 -03:00
|
|
|
support.EnvironGuard,
|
2009-05-10 08:45:41 -03:00
|
|
|
unittest.TestCase):
|
|
|
|
|
|
|
|
def test_finalize_options(self):
|
2011-11-02 23:45:33 -03:00
|
|
|
dist = self.create_dist()[1]
|
2009-05-10 08:45:41 -03:00
|
|
|
cmd = install_lib(dist)
|
|
|
|
|
|
|
|
cmd.finalize_options()
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(cmd.compile, 1)
|
|
|
|
self.assertEqual(cmd.optimize, 0)
|
2009-05-10 08:45:41 -03:00
|
|
|
|
|
|
|
# optimize must be 0, 1, or 2
|
|
|
|
cmd.optimize = 'foo'
|
|
|
|
self.assertRaises(DistutilsOptionError, cmd.finalize_options)
|
|
|
|
cmd.optimize = '4'
|
|
|
|
self.assertRaises(DistutilsOptionError, cmd.finalize_options)
|
|
|
|
|
|
|
|
cmd.optimize = '2'
|
|
|
|
cmd.finalize_options()
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(cmd.optimize, 2)
|
2009-05-10 08:45:41 -03:00
|
|
|
|
2011-10-07 19:34:13 -03:00
|
|
|
@unittest.skipIf(sys.dont_write_bytecode, 'byte-compile disabled')
|
2010-07-22 09:50:05 -03:00
|
|
|
def test_byte_compile(self):
|
2011-11-02 23:45:33 -03:00
|
|
|
project_dir, dist = self.create_dist()
|
|
|
|
os.chdir(project_dir)
|
2009-05-10 08:45:41 -03:00
|
|
|
cmd = install_lib(dist)
|
|
|
|
cmd.compile = cmd.optimize = 1
|
|
|
|
|
2011-11-02 23:45:33 -03:00
|
|
|
f = os.path.join(project_dir, 'foo.py')
|
2009-05-10 08:45:41 -03:00
|
|
|
self.write_file(f, '# python file')
|
|
|
|
cmd.byte_compile([f])
|
2015-04-13 15:21:02 -03:00
|
|
|
pyc_file = importlib.util.cache_from_source('foo.py', optimization='')
|
|
|
|
pyc_opt_file = importlib.util.cache_from_source('foo.py',
|
|
|
|
optimization=cmd.optimize)
|
2011-10-07 19:34:13 -03:00
|
|
|
self.assertTrue(os.path.exists(pyc_file))
|
2015-04-13 15:21:02 -03:00
|
|
|
self.assertTrue(os.path.exists(pyc_opt_file))
|
2009-05-10 08:45:41 -03:00
|
|
|
|
|
|
|
def test_get_outputs(self):
|
2011-11-02 23:45:33 -03:00
|
|
|
project_dir, dist = self.create_dist()
|
|
|
|
os.chdir(project_dir)
|
|
|
|
os.mkdir('spam')
|
2009-05-10 08:45:41 -03:00
|
|
|
cmd = install_lib(dist)
|
|
|
|
|
|
|
|
# setting up a dist environment
|
|
|
|
cmd.compile = cmd.optimize = 1
|
2011-11-02 23:45:33 -03:00
|
|
|
cmd.install_dir = self.mkdtemp()
|
|
|
|
f = os.path.join(project_dir, 'spam', '__init__.py')
|
|
|
|
self.write_file(f, '# python package')
|
2009-05-10 08:45:41 -03:00
|
|
|
cmd.distribution.ext_modules = [Extension('foo', ['xxx'])]
|
2011-11-02 23:45:33 -03:00
|
|
|
cmd.distribution.packages = ['spam']
|
2009-05-10 08:45:41 -03:00
|
|
|
cmd.distribution.script_name = 'setup.py'
|
|
|
|
|
2015-04-13 15:21:02 -03:00
|
|
|
# get_outputs should return 4 elements: spam/__init__.py and .pyc,
|
|
|
|
# foo.import-tag-abiflags.so / foo.pyd
|
2011-11-02 23:45:33 -03:00
|
|
|
outputs = cmd.get_outputs()
|
|
|
|
self.assertEqual(len(outputs), 4, outputs)
|
2009-05-10 08:45:41 -03:00
|
|
|
|
|
|
|
def test_get_inputs(self):
|
2011-11-02 23:45:33 -03:00
|
|
|
project_dir, dist = self.create_dist()
|
|
|
|
os.chdir(project_dir)
|
|
|
|
os.mkdir('spam')
|
2009-05-10 08:45:41 -03:00
|
|
|
cmd = install_lib(dist)
|
|
|
|
|
|
|
|
# setting up a dist environment
|
|
|
|
cmd.compile = cmd.optimize = 1
|
2011-11-02 23:45:33 -03:00
|
|
|
cmd.install_dir = self.mkdtemp()
|
|
|
|
f = os.path.join(project_dir, 'spam', '__init__.py')
|
|
|
|
self.write_file(f, '# python package')
|
2009-05-10 08:45:41 -03:00
|
|
|
cmd.distribution.ext_modules = [Extension('foo', ['xxx'])]
|
2011-11-02 23:45:33 -03:00
|
|
|
cmd.distribution.packages = ['spam']
|
2009-05-10 08:45:41 -03:00
|
|
|
cmd.distribution.script_name = 'setup.py'
|
|
|
|
|
2011-11-02 23:45:33 -03:00
|
|
|
# get_inputs should return 2 elements: spam/__init__.py and
|
|
|
|
# foo.import-tag-abiflags.so / foo.pyd
|
|
|
|
inputs = cmd.get_inputs()
|
|
|
|
self.assertEqual(len(inputs), 2, inputs)
|
2009-05-10 08:45:41 -03:00
|
|
|
|
2009-10-25 20:08:47 -03:00
|
|
|
def test_dont_write_bytecode(self):
|
|
|
|
# makes sure byte_compile is not used
|
2011-11-02 23:45:33 -03:00
|
|
|
dist = self.create_dist()[1]
|
2009-10-25 20:08:47 -03:00
|
|
|
cmd = install_lib(dist)
|
|
|
|
cmd.compile = 1
|
|
|
|
cmd.optimize = 1
|
|
|
|
|
|
|
|
old_dont_write_bytecode = sys.dont_write_bytecode
|
|
|
|
sys.dont_write_bytecode = True
|
|
|
|
try:
|
|
|
|
cmd.byte_compile([])
|
|
|
|
finally:
|
|
|
|
sys.dont_write_bytecode = old_dont_write_bytecode
|
|
|
|
|
2016-08-31 12:39:35 -03:00
|
|
|
self.assertIn('byte-compiling is disabled',
|
|
|
|
self.logs[0][1] % self.logs[0][2])
|
2009-05-10 08:45:41 -03:00
|
|
|
|
2011-11-02 23:45:33 -03:00
|
|
|
|
2009-05-10 08:45:41 -03:00
|
|
|
def test_suite():
|
|
|
|
return unittest.makeSuite(InstallLibTestCase)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2010-11-05 23:44:43 -03:00
|
|
|
run_unittest(test_suite())
|