blob: cb59fd71b38254f172dd41b63b82b8da4195b135 [file] [log] [blame]
Martin v. Löwis4b003072010-03-16 13:19:21 +00001import sys
Brett Cannonbefb14f2009-02-10 02:10:16 +00002import compileall
Brett Cannon7822e122013-06-14 23:04:02 -04003import importlib.util
Brett Cannon1e3c3e92015-12-27 13:17:04 -08004import test.test_importlib.util
Brett Cannonbefb14f2009-02-10 02:10:16 +00005import os
Brett Cannon65ed7502015-10-09 15:09:43 -07006import pathlib
Brett Cannonbefb14f2009-02-10 02:10:16 +00007import py_compile
8import shutil
9import struct
Brett Cannonbefb14f2009-02-10 02:10:16 +000010import tempfile
R. David Murray650f1472010-11-20 21:18:51 +000011import time
Brett Cannonbefb14f2009-02-10 02:10:16 +000012import unittest
Martin v. Löwis4b003072010-03-16 13:19:21 +000013import io
Petr Viktorin4267c982019-09-26 11:53:51 +020014import errno
Brett Cannonbefb14f2009-02-10 02:10:16 +000015
Brett Cannonf1a8df02014-09-12 10:39:48 -040016from unittest import mock, skipUnless
17try:
18 from concurrent.futures import ProcessPoolExecutor
19 _have_multiprocessing = True
20except ImportError:
21 _have_multiprocessing = False
22
Berker Peksagce643912015-05-06 06:33:17 +030023from test import support
24from test.support import script_helper
Brett Cannonbefb14f2009-02-10 02:10:16 +000025
Elvis Pranskevichusa6b3ec52018-10-10 12:43:14 -040026from .test_py_compile import without_source_date_epoch
27from .test_py_compile import SourceDateEpochTestMeta
28
29
30class CompileallTestsBase:
Brett Cannonbefb14f2009-02-10 02:10:16 +000031
32 def setUp(self):
33 self.directory = tempfile.mkdtemp()
34 self.source_path = os.path.join(self.directory, '_test.py')
Brett Cannon7822e122013-06-14 23:04:02 -040035 self.bc_path = importlib.util.cache_from_source(self.source_path)
Brett Cannonbefb14f2009-02-10 02:10:16 +000036 with open(self.source_path, 'w') as file:
37 file.write('x = 123\n')
Matthias Klosec33b9022010-03-16 00:36:26 +000038 self.source_path2 = os.path.join(self.directory, '_test2.py')
Brett Cannon7822e122013-06-14 23:04:02 -040039 self.bc_path2 = importlib.util.cache_from_source(self.source_path2)
Matthias Klosec33b9022010-03-16 00:36:26 +000040 shutil.copyfile(self.source_path, self.source_path2)
Georg Brandl45438462011-02-07 12:36:54 +000041 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 Cannonbefb14f2009-02-10 02:10:16 +000045
46 def tearDown(self):
47 shutil.rmtree(self.directory)
48
Brett Cannon1e3c3e92015-12-27 13:17:04 -080049 def add_bad_source_file(self):
50 self.bad_source_path = os.path.join(self.directory, '_test_bad.py')
51 with open(self.bad_source_path, 'w') as file:
52 file.write('x (\n')
53
Elvis Pranskevichusa6b3ec52018-10-10 12:43:14 -040054 def timestamp_metadata(self):
Brett Cannonbefb14f2009-02-10 02:10:16 +000055 with open(self.bc_path, 'rb') as file:
Benjamin Peterson42aa93b2017-12-09 10:26:52 -080056 data = file.read(12)
Brett Cannonbefb14f2009-02-10 02:10:16 +000057 mtime = int(os.stat(self.source_path).st_mtime)
Benjamin Peterson42aa93b2017-12-09 10:26:52 -080058 compare = struct.pack('<4sll', importlib.util.MAGIC_NUMBER, 0, mtime)
Brett Cannonbefb14f2009-02-10 02:10:16 +000059 return data, compare
60
61 def recreation_check(self, metadata):
62 """Check that compileall recreates bytecode when the new metadata is
63 used."""
Elvis Pranskevichusa6b3ec52018-10-10 12:43:14 -040064 if os.environ.get('SOURCE_DATE_EPOCH'):
65 raise unittest.SkipTest('SOURCE_DATE_EPOCH is set')
Brett Cannonbefb14f2009-02-10 02:10:16 +000066 py_compile.compile(self.source_path)
Elvis Pranskevichusa6b3ec52018-10-10 12:43:14 -040067 self.assertEqual(*self.timestamp_metadata())
Brett Cannonbefb14f2009-02-10 02:10:16 +000068 with open(self.bc_path, 'rb') as file:
69 bc = file.read()[len(metadata):]
70 with open(self.bc_path, 'wb') as file:
71 file.write(metadata)
72 file.write(bc)
Elvis Pranskevichusa6b3ec52018-10-10 12:43:14 -040073 self.assertNotEqual(*self.timestamp_metadata())
Brett Cannonbefb14f2009-02-10 02:10:16 +000074 compileall.compile_dir(self.directory, force=False, quiet=True)
Elvis Pranskevichusa6b3ec52018-10-10 12:43:14 -040075 self.assertTrue(*self.timestamp_metadata())
Brett Cannonbefb14f2009-02-10 02:10:16 +000076
77 def test_mtime(self):
78 # Test a change in mtime leads to a new .pyc.
Benjamin Peterson42aa93b2017-12-09 10:26:52 -080079 self.recreation_check(struct.pack('<4sll', importlib.util.MAGIC_NUMBER,
80 0, 1))
Brett Cannonbefb14f2009-02-10 02:10:16 +000081
82 def test_magic_number(self):
83 # Test a change in mtime leads to a new .pyc.
84 self.recreation_check(b'\0\0\0\0')
85
Matthias Klosec33b9022010-03-16 00:36:26 +000086 def test_compile_files(self):
87 # Test compiling a single file, and complete directory
88 for fn in (self.bc_path, self.bc_path2):
89 try:
90 os.unlink(fn)
91 except:
92 pass
Brett Cannon1e3c3e92015-12-27 13:17:04 -080093 self.assertTrue(compileall.compile_file(self.source_path,
94 force=False, quiet=True))
Barry Warsaw28a691b2010-04-17 00:19:56 +000095 self.assertTrue(os.path.isfile(self.bc_path) and
96 not os.path.isfile(self.bc_path2))
Matthias Klosec33b9022010-03-16 00:36:26 +000097 os.unlink(self.bc_path)
Brett Cannon1e3c3e92015-12-27 13:17:04 -080098 self.assertTrue(compileall.compile_dir(self.directory, force=False,
99 quiet=True))
Barry Warsaw28a691b2010-04-17 00:19:56 +0000100 self.assertTrue(os.path.isfile(self.bc_path) and
101 os.path.isfile(self.bc_path2))
Matthias Klosec33b9022010-03-16 00:36:26 +0000102 os.unlink(self.bc_path)
103 os.unlink(self.bc_path2)
Brett Cannon1e3c3e92015-12-27 13:17:04 -0800104 # Test against bad files
105 self.add_bad_source_file()
106 self.assertFalse(compileall.compile_file(self.bad_source_path,
107 force=False, quiet=2))
108 self.assertFalse(compileall.compile_dir(self.directory,
109 force=False, quiet=2))
110
Berker Peksag812a2b62016-10-01 00:54:18 +0300111 def test_compile_file_pathlike(self):
112 self.assertFalse(os.path.isfile(self.bc_path))
113 # we should also test the output
114 with support.captured_stdout() as stdout:
115 self.assertTrue(compileall.compile_file(pathlib.Path(self.source_path)))
Berker Peksagd8e97132016-10-01 02:44:37 +0300116 self.assertRegex(stdout.getvalue(), r'Compiling ([^WindowsPath|PosixPath].*)')
Berker Peksag812a2b62016-10-01 00:54:18 +0300117 self.assertTrue(os.path.isfile(self.bc_path))
118
119 def test_compile_file_pathlike_ddir(self):
120 self.assertFalse(os.path.isfile(self.bc_path))
121 self.assertTrue(compileall.compile_file(pathlib.Path(self.source_path),
122 ddir=pathlib.Path('ddir_path'),
123 quiet=2))
124 self.assertTrue(os.path.isfile(self.bc_path))
125
Brett Cannon1e3c3e92015-12-27 13:17:04 -0800126 def test_compile_path(self):
Berker Peksag408b78c2016-09-28 17:38:53 +0300127 with test.test_importlib.util.import_state(path=[self.directory]):
128 self.assertTrue(compileall.compile_path(quiet=2))
Brett Cannon1e3c3e92015-12-27 13:17:04 -0800129
130 with test.test_importlib.util.import_state(path=[self.directory]):
131 self.add_bad_source_file()
132 self.assertFalse(compileall.compile_path(skip_curdir=False,
133 force=True, quiet=2))
Brett Cannonbefb14f2009-02-10 02:10:16 +0000134
Barry Warsawc8a99de2010-04-29 18:43:10 +0000135 def test_no_pycache_in_non_package(self):
136 # Bug 8563 reported that __pycache__ directories got created by
137 # compile_file() for non-.py files.
138 data_dir = os.path.join(self.directory, 'data')
139 data_file = os.path.join(data_dir, 'file')
140 os.mkdir(data_dir)
141 # touch data/file
142 with open(data_file, 'w'):
143 pass
144 compileall.compile_file(data_file)
145 self.assertFalse(os.path.exists(os.path.join(data_dir, '__pycache__')))
146
Georg Brandl8334fd92010-12-04 10:26:46 +0000147 def test_optimize(self):
148 # make sure compiling with different optimization settings than the
149 # interpreter's creates the correct file names
Brett Cannonf299abd2015-04-13 14:21:02 -0400150 optimize, opt = (1, 1) if __debug__ else (0, '')
Georg Brandl8334fd92010-12-04 10:26:46 +0000151 compileall.compile_dir(self.directory, quiet=True, optimize=optimize)
Brett Cannon7822e122013-06-14 23:04:02 -0400152 cached = importlib.util.cache_from_source(self.source_path,
Brett Cannonf299abd2015-04-13 14:21:02 -0400153 optimization=opt)
Georg Brandl8334fd92010-12-04 10:26:46 +0000154 self.assertTrue(os.path.isfile(cached))
Brett Cannon7822e122013-06-14 23:04:02 -0400155 cached2 = importlib.util.cache_from_source(self.source_path2,
Brett Cannonf299abd2015-04-13 14:21:02 -0400156 optimization=opt)
Georg Brandl45438462011-02-07 12:36:54 +0000157 self.assertTrue(os.path.isfile(cached2))
Brett Cannon7822e122013-06-14 23:04:02 -0400158 cached3 = importlib.util.cache_from_source(self.source_path3,
Brett Cannonf299abd2015-04-13 14:21:02 -0400159 optimization=opt)
Georg Brandl45438462011-02-07 12:36:54 +0000160 self.assertTrue(os.path.isfile(cached3))
Georg Brandl8334fd92010-12-04 10:26:46 +0000161
Berker Peksag812a2b62016-10-01 00:54:18 +0300162 def test_compile_dir_pathlike(self):
163 self.assertFalse(os.path.isfile(self.bc_path))
164 with support.captured_stdout() as stdout:
165 compileall.compile_dir(pathlib.Path(self.directory))
Berker Peksagd8e97132016-10-01 02:44:37 +0300166 line = stdout.getvalue().splitlines()[0]
167 self.assertRegex(line, r'Listing ([^WindowsPath|PosixPath].*)')
Berker Peksag812a2b62016-10-01 00:54:18 +0300168 self.assertTrue(os.path.isfile(self.bc_path))
169
Dustin Spicuzza1d817e42018-11-23 12:06:55 -0500170 @mock.patch('concurrent.futures.ProcessPoolExecutor')
Brett Cannonf1a8df02014-09-12 10:39:48 -0400171 def test_compile_pool_called(self, pool_mock):
172 compileall.compile_dir(self.directory, quiet=True, workers=5)
173 self.assertTrue(pool_mock.called)
174
175 def test_compile_workers_non_positive(self):
176 with self.assertRaisesRegex(ValueError,
177 "workers must be greater or equal to 0"):
178 compileall.compile_dir(self.directory, workers=-1)
179
Dustin Spicuzza1d817e42018-11-23 12:06:55 -0500180 @mock.patch('concurrent.futures.ProcessPoolExecutor')
Brett Cannonf1a8df02014-09-12 10:39:48 -0400181 def test_compile_workers_cpu_count(self, pool_mock):
182 compileall.compile_dir(self.directory, quiet=True, workers=0)
183 self.assertEqual(pool_mock.call_args[1]['max_workers'], None)
184
Dustin Spicuzza1d817e42018-11-23 12:06:55 -0500185 @mock.patch('concurrent.futures.ProcessPoolExecutor')
Brett Cannonf1a8df02014-09-12 10:39:48 -0400186 @mock.patch('compileall.compile_file')
187 def test_compile_one_worker(self, compile_file_mock, pool_mock):
188 compileall.compile_dir(self.directory, quiet=True)
189 self.assertFalse(pool_mock.called)
190 self.assertTrue(compile_file_mock.called)
191
Dustin Spicuzza1d817e42018-11-23 12:06:55 -0500192 @mock.patch('concurrent.futures.ProcessPoolExecutor', new=None)
Berker Peksagd86ef052015-04-22 09:39:19 +0300193 @mock.patch('compileall.compile_file')
194 def test_compile_missing_multiprocessing(self, compile_file_mock):
195 compileall.compile_dir(self.directory, quiet=True, workers=5)
196 self.assertTrue(compile_file_mock.called)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000197
Petr Viktorin4267c982019-09-26 11:53:51 +0200198 def test_compile_dir_maxlevels(self):
Victor Stinnereb1dda22019-10-15 11:26:13 +0200199 # Test the actual impact of maxlevels parameter
200 depth = 3
201 path = self.directory
202 for i in range(1, depth + 1):
203 path = os.path.join(path, f"dir_{i}")
204 source = os.path.join(path, 'script.py')
205 os.mkdir(path)
206 shutil.copyfile(self.source_path, source)
207 pyc_filename = importlib.util.cache_from_source(source)
208
209 compileall.compile_dir(self.directory, quiet=True, maxlevels=depth - 1)
210 self.assertFalse(os.path.isfile(pyc_filename))
211
212 compileall.compile_dir(self.directory, quiet=True, maxlevels=depth)
213 self.assertTrue(os.path.isfile(pyc_filename))
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200214
Gregory P. Smith02673352020-02-28 17:28:37 -0800215 def _test_ddir_only(self, *, ddir, parallel=True):
216 """Recursive compile_dir ddir must contain package paths; bpo39769."""
217 fullpath = ["test", "foo"]
218 path = self.directory
219 mods = []
220 for subdir in fullpath:
221 path = os.path.join(path, subdir)
222 os.mkdir(path)
223 script_helper.make_script(path, "__init__", "")
224 mods.append(script_helper.make_script(path, "mod",
225 "def fn(): 1/0\nfn()\n"))
226 compileall.compile_dir(
227 self.directory, quiet=True, ddir=ddir,
228 workers=2 if parallel else 1)
229 self.assertTrue(mods)
230 for mod in mods:
231 self.assertTrue(mod.startswith(self.directory), mod)
232 modcode = importlib.util.cache_from_source(mod)
233 modpath = mod[len(self.directory+os.sep):]
234 _, _, err = script_helper.assert_python_failure(modcode)
235 expected_in = os.path.join(ddir, modpath)
236 mod_code_obj = test.test_importlib.util.get_code_from_pyc(modcode)
237 self.assertEqual(mod_code_obj.co_filename, expected_in)
238 self.assertIn(f'"{expected_in}"', os.fsdecode(err))
239
240 def test_ddir_only_one_worker(self):
241 """Recursive compile_dir ddir= contains package paths; bpo39769."""
242 return self._test_ddir_only(ddir="<a prefix>", parallel=False)
243
244 def test_ddir_multiple_workers(self):
245 """Recursive compile_dir ddir= contains package paths; bpo39769."""
246 return self._test_ddir_only(ddir="<a prefix>", parallel=True)
247
248 def test_ddir_empty_only_one_worker(self):
249 """Recursive compile_dir ddir='' contains package paths; bpo39769."""
250 return self._test_ddir_only(ddir="", parallel=False)
251
252 def test_ddir_empty_multiple_workers(self):
253 """Recursive compile_dir ddir='' contains package paths; bpo39769."""
254 return self._test_ddir_only(ddir="", parallel=True)
255
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200256 def test_strip_only(self):
257 fullpath = ["test", "build", "real", "path"]
258 path = os.path.join(self.directory, *fullpath)
259 os.makedirs(path)
260 script = script_helper.make_script(path, "test", "1 / 0")
261 bc = importlib.util.cache_from_source(script)
262 stripdir = os.path.join(self.directory, *fullpath[:2])
263 compileall.compile_dir(path, quiet=True, stripdir=stripdir)
264 rc, out, err = script_helper.assert_python_failure(bc)
265 expected_in = os.path.join(*fullpath[2:])
266 self.assertIn(
267 expected_in,
268 str(err, encoding=sys.getdefaultencoding())
269 )
270 self.assertNotIn(
271 stripdir,
272 str(err, encoding=sys.getdefaultencoding())
273 )
274
275 def test_prepend_only(self):
276 fullpath = ["test", "build", "real", "path"]
277 path = os.path.join(self.directory, *fullpath)
278 os.makedirs(path)
279 script = script_helper.make_script(path, "test", "1 / 0")
280 bc = importlib.util.cache_from_source(script)
281 prependdir = "/foo"
282 compileall.compile_dir(path, quiet=True, prependdir=prependdir)
283 rc, out, err = script_helper.assert_python_failure(bc)
284 expected_in = os.path.join(prependdir, self.directory, *fullpath)
285 self.assertIn(
286 expected_in,
287 str(err, encoding=sys.getdefaultencoding())
288 )
289
290 def test_strip_and_prepend(self):
291 fullpath = ["test", "build", "real", "path"]
292 path = os.path.join(self.directory, *fullpath)
293 os.makedirs(path)
294 script = script_helper.make_script(path, "test", "1 / 0")
295 bc = importlib.util.cache_from_source(script)
296 stripdir = os.path.join(self.directory, *fullpath[:2])
297 prependdir = "/foo"
298 compileall.compile_dir(path, quiet=True,
299 stripdir=stripdir, prependdir=prependdir)
300 rc, out, err = script_helper.assert_python_failure(bc)
301 expected_in = os.path.join(prependdir, *fullpath[2:])
302 self.assertIn(
303 expected_in,
304 str(err, encoding=sys.getdefaultencoding())
305 )
306 self.assertNotIn(
307 stripdir,
308 str(err, encoding=sys.getdefaultencoding())
309 )
310
311 def test_strip_prepend_and_ddir(self):
312 fullpath = ["test", "build", "real", "path", "ddir"]
313 path = os.path.join(self.directory, *fullpath)
314 os.makedirs(path)
315 script_helper.make_script(path, "test", "1 / 0")
316 with self.assertRaises(ValueError):
317 compileall.compile_dir(path, quiet=True, ddir="/bar",
318 stripdir="/foo", prependdir="/bar")
319
320 def test_multiple_optimization_levels(self):
321 script = script_helper.make_script(self.directory,
322 "test_optimization",
323 "a = 0")
324 bc = []
325 for opt_level in "", 1, 2, 3:
326 bc.append(importlib.util.cache_from_source(script,
327 optimization=opt_level))
328 test_combinations = [[0, 1], [1, 2], [0, 2], [0, 1, 2]]
329 for opt_combination in test_combinations:
330 compileall.compile_file(script, quiet=True,
331 optimize=opt_combination)
332 for opt_level in opt_combination:
333 self.assertTrue(os.path.isfile(bc[opt_level]))
334 try:
335 os.unlink(bc[opt_level])
336 except Exception:
337 pass
338
339 @support.skip_unless_symlink
340 def test_ignore_symlink_destination(self):
341 # Create folders for allowed files, symlinks and prohibited area
342 allowed_path = os.path.join(self.directory, "test", "dir", "allowed")
343 symlinks_path = os.path.join(self.directory, "test", "dir", "symlinks")
344 prohibited_path = os.path.join(self.directory, "test", "dir", "prohibited")
345 os.makedirs(allowed_path)
346 os.makedirs(symlinks_path)
347 os.makedirs(prohibited_path)
348
349 # Create scripts and symlinks and remember their byte-compiled versions
350 allowed_script = script_helper.make_script(allowed_path, "test_allowed", "a = 0")
351 prohibited_script = script_helper.make_script(prohibited_path, "test_prohibited", "a = 0")
352 allowed_symlink = os.path.join(symlinks_path, "test_allowed.py")
353 prohibited_symlink = os.path.join(symlinks_path, "test_prohibited.py")
354 os.symlink(allowed_script, allowed_symlink)
355 os.symlink(prohibited_script, prohibited_symlink)
356 allowed_bc = importlib.util.cache_from_source(allowed_symlink)
357 prohibited_bc = importlib.util.cache_from_source(prohibited_symlink)
358
359 compileall.compile_dir(symlinks_path, quiet=True, limit_sl_dest=allowed_path)
360
361 self.assertTrue(os.path.isfile(allowed_bc))
362 self.assertFalse(os.path.isfile(prohibited_bc))
363
Elvis Pranskevichusa6b3ec52018-10-10 12:43:14 -0400364
365class CompileallTestsWithSourceEpoch(CompileallTestsBase,
366 unittest.TestCase,
367 metaclass=SourceDateEpochTestMeta,
368 source_date_epoch=True):
369 pass
370
371
372class CompileallTestsWithoutSourceEpoch(CompileallTestsBase,
373 unittest.TestCase,
374 metaclass=SourceDateEpochTestMeta,
375 source_date_epoch=False):
376 pass
377
378
Martin v. Löwis4b003072010-03-16 13:19:21 +0000379class EncodingTest(unittest.TestCase):
Barry Warsaw28a691b2010-04-17 00:19:56 +0000380 """Issue 6716: compileall should escape source code when printing errors
381 to stdout."""
Martin v. Löwis4b003072010-03-16 13:19:21 +0000382
383 def setUp(self):
384 self.directory = tempfile.mkdtemp()
385 self.source_path = os.path.join(self.directory, '_test.py')
386 with open(self.source_path, 'w', encoding='utf-8') as file:
387 file.write('# -*- coding: utf-8 -*-\n')
388 file.write('print u"\u20ac"\n')
389
390 def tearDown(self):
391 shutil.rmtree(self.directory)
392
393 def test_error(self):
394 try:
395 orig_stdout = sys.stdout
396 sys.stdout = io.TextIOWrapper(io.BytesIO(),encoding='ascii')
397 compileall.compile_dir(self.directory)
398 finally:
399 sys.stdout = orig_stdout
400
Barry Warsawc8a99de2010-04-29 18:43:10 +0000401
Elvis Pranskevichusa6b3ec52018-10-10 12:43:14 -0400402class CommandLineTestsBase:
R. David Murray650f1472010-11-20 21:18:51 +0000403 """Test compileall's CLI."""
Barry Warsaw28a691b2010-04-17 00:19:56 +0000404
Brett Cannon65ed7502015-10-09 15:09:43 -0700405 @classmethod
406 def setUpClass(cls):
407 for path in filter(os.path.isdir, sys.path):
408 directory_created = False
409 directory = pathlib.Path(path) / '__pycache__'
410 path = directory / 'test.try'
411 try:
412 if not directory.is_dir():
413 directory.mkdir()
414 directory_created = True
415 with path.open('w') as file:
416 file.write('# for test_compileall')
417 except OSError:
418 sys_path_writable = False
419 break
420 finally:
421 support.unlink(str(path))
422 if directory_created:
423 directory.rmdir()
424 else:
425 sys_path_writable = True
426 cls._sys_path_writable = sys_path_writable
427
428 def _skip_if_sys_path_not_writable(self):
429 if not self._sys_path_writable:
430 raise unittest.SkipTest('not all entries on sys.path are writable')
431
Benjamin Petersona820c7c2012-09-25 11:42:35 -0400432 def _get_run_args(self, args):
Victor Stinner9def2842016-01-18 12:15:08 +0100433 return [*support.optim_args_from_interpreter_flags(),
434 '-S', '-m', 'compileall',
435 *args]
Benjamin Petersona820c7c2012-09-25 11:42:35 -0400436
R. David Murray5317e9c2010-12-16 19:08:51 +0000437 def assertRunOK(self, *args, **env_vars):
438 rc, out, err = script_helper.assert_python_ok(
Benjamin Petersona820c7c2012-09-25 11:42:35 -0400439 *self._get_run_args(args), **env_vars)
R. David Murray95333e32010-12-14 22:32:50 +0000440 self.assertEqual(b'', err)
441 return out
442
R. David Murray5317e9c2010-12-16 19:08:51 +0000443 def assertRunNotOK(self, *args, **env_vars):
R. David Murray95333e32010-12-14 22:32:50 +0000444 rc, out, err = script_helper.assert_python_failure(
Benjamin Petersona820c7c2012-09-25 11:42:35 -0400445 *self._get_run_args(args), **env_vars)
R. David Murray95333e32010-12-14 22:32:50 +0000446 return rc, out, err
447
448 def assertCompiled(self, fn):
Brett Cannon7822e122013-06-14 23:04:02 -0400449 path = importlib.util.cache_from_source(fn)
450 self.assertTrue(os.path.exists(path))
R. David Murray95333e32010-12-14 22:32:50 +0000451
452 def assertNotCompiled(self, fn):
Brett Cannon7822e122013-06-14 23:04:02 -0400453 path = importlib.util.cache_from_source(fn)
454 self.assertFalse(os.path.exists(path))
R. David Murray95333e32010-12-14 22:32:50 +0000455
Barry Warsaw28a691b2010-04-17 00:19:56 +0000456 def setUp(self):
Barry Warsaw28a691b2010-04-17 00:19:56 +0000457 self.directory = tempfile.mkdtemp()
Brett Cannon65ed7502015-10-09 15:09:43 -0700458 self.addCleanup(support.rmtree, self.directory)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000459 self.pkgdir = os.path.join(self.directory, 'foo')
460 os.mkdir(self.pkgdir)
R. David Murray95333e32010-12-14 22:32:50 +0000461 self.pkgdir_cachedir = os.path.join(self.pkgdir, '__pycache__')
462 # Create the __init__.py and a package module.
463 self.initfn = script_helper.make_script(self.pkgdir, '__init__', '')
464 self.barfn = script_helper.make_script(self.pkgdir, 'bar', '')
Barry Warsaw28a691b2010-04-17 00:19:56 +0000465
R. David Murray5317e9c2010-12-16 19:08:51 +0000466 def test_no_args_compiles_path(self):
467 # Note that -l is implied for the no args case.
Brett Cannon65ed7502015-10-09 15:09:43 -0700468 self._skip_if_sys_path_not_writable()
R. David Murray5317e9c2010-12-16 19:08:51 +0000469 bazfn = script_helper.make_script(self.directory, 'baz', '')
470 self.assertRunOK(PYTHONPATH=self.directory)
471 self.assertCompiled(bazfn)
472 self.assertNotCompiled(self.initfn)
473 self.assertNotCompiled(self.barfn)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000474
Elvis Pranskevichusa6b3ec52018-10-10 12:43:14 -0400475 @without_source_date_epoch # timestamp invalidation test
R David Murray8a1d1e62013-12-15 20:49:38 -0500476 def test_no_args_respects_force_flag(self):
Brett Cannon65ed7502015-10-09 15:09:43 -0700477 self._skip_if_sys_path_not_writable()
R David Murray8a1d1e62013-12-15 20:49:38 -0500478 bazfn = script_helper.make_script(self.directory, 'baz', '')
479 self.assertRunOK(PYTHONPATH=self.directory)
R David Murray755d5ea2013-12-15 20:56:00 -0500480 pycpath = importlib.util.cache_from_source(bazfn)
R David Murray8a1d1e62013-12-15 20:49:38 -0500481 # Set atime/mtime backward to avoid file timestamp resolution issues
482 os.utime(pycpath, (time.time()-60,)*2)
483 mtime = os.stat(pycpath).st_mtime
484 # Without force, no recompilation
485 self.assertRunOK(PYTHONPATH=self.directory)
486 mtime2 = os.stat(pycpath).st_mtime
487 self.assertEqual(mtime, mtime2)
488 # Now force it.
489 self.assertRunOK('-f', PYTHONPATH=self.directory)
490 mtime2 = os.stat(pycpath).st_mtime
491 self.assertNotEqual(mtime, mtime2)
492
493 def test_no_args_respects_quiet_flag(self):
Brett Cannon65ed7502015-10-09 15:09:43 -0700494 self._skip_if_sys_path_not_writable()
R David Murray8a1d1e62013-12-15 20:49:38 -0500495 script_helper.make_script(self.directory, 'baz', '')
496 noisy = self.assertRunOK(PYTHONPATH=self.directory)
497 self.assertIn(b'Listing ', noisy)
498 quiet = self.assertRunOK('-q', PYTHONPATH=self.directory)
499 self.assertNotIn(b'Listing ', quiet)
500
Georg Brandl1463a3f2010-10-14 07:42:27 +0000501 # Ensure that the default behavior of compileall's CLI is to create
Brett Cannonf299abd2015-04-13 14:21:02 -0400502 # PEP 3147/PEP 488 pyc files.
Georg Brandl1463a3f2010-10-14 07:42:27 +0000503 for name, ext, switch in [
504 ('normal', 'pyc', []),
Brett Cannonf299abd2015-04-13 14:21:02 -0400505 ('optimize', 'opt-1.pyc', ['-O']),
506 ('doubleoptimize', 'opt-2.pyc', ['-OO']),
Georg Brandl1463a3f2010-10-14 07:42:27 +0000507 ]:
508 def f(self, ext=ext, switch=switch):
R. David Murray95333e32010-12-14 22:32:50 +0000509 script_helper.assert_python_ok(*(switch +
510 ['-m', 'compileall', '-q', self.pkgdir]))
Georg Brandl1463a3f2010-10-14 07:42:27 +0000511 # Verify the __pycache__ directory contents.
R. David Murray95333e32010-12-14 22:32:50 +0000512 self.assertTrue(os.path.exists(self.pkgdir_cachedir))
Brett Cannon7822e122013-06-14 23:04:02 -0400513 expected = sorted(base.format(sys.implementation.cache_tag, ext)
514 for base in ('__init__.{}.{}', 'bar.{}.{}'))
R. David Murray95333e32010-12-14 22:32:50 +0000515 self.assertEqual(sorted(os.listdir(self.pkgdir_cachedir)), expected)
Georg Brandl1463a3f2010-10-14 07:42:27 +0000516 # Make sure there are no .pyc files in the source directory.
R. David Murray95333e32010-12-14 22:32:50 +0000517 self.assertFalse([fn for fn in os.listdir(self.pkgdir)
518 if fn.endswith(ext)])
Georg Brandl1463a3f2010-10-14 07:42:27 +0000519 locals()['test_pep3147_paths_' + name] = f
Barry Warsaw28a691b2010-04-17 00:19:56 +0000520
521 def test_legacy_paths(self):
522 # Ensure that with the proper switch, compileall leaves legacy
Brett Cannonf299abd2015-04-13 14:21:02 -0400523 # pyc files, and no __pycache__ directory.
R. David Murray95333e32010-12-14 22:32:50 +0000524 self.assertRunOK('-b', '-q', self.pkgdir)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000525 # Verify the __pycache__ directory contents.
R. David Murray95333e32010-12-14 22:32:50 +0000526 self.assertFalse(os.path.exists(self.pkgdir_cachedir))
Brett Cannonf299abd2015-04-13 14:21:02 -0400527 expected = sorted(['__init__.py', '__init__.pyc', 'bar.py',
528 'bar.pyc'])
Barry Warsaw28a691b2010-04-17 00:19:56 +0000529 self.assertEqual(sorted(os.listdir(self.pkgdir)), expected)
530
Barry Warsawc04317f2010-04-26 15:59:03 +0000531 def test_multiple_runs(self):
532 # Bug 8527 reported that multiple calls produced empty
533 # __pycache__/__pycache__ directories.
R. David Murray95333e32010-12-14 22:32:50 +0000534 self.assertRunOK('-q', self.pkgdir)
Barry Warsawc04317f2010-04-26 15:59:03 +0000535 # Verify the __pycache__ directory contents.
R. David Murray95333e32010-12-14 22:32:50 +0000536 self.assertTrue(os.path.exists(self.pkgdir_cachedir))
537 cachecachedir = os.path.join(self.pkgdir_cachedir, '__pycache__')
Barry Warsawc04317f2010-04-26 15:59:03 +0000538 self.assertFalse(os.path.exists(cachecachedir))
539 # Call compileall again.
R. David Murray95333e32010-12-14 22:32:50 +0000540 self.assertRunOK('-q', self.pkgdir)
541 self.assertTrue(os.path.exists(self.pkgdir_cachedir))
Barry Warsawc04317f2010-04-26 15:59:03 +0000542 self.assertFalse(os.path.exists(cachecachedir))
543
Elvis Pranskevichusa6b3ec52018-10-10 12:43:14 -0400544 @without_source_date_epoch # timestamp invalidation test
R. David Murray650f1472010-11-20 21:18:51 +0000545 def test_force(self):
R. David Murray95333e32010-12-14 22:32:50 +0000546 self.assertRunOK('-q', self.pkgdir)
Brett Cannon7822e122013-06-14 23:04:02 -0400547 pycpath = importlib.util.cache_from_source(self.barfn)
R. David Murray650f1472010-11-20 21:18:51 +0000548 # set atime/mtime backward to avoid file timestamp resolution issues
549 os.utime(pycpath, (time.time()-60,)*2)
R. David Murray95333e32010-12-14 22:32:50 +0000550 mtime = os.stat(pycpath).st_mtime
551 # without force, no recompilation
552 self.assertRunOK('-q', self.pkgdir)
553 mtime2 = os.stat(pycpath).st_mtime
554 self.assertEqual(mtime, mtime2)
555 # now force it.
556 self.assertRunOK('-q', '-f', self.pkgdir)
557 mtime2 = os.stat(pycpath).st_mtime
558 self.assertNotEqual(mtime, mtime2)
R. David Murray650f1472010-11-20 21:18:51 +0000559
R. David Murray95333e32010-12-14 22:32:50 +0000560 def test_recursion_control(self):
561 subpackage = os.path.join(self.pkgdir, 'spam')
562 os.mkdir(subpackage)
563 subinitfn = script_helper.make_script(subpackage, '__init__', '')
564 hamfn = script_helper.make_script(subpackage, 'ham', '')
565 self.assertRunOK('-q', '-l', self.pkgdir)
566 self.assertNotCompiled(subinitfn)
567 self.assertFalse(os.path.exists(os.path.join(subpackage, '__pycache__')))
568 self.assertRunOK('-q', self.pkgdir)
569 self.assertCompiled(subinitfn)
570 self.assertCompiled(hamfn)
R. David Murray650f1472010-11-20 21:18:51 +0000571
Benjamin Peterson344ff4a2014-08-19 16:13:26 -0500572 def test_recursion_limit(self):
573 subpackage = os.path.join(self.pkgdir, 'spam')
574 subpackage2 = os.path.join(subpackage, 'ham')
575 subpackage3 = os.path.join(subpackage2, 'eggs')
576 for pkg in (subpackage, subpackage2, subpackage3):
577 script_helper.make_pkg(pkg)
578
579 subinitfn = os.path.join(subpackage, '__init__.py')
580 hamfn = script_helper.make_script(subpackage, 'ham', '')
581 spamfn = script_helper.make_script(subpackage2, 'spam', '')
582 eggfn = script_helper.make_script(subpackage3, 'egg', '')
583
584 self.assertRunOK('-q', '-r 0', self.pkgdir)
585 self.assertNotCompiled(subinitfn)
586 self.assertFalse(
587 os.path.exists(os.path.join(subpackage, '__pycache__')))
588
589 self.assertRunOK('-q', '-r 1', self.pkgdir)
590 self.assertCompiled(subinitfn)
591 self.assertCompiled(hamfn)
592 self.assertNotCompiled(spamfn)
593
594 self.assertRunOK('-q', '-r 2', self.pkgdir)
595 self.assertCompiled(subinitfn)
596 self.assertCompiled(hamfn)
597 self.assertCompiled(spamfn)
598 self.assertNotCompiled(eggfn)
599
600 self.assertRunOK('-q', '-r 5', self.pkgdir)
601 self.assertCompiled(subinitfn)
602 self.assertCompiled(hamfn)
603 self.assertCompiled(spamfn)
604 self.assertCompiled(eggfn)
605
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200606 @support.skip_unless_symlink
607 def test_symlink_loop(self):
608 # Currently, compileall ignores symlinks to directories.
609 # If that limitation is ever lifted, it should protect against
610 # recursion in symlink loops.
611 pkg = os.path.join(self.pkgdir, 'spam')
612 script_helper.make_pkg(pkg)
613 os.symlink('.', os.path.join(pkg, 'evil'))
614 os.symlink('.', os.path.join(pkg, 'evil2'))
615 self.assertRunOK('-q', self.pkgdir)
616 self.assertCompiled(os.path.join(
617 self.pkgdir, 'spam', 'evil', 'evil2', '__init__.py'
618 ))
619
R. David Murray650f1472010-11-20 21:18:51 +0000620 def test_quiet(self):
R. David Murray95333e32010-12-14 22:32:50 +0000621 noisy = self.assertRunOK(self.pkgdir)
622 quiet = self.assertRunOK('-q', self.pkgdir)
623 self.assertNotEqual(b'', noisy)
624 self.assertEqual(b'', quiet)
R. David Murray650f1472010-11-20 21:18:51 +0000625
Berker Peksag6554b862014-10-15 11:10:57 +0300626 def test_silent(self):
627 script_helper.make_script(self.pkgdir, 'crunchyfrog', 'bad(syntax')
628 _, quiet, _ = self.assertRunNotOK('-q', self.pkgdir)
629 _, silent, _ = self.assertRunNotOK('-qq', self.pkgdir)
630 self.assertNotEqual(b'', quiet)
631 self.assertEqual(b'', silent)
632
R. David Murray650f1472010-11-20 21:18:51 +0000633 def test_regexp(self):
R David Murrayee1a7cb2011-07-01 14:55:43 -0400634 self.assertRunOK('-q', '-x', r'ba[^\\/]*$', self.pkgdir)
R. David Murray95333e32010-12-14 22:32:50 +0000635 self.assertNotCompiled(self.barfn)
636 self.assertCompiled(self.initfn)
R. David Murray650f1472010-11-20 21:18:51 +0000637
R. David Murray95333e32010-12-14 22:32:50 +0000638 def test_multiple_dirs(self):
639 pkgdir2 = os.path.join(self.directory, 'foo2')
640 os.mkdir(pkgdir2)
641 init2fn = script_helper.make_script(pkgdir2, '__init__', '')
642 bar2fn = script_helper.make_script(pkgdir2, 'bar2', '')
643 self.assertRunOK('-q', self.pkgdir, pkgdir2)
644 self.assertCompiled(self.initfn)
645 self.assertCompiled(self.barfn)
646 self.assertCompiled(init2fn)
647 self.assertCompiled(bar2fn)
648
R. David Murray95333e32010-12-14 22:32:50 +0000649 def test_d_compile_error(self):
650 script_helper.make_script(self.pkgdir, 'crunchyfrog', 'bad(syntax')
651 rc, out, err = self.assertRunNotOK('-q', '-d', 'dinsdale', self.pkgdir)
652 self.assertRegex(out, b'File "dinsdale')
653
654 def test_d_runtime_error(self):
655 bazfn = script_helper.make_script(self.pkgdir, 'baz', 'raise Exception')
656 self.assertRunOK('-q', '-d', 'dinsdale', self.pkgdir)
657 fn = script_helper.make_script(self.pkgdir, 'bing', 'import baz')
Brett Cannon7822e122013-06-14 23:04:02 -0400658 pyc = importlib.util.cache_from_source(bazfn)
R. David Murray95333e32010-12-14 22:32:50 +0000659 os.rename(pyc, os.path.join(self.pkgdir, 'baz.pyc'))
660 os.remove(bazfn)
Victor Stinnere8785ff2013-10-12 14:44:01 +0200661 rc, out, err = script_helper.assert_python_failure(fn, __isolated=False)
R. David Murray95333e32010-12-14 22:32:50 +0000662 self.assertRegex(err, b'File "dinsdale')
663
664 def test_include_bad_file(self):
665 rc, out, err = self.assertRunNotOK(
666 '-i', os.path.join(self.directory, 'nosuchfile'), self.pkgdir)
667 self.assertRegex(out, b'rror.*nosuchfile')
668 self.assertNotRegex(err, b'Traceback')
Brett Cannon7822e122013-06-14 23:04:02 -0400669 self.assertFalse(os.path.exists(importlib.util.cache_from_source(
R. David Murray95333e32010-12-14 22:32:50 +0000670 self.pkgdir_cachedir)))
671
672 def test_include_file_with_arg(self):
673 f1 = script_helper.make_script(self.pkgdir, 'f1', '')
674 f2 = script_helper.make_script(self.pkgdir, 'f2', '')
675 f3 = script_helper.make_script(self.pkgdir, 'f3', '')
676 f4 = script_helper.make_script(self.pkgdir, 'f4', '')
677 with open(os.path.join(self.directory, 'l1'), 'w') as l1:
678 l1.write(os.path.join(self.pkgdir, 'f1.py')+os.linesep)
679 l1.write(os.path.join(self.pkgdir, 'f2.py')+os.linesep)
680 self.assertRunOK('-i', os.path.join(self.directory, 'l1'), f4)
681 self.assertCompiled(f1)
682 self.assertCompiled(f2)
683 self.assertNotCompiled(f3)
684 self.assertCompiled(f4)
685
686 def test_include_file_no_arg(self):
687 f1 = script_helper.make_script(self.pkgdir, 'f1', '')
688 f2 = script_helper.make_script(self.pkgdir, 'f2', '')
689 f3 = script_helper.make_script(self.pkgdir, 'f3', '')
690 f4 = script_helper.make_script(self.pkgdir, 'f4', '')
691 with open(os.path.join(self.directory, 'l1'), 'w') as l1:
692 l1.write(os.path.join(self.pkgdir, 'f2.py')+os.linesep)
693 self.assertRunOK('-i', os.path.join(self.directory, 'l1'))
694 self.assertNotCompiled(f1)
695 self.assertCompiled(f2)
696 self.assertNotCompiled(f3)
697 self.assertNotCompiled(f4)
698
699 def test_include_on_stdin(self):
700 f1 = script_helper.make_script(self.pkgdir, 'f1', '')
701 f2 = script_helper.make_script(self.pkgdir, 'f2', '')
702 f3 = script_helper.make_script(self.pkgdir, 'f3', '')
703 f4 = script_helper.make_script(self.pkgdir, 'f4', '')
Benjamin Petersona820c7c2012-09-25 11:42:35 -0400704 p = script_helper.spawn_python(*(self._get_run_args(()) + ['-i', '-']))
R. David Murray95333e32010-12-14 22:32:50 +0000705 p.stdin.write((f3+os.linesep).encode('ascii'))
706 script_helper.kill_python(p)
707 self.assertNotCompiled(f1)
708 self.assertNotCompiled(f2)
709 self.assertCompiled(f3)
710 self.assertNotCompiled(f4)
711
712 def test_compiles_as_much_as_possible(self):
713 bingfn = script_helper.make_script(self.pkgdir, 'bing', 'syntax(error')
714 rc, out, err = self.assertRunNotOK('nosuchfile', self.initfn,
715 bingfn, self.barfn)
R. David Murray5317e9c2010-12-16 19:08:51 +0000716 self.assertRegex(out, b'rror')
R. David Murray95333e32010-12-14 22:32:50 +0000717 self.assertNotCompiled(bingfn)
718 self.assertCompiled(self.initfn)
719 self.assertCompiled(self.barfn)
720
R. David Murray5317e9c2010-12-16 19:08:51 +0000721 def test_invalid_arg_produces_message(self):
722 out = self.assertRunOK('badfilename')
Victor Stinner53071262011-05-11 00:36:28 +0200723 self.assertRegex(out, b"Can't list 'badfilename'")
R. David Murray650f1472010-11-20 21:18:51 +0000724
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800725 def test_pyc_invalidation_mode(self):
726 script_helper.make_script(self.pkgdir, 'f1', '')
727 pyc = importlib.util.cache_from_source(
728 os.path.join(self.pkgdir, 'f1.py'))
729 self.assertRunOK('--invalidation-mode=checked-hash', self.pkgdir)
730 with open(pyc, 'rb') as fp:
731 data = fp.read()
732 self.assertEqual(int.from_bytes(data[4:8], 'little'), 0b11)
733 self.assertRunOK('--invalidation-mode=unchecked-hash', self.pkgdir)
734 with open(pyc, 'rb') as fp:
735 data = fp.read()
736 self.assertEqual(int.from_bytes(data[4:8], 'little'), 0b01)
737
Brett Cannonf1a8df02014-09-12 10:39:48 -0400738 @skipUnless(_have_multiprocessing, "requires multiprocessing")
739 def test_workers(self):
740 bar2fn = script_helper.make_script(self.directory, 'bar2', '')
741 files = []
742 for suffix in range(5):
743 pkgdir = os.path.join(self.directory, 'foo{}'.format(suffix))
744 os.mkdir(pkgdir)
745 fn = script_helper.make_script(pkgdir, '__init__', '')
746 files.append(script_helper.make_script(pkgdir, 'bar2', ''))
747
748 self.assertRunOK(self.directory, '-j', '0')
749 self.assertCompiled(bar2fn)
750 for file in files:
751 self.assertCompiled(file)
752
753 @mock.patch('compileall.compile_dir')
754 def test_workers_available_cores(self, compile_dir):
755 with mock.patch("sys.argv",
756 new=[sys.executable, self.directory, "-j0"]):
757 compileall.main()
758 self.assertTrue(compile_dir.called)
Antoine Pitrou1a2dd822019-05-15 23:45:18 +0200759 self.assertEqual(compile_dir.call_args[-1]['workers'], 0)
Brett Cannonf1a8df02014-09-12 10:39:48 -0400760
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200761 def test_strip_and_prepend(self):
762 fullpath = ["test", "build", "real", "path"]
763 path = os.path.join(self.directory, *fullpath)
764 os.makedirs(path)
765 script = script_helper.make_script(path, "test", "1 / 0")
766 bc = importlib.util.cache_from_source(script)
767 stripdir = os.path.join(self.directory, *fullpath[:2])
768 prependdir = "/foo"
769 self.assertRunOK("-s", stripdir, "-p", prependdir, path)
770 rc, out, err = script_helper.assert_python_failure(bc)
771 expected_in = os.path.join(prependdir, *fullpath[2:])
772 self.assertIn(
773 expected_in,
774 str(err, encoding=sys.getdefaultencoding())
775 )
776 self.assertNotIn(
777 stripdir,
778 str(err, encoding=sys.getdefaultencoding())
779 )
780
781 def test_multiple_optimization_levels(self):
782 path = os.path.join(self.directory, "optimizations")
783 os.makedirs(path)
784 script = script_helper.make_script(path,
785 "test_optimization",
786 "a = 0")
787 bc = []
788 for opt_level in "", 1, 2, 3:
789 bc.append(importlib.util.cache_from_source(script,
790 optimization=opt_level))
791 test_combinations = [["0", "1"],
792 ["1", "2"],
793 ["0", "2"],
794 ["0", "1", "2"]]
795 for opt_combination in test_combinations:
796 self.assertRunOK(path, *("-o" + str(n) for n in opt_combination))
797 for opt_level in opt_combination:
798 self.assertTrue(os.path.isfile(bc[int(opt_level)]))
799 try:
800 os.unlink(bc[opt_level])
801 except Exception:
802 pass
803
804 @support.skip_unless_symlink
805 def test_ignore_symlink_destination(self):
806 # Create folders for allowed files, symlinks and prohibited area
807 allowed_path = os.path.join(self.directory, "test", "dir", "allowed")
808 symlinks_path = os.path.join(self.directory, "test", "dir", "symlinks")
809 prohibited_path = os.path.join(self.directory, "test", "dir", "prohibited")
810 os.makedirs(allowed_path)
811 os.makedirs(symlinks_path)
812 os.makedirs(prohibited_path)
813
814 # Create scripts and symlinks and remember their byte-compiled versions
815 allowed_script = script_helper.make_script(allowed_path, "test_allowed", "a = 0")
816 prohibited_script = script_helper.make_script(prohibited_path, "test_prohibited", "a = 0")
817 allowed_symlink = os.path.join(symlinks_path, "test_allowed.py")
818 prohibited_symlink = os.path.join(symlinks_path, "test_prohibited.py")
819 os.symlink(allowed_script, allowed_symlink)
820 os.symlink(prohibited_script, prohibited_symlink)
821 allowed_bc = importlib.util.cache_from_source(allowed_symlink)
822 prohibited_bc = importlib.util.cache_from_source(prohibited_symlink)
823
824 self.assertRunOK(symlinks_path, "-e", allowed_path)
825
826 self.assertTrue(os.path.isfile(allowed_bc))
827 self.assertFalse(os.path.isfile(prohibited_bc))
828
Barry Warsaw28a691b2010-04-17 00:19:56 +0000829
Min ho Kimc4cacc82019-07-31 08:16:13 +1000830class CommandLineTestsWithSourceEpoch(CommandLineTestsBase,
Elvis Pranskevichusa6b3ec52018-10-10 12:43:14 -0400831 unittest.TestCase,
832 metaclass=SourceDateEpochTestMeta,
833 source_date_epoch=True):
834 pass
835
836
Min ho Kimc4cacc82019-07-31 08:16:13 +1000837class CommandLineTestsNoSourceEpoch(CommandLineTestsBase,
Elvis Pranskevichusa6b3ec52018-10-10 12:43:14 -0400838 unittest.TestCase,
839 metaclass=SourceDateEpochTestMeta,
840 source_date_epoch=False):
841 pass
842
843
844
Brett Cannonbefb14f2009-02-10 02:10:16 +0000845if __name__ == "__main__":
Brett Cannon7822e122013-06-14 23:04:02 -0400846 unittest.main()