diff options
author | Barry Warsaw <barry@python.org> | 2010-04-17 00:19:56 (GMT) |
---|---|---|
committer | Barry Warsaw <barry@python.org> | 2010-04-17 00:19:56 (GMT) |
commit | 28a691b7fdde1b8abafa4c4a5025e6bfa44f48b9 (patch) | |
tree | ca0098063694e0f91d1bcd785d0044e96e1bf389 /Lib/test/test_compileall.py | |
parent | 0e59cc3fc347582d8625050de258a2dd6b87f978 (diff) | |
download | cpython-28a691b7fdde1b8abafa4c4a5025e6bfa44f48b9.zip cpython-28a691b7fdde1b8abafa4c4a5025e6bfa44f48b9.tar.gz cpython-28a691b7fdde1b8abafa4c4a5025e6bfa44f48b9.tar.bz2 |
PEP 3147
Diffstat (limited to 'Lib/test/test_compileall.py')
-rw-r--r-- | Lib/test/test_compileall.py | 79 |
1 files changed, 69 insertions, 10 deletions
diff --git a/Lib/test/test_compileall.py b/Lib/test/test_compileall.py index 4b6feba..8b34587 100644 --- a/Lib/test/test_compileall.py +++ b/Lib/test/test_compileall.py @@ -5,22 +5,23 @@ import os import py_compile import shutil import struct +import subprocess import tempfile -from test import support import unittest import io +from test import support class CompileallTests(unittest.TestCase): def setUp(self): self.directory = tempfile.mkdtemp() self.source_path = os.path.join(self.directory, '_test.py') - self.bc_path = self.source_path + ('c' if __debug__ else 'o') + self.bc_path = imp.cache_from_source(self.source_path) with open(self.source_path, 'w') as file: file.write('x = 123\n') self.source_path2 = os.path.join(self.directory, '_test2.py') - self.bc_path2 = self.source_path2 + ('c' if __debug__ else 'o') + self.bc_path2 = imp.cache_from_source(self.source_path2) shutil.copyfile(self.source_path, self.source_path2) def tearDown(self): @@ -65,17 +66,19 @@ class CompileallTests(unittest.TestCase): except: pass compileall.compile_file(self.source_path, force=False, quiet=True) - self.assertTrue(os.path.isfile(self.bc_path) \ - and not os.path.isfile(self.bc_path2)) + self.assertTrue(os.path.isfile(self.bc_path) and + not os.path.isfile(self.bc_path2)) os.unlink(self.bc_path) compileall.compile_dir(self.directory, force=False, quiet=True) - self.assertTrue(os.path.isfile(self.bc_path) \ - and os.path.isfile(self.bc_path2)) + self.assertTrue(os.path.isfile(self.bc_path) and + os.path.isfile(self.bc_path2)) os.unlink(self.bc_path) os.unlink(self.bc_path2) + class EncodingTest(unittest.TestCase): - 'Issue 6716: compileall should escape source code when printing errors to stdout.' + """Issue 6716: compileall should escape source code when printing errors + to stdout.""" def setUp(self): self.directory = tempfile.mkdtemp() @@ -95,9 +98,65 @@ class EncodingTest(unittest.TestCase): finally: sys.stdout = orig_stdout +class CommandLineTests(unittest.TestCase): + """Test some aspects of compileall's CLI.""" + + def setUp(self): + self.addCleanup(self._cleanup) + self.directory = tempfile.mkdtemp() + self.pkgdir = os.path.join(self.directory, 'foo') + os.mkdir(self.pkgdir) + # Touch the __init__.py and a package module. + with open(os.path.join(self.pkgdir, '__init__.py'), 'w'): + pass + with open(os.path.join(self.pkgdir, 'bar.py'), 'w'): + pass + sys.path.insert(0, self.directory) + + def _cleanup(self): + support.rmtree(self.directory) + assert sys.path[0] == self.directory, 'Missing path' + del sys.path[0] + + def test_pep3147_paths(self): + # Ensure that the default behavior of compileall's CLI is to create + # PEP 3147 pyc/pyo files. + retcode = subprocess.call( + (sys.executable, '-m', 'compileall', '-q', self.pkgdir)) + self.assertEqual(retcode, 0) + # Verify the __pycache__ directory contents. + cachedir = os.path.join(self.pkgdir, '__pycache__') + self.assertTrue(os.path.exists(cachedir)) + ext = ('pyc' if __debug__ else 'pyo') + expected = sorted(base.format(imp.get_tag(), ext) for base in + ('__init__.{}.{}', 'bar.{}.{}')) + self.assertEqual(sorted(os.listdir(cachedir)), expected) + # Make sure there are no .pyc files in the source directory. + self.assertFalse([pyc_file for pyc_file in os.listdir(self.pkgdir) + if pyc_file.endswith(ext)]) + + def test_legacy_paths(self): + # Ensure that with the proper switch, compileall leaves legacy + # pyc/pyo files, and no __pycache__ directory. + retcode = subprocess.call( + (sys.executable, '-m', 'compileall', '-b', '-q', self.pkgdir)) + self.assertEqual(retcode, 0) + # Verify the __pycache__ directory contents. + cachedir = os.path.join(self.pkgdir, '__pycache__') + self.assertFalse(os.path.exists(cachedir)) + ext = ('pyc' if __debug__ else 'pyo') + expected = [base.format(ext) for base in ('__init__.{}', 'bar.{}')] + expected.extend(['__init__.py', 'bar.py']) + expected.sort() + self.assertEqual(sorted(os.listdir(self.pkgdir)), expected) + + def test_main(): - support.run_unittest(CompileallTests, - EncodingTest) + support.run_unittest( + CommandLineTests, + CompileallTests, + EncodingTest, + ) if __name__ == "__main__": |