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 |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 14 | |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 15 | from unittest import mock, skipUnless |
| 16 | try: |
| 17 | from concurrent.futures import ProcessPoolExecutor |
| 18 | _have_multiprocessing = True |
| 19 | except ImportError: |
| 20 | _have_multiprocessing = False |
| 21 | |
Berker Peksag | ce64391 | 2015-05-06 06:33:17 +0300 | [diff] [blame] | 22 | from test import support |
| 23 | from test.support import script_helper |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 24 | |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 25 | from .test_py_compile import without_source_date_epoch |
| 26 | from .test_py_compile import SourceDateEpochTestMeta |
| 27 | |
| 28 | |
| 29 | class CompileallTestsBase: |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 30 | |
| 31 | def setUp(self): |
| 32 | self.directory = tempfile.mkdtemp() |
| 33 | self.source_path = os.path.join(self.directory, '_test.py') |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 34 | self.bc_path = importlib.util.cache_from_source(self.source_path) |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 35 | with open(self.source_path, 'w') as file: |
| 36 | file.write('x = 123\n') |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 37 | self.source_path2 = os.path.join(self.directory, '_test2.py') |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 38 | self.bc_path2 = importlib.util.cache_from_source(self.source_path2) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 39 | shutil.copyfile(self.source_path, self.source_path2) |
Georg Brandl | 4543846 | 2011-02-07 12:36:54 +0000 | [diff] [blame] | 40 | self.subdirectory = os.path.join(self.directory, '_subdir') |
| 41 | os.mkdir(self.subdirectory) |
| 42 | self.source_path3 = os.path.join(self.subdirectory, '_test3.py') |
| 43 | shutil.copyfile(self.source_path, self.source_path3) |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 44 | |
| 45 | def tearDown(self): |
| 46 | shutil.rmtree(self.directory) |
| 47 | |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 48 | def add_bad_source_file(self): |
| 49 | self.bad_source_path = os.path.join(self.directory, '_test_bad.py') |
| 50 | with open(self.bad_source_path, 'w') as file: |
| 51 | file.write('x (\n') |
| 52 | |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 53 | def timestamp_metadata(self): |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 54 | with open(self.bc_path, 'rb') as file: |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 55 | data = file.read(12) |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 56 | mtime = int(os.stat(self.source_path).st_mtime) |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 57 | compare = struct.pack('<4sll', importlib.util.MAGIC_NUMBER, 0, mtime) |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 58 | return data, compare |
| 59 | |
| 60 | def recreation_check(self, metadata): |
| 61 | """Check that compileall recreates bytecode when the new metadata is |
| 62 | used.""" |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 63 | if os.environ.get('SOURCE_DATE_EPOCH'): |
| 64 | raise unittest.SkipTest('SOURCE_DATE_EPOCH is set') |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 65 | py_compile.compile(self.source_path) |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 66 | self.assertEqual(*self.timestamp_metadata()) |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 67 | with open(self.bc_path, 'rb') as file: |
| 68 | bc = file.read()[len(metadata):] |
| 69 | with open(self.bc_path, 'wb') as file: |
| 70 | file.write(metadata) |
| 71 | file.write(bc) |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 72 | self.assertNotEqual(*self.timestamp_metadata()) |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 73 | compileall.compile_dir(self.directory, force=False, quiet=True) |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 74 | self.assertTrue(*self.timestamp_metadata()) |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 75 | |
| 76 | def test_mtime(self): |
| 77 | # Test a change in mtime leads to a new .pyc. |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 78 | self.recreation_check(struct.pack('<4sll', importlib.util.MAGIC_NUMBER, |
| 79 | 0, 1)) |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 80 | |
| 81 | def test_magic_number(self): |
| 82 | # Test a change in mtime leads to a new .pyc. |
| 83 | self.recreation_check(b'\0\0\0\0') |
| 84 | |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 85 | def test_compile_files(self): |
| 86 | # Test compiling a single file, and complete directory |
| 87 | for fn in (self.bc_path, self.bc_path2): |
| 88 | try: |
| 89 | os.unlink(fn) |
| 90 | except: |
| 91 | pass |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 92 | self.assertTrue(compileall.compile_file(self.source_path, |
| 93 | force=False, quiet=True)) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 94 | self.assertTrue(os.path.isfile(self.bc_path) and |
| 95 | not os.path.isfile(self.bc_path2)) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 96 | os.unlink(self.bc_path) |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 97 | self.assertTrue(compileall.compile_dir(self.directory, force=False, |
| 98 | quiet=True)) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 99 | self.assertTrue(os.path.isfile(self.bc_path) and |
| 100 | os.path.isfile(self.bc_path2)) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 101 | os.unlink(self.bc_path) |
| 102 | os.unlink(self.bc_path2) |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 103 | # Test against bad files |
| 104 | self.add_bad_source_file() |
| 105 | self.assertFalse(compileall.compile_file(self.bad_source_path, |
| 106 | force=False, quiet=2)) |
| 107 | self.assertFalse(compileall.compile_dir(self.directory, |
| 108 | force=False, quiet=2)) |
| 109 | |
Berker Peksag | 812a2b6 | 2016-10-01 00:54:18 +0300 | [diff] [blame] | 110 | def test_compile_file_pathlike(self): |
| 111 | self.assertFalse(os.path.isfile(self.bc_path)) |
| 112 | # we should also test the output |
| 113 | with support.captured_stdout() as stdout: |
| 114 | self.assertTrue(compileall.compile_file(pathlib.Path(self.source_path))) |
Berker Peksag | d8e9713 | 2016-10-01 02:44:37 +0300 | [diff] [blame] | 115 | self.assertRegex(stdout.getvalue(), r'Compiling ([^WindowsPath|PosixPath].*)') |
Berker Peksag | 812a2b6 | 2016-10-01 00:54:18 +0300 | [diff] [blame] | 116 | self.assertTrue(os.path.isfile(self.bc_path)) |
| 117 | |
| 118 | def test_compile_file_pathlike_ddir(self): |
| 119 | self.assertFalse(os.path.isfile(self.bc_path)) |
| 120 | self.assertTrue(compileall.compile_file(pathlib.Path(self.source_path), |
| 121 | ddir=pathlib.Path('ddir_path'), |
| 122 | quiet=2)) |
| 123 | self.assertTrue(os.path.isfile(self.bc_path)) |
| 124 | |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 125 | def test_compile_path(self): |
Berker Peksag | 408b78c | 2016-09-28 17:38:53 +0300 | [diff] [blame] | 126 | with test.test_importlib.util.import_state(path=[self.directory]): |
| 127 | self.assertTrue(compileall.compile_path(quiet=2)) |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 128 | |
| 129 | with test.test_importlib.util.import_state(path=[self.directory]): |
| 130 | self.add_bad_source_file() |
| 131 | self.assertFalse(compileall.compile_path(skip_curdir=False, |
| 132 | force=True, quiet=2)) |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 133 | |
Barry Warsaw | c8a99de | 2010-04-29 18:43:10 +0000 | [diff] [blame] | 134 | def test_no_pycache_in_non_package(self): |
| 135 | # Bug 8563 reported that __pycache__ directories got created by |
| 136 | # compile_file() for non-.py files. |
| 137 | data_dir = os.path.join(self.directory, 'data') |
| 138 | data_file = os.path.join(data_dir, 'file') |
| 139 | os.mkdir(data_dir) |
| 140 | # touch data/file |
| 141 | with open(data_file, 'w'): |
| 142 | pass |
| 143 | compileall.compile_file(data_file) |
| 144 | self.assertFalse(os.path.exists(os.path.join(data_dir, '__pycache__'))) |
| 145 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 146 | def test_optimize(self): |
| 147 | # make sure compiling with different optimization settings than the |
| 148 | # interpreter's creates the correct file names |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 149 | optimize, opt = (1, 1) if __debug__ else (0, '') |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 150 | compileall.compile_dir(self.directory, quiet=True, optimize=optimize) |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 151 | cached = importlib.util.cache_from_source(self.source_path, |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 152 | optimization=opt) |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 153 | self.assertTrue(os.path.isfile(cached)) |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 154 | cached2 = importlib.util.cache_from_source(self.source_path2, |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 155 | optimization=opt) |
Georg Brandl | 4543846 | 2011-02-07 12:36:54 +0000 | [diff] [blame] | 156 | self.assertTrue(os.path.isfile(cached2)) |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 157 | cached3 = importlib.util.cache_from_source(self.source_path3, |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 158 | optimization=opt) |
Georg Brandl | 4543846 | 2011-02-07 12:36:54 +0000 | [diff] [blame] | 159 | self.assertTrue(os.path.isfile(cached3)) |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 160 | |
Berker Peksag | 812a2b6 | 2016-10-01 00:54:18 +0300 | [diff] [blame] | 161 | def test_compile_dir_pathlike(self): |
| 162 | self.assertFalse(os.path.isfile(self.bc_path)) |
| 163 | with support.captured_stdout() as stdout: |
| 164 | compileall.compile_dir(pathlib.Path(self.directory)) |
Berker Peksag | d8e9713 | 2016-10-01 02:44:37 +0300 | [diff] [blame] | 165 | line = stdout.getvalue().splitlines()[0] |
| 166 | self.assertRegex(line, r'Listing ([^WindowsPath|PosixPath].*)') |
Berker Peksag | 812a2b6 | 2016-10-01 00:54:18 +0300 | [diff] [blame] | 167 | self.assertTrue(os.path.isfile(self.bc_path)) |
| 168 | |
Dustin Spicuzza | 1d817e4 | 2018-11-23 12:06:55 -0500 | [diff] [blame] | 169 | @mock.patch('concurrent.futures.ProcessPoolExecutor') |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 170 | def test_compile_pool_called(self, pool_mock): |
| 171 | compileall.compile_dir(self.directory, quiet=True, workers=5) |
| 172 | self.assertTrue(pool_mock.called) |
| 173 | |
| 174 | def test_compile_workers_non_positive(self): |
| 175 | with self.assertRaisesRegex(ValueError, |
| 176 | "workers must be greater or equal to 0"): |
| 177 | compileall.compile_dir(self.directory, workers=-1) |
| 178 | |
Dustin Spicuzza | 1d817e4 | 2018-11-23 12:06:55 -0500 | [diff] [blame] | 179 | @mock.patch('concurrent.futures.ProcessPoolExecutor') |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 180 | def test_compile_workers_cpu_count(self, pool_mock): |
| 181 | compileall.compile_dir(self.directory, quiet=True, workers=0) |
| 182 | self.assertEqual(pool_mock.call_args[1]['max_workers'], None) |
| 183 | |
Dustin Spicuzza | 1d817e4 | 2018-11-23 12:06:55 -0500 | [diff] [blame] | 184 | @mock.patch('concurrent.futures.ProcessPoolExecutor') |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 185 | @mock.patch('compileall.compile_file') |
| 186 | def test_compile_one_worker(self, compile_file_mock, pool_mock): |
| 187 | compileall.compile_dir(self.directory, quiet=True) |
| 188 | self.assertFalse(pool_mock.called) |
| 189 | self.assertTrue(compile_file_mock.called) |
| 190 | |
Dustin Spicuzza | 1d817e4 | 2018-11-23 12:06:55 -0500 | [diff] [blame] | 191 | @mock.patch('concurrent.futures.ProcessPoolExecutor', new=None) |
Berker Peksag | d86ef05 | 2015-04-22 09:39:19 +0300 | [diff] [blame] | 192 | @mock.patch('compileall.compile_file') |
| 193 | def test_compile_missing_multiprocessing(self, compile_file_mock): |
| 194 | compileall.compile_dir(self.directory, quiet=True, workers=5) |
| 195 | self.assertTrue(compile_file_mock.called) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 196 | |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 197 | |
| 198 | class CompileallTestsWithSourceEpoch(CompileallTestsBase, |
| 199 | unittest.TestCase, |
| 200 | metaclass=SourceDateEpochTestMeta, |
| 201 | source_date_epoch=True): |
| 202 | pass |
| 203 | |
| 204 | |
| 205 | class CompileallTestsWithoutSourceEpoch(CompileallTestsBase, |
| 206 | unittest.TestCase, |
| 207 | metaclass=SourceDateEpochTestMeta, |
| 208 | source_date_epoch=False): |
| 209 | pass |
| 210 | |
| 211 | |
Martin v. Löwis | 4b00307 | 2010-03-16 13:19:21 +0000 | [diff] [blame] | 212 | class EncodingTest(unittest.TestCase): |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 213 | """Issue 6716: compileall should escape source code when printing errors |
| 214 | to stdout.""" |
Martin v. Löwis | 4b00307 | 2010-03-16 13:19:21 +0000 | [diff] [blame] | 215 | |
| 216 | def setUp(self): |
| 217 | self.directory = tempfile.mkdtemp() |
| 218 | self.source_path = os.path.join(self.directory, '_test.py') |
| 219 | with open(self.source_path, 'w', encoding='utf-8') as file: |
| 220 | file.write('# -*- coding: utf-8 -*-\n') |
| 221 | file.write('print u"\u20ac"\n') |
| 222 | |
| 223 | def tearDown(self): |
| 224 | shutil.rmtree(self.directory) |
| 225 | |
| 226 | def test_error(self): |
| 227 | try: |
| 228 | orig_stdout = sys.stdout |
| 229 | sys.stdout = io.TextIOWrapper(io.BytesIO(),encoding='ascii') |
| 230 | compileall.compile_dir(self.directory) |
| 231 | finally: |
| 232 | sys.stdout = orig_stdout |
| 233 | |
Barry Warsaw | c8a99de | 2010-04-29 18:43:10 +0000 | [diff] [blame] | 234 | |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 235 | class CommandLineTestsBase: |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 236 | """Test compileall's CLI.""" |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 237 | |
Brett Cannon | 65ed750 | 2015-10-09 15:09:43 -0700 | [diff] [blame] | 238 | @classmethod |
| 239 | def setUpClass(cls): |
| 240 | for path in filter(os.path.isdir, sys.path): |
| 241 | directory_created = False |
| 242 | directory = pathlib.Path(path) / '__pycache__' |
| 243 | path = directory / 'test.try' |
| 244 | try: |
| 245 | if not directory.is_dir(): |
| 246 | directory.mkdir() |
| 247 | directory_created = True |
| 248 | with path.open('w') as file: |
| 249 | file.write('# for test_compileall') |
| 250 | except OSError: |
| 251 | sys_path_writable = False |
| 252 | break |
| 253 | finally: |
| 254 | support.unlink(str(path)) |
| 255 | if directory_created: |
| 256 | directory.rmdir() |
| 257 | else: |
| 258 | sys_path_writable = True |
| 259 | cls._sys_path_writable = sys_path_writable |
| 260 | |
| 261 | def _skip_if_sys_path_not_writable(self): |
| 262 | if not self._sys_path_writable: |
| 263 | raise unittest.SkipTest('not all entries on sys.path are writable') |
| 264 | |
Benjamin Peterson | a820c7c | 2012-09-25 11:42:35 -0400 | [diff] [blame] | 265 | def _get_run_args(self, args): |
Victor Stinner | 9def284 | 2016-01-18 12:15:08 +0100 | [diff] [blame] | 266 | return [*support.optim_args_from_interpreter_flags(), |
| 267 | '-S', '-m', 'compileall', |
| 268 | *args] |
Benjamin Peterson | a820c7c | 2012-09-25 11:42:35 -0400 | [diff] [blame] | 269 | |
R. David Murray | 5317e9c | 2010-12-16 19:08:51 +0000 | [diff] [blame] | 270 | def assertRunOK(self, *args, **env_vars): |
| 271 | rc, out, err = script_helper.assert_python_ok( |
Benjamin Peterson | a820c7c | 2012-09-25 11:42:35 -0400 | [diff] [blame] | 272 | *self._get_run_args(args), **env_vars) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 273 | self.assertEqual(b'', err) |
| 274 | return out |
| 275 | |
R. David Murray | 5317e9c | 2010-12-16 19:08:51 +0000 | [diff] [blame] | 276 | def assertRunNotOK(self, *args, **env_vars): |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 277 | rc, out, err = script_helper.assert_python_failure( |
Benjamin Peterson | a820c7c | 2012-09-25 11:42:35 -0400 | [diff] [blame] | 278 | *self._get_run_args(args), **env_vars) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 279 | return rc, out, err |
| 280 | |
| 281 | def assertCompiled(self, fn): |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 282 | path = importlib.util.cache_from_source(fn) |
| 283 | self.assertTrue(os.path.exists(path)) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 284 | |
| 285 | def assertNotCompiled(self, fn): |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 286 | path = importlib.util.cache_from_source(fn) |
| 287 | self.assertFalse(os.path.exists(path)) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 288 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 289 | def setUp(self): |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 290 | self.directory = tempfile.mkdtemp() |
Brett Cannon | 65ed750 | 2015-10-09 15:09:43 -0700 | [diff] [blame] | 291 | self.addCleanup(support.rmtree, self.directory) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 292 | self.pkgdir = os.path.join(self.directory, 'foo') |
| 293 | os.mkdir(self.pkgdir) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 294 | self.pkgdir_cachedir = os.path.join(self.pkgdir, '__pycache__') |
| 295 | # Create the __init__.py and a package module. |
| 296 | self.initfn = script_helper.make_script(self.pkgdir, '__init__', '') |
| 297 | self.barfn = script_helper.make_script(self.pkgdir, 'bar', '') |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 298 | |
R. David Murray | 5317e9c | 2010-12-16 19:08:51 +0000 | [diff] [blame] | 299 | def test_no_args_compiles_path(self): |
| 300 | # Note that -l is implied for the no args case. |
Brett Cannon | 65ed750 | 2015-10-09 15:09:43 -0700 | [diff] [blame] | 301 | self._skip_if_sys_path_not_writable() |
R. David Murray | 5317e9c | 2010-12-16 19:08:51 +0000 | [diff] [blame] | 302 | bazfn = script_helper.make_script(self.directory, 'baz', '') |
| 303 | self.assertRunOK(PYTHONPATH=self.directory) |
| 304 | self.assertCompiled(bazfn) |
| 305 | self.assertNotCompiled(self.initfn) |
| 306 | self.assertNotCompiled(self.barfn) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 307 | |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 308 | @without_source_date_epoch # timestamp invalidation test |
R David Murray | 8a1d1e6 | 2013-12-15 20:49:38 -0500 | [diff] [blame] | 309 | def test_no_args_respects_force_flag(self): |
Brett Cannon | 65ed750 | 2015-10-09 15:09:43 -0700 | [diff] [blame] | 310 | self._skip_if_sys_path_not_writable() |
R David Murray | 8a1d1e6 | 2013-12-15 20:49:38 -0500 | [diff] [blame] | 311 | bazfn = script_helper.make_script(self.directory, 'baz', '') |
| 312 | self.assertRunOK(PYTHONPATH=self.directory) |
R David Murray | 755d5ea | 2013-12-15 20:56:00 -0500 | [diff] [blame] | 313 | pycpath = importlib.util.cache_from_source(bazfn) |
R David Murray | 8a1d1e6 | 2013-12-15 20:49:38 -0500 | [diff] [blame] | 314 | # Set atime/mtime backward to avoid file timestamp resolution issues |
| 315 | os.utime(pycpath, (time.time()-60,)*2) |
| 316 | mtime = os.stat(pycpath).st_mtime |
| 317 | # Without force, no recompilation |
| 318 | self.assertRunOK(PYTHONPATH=self.directory) |
| 319 | mtime2 = os.stat(pycpath).st_mtime |
| 320 | self.assertEqual(mtime, mtime2) |
| 321 | # Now force it. |
| 322 | self.assertRunOK('-f', PYTHONPATH=self.directory) |
| 323 | mtime2 = os.stat(pycpath).st_mtime |
| 324 | self.assertNotEqual(mtime, mtime2) |
| 325 | |
| 326 | def test_no_args_respects_quiet_flag(self): |
Brett Cannon | 65ed750 | 2015-10-09 15:09:43 -0700 | [diff] [blame] | 327 | self._skip_if_sys_path_not_writable() |
R David Murray | 8a1d1e6 | 2013-12-15 20:49:38 -0500 | [diff] [blame] | 328 | script_helper.make_script(self.directory, 'baz', '') |
| 329 | noisy = self.assertRunOK(PYTHONPATH=self.directory) |
| 330 | self.assertIn(b'Listing ', noisy) |
| 331 | quiet = self.assertRunOK('-q', PYTHONPATH=self.directory) |
| 332 | self.assertNotIn(b'Listing ', quiet) |
| 333 | |
Georg Brandl | 1463a3f | 2010-10-14 07:42:27 +0000 | [diff] [blame] | 334 | # Ensure that the default behavior of compileall's CLI is to create |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 335 | # PEP 3147/PEP 488 pyc files. |
Georg Brandl | 1463a3f | 2010-10-14 07:42:27 +0000 | [diff] [blame] | 336 | for name, ext, switch in [ |
| 337 | ('normal', 'pyc', []), |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 338 | ('optimize', 'opt-1.pyc', ['-O']), |
| 339 | ('doubleoptimize', 'opt-2.pyc', ['-OO']), |
Georg Brandl | 1463a3f | 2010-10-14 07:42:27 +0000 | [diff] [blame] | 340 | ]: |
| 341 | def f(self, ext=ext, switch=switch): |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 342 | script_helper.assert_python_ok(*(switch + |
| 343 | ['-m', 'compileall', '-q', self.pkgdir])) |
Georg Brandl | 1463a3f | 2010-10-14 07:42:27 +0000 | [diff] [blame] | 344 | # Verify the __pycache__ directory contents. |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 345 | self.assertTrue(os.path.exists(self.pkgdir_cachedir)) |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 346 | expected = sorted(base.format(sys.implementation.cache_tag, ext) |
| 347 | for base in ('__init__.{}.{}', 'bar.{}.{}')) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 348 | self.assertEqual(sorted(os.listdir(self.pkgdir_cachedir)), expected) |
Georg Brandl | 1463a3f | 2010-10-14 07:42:27 +0000 | [diff] [blame] | 349 | # Make sure there are no .pyc files in the source directory. |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 350 | self.assertFalse([fn for fn in os.listdir(self.pkgdir) |
| 351 | if fn.endswith(ext)]) |
Georg Brandl | 1463a3f | 2010-10-14 07:42:27 +0000 | [diff] [blame] | 352 | locals()['test_pep3147_paths_' + name] = f |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 353 | |
| 354 | def test_legacy_paths(self): |
| 355 | # Ensure that with the proper switch, compileall leaves legacy |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 356 | # pyc files, and no __pycache__ directory. |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 357 | self.assertRunOK('-b', '-q', self.pkgdir) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 358 | # Verify the __pycache__ directory contents. |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 359 | self.assertFalse(os.path.exists(self.pkgdir_cachedir)) |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 360 | expected = sorted(['__init__.py', '__init__.pyc', 'bar.py', |
| 361 | 'bar.pyc']) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 362 | self.assertEqual(sorted(os.listdir(self.pkgdir)), expected) |
| 363 | |
Barry Warsaw | c04317f | 2010-04-26 15:59:03 +0000 | [diff] [blame] | 364 | def test_multiple_runs(self): |
| 365 | # Bug 8527 reported that multiple calls produced empty |
| 366 | # __pycache__/__pycache__ directories. |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 367 | self.assertRunOK('-q', self.pkgdir) |
Barry Warsaw | c04317f | 2010-04-26 15:59:03 +0000 | [diff] [blame] | 368 | # Verify the __pycache__ directory contents. |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 369 | self.assertTrue(os.path.exists(self.pkgdir_cachedir)) |
| 370 | cachecachedir = os.path.join(self.pkgdir_cachedir, '__pycache__') |
Barry Warsaw | c04317f | 2010-04-26 15:59:03 +0000 | [diff] [blame] | 371 | self.assertFalse(os.path.exists(cachecachedir)) |
| 372 | # Call compileall again. |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 373 | self.assertRunOK('-q', self.pkgdir) |
| 374 | self.assertTrue(os.path.exists(self.pkgdir_cachedir)) |
Barry Warsaw | c04317f | 2010-04-26 15:59:03 +0000 | [diff] [blame] | 375 | self.assertFalse(os.path.exists(cachecachedir)) |
| 376 | |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 377 | @without_source_date_epoch # timestamp invalidation test |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 378 | def test_force(self): |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 379 | self.assertRunOK('-q', self.pkgdir) |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 380 | pycpath = importlib.util.cache_from_source(self.barfn) |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 381 | # set atime/mtime backward to avoid file timestamp resolution issues |
| 382 | os.utime(pycpath, (time.time()-60,)*2) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 383 | mtime = os.stat(pycpath).st_mtime |
| 384 | # without force, no recompilation |
| 385 | self.assertRunOK('-q', self.pkgdir) |
| 386 | mtime2 = os.stat(pycpath).st_mtime |
| 387 | self.assertEqual(mtime, mtime2) |
| 388 | # now force it. |
| 389 | self.assertRunOK('-q', '-f', self.pkgdir) |
| 390 | mtime2 = os.stat(pycpath).st_mtime |
| 391 | self.assertNotEqual(mtime, mtime2) |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 392 | |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 393 | def test_recursion_control(self): |
| 394 | subpackage = os.path.join(self.pkgdir, 'spam') |
| 395 | os.mkdir(subpackage) |
| 396 | subinitfn = script_helper.make_script(subpackage, '__init__', '') |
| 397 | hamfn = script_helper.make_script(subpackage, 'ham', '') |
| 398 | self.assertRunOK('-q', '-l', self.pkgdir) |
| 399 | self.assertNotCompiled(subinitfn) |
| 400 | self.assertFalse(os.path.exists(os.path.join(subpackage, '__pycache__'))) |
| 401 | self.assertRunOK('-q', self.pkgdir) |
| 402 | self.assertCompiled(subinitfn) |
| 403 | self.assertCompiled(hamfn) |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 404 | |
Benjamin Peterson | 344ff4a | 2014-08-19 16:13:26 -0500 | [diff] [blame] | 405 | def test_recursion_limit(self): |
| 406 | subpackage = os.path.join(self.pkgdir, 'spam') |
| 407 | subpackage2 = os.path.join(subpackage, 'ham') |
| 408 | subpackage3 = os.path.join(subpackage2, 'eggs') |
| 409 | for pkg in (subpackage, subpackage2, subpackage3): |
| 410 | script_helper.make_pkg(pkg) |
| 411 | |
| 412 | subinitfn = os.path.join(subpackage, '__init__.py') |
| 413 | hamfn = script_helper.make_script(subpackage, 'ham', '') |
| 414 | spamfn = script_helper.make_script(subpackage2, 'spam', '') |
| 415 | eggfn = script_helper.make_script(subpackage3, 'egg', '') |
| 416 | |
| 417 | self.assertRunOK('-q', '-r 0', self.pkgdir) |
| 418 | self.assertNotCompiled(subinitfn) |
| 419 | self.assertFalse( |
| 420 | os.path.exists(os.path.join(subpackage, '__pycache__'))) |
| 421 | |
| 422 | self.assertRunOK('-q', '-r 1', self.pkgdir) |
| 423 | self.assertCompiled(subinitfn) |
| 424 | self.assertCompiled(hamfn) |
| 425 | self.assertNotCompiled(spamfn) |
| 426 | |
| 427 | self.assertRunOK('-q', '-r 2', self.pkgdir) |
| 428 | self.assertCompiled(subinitfn) |
| 429 | self.assertCompiled(hamfn) |
| 430 | self.assertCompiled(spamfn) |
| 431 | self.assertNotCompiled(eggfn) |
| 432 | |
| 433 | self.assertRunOK('-q', '-r 5', self.pkgdir) |
| 434 | self.assertCompiled(subinitfn) |
| 435 | self.assertCompiled(hamfn) |
| 436 | self.assertCompiled(spamfn) |
| 437 | self.assertCompiled(eggfn) |
| 438 | |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 439 | def test_quiet(self): |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 440 | noisy = self.assertRunOK(self.pkgdir) |
| 441 | quiet = self.assertRunOK('-q', self.pkgdir) |
| 442 | self.assertNotEqual(b'', noisy) |
| 443 | self.assertEqual(b'', quiet) |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 444 | |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 445 | def test_silent(self): |
| 446 | script_helper.make_script(self.pkgdir, 'crunchyfrog', 'bad(syntax') |
| 447 | _, quiet, _ = self.assertRunNotOK('-q', self.pkgdir) |
| 448 | _, silent, _ = self.assertRunNotOK('-qq', self.pkgdir) |
| 449 | self.assertNotEqual(b'', quiet) |
| 450 | self.assertEqual(b'', silent) |
| 451 | |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 452 | def test_regexp(self): |
R David Murray | ee1a7cb | 2011-07-01 14:55:43 -0400 | [diff] [blame] | 453 | self.assertRunOK('-q', '-x', r'ba[^\\/]*$', self.pkgdir) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 454 | self.assertNotCompiled(self.barfn) |
| 455 | self.assertCompiled(self.initfn) |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 456 | |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 457 | def test_multiple_dirs(self): |
| 458 | pkgdir2 = os.path.join(self.directory, 'foo2') |
| 459 | os.mkdir(pkgdir2) |
| 460 | init2fn = script_helper.make_script(pkgdir2, '__init__', '') |
| 461 | bar2fn = script_helper.make_script(pkgdir2, 'bar2', '') |
| 462 | self.assertRunOK('-q', self.pkgdir, pkgdir2) |
| 463 | self.assertCompiled(self.initfn) |
| 464 | self.assertCompiled(self.barfn) |
| 465 | self.assertCompiled(init2fn) |
| 466 | self.assertCompiled(bar2fn) |
| 467 | |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 468 | def test_d_compile_error(self): |
| 469 | script_helper.make_script(self.pkgdir, 'crunchyfrog', 'bad(syntax') |
| 470 | rc, out, err = self.assertRunNotOK('-q', '-d', 'dinsdale', self.pkgdir) |
| 471 | self.assertRegex(out, b'File "dinsdale') |
| 472 | |
| 473 | def test_d_runtime_error(self): |
| 474 | bazfn = script_helper.make_script(self.pkgdir, 'baz', 'raise Exception') |
| 475 | self.assertRunOK('-q', '-d', 'dinsdale', self.pkgdir) |
| 476 | fn = script_helper.make_script(self.pkgdir, 'bing', 'import baz') |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 477 | pyc = importlib.util.cache_from_source(bazfn) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 478 | os.rename(pyc, os.path.join(self.pkgdir, 'baz.pyc')) |
| 479 | os.remove(bazfn) |
Victor Stinner | e8785ff | 2013-10-12 14:44:01 +0200 | [diff] [blame] | 480 | rc, out, err = script_helper.assert_python_failure(fn, __isolated=False) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 481 | self.assertRegex(err, b'File "dinsdale') |
| 482 | |
| 483 | def test_include_bad_file(self): |
| 484 | rc, out, err = self.assertRunNotOK( |
| 485 | '-i', os.path.join(self.directory, 'nosuchfile'), self.pkgdir) |
| 486 | self.assertRegex(out, b'rror.*nosuchfile') |
| 487 | self.assertNotRegex(err, b'Traceback') |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 488 | self.assertFalse(os.path.exists(importlib.util.cache_from_source( |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 489 | self.pkgdir_cachedir))) |
| 490 | |
| 491 | def test_include_file_with_arg(self): |
| 492 | f1 = script_helper.make_script(self.pkgdir, 'f1', '') |
| 493 | f2 = script_helper.make_script(self.pkgdir, 'f2', '') |
| 494 | f3 = script_helper.make_script(self.pkgdir, 'f3', '') |
| 495 | f4 = script_helper.make_script(self.pkgdir, 'f4', '') |
| 496 | with open(os.path.join(self.directory, 'l1'), 'w') as l1: |
| 497 | l1.write(os.path.join(self.pkgdir, 'f1.py')+os.linesep) |
| 498 | l1.write(os.path.join(self.pkgdir, 'f2.py')+os.linesep) |
| 499 | self.assertRunOK('-i', os.path.join(self.directory, 'l1'), f4) |
| 500 | self.assertCompiled(f1) |
| 501 | self.assertCompiled(f2) |
| 502 | self.assertNotCompiled(f3) |
| 503 | self.assertCompiled(f4) |
| 504 | |
| 505 | def test_include_file_no_arg(self): |
| 506 | f1 = script_helper.make_script(self.pkgdir, 'f1', '') |
| 507 | f2 = script_helper.make_script(self.pkgdir, 'f2', '') |
| 508 | f3 = script_helper.make_script(self.pkgdir, 'f3', '') |
| 509 | f4 = script_helper.make_script(self.pkgdir, 'f4', '') |
| 510 | with open(os.path.join(self.directory, 'l1'), 'w') as l1: |
| 511 | l1.write(os.path.join(self.pkgdir, 'f2.py')+os.linesep) |
| 512 | self.assertRunOK('-i', os.path.join(self.directory, 'l1')) |
| 513 | self.assertNotCompiled(f1) |
| 514 | self.assertCompiled(f2) |
| 515 | self.assertNotCompiled(f3) |
| 516 | self.assertNotCompiled(f4) |
| 517 | |
| 518 | def test_include_on_stdin(self): |
| 519 | f1 = script_helper.make_script(self.pkgdir, 'f1', '') |
| 520 | f2 = script_helper.make_script(self.pkgdir, 'f2', '') |
| 521 | f3 = script_helper.make_script(self.pkgdir, 'f3', '') |
| 522 | f4 = script_helper.make_script(self.pkgdir, 'f4', '') |
Benjamin Peterson | a820c7c | 2012-09-25 11:42:35 -0400 | [diff] [blame] | 523 | p = script_helper.spawn_python(*(self._get_run_args(()) + ['-i', '-'])) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 524 | p.stdin.write((f3+os.linesep).encode('ascii')) |
| 525 | script_helper.kill_python(p) |
| 526 | self.assertNotCompiled(f1) |
| 527 | self.assertNotCompiled(f2) |
| 528 | self.assertCompiled(f3) |
| 529 | self.assertNotCompiled(f4) |
| 530 | |
| 531 | def test_compiles_as_much_as_possible(self): |
| 532 | bingfn = script_helper.make_script(self.pkgdir, 'bing', 'syntax(error') |
| 533 | rc, out, err = self.assertRunNotOK('nosuchfile', self.initfn, |
| 534 | bingfn, self.barfn) |
R. David Murray | 5317e9c | 2010-12-16 19:08:51 +0000 | [diff] [blame] | 535 | self.assertRegex(out, b'rror') |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 536 | self.assertNotCompiled(bingfn) |
| 537 | self.assertCompiled(self.initfn) |
| 538 | self.assertCompiled(self.barfn) |
| 539 | |
R. David Murray | 5317e9c | 2010-12-16 19:08:51 +0000 | [diff] [blame] | 540 | def test_invalid_arg_produces_message(self): |
| 541 | out = self.assertRunOK('badfilename') |
Victor Stinner | 5307126 | 2011-05-11 00:36:28 +0200 | [diff] [blame] | 542 | self.assertRegex(out, b"Can't list 'badfilename'") |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 543 | |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 544 | def test_pyc_invalidation_mode(self): |
| 545 | script_helper.make_script(self.pkgdir, 'f1', '') |
| 546 | pyc = importlib.util.cache_from_source( |
| 547 | os.path.join(self.pkgdir, 'f1.py')) |
| 548 | self.assertRunOK('--invalidation-mode=checked-hash', self.pkgdir) |
| 549 | with open(pyc, 'rb') as fp: |
| 550 | data = fp.read() |
| 551 | self.assertEqual(int.from_bytes(data[4:8], 'little'), 0b11) |
| 552 | self.assertRunOK('--invalidation-mode=unchecked-hash', self.pkgdir) |
| 553 | with open(pyc, 'rb') as fp: |
| 554 | data = fp.read() |
| 555 | self.assertEqual(int.from_bytes(data[4:8], 'little'), 0b01) |
| 556 | |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 557 | @skipUnless(_have_multiprocessing, "requires multiprocessing") |
| 558 | def test_workers(self): |
| 559 | bar2fn = script_helper.make_script(self.directory, 'bar2', '') |
| 560 | files = [] |
| 561 | for suffix in range(5): |
| 562 | pkgdir = os.path.join(self.directory, 'foo{}'.format(suffix)) |
| 563 | os.mkdir(pkgdir) |
| 564 | fn = script_helper.make_script(pkgdir, '__init__', '') |
| 565 | files.append(script_helper.make_script(pkgdir, 'bar2', '')) |
| 566 | |
| 567 | self.assertRunOK(self.directory, '-j', '0') |
| 568 | self.assertCompiled(bar2fn) |
| 569 | for file in files: |
| 570 | self.assertCompiled(file) |
| 571 | |
| 572 | @mock.patch('compileall.compile_dir') |
| 573 | def test_workers_available_cores(self, compile_dir): |
| 574 | with mock.patch("sys.argv", |
| 575 | new=[sys.executable, self.directory, "-j0"]): |
| 576 | compileall.main() |
| 577 | self.assertTrue(compile_dir.called) |
Antoine Pitrou | 1a2dd82 | 2019-05-15 23:45:18 +0200 | [diff] [blame] | 578 | self.assertEqual(compile_dir.call_args[-1]['workers'], 0) |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 579 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 580 | |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 581 | class CommmandLineTestsWithSourceEpoch(CommandLineTestsBase, |
| 582 | unittest.TestCase, |
| 583 | metaclass=SourceDateEpochTestMeta, |
| 584 | source_date_epoch=True): |
| 585 | pass |
| 586 | |
| 587 | |
| 588 | class CommmandLineTestsNoSourceEpoch(CommandLineTestsBase, |
| 589 | unittest.TestCase, |
| 590 | metaclass=SourceDateEpochTestMeta, |
| 591 | source_date_epoch=False): |
| 592 | pass |
| 593 | |
| 594 | |
| 595 | |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 596 | if __name__ == "__main__": |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 597 | unittest.main() |