Martin v. Löwis | 4b00307 | 2010-03-16 13:19:21 +0000 | [diff] [blame] | 1 | import sys |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 2 | import compileall |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 3 | import importlib.util |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 4 | import test.test_importlib.util |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 5 | import os |
Brett Cannon | 65ed750 | 2015-10-09 15:09:43 -0700 | [diff] [blame] | 6 | import pathlib |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 7 | import py_compile |
| 8 | import shutil |
| 9 | import struct |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 10 | import tempfile |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 11 | import time |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 12 | import unittest |
Martin v. Löwis | 4b00307 | 2010-03-16 13:19:21 +0000 | [diff] [blame] | 13 | import io |
Petr Viktorin | 4267c98 | 2019-09-26 11:53:51 +0200 | [diff] [blame] | 14 | import errno |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 15 | |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 16 | from unittest import mock, skipUnless |
| 17 | try: |
| 18 | from concurrent.futures import ProcessPoolExecutor |
| 19 | _have_multiprocessing = True |
| 20 | except ImportError: |
| 21 | _have_multiprocessing = False |
| 22 | |
Berker Peksag | ce64391 | 2015-05-06 06:33:17 +0300 | [diff] [blame] | 23 | from test import support |
| 24 | from test.support import script_helper |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 25 | |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 26 | from .test_py_compile import without_source_date_epoch |
| 27 | from .test_py_compile import SourceDateEpochTestMeta |
| 28 | |
| 29 | |
| 30 | class CompileallTestsBase: |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 31 | |
| 32 | def setUp(self): |
| 33 | self.directory = tempfile.mkdtemp() |
| 34 | self.source_path = os.path.join(self.directory, '_test.py') |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 35 | self.bc_path = importlib.util.cache_from_source(self.source_path) |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 36 | with open(self.source_path, 'w') as file: |
| 37 | file.write('x = 123\n') |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 38 | self.source_path2 = os.path.join(self.directory, '_test2.py') |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 39 | self.bc_path2 = importlib.util.cache_from_source(self.source_path2) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 40 | shutil.copyfile(self.source_path, self.source_path2) |
Georg Brandl | 4543846 | 2011-02-07 12:36:54 +0000 | [diff] [blame] | 41 | self.subdirectory = os.path.join(self.directory, '_subdir') |
| 42 | os.mkdir(self.subdirectory) |
| 43 | self.source_path3 = os.path.join(self.subdirectory, '_test3.py') |
| 44 | shutil.copyfile(self.source_path, self.source_path3) |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 45 | |
| 46 | def tearDown(self): |
| 47 | shutil.rmtree(self.directory) |
| 48 | |
Petr Viktorin | 4267c98 | 2019-09-26 11:53:51 +0200 | [diff] [blame] | 49 | def create_long_path(self): |
| 50 | long_path = os.path.join(self.directory, "long") |
| 51 | |
| 52 | # Create a long path, 10 directories at a time. |
| 53 | # It will be 100 directories deep, or shorter if the OS limits it. |
| 54 | for i in range(10): |
| 55 | longer_path = os.path.join( |
Petr Viktorin | 3d984a1 | 2019-09-26 15:16:32 +0200 | [diff] [blame^] | 56 | long_path, *(f"dir_{i}_{j}" for j in range(10)) |
Petr Viktorin | 4267c98 | 2019-09-26 11:53:51 +0200 | [diff] [blame] | 57 | ) |
| 58 | |
| 59 | # Check if we can open __pycache__/*.pyc. |
| 60 | # Also, put in the source file that we want to compile |
| 61 | longer_source = os.path.join(longer_path, '_test_long.py') |
| 62 | longer_cache = importlib.util.cache_from_source(longer_source) |
| 63 | try: |
| 64 | os.makedirs(longer_path) |
| 65 | shutil.copyfile(self.source_path, longer_source) |
| 66 | os.makedirs(os.path.dirname(longer_cache)) |
| 67 | # Make sure we can write to the cache |
| 68 | with open(longer_cache, 'w'): |
| 69 | pass |
| 70 | except FileNotFoundError: |
| 71 | # On Windows, a FileNotFoundError("The filename or extension |
| 72 | # is too long") is raised for long paths |
| 73 | if sys.platform == "win32": |
| 74 | break |
| 75 | else: |
| 76 | raise |
| 77 | except OSError as exc: |
| 78 | if exc.errno == errno.ENAMETOOLONG: |
| 79 | break |
| 80 | else: |
| 81 | raise |
| 82 | |
| 83 | # Remove the __pycache__ |
| 84 | shutil.rmtree(os.path.dirname(longer_cache)) |
| 85 | |
| 86 | long_path = longer_path |
| 87 | long_source = longer_source |
| 88 | long_cache = longer_cache |
| 89 | |
Petr Viktorin | 3d984a1 | 2019-09-26 15:16:32 +0200 | [diff] [blame^] | 90 | # On Windows, MAX_PATH is 260 characters, our path with the 20 |
| 91 | # directories is 160 characters long, leaving something for the |
| 92 | # root (self.directory) as well. |
| 93 | # Tests assume long_path contains at least 10 directories. |
Petr Viktorin | 4267c98 | 2019-09-26 11:53:51 +0200 | [diff] [blame] | 94 | if i < 2: |
Petr Viktorin | 3d984a1 | 2019-09-26 15:16:32 +0200 | [diff] [blame^] | 95 | raise ValueError(f'"Long path" is too short: {long_path}') |
Petr Viktorin | 4267c98 | 2019-09-26 11:53:51 +0200 | [diff] [blame] | 96 | |
| 97 | self.source_path_long = long_source |
| 98 | self.bc_path_long = long_cache |
| 99 | |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 100 | def add_bad_source_file(self): |
| 101 | self.bad_source_path = os.path.join(self.directory, '_test_bad.py') |
| 102 | with open(self.bad_source_path, 'w') as file: |
| 103 | file.write('x (\n') |
| 104 | |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 105 | def timestamp_metadata(self): |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 106 | with open(self.bc_path, 'rb') as file: |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 107 | data = file.read(12) |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 108 | mtime = int(os.stat(self.source_path).st_mtime) |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 109 | compare = struct.pack('<4sll', importlib.util.MAGIC_NUMBER, 0, mtime) |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 110 | return data, compare |
| 111 | |
| 112 | def recreation_check(self, metadata): |
| 113 | """Check that compileall recreates bytecode when the new metadata is |
| 114 | used.""" |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 115 | if os.environ.get('SOURCE_DATE_EPOCH'): |
| 116 | raise unittest.SkipTest('SOURCE_DATE_EPOCH is set') |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 117 | py_compile.compile(self.source_path) |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 118 | self.assertEqual(*self.timestamp_metadata()) |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 119 | with open(self.bc_path, 'rb') as file: |
| 120 | bc = file.read()[len(metadata):] |
| 121 | with open(self.bc_path, 'wb') as file: |
| 122 | file.write(metadata) |
| 123 | file.write(bc) |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 124 | self.assertNotEqual(*self.timestamp_metadata()) |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 125 | compileall.compile_dir(self.directory, force=False, quiet=True) |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 126 | self.assertTrue(*self.timestamp_metadata()) |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 127 | |
| 128 | def test_mtime(self): |
| 129 | # Test a change in mtime leads to a new .pyc. |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 130 | self.recreation_check(struct.pack('<4sll', importlib.util.MAGIC_NUMBER, |
| 131 | 0, 1)) |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 132 | |
| 133 | def test_magic_number(self): |
| 134 | # Test a change in mtime leads to a new .pyc. |
| 135 | self.recreation_check(b'\0\0\0\0') |
| 136 | |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 137 | def test_compile_files(self): |
| 138 | # Test compiling a single file, and complete directory |
| 139 | for fn in (self.bc_path, self.bc_path2): |
| 140 | try: |
| 141 | os.unlink(fn) |
| 142 | except: |
| 143 | pass |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 144 | self.assertTrue(compileall.compile_file(self.source_path, |
| 145 | force=False, quiet=True)) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 146 | self.assertTrue(os.path.isfile(self.bc_path) and |
| 147 | not os.path.isfile(self.bc_path2)) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 148 | os.unlink(self.bc_path) |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 149 | self.assertTrue(compileall.compile_dir(self.directory, force=False, |
| 150 | quiet=True)) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 151 | self.assertTrue(os.path.isfile(self.bc_path) and |
| 152 | os.path.isfile(self.bc_path2)) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 153 | os.unlink(self.bc_path) |
| 154 | os.unlink(self.bc_path2) |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 155 | # Test against bad files |
| 156 | self.add_bad_source_file() |
| 157 | self.assertFalse(compileall.compile_file(self.bad_source_path, |
| 158 | force=False, quiet=2)) |
| 159 | self.assertFalse(compileall.compile_dir(self.directory, |
| 160 | force=False, quiet=2)) |
| 161 | |
Berker Peksag | 812a2b6 | 2016-10-01 00:54:18 +0300 | [diff] [blame] | 162 | def test_compile_file_pathlike(self): |
| 163 | self.assertFalse(os.path.isfile(self.bc_path)) |
| 164 | # we should also test the output |
| 165 | with support.captured_stdout() as stdout: |
| 166 | self.assertTrue(compileall.compile_file(pathlib.Path(self.source_path))) |
Berker Peksag | d8e9713 | 2016-10-01 02:44:37 +0300 | [diff] [blame] | 167 | self.assertRegex(stdout.getvalue(), r'Compiling ([^WindowsPath|PosixPath].*)') |
Berker Peksag | 812a2b6 | 2016-10-01 00:54:18 +0300 | [diff] [blame] | 168 | self.assertTrue(os.path.isfile(self.bc_path)) |
| 169 | |
| 170 | def test_compile_file_pathlike_ddir(self): |
| 171 | self.assertFalse(os.path.isfile(self.bc_path)) |
| 172 | self.assertTrue(compileall.compile_file(pathlib.Path(self.source_path), |
| 173 | ddir=pathlib.Path('ddir_path'), |
| 174 | quiet=2)) |
| 175 | self.assertTrue(os.path.isfile(self.bc_path)) |
| 176 | |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 177 | def test_compile_path(self): |
Berker Peksag | 408b78c | 2016-09-28 17:38:53 +0300 | [diff] [blame] | 178 | with test.test_importlib.util.import_state(path=[self.directory]): |
| 179 | self.assertTrue(compileall.compile_path(quiet=2)) |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 180 | |
| 181 | with test.test_importlib.util.import_state(path=[self.directory]): |
| 182 | self.add_bad_source_file() |
| 183 | self.assertFalse(compileall.compile_path(skip_curdir=False, |
| 184 | force=True, quiet=2)) |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 185 | |
Barry Warsaw | c8a99de | 2010-04-29 18:43:10 +0000 | [diff] [blame] | 186 | def test_no_pycache_in_non_package(self): |
| 187 | # Bug 8563 reported that __pycache__ directories got created by |
| 188 | # compile_file() for non-.py files. |
| 189 | data_dir = os.path.join(self.directory, 'data') |
| 190 | data_file = os.path.join(data_dir, 'file') |
| 191 | os.mkdir(data_dir) |
| 192 | # touch data/file |
| 193 | with open(data_file, 'w'): |
| 194 | pass |
| 195 | compileall.compile_file(data_file) |
| 196 | self.assertFalse(os.path.exists(os.path.join(data_dir, '__pycache__'))) |
| 197 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 198 | def test_optimize(self): |
| 199 | # make sure compiling with different optimization settings than the |
| 200 | # interpreter's creates the correct file names |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 201 | optimize, opt = (1, 1) if __debug__ else (0, '') |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 202 | compileall.compile_dir(self.directory, quiet=True, optimize=optimize) |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 203 | cached = importlib.util.cache_from_source(self.source_path, |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 204 | optimization=opt) |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 205 | self.assertTrue(os.path.isfile(cached)) |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 206 | cached2 = importlib.util.cache_from_source(self.source_path2, |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 207 | optimization=opt) |
Georg Brandl | 4543846 | 2011-02-07 12:36:54 +0000 | [diff] [blame] | 208 | self.assertTrue(os.path.isfile(cached2)) |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 209 | cached3 = importlib.util.cache_from_source(self.source_path3, |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 210 | optimization=opt) |
Georg Brandl | 4543846 | 2011-02-07 12:36:54 +0000 | [diff] [blame] | 211 | self.assertTrue(os.path.isfile(cached3)) |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 212 | |
Berker Peksag | 812a2b6 | 2016-10-01 00:54:18 +0300 | [diff] [blame] | 213 | def test_compile_dir_pathlike(self): |
| 214 | self.assertFalse(os.path.isfile(self.bc_path)) |
| 215 | with support.captured_stdout() as stdout: |
| 216 | compileall.compile_dir(pathlib.Path(self.directory)) |
Berker Peksag | d8e9713 | 2016-10-01 02:44:37 +0300 | [diff] [blame] | 217 | line = stdout.getvalue().splitlines()[0] |
| 218 | self.assertRegex(line, r'Listing ([^WindowsPath|PosixPath].*)') |
Berker Peksag | 812a2b6 | 2016-10-01 00:54:18 +0300 | [diff] [blame] | 219 | self.assertTrue(os.path.isfile(self.bc_path)) |
| 220 | |
Dustin Spicuzza | 1d817e4 | 2018-11-23 12:06:55 -0500 | [diff] [blame] | 221 | @mock.patch('concurrent.futures.ProcessPoolExecutor') |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 222 | def test_compile_pool_called(self, pool_mock): |
| 223 | compileall.compile_dir(self.directory, quiet=True, workers=5) |
| 224 | self.assertTrue(pool_mock.called) |
| 225 | |
| 226 | def test_compile_workers_non_positive(self): |
| 227 | with self.assertRaisesRegex(ValueError, |
| 228 | "workers must be greater or equal to 0"): |
| 229 | compileall.compile_dir(self.directory, workers=-1) |
| 230 | |
Dustin Spicuzza | 1d817e4 | 2018-11-23 12:06:55 -0500 | [diff] [blame] | 231 | @mock.patch('concurrent.futures.ProcessPoolExecutor') |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 232 | def test_compile_workers_cpu_count(self, pool_mock): |
| 233 | compileall.compile_dir(self.directory, quiet=True, workers=0) |
| 234 | self.assertEqual(pool_mock.call_args[1]['max_workers'], None) |
| 235 | |
Dustin Spicuzza | 1d817e4 | 2018-11-23 12:06:55 -0500 | [diff] [blame] | 236 | @mock.patch('concurrent.futures.ProcessPoolExecutor') |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 237 | @mock.patch('compileall.compile_file') |
| 238 | def test_compile_one_worker(self, compile_file_mock, pool_mock): |
| 239 | compileall.compile_dir(self.directory, quiet=True) |
| 240 | self.assertFalse(pool_mock.called) |
| 241 | self.assertTrue(compile_file_mock.called) |
| 242 | |
Dustin Spicuzza | 1d817e4 | 2018-11-23 12:06:55 -0500 | [diff] [blame] | 243 | @mock.patch('concurrent.futures.ProcessPoolExecutor', new=None) |
Berker Peksag | d86ef05 | 2015-04-22 09:39:19 +0300 | [diff] [blame] | 244 | @mock.patch('compileall.compile_file') |
| 245 | def test_compile_missing_multiprocessing(self, compile_file_mock): |
| 246 | compileall.compile_dir(self.directory, quiet=True, workers=5) |
| 247 | self.assertTrue(compile_file_mock.called) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 248 | |
Petr Viktorin | 4267c98 | 2019-09-26 11:53:51 +0200 | [diff] [blame] | 249 | def test_compile_dir_maxlevels(self): |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 250 | # Test the actual impact of maxlevels attr |
Petr Viktorin | 4267c98 | 2019-09-26 11:53:51 +0200 | [diff] [blame] | 251 | self.create_long_path() |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 252 | compileall.compile_dir(os.path.join(self.directory, "long"), |
| 253 | maxlevels=10, quiet=True) |
| 254 | self.assertFalse(os.path.isfile(self.bc_path_long)) |
| 255 | compileall.compile_dir(os.path.join(self.directory, "long"), |
Petr Viktorin | 4267c98 | 2019-09-26 11:53:51 +0200 | [diff] [blame] | 256 | quiet=True) |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 257 | self.assertTrue(os.path.isfile(self.bc_path_long)) |
| 258 | |
| 259 | def test_strip_only(self): |
| 260 | fullpath = ["test", "build", "real", "path"] |
| 261 | path = os.path.join(self.directory, *fullpath) |
| 262 | os.makedirs(path) |
| 263 | script = script_helper.make_script(path, "test", "1 / 0") |
| 264 | bc = importlib.util.cache_from_source(script) |
| 265 | stripdir = os.path.join(self.directory, *fullpath[:2]) |
| 266 | compileall.compile_dir(path, quiet=True, stripdir=stripdir) |
| 267 | rc, out, err = script_helper.assert_python_failure(bc) |
| 268 | expected_in = os.path.join(*fullpath[2:]) |
| 269 | self.assertIn( |
| 270 | expected_in, |
| 271 | str(err, encoding=sys.getdefaultencoding()) |
| 272 | ) |
| 273 | self.assertNotIn( |
| 274 | stripdir, |
| 275 | str(err, encoding=sys.getdefaultencoding()) |
| 276 | ) |
| 277 | |
| 278 | def test_prepend_only(self): |
| 279 | fullpath = ["test", "build", "real", "path"] |
| 280 | path = os.path.join(self.directory, *fullpath) |
| 281 | os.makedirs(path) |
| 282 | script = script_helper.make_script(path, "test", "1 / 0") |
| 283 | bc = importlib.util.cache_from_source(script) |
| 284 | prependdir = "/foo" |
| 285 | compileall.compile_dir(path, quiet=True, prependdir=prependdir) |
| 286 | rc, out, err = script_helper.assert_python_failure(bc) |
| 287 | expected_in = os.path.join(prependdir, self.directory, *fullpath) |
| 288 | self.assertIn( |
| 289 | expected_in, |
| 290 | str(err, encoding=sys.getdefaultencoding()) |
| 291 | ) |
| 292 | |
| 293 | def test_strip_and_prepend(self): |
| 294 | fullpath = ["test", "build", "real", "path"] |
| 295 | path = os.path.join(self.directory, *fullpath) |
| 296 | os.makedirs(path) |
| 297 | script = script_helper.make_script(path, "test", "1 / 0") |
| 298 | bc = importlib.util.cache_from_source(script) |
| 299 | stripdir = os.path.join(self.directory, *fullpath[:2]) |
| 300 | prependdir = "/foo" |
| 301 | compileall.compile_dir(path, quiet=True, |
| 302 | stripdir=stripdir, prependdir=prependdir) |
| 303 | rc, out, err = script_helper.assert_python_failure(bc) |
| 304 | expected_in = os.path.join(prependdir, *fullpath[2:]) |
| 305 | self.assertIn( |
| 306 | expected_in, |
| 307 | str(err, encoding=sys.getdefaultencoding()) |
| 308 | ) |
| 309 | self.assertNotIn( |
| 310 | stripdir, |
| 311 | str(err, encoding=sys.getdefaultencoding()) |
| 312 | ) |
| 313 | |
| 314 | def test_strip_prepend_and_ddir(self): |
| 315 | fullpath = ["test", "build", "real", "path", "ddir"] |
| 316 | path = os.path.join(self.directory, *fullpath) |
| 317 | os.makedirs(path) |
| 318 | script_helper.make_script(path, "test", "1 / 0") |
| 319 | with self.assertRaises(ValueError): |
| 320 | compileall.compile_dir(path, quiet=True, ddir="/bar", |
| 321 | stripdir="/foo", prependdir="/bar") |
| 322 | |
| 323 | def test_multiple_optimization_levels(self): |
| 324 | script = script_helper.make_script(self.directory, |
| 325 | "test_optimization", |
| 326 | "a = 0") |
| 327 | bc = [] |
| 328 | for opt_level in "", 1, 2, 3: |
| 329 | bc.append(importlib.util.cache_from_source(script, |
| 330 | optimization=opt_level)) |
| 331 | test_combinations = [[0, 1], [1, 2], [0, 2], [0, 1, 2]] |
| 332 | for opt_combination in test_combinations: |
| 333 | compileall.compile_file(script, quiet=True, |
| 334 | optimize=opt_combination) |
| 335 | for opt_level in opt_combination: |
| 336 | self.assertTrue(os.path.isfile(bc[opt_level])) |
| 337 | try: |
| 338 | os.unlink(bc[opt_level]) |
| 339 | except Exception: |
| 340 | pass |
| 341 | |
| 342 | @support.skip_unless_symlink |
| 343 | def test_ignore_symlink_destination(self): |
| 344 | # Create folders for allowed files, symlinks and prohibited area |
| 345 | allowed_path = os.path.join(self.directory, "test", "dir", "allowed") |
| 346 | symlinks_path = os.path.join(self.directory, "test", "dir", "symlinks") |
| 347 | prohibited_path = os.path.join(self.directory, "test", "dir", "prohibited") |
| 348 | os.makedirs(allowed_path) |
| 349 | os.makedirs(symlinks_path) |
| 350 | os.makedirs(prohibited_path) |
| 351 | |
| 352 | # Create scripts and symlinks and remember their byte-compiled versions |
| 353 | allowed_script = script_helper.make_script(allowed_path, "test_allowed", "a = 0") |
| 354 | prohibited_script = script_helper.make_script(prohibited_path, "test_prohibited", "a = 0") |
| 355 | allowed_symlink = os.path.join(symlinks_path, "test_allowed.py") |
| 356 | prohibited_symlink = os.path.join(symlinks_path, "test_prohibited.py") |
| 357 | os.symlink(allowed_script, allowed_symlink) |
| 358 | os.symlink(prohibited_script, prohibited_symlink) |
| 359 | allowed_bc = importlib.util.cache_from_source(allowed_symlink) |
| 360 | prohibited_bc = importlib.util.cache_from_source(prohibited_symlink) |
| 361 | |
| 362 | compileall.compile_dir(symlinks_path, quiet=True, limit_sl_dest=allowed_path) |
| 363 | |
| 364 | self.assertTrue(os.path.isfile(allowed_bc)) |
| 365 | self.assertFalse(os.path.isfile(prohibited_bc)) |
| 366 | |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 367 | |
| 368 | class CompileallTestsWithSourceEpoch(CompileallTestsBase, |
| 369 | unittest.TestCase, |
| 370 | metaclass=SourceDateEpochTestMeta, |
| 371 | source_date_epoch=True): |
| 372 | pass |
| 373 | |
| 374 | |
| 375 | class CompileallTestsWithoutSourceEpoch(CompileallTestsBase, |
| 376 | unittest.TestCase, |
| 377 | metaclass=SourceDateEpochTestMeta, |
| 378 | source_date_epoch=False): |
| 379 | pass |
| 380 | |
| 381 | |
Martin v. Löwis | 4b00307 | 2010-03-16 13:19:21 +0000 | [diff] [blame] | 382 | class EncodingTest(unittest.TestCase): |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 383 | """Issue 6716: compileall should escape source code when printing errors |
| 384 | to stdout.""" |
Martin v. Löwis | 4b00307 | 2010-03-16 13:19:21 +0000 | [diff] [blame] | 385 | |
| 386 | def setUp(self): |
| 387 | self.directory = tempfile.mkdtemp() |
| 388 | self.source_path = os.path.join(self.directory, '_test.py') |
| 389 | with open(self.source_path, 'w', encoding='utf-8') as file: |
| 390 | file.write('# -*- coding: utf-8 -*-\n') |
| 391 | file.write('print u"\u20ac"\n') |
| 392 | |
| 393 | def tearDown(self): |
| 394 | shutil.rmtree(self.directory) |
| 395 | |
| 396 | def test_error(self): |
| 397 | try: |
| 398 | orig_stdout = sys.stdout |
| 399 | sys.stdout = io.TextIOWrapper(io.BytesIO(),encoding='ascii') |
| 400 | compileall.compile_dir(self.directory) |
| 401 | finally: |
| 402 | sys.stdout = orig_stdout |
| 403 | |
Barry Warsaw | c8a99de | 2010-04-29 18:43:10 +0000 | [diff] [blame] | 404 | |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 405 | class CommandLineTestsBase: |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 406 | """Test compileall's CLI.""" |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 407 | |
Brett Cannon | 65ed750 | 2015-10-09 15:09:43 -0700 | [diff] [blame] | 408 | @classmethod |
| 409 | def setUpClass(cls): |
| 410 | for path in filter(os.path.isdir, sys.path): |
| 411 | directory_created = False |
| 412 | directory = pathlib.Path(path) / '__pycache__' |
| 413 | path = directory / 'test.try' |
| 414 | try: |
| 415 | if not directory.is_dir(): |
| 416 | directory.mkdir() |
| 417 | directory_created = True |
| 418 | with path.open('w') as file: |
| 419 | file.write('# for test_compileall') |
| 420 | except OSError: |
| 421 | sys_path_writable = False |
| 422 | break |
| 423 | finally: |
| 424 | support.unlink(str(path)) |
| 425 | if directory_created: |
| 426 | directory.rmdir() |
| 427 | else: |
| 428 | sys_path_writable = True |
| 429 | cls._sys_path_writable = sys_path_writable |
| 430 | |
| 431 | def _skip_if_sys_path_not_writable(self): |
| 432 | if not self._sys_path_writable: |
| 433 | raise unittest.SkipTest('not all entries on sys.path are writable') |
| 434 | |
Benjamin Peterson | a820c7c | 2012-09-25 11:42:35 -0400 | [diff] [blame] | 435 | def _get_run_args(self, args): |
Victor Stinner | 9def284 | 2016-01-18 12:15:08 +0100 | [diff] [blame] | 436 | return [*support.optim_args_from_interpreter_flags(), |
| 437 | '-S', '-m', 'compileall', |
| 438 | *args] |
Benjamin Peterson | a820c7c | 2012-09-25 11:42:35 -0400 | [diff] [blame] | 439 | |
R. David Murray | 5317e9c | 2010-12-16 19:08:51 +0000 | [diff] [blame] | 440 | def assertRunOK(self, *args, **env_vars): |
| 441 | rc, out, err = script_helper.assert_python_ok( |
Benjamin Peterson | a820c7c | 2012-09-25 11:42:35 -0400 | [diff] [blame] | 442 | *self._get_run_args(args), **env_vars) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 443 | self.assertEqual(b'', err) |
| 444 | return out |
| 445 | |
R. David Murray | 5317e9c | 2010-12-16 19:08:51 +0000 | [diff] [blame] | 446 | def assertRunNotOK(self, *args, **env_vars): |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 447 | rc, out, err = script_helper.assert_python_failure( |
Benjamin Peterson | a820c7c | 2012-09-25 11:42:35 -0400 | [diff] [blame] | 448 | *self._get_run_args(args), **env_vars) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 449 | return rc, out, err |
| 450 | |
| 451 | def assertCompiled(self, fn): |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 452 | path = importlib.util.cache_from_source(fn) |
| 453 | self.assertTrue(os.path.exists(path)) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 454 | |
| 455 | def assertNotCompiled(self, fn): |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 456 | path = importlib.util.cache_from_source(fn) |
| 457 | self.assertFalse(os.path.exists(path)) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 458 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 459 | def setUp(self): |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 460 | self.directory = tempfile.mkdtemp() |
Brett Cannon | 65ed750 | 2015-10-09 15:09:43 -0700 | [diff] [blame] | 461 | self.addCleanup(support.rmtree, self.directory) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 462 | self.pkgdir = os.path.join(self.directory, 'foo') |
| 463 | os.mkdir(self.pkgdir) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 464 | self.pkgdir_cachedir = os.path.join(self.pkgdir, '__pycache__') |
| 465 | # Create the __init__.py and a package module. |
| 466 | self.initfn = script_helper.make_script(self.pkgdir, '__init__', '') |
| 467 | self.barfn = script_helper.make_script(self.pkgdir, 'bar', '') |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 468 | |
R. David Murray | 5317e9c | 2010-12-16 19:08:51 +0000 | [diff] [blame] | 469 | def test_no_args_compiles_path(self): |
| 470 | # Note that -l is implied for the no args case. |
Brett Cannon | 65ed750 | 2015-10-09 15:09:43 -0700 | [diff] [blame] | 471 | self._skip_if_sys_path_not_writable() |
R. David Murray | 5317e9c | 2010-12-16 19:08:51 +0000 | [diff] [blame] | 472 | bazfn = script_helper.make_script(self.directory, 'baz', '') |
| 473 | self.assertRunOK(PYTHONPATH=self.directory) |
| 474 | self.assertCompiled(bazfn) |
| 475 | self.assertNotCompiled(self.initfn) |
| 476 | self.assertNotCompiled(self.barfn) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 477 | |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 478 | @without_source_date_epoch # timestamp invalidation test |
R David Murray | 8a1d1e6 | 2013-12-15 20:49:38 -0500 | [diff] [blame] | 479 | def test_no_args_respects_force_flag(self): |
Brett Cannon | 65ed750 | 2015-10-09 15:09:43 -0700 | [diff] [blame] | 480 | self._skip_if_sys_path_not_writable() |
R David Murray | 8a1d1e6 | 2013-12-15 20:49:38 -0500 | [diff] [blame] | 481 | bazfn = script_helper.make_script(self.directory, 'baz', '') |
| 482 | self.assertRunOK(PYTHONPATH=self.directory) |
R David Murray | 755d5ea | 2013-12-15 20:56:00 -0500 | [diff] [blame] | 483 | pycpath = importlib.util.cache_from_source(bazfn) |
R David Murray | 8a1d1e6 | 2013-12-15 20:49:38 -0500 | [diff] [blame] | 484 | # Set atime/mtime backward to avoid file timestamp resolution issues |
| 485 | os.utime(pycpath, (time.time()-60,)*2) |
| 486 | mtime = os.stat(pycpath).st_mtime |
| 487 | # Without force, no recompilation |
| 488 | self.assertRunOK(PYTHONPATH=self.directory) |
| 489 | mtime2 = os.stat(pycpath).st_mtime |
| 490 | self.assertEqual(mtime, mtime2) |
| 491 | # Now force it. |
| 492 | self.assertRunOK('-f', PYTHONPATH=self.directory) |
| 493 | mtime2 = os.stat(pycpath).st_mtime |
| 494 | self.assertNotEqual(mtime, mtime2) |
| 495 | |
| 496 | def test_no_args_respects_quiet_flag(self): |
Brett Cannon | 65ed750 | 2015-10-09 15:09:43 -0700 | [diff] [blame] | 497 | self._skip_if_sys_path_not_writable() |
R David Murray | 8a1d1e6 | 2013-12-15 20:49:38 -0500 | [diff] [blame] | 498 | script_helper.make_script(self.directory, 'baz', '') |
| 499 | noisy = self.assertRunOK(PYTHONPATH=self.directory) |
| 500 | self.assertIn(b'Listing ', noisy) |
| 501 | quiet = self.assertRunOK('-q', PYTHONPATH=self.directory) |
| 502 | self.assertNotIn(b'Listing ', quiet) |
| 503 | |
Georg Brandl | 1463a3f | 2010-10-14 07:42:27 +0000 | [diff] [blame] | 504 | # Ensure that the default behavior of compileall's CLI is to create |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 505 | # PEP 3147/PEP 488 pyc files. |
Georg Brandl | 1463a3f | 2010-10-14 07:42:27 +0000 | [diff] [blame] | 506 | for name, ext, switch in [ |
| 507 | ('normal', 'pyc', []), |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 508 | ('optimize', 'opt-1.pyc', ['-O']), |
| 509 | ('doubleoptimize', 'opt-2.pyc', ['-OO']), |
Georg Brandl | 1463a3f | 2010-10-14 07:42:27 +0000 | [diff] [blame] | 510 | ]: |
| 511 | def f(self, ext=ext, switch=switch): |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 512 | script_helper.assert_python_ok(*(switch + |
| 513 | ['-m', 'compileall', '-q', self.pkgdir])) |
Georg Brandl | 1463a3f | 2010-10-14 07:42:27 +0000 | [diff] [blame] | 514 | # Verify the __pycache__ directory contents. |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 515 | self.assertTrue(os.path.exists(self.pkgdir_cachedir)) |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 516 | expected = sorted(base.format(sys.implementation.cache_tag, ext) |
| 517 | for base in ('__init__.{}.{}', 'bar.{}.{}')) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 518 | self.assertEqual(sorted(os.listdir(self.pkgdir_cachedir)), expected) |
Georg Brandl | 1463a3f | 2010-10-14 07:42:27 +0000 | [diff] [blame] | 519 | # Make sure there are no .pyc files in the source directory. |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 520 | self.assertFalse([fn for fn in os.listdir(self.pkgdir) |
| 521 | if fn.endswith(ext)]) |
Georg Brandl | 1463a3f | 2010-10-14 07:42:27 +0000 | [diff] [blame] | 522 | locals()['test_pep3147_paths_' + name] = f |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 523 | |
| 524 | def test_legacy_paths(self): |
| 525 | # Ensure that with the proper switch, compileall leaves legacy |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 526 | # pyc files, and no __pycache__ directory. |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 527 | self.assertRunOK('-b', '-q', self.pkgdir) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 528 | # Verify the __pycache__ directory contents. |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 529 | self.assertFalse(os.path.exists(self.pkgdir_cachedir)) |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 530 | expected = sorted(['__init__.py', '__init__.pyc', 'bar.py', |
| 531 | 'bar.pyc']) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 532 | self.assertEqual(sorted(os.listdir(self.pkgdir)), expected) |
| 533 | |
Barry Warsaw | c04317f | 2010-04-26 15:59:03 +0000 | [diff] [blame] | 534 | def test_multiple_runs(self): |
| 535 | # Bug 8527 reported that multiple calls produced empty |
| 536 | # __pycache__/__pycache__ directories. |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 537 | self.assertRunOK('-q', self.pkgdir) |
Barry Warsaw | c04317f | 2010-04-26 15:59:03 +0000 | [diff] [blame] | 538 | # Verify the __pycache__ directory contents. |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 539 | self.assertTrue(os.path.exists(self.pkgdir_cachedir)) |
| 540 | cachecachedir = os.path.join(self.pkgdir_cachedir, '__pycache__') |
Barry Warsaw | c04317f | 2010-04-26 15:59:03 +0000 | [diff] [blame] | 541 | self.assertFalse(os.path.exists(cachecachedir)) |
| 542 | # Call compileall again. |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 543 | self.assertRunOK('-q', self.pkgdir) |
| 544 | self.assertTrue(os.path.exists(self.pkgdir_cachedir)) |
Barry Warsaw | c04317f | 2010-04-26 15:59:03 +0000 | [diff] [blame] | 545 | self.assertFalse(os.path.exists(cachecachedir)) |
| 546 | |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 547 | @without_source_date_epoch # timestamp invalidation test |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 548 | def test_force(self): |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 549 | self.assertRunOK('-q', self.pkgdir) |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 550 | pycpath = importlib.util.cache_from_source(self.barfn) |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 551 | # set atime/mtime backward to avoid file timestamp resolution issues |
| 552 | os.utime(pycpath, (time.time()-60,)*2) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 553 | mtime = os.stat(pycpath).st_mtime |
| 554 | # without force, no recompilation |
| 555 | self.assertRunOK('-q', self.pkgdir) |
| 556 | mtime2 = os.stat(pycpath).st_mtime |
| 557 | self.assertEqual(mtime, mtime2) |
| 558 | # now force it. |
| 559 | self.assertRunOK('-q', '-f', self.pkgdir) |
| 560 | mtime2 = os.stat(pycpath).st_mtime |
| 561 | self.assertNotEqual(mtime, mtime2) |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 562 | |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 563 | def test_recursion_control(self): |
| 564 | subpackage = os.path.join(self.pkgdir, 'spam') |
| 565 | os.mkdir(subpackage) |
| 566 | subinitfn = script_helper.make_script(subpackage, '__init__', '') |
| 567 | hamfn = script_helper.make_script(subpackage, 'ham', '') |
| 568 | self.assertRunOK('-q', '-l', self.pkgdir) |
| 569 | self.assertNotCompiled(subinitfn) |
| 570 | self.assertFalse(os.path.exists(os.path.join(subpackage, '__pycache__'))) |
| 571 | self.assertRunOK('-q', self.pkgdir) |
| 572 | self.assertCompiled(subinitfn) |
| 573 | self.assertCompiled(hamfn) |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 574 | |
Benjamin Peterson | 344ff4a | 2014-08-19 16:13:26 -0500 | [diff] [blame] | 575 | def test_recursion_limit(self): |
| 576 | subpackage = os.path.join(self.pkgdir, 'spam') |
| 577 | subpackage2 = os.path.join(subpackage, 'ham') |
| 578 | subpackage3 = os.path.join(subpackage2, 'eggs') |
| 579 | for pkg in (subpackage, subpackage2, subpackage3): |
| 580 | script_helper.make_pkg(pkg) |
| 581 | |
| 582 | subinitfn = os.path.join(subpackage, '__init__.py') |
| 583 | hamfn = script_helper.make_script(subpackage, 'ham', '') |
| 584 | spamfn = script_helper.make_script(subpackage2, 'spam', '') |
| 585 | eggfn = script_helper.make_script(subpackage3, 'egg', '') |
| 586 | |
| 587 | self.assertRunOK('-q', '-r 0', self.pkgdir) |
| 588 | self.assertNotCompiled(subinitfn) |
| 589 | self.assertFalse( |
| 590 | os.path.exists(os.path.join(subpackage, '__pycache__'))) |
| 591 | |
| 592 | self.assertRunOK('-q', '-r 1', self.pkgdir) |
| 593 | self.assertCompiled(subinitfn) |
| 594 | self.assertCompiled(hamfn) |
| 595 | self.assertNotCompiled(spamfn) |
| 596 | |
| 597 | self.assertRunOK('-q', '-r 2', self.pkgdir) |
| 598 | self.assertCompiled(subinitfn) |
| 599 | self.assertCompiled(hamfn) |
| 600 | self.assertCompiled(spamfn) |
| 601 | self.assertNotCompiled(eggfn) |
| 602 | |
| 603 | self.assertRunOK('-q', '-r 5', self.pkgdir) |
| 604 | self.assertCompiled(subinitfn) |
| 605 | self.assertCompiled(hamfn) |
| 606 | self.assertCompiled(spamfn) |
| 607 | self.assertCompiled(eggfn) |
| 608 | |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 609 | @support.skip_unless_symlink |
| 610 | def test_symlink_loop(self): |
| 611 | # Currently, compileall ignores symlinks to directories. |
| 612 | # If that limitation is ever lifted, it should protect against |
| 613 | # recursion in symlink loops. |
| 614 | pkg = os.path.join(self.pkgdir, 'spam') |
| 615 | script_helper.make_pkg(pkg) |
| 616 | os.symlink('.', os.path.join(pkg, 'evil')) |
| 617 | os.symlink('.', os.path.join(pkg, 'evil2')) |
| 618 | self.assertRunOK('-q', self.pkgdir) |
| 619 | self.assertCompiled(os.path.join( |
| 620 | self.pkgdir, 'spam', 'evil', 'evil2', '__init__.py' |
| 621 | )) |
| 622 | |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 623 | def test_quiet(self): |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 624 | noisy = self.assertRunOK(self.pkgdir) |
| 625 | quiet = self.assertRunOK('-q', self.pkgdir) |
| 626 | self.assertNotEqual(b'', noisy) |
| 627 | self.assertEqual(b'', quiet) |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 628 | |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 629 | def test_silent(self): |
| 630 | script_helper.make_script(self.pkgdir, 'crunchyfrog', 'bad(syntax') |
| 631 | _, quiet, _ = self.assertRunNotOK('-q', self.pkgdir) |
| 632 | _, silent, _ = self.assertRunNotOK('-qq', self.pkgdir) |
| 633 | self.assertNotEqual(b'', quiet) |
| 634 | self.assertEqual(b'', silent) |
| 635 | |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 636 | def test_regexp(self): |
R David Murray | ee1a7cb | 2011-07-01 14:55:43 -0400 | [diff] [blame] | 637 | self.assertRunOK('-q', '-x', r'ba[^\\/]*$', self.pkgdir) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 638 | self.assertNotCompiled(self.barfn) |
| 639 | self.assertCompiled(self.initfn) |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 640 | |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 641 | def test_multiple_dirs(self): |
| 642 | pkgdir2 = os.path.join(self.directory, 'foo2') |
| 643 | os.mkdir(pkgdir2) |
| 644 | init2fn = script_helper.make_script(pkgdir2, '__init__', '') |
| 645 | bar2fn = script_helper.make_script(pkgdir2, 'bar2', '') |
| 646 | self.assertRunOK('-q', self.pkgdir, pkgdir2) |
| 647 | self.assertCompiled(self.initfn) |
| 648 | self.assertCompiled(self.barfn) |
| 649 | self.assertCompiled(init2fn) |
| 650 | self.assertCompiled(bar2fn) |
| 651 | |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 652 | def test_d_compile_error(self): |
| 653 | script_helper.make_script(self.pkgdir, 'crunchyfrog', 'bad(syntax') |
| 654 | rc, out, err = self.assertRunNotOK('-q', '-d', 'dinsdale', self.pkgdir) |
| 655 | self.assertRegex(out, b'File "dinsdale') |
| 656 | |
| 657 | def test_d_runtime_error(self): |
| 658 | bazfn = script_helper.make_script(self.pkgdir, 'baz', 'raise Exception') |
| 659 | self.assertRunOK('-q', '-d', 'dinsdale', self.pkgdir) |
| 660 | fn = script_helper.make_script(self.pkgdir, 'bing', 'import baz') |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 661 | pyc = importlib.util.cache_from_source(bazfn) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 662 | os.rename(pyc, os.path.join(self.pkgdir, 'baz.pyc')) |
| 663 | os.remove(bazfn) |
Victor Stinner | e8785ff | 2013-10-12 14:44:01 +0200 | [diff] [blame] | 664 | rc, out, err = script_helper.assert_python_failure(fn, __isolated=False) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 665 | self.assertRegex(err, b'File "dinsdale') |
| 666 | |
| 667 | def test_include_bad_file(self): |
| 668 | rc, out, err = self.assertRunNotOK( |
| 669 | '-i', os.path.join(self.directory, 'nosuchfile'), self.pkgdir) |
| 670 | self.assertRegex(out, b'rror.*nosuchfile') |
| 671 | self.assertNotRegex(err, b'Traceback') |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 672 | self.assertFalse(os.path.exists(importlib.util.cache_from_source( |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 673 | self.pkgdir_cachedir))) |
| 674 | |
| 675 | def test_include_file_with_arg(self): |
| 676 | f1 = script_helper.make_script(self.pkgdir, 'f1', '') |
| 677 | f2 = script_helper.make_script(self.pkgdir, 'f2', '') |
| 678 | f3 = script_helper.make_script(self.pkgdir, 'f3', '') |
| 679 | f4 = script_helper.make_script(self.pkgdir, 'f4', '') |
| 680 | with open(os.path.join(self.directory, 'l1'), 'w') as l1: |
| 681 | l1.write(os.path.join(self.pkgdir, 'f1.py')+os.linesep) |
| 682 | l1.write(os.path.join(self.pkgdir, 'f2.py')+os.linesep) |
| 683 | self.assertRunOK('-i', os.path.join(self.directory, 'l1'), f4) |
| 684 | self.assertCompiled(f1) |
| 685 | self.assertCompiled(f2) |
| 686 | self.assertNotCompiled(f3) |
| 687 | self.assertCompiled(f4) |
| 688 | |
| 689 | def test_include_file_no_arg(self): |
| 690 | f1 = script_helper.make_script(self.pkgdir, 'f1', '') |
| 691 | f2 = script_helper.make_script(self.pkgdir, 'f2', '') |
| 692 | f3 = script_helper.make_script(self.pkgdir, 'f3', '') |
| 693 | f4 = script_helper.make_script(self.pkgdir, 'f4', '') |
| 694 | with open(os.path.join(self.directory, 'l1'), 'w') as l1: |
| 695 | l1.write(os.path.join(self.pkgdir, 'f2.py')+os.linesep) |
| 696 | self.assertRunOK('-i', os.path.join(self.directory, 'l1')) |
| 697 | self.assertNotCompiled(f1) |
| 698 | self.assertCompiled(f2) |
| 699 | self.assertNotCompiled(f3) |
| 700 | self.assertNotCompiled(f4) |
| 701 | |
| 702 | def test_include_on_stdin(self): |
| 703 | f1 = script_helper.make_script(self.pkgdir, 'f1', '') |
| 704 | f2 = script_helper.make_script(self.pkgdir, 'f2', '') |
| 705 | f3 = script_helper.make_script(self.pkgdir, 'f3', '') |
| 706 | f4 = script_helper.make_script(self.pkgdir, 'f4', '') |
Benjamin Peterson | a820c7c | 2012-09-25 11:42:35 -0400 | [diff] [blame] | 707 | p = script_helper.spawn_python(*(self._get_run_args(()) + ['-i', '-'])) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 708 | p.stdin.write((f3+os.linesep).encode('ascii')) |
| 709 | script_helper.kill_python(p) |
| 710 | self.assertNotCompiled(f1) |
| 711 | self.assertNotCompiled(f2) |
| 712 | self.assertCompiled(f3) |
| 713 | self.assertNotCompiled(f4) |
| 714 | |
| 715 | def test_compiles_as_much_as_possible(self): |
| 716 | bingfn = script_helper.make_script(self.pkgdir, 'bing', 'syntax(error') |
| 717 | rc, out, err = self.assertRunNotOK('nosuchfile', self.initfn, |
| 718 | bingfn, self.barfn) |
R. David Murray | 5317e9c | 2010-12-16 19:08:51 +0000 | [diff] [blame] | 719 | self.assertRegex(out, b'rror') |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 720 | self.assertNotCompiled(bingfn) |
| 721 | self.assertCompiled(self.initfn) |
| 722 | self.assertCompiled(self.barfn) |
| 723 | |
R. David Murray | 5317e9c | 2010-12-16 19:08:51 +0000 | [diff] [blame] | 724 | def test_invalid_arg_produces_message(self): |
| 725 | out = self.assertRunOK('badfilename') |
Victor Stinner | 5307126 | 2011-05-11 00:36:28 +0200 | [diff] [blame] | 726 | self.assertRegex(out, b"Can't list 'badfilename'") |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 727 | |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 728 | def test_pyc_invalidation_mode(self): |
| 729 | script_helper.make_script(self.pkgdir, 'f1', '') |
| 730 | pyc = importlib.util.cache_from_source( |
| 731 | os.path.join(self.pkgdir, 'f1.py')) |
| 732 | self.assertRunOK('--invalidation-mode=checked-hash', self.pkgdir) |
| 733 | with open(pyc, 'rb') as fp: |
| 734 | data = fp.read() |
| 735 | self.assertEqual(int.from_bytes(data[4:8], 'little'), 0b11) |
| 736 | self.assertRunOK('--invalidation-mode=unchecked-hash', self.pkgdir) |
| 737 | with open(pyc, 'rb') as fp: |
| 738 | data = fp.read() |
| 739 | self.assertEqual(int.from_bytes(data[4:8], 'little'), 0b01) |
| 740 | |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 741 | @skipUnless(_have_multiprocessing, "requires multiprocessing") |
| 742 | def test_workers(self): |
| 743 | bar2fn = script_helper.make_script(self.directory, 'bar2', '') |
| 744 | files = [] |
| 745 | for suffix in range(5): |
| 746 | pkgdir = os.path.join(self.directory, 'foo{}'.format(suffix)) |
| 747 | os.mkdir(pkgdir) |
| 748 | fn = script_helper.make_script(pkgdir, '__init__', '') |
| 749 | files.append(script_helper.make_script(pkgdir, 'bar2', '')) |
| 750 | |
| 751 | self.assertRunOK(self.directory, '-j', '0') |
| 752 | self.assertCompiled(bar2fn) |
| 753 | for file in files: |
| 754 | self.assertCompiled(file) |
| 755 | |
| 756 | @mock.patch('compileall.compile_dir') |
| 757 | def test_workers_available_cores(self, compile_dir): |
| 758 | with mock.patch("sys.argv", |
| 759 | new=[sys.executable, self.directory, "-j0"]): |
| 760 | compileall.main() |
| 761 | self.assertTrue(compile_dir.called) |
Antoine Pitrou | 1a2dd82 | 2019-05-15 23:45:18 +0200 | [diff] [blame] | 762 | self.assertEqual(compile_dir.call_args[-1]['workers'], 0) |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 763 | |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 764 | def test_strip_and_prepend(self): |
| 765 | fullpath = ["test", "build", "real", "path"] |
| 766 | path = os.path.join(self.directory, *fullpath) |
| 767 | os.makedirs(path) |
| 768 | script = script_helper.make_script(path, "test", "1 / 0") |
| 769 | bc = importlib.util.cache_from_source(script) |
| 770 | stripdir = os.path.join(self.directory, *fullpath[:2]) |
| 771 | prependdir = "/foo" |
| 772 | self.assertRunOK("-s", stripdir, "-p", prependdir, path) |
| 773 | rc, out, err = script_helper.assert_python_failure(bc) |
| 774 | expected_in = os.path.join(prependdir, *fullpath[2:]) |
| 775 | self.assertIn( |
| 776 | expected_in, |
| 777 | str(err, encoding=sys.getdefaultencoding()) |
| 778 | ) |
| 779 | self.assertNotIn( |
| 780 | stripdir, |
| 781 | str(err, encoding=sys.getdefaultencoding()) |
| 782 | ) |
| 783 | |
| 784 | def test_multiple_optimization_levels(self): |
| 785 | path = os.path.join(self.directory, "optimizations") |
| 786 | os.makedirs(path) |
| 787 | script = script_helper.make_script(path, |
| 788 | "test_optimization", |
| 789 | "a = 0") |
| 790 | bc = [] |
| 791 | for opt_level in "", 1, 2, 3: |
| 792 | bc.append(importlib.util.cache_from_source(script, |
| 793 | optimization=opt_level)) |
| 794 | test_combinations = [["0", "1"], |
| 795 | ["1", "2"], |
| 796 | ["0", "2"], |
| 797 | ["0", "1", "2"]] |
| 798 | for opt_combination in test_combinations: |
| 799 | self.assertRunOK(path, *("-o" + str(n) for n in opt_combination)) |
| 800 | for opt_level in opt_combination: |
| 801 | self.assertTrue(os.path.isfile(bc[int(opt_level)])) |
| 802 | try: |
| 803 | os.unlink(bc[opt_level]) |
| 804 | except Exception: |
| 805 | pass |
| 806 | |
| 807 | @support.skip_unless_symlink |
| 808 | def test_ignore_symlink_destination(self): |
| 809 | # Create folders for allowed files, symlinks and prohibited area |
| 810 | allowed_path = os.path.join(self.directory, "test", "dir", "allowed") |
| 811 | symlinks_path = os.path.join(self.directory, "test", "dir", "symlinks") |
| 812 | prohibited_path = os.path.join(self.directory, "test", "dir", "prohibited") |
| 813 | os.makedirs(allowed_path) |
| 814 | os.makedirs(symlinks_path) |
| 815 | os.makedirs(prohibited_path) |
| 816 | |
| 817 | # Create scripts and symlinks and remember their byte-compiled versions |
| 818 | allowed_script = script_helper.make_script(allowed_path, "test_allowed", "a = 0") |
| 819 | prohibited_script = script_helper.make_script(prohibited_path, "test_prohibited", "a = 0") |
| 820 | allowed_symlink = os.path.join(symlinks_path, "test_allowed.py") |
| 821 | prohibited_symlink = os.path.join(symlinks_path, "test_prohibited.py") |
| 822 | os.symlink(allowed_script, allowed_symlink) |
| 823 | os.symlink(prohibited_script, prohibited_symlink) |
| 824 | allowed_bc = importlib.util.cache_from_source(allowed_symlink) |
| 825 | prohibited_bc = importlib.util.cache_from_source(prohibited_symlink) |
| 826 | |
| 827 | self.assertRunOK(symlinks_path, "-e", allowed_path) |
| 828 | |
| 829 | self.assertTrue(os.path.isfile(allowed_bc)) |
| 830 | self.assertFalse(os.path.isfile(prohibited_bc)) |
| 831 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 832 | |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 833 | class CommandLineTestsWithSourceEpoch(CommandLineTestsBase, |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 834 | unittest.TestCase, |
| 835 | metaclass=SourceDateEpochTestMeta, |
| 836 | source_date_epoch=True): |
| 837 | pass |
| 838 | |
| 839 | |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 840 | class CommandLineTestsNoSourceEpoch(CommandLineTestsBase, |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 841 | unittest.TestCase, |
| 842 | metaclass=SourceDateEpochTestMeta, |
| 843 | source_date_epoch=False): |
| 844 | pass |
| 845 | |
| 846 | |
| 847 | |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 848 | if __name__ == "__main__": |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 849 | unittest.main() |