blob: 3bb03b3dd384640669d620f2a114ff79ca6fb65e [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
Petr Viktorin4267c982019-09-26 11:53:51 +020049 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 Viktorin3d984a12019-09-26 15:16:32 +020056 long_path, *(f"dir_{i}_{j}" for j in range(10))
Petr Viktorin4267c982019-09-26 11:53:51 +020057 )
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 Viktorin3d984a12019-09-26 15:16:32 +020090 # 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 Viktorin4267c982019-09-26 11:53:51 +020094 if i < 2:
Petr Viktorin3d984a12019-09-26 15:16:32 +020095 raise ValueError(f'"Long path" is too short: {long_path}')
Petr Viktorin4267c982019-09-26 11:53:51 +020096
97 self.source_path_long = long_source
98 self.bc_path_long = long_cache
99
Brett Cannon1e3c3e92015-12-27 13:17:04 -0800100 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 Pranskevichusa6b3ec52018-10-10 12:43:14 -0400105 def timestamp_metadata(self):
Brett Cannonbefb14f2009-02-10 02:10:16 +0000106 with open(self.bc_path, 'rb') as file:
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800107 data = file.read(12)
Brett Cannonbefb14f2009-02-10 02:10:16 +0000108 mtime = int(os.stat(self.source_path).st_mtime)
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800109 compare = struct.pack('<4sll', importlib.util.MAGIC_NUMBER, 0, mtime)
Brett Cannonbefb14f2009-02-10 02:10:16 +0000110 return data, compare
111
112 def recreation_check(self, metadata):
113 """Check that compileall recreates bytecode when the new metadata is
114 used."""
Elvis Pranskevichusa6b3ec52018-10-10 12:43:14 -0400115 if os.environ.get('SOURCE_DATE_EPOCH'):
116 raise unittest.SkipTest('SOURCE_DATE_EPOCH is set')
Brett Cannonbefb14f2009-02-10 02:10:16 +0000117 py_compile.compile(self.source_path)
Elvis Pranskevichusa6b3ec52018-10-10 12:43:14 -0400118 self.assertEqual(*self.timestamp_metadata())
Brett Cannonbefb14f2009-02-10 02:10:16 +0000119 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 Pranskevichusa6b3ec52018-10-10 12:43:14 -0400124 self.assertNotEqual(*self.timestamp_metadata())
Brett Cannonbefb14f2009-02-10 02:10:16 +0000125 compileall.compile_dir(self.directory, force=False, quiet=True)
Elvis Pranskevichusa6b3ec52018-10-10 12:43:14 -0400126 self.assertTrue(*self.timestamp_metadata())
Brett Cannonbefb14f2009-02-10 02:10:16 +0000127
128 def test_mtime(self):
129 # Test a change in mtime leads to a new .pyc.
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800130 self.recreation_check(struct.pack('<4sll', importlib.util.MAGIC_NUMBER,
131 0, 1))
Brett Cannonbefb14f2009-02-10 02:10:16 +0000132
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 Klosec33b9022010-03-16 00:36:26 +0000137 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 Cannon1e3c3e92015-12-27 13:17:04 -0800144 self.assertTrue(compileall.compile_file(self.source_path,
145 force=False, quiet=True))
Barry Warsaw28a691b2010-04-17 00:19:56 +0000146 self.assertTrue(os.path.isfile(self.bc_path) and
147 not os.path.isfile(self.bc_path2))
Matthias Klosec33b9022010-03-16 00:36:26 +0000148 os.unlink(self.bc_path)
Brett Cannon1e3c3e92015-12-27 13:17:04 -0800149 self.assertTrue(compileall.compile_dir(self.directory, force=False,
150 quiet=True))
Barry Warsaw28a691b2010-04-17 00:19:56 +0000151 self.assertTrue(os.path.isfile(self.bc_path) and
152 os.path.isfile(self.bc_path2))
Matthias Klosec33b9022010-03-16 00:36:26 +0000153 os.unlink(self.bc_path)
154 os.unlink(self.bc_path2)
Brett Cannon1e3c3e92015-12-27 13:17:04 -0800155 # 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 Peksag812a2b62016-10-01 00:54:18 +0300162 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 Peksagd8e97132016-10-01 02:44:37 +0300167 self.assertRegex(stdout.getvalue(), r'Compiling ([^WindowsPath|PosixPath].*)')
Berker Peksag812a2b62016-10-01 00:54:18 +0300168 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 Cannon1e3c3e92015-12-27 13:17:04 -0800177 def test_compile_path(self):
Berker Peksag408b78c2016-09-28 17:38:53 +0300178 with test.test_importlib.util.import_state(path=[self.directory]):
179 self.assertTrue(compileall.compile_path(quiet=2))
Brett Cannon1e3c3e92015-12-27 13:17:04 -0800180
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 Cannonbefb14f2009-02-10 02:10:16 +0000185
Barry Warsawc8a99de2010-04-29 18:43:10 +0000186 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 Brandl8334fd92010-12-04 10:26:46 +0000198 def test_optimize(self):
199 # make sure compiling with different optimization settings than the
200 # interpreter's creates the correct file names
Brett Cannonf299abd2015-04-13 14:21:02 -0400201 optimize, opt = (1, 1) if __debug__ else (0, '')
Georg Brandl8334fd92010-12-04 10:26:46 +0000202 compileall.compile_dir(self.directory, quiet=True, optimize=optimize)
Brett Cannon7822e122013-06-14 23:04:02 -0400203 cached = importlib.util.cache_from_source(self.source_path,
Brett Cannonf299abd2015-04-13 14:21:02 -0400204 optimization=opt)
Georg Brandl8334fd92010-12-04 10:26:46 +0000205 self.assertTrue(os.path.isfile(cached))
Brett Cannon7822e122013-06-14 23:04:02 -0400206 cached2 = importlib.util.cache_from_source(self.source_path2,
Brett Cannonf299abd2015-04-13 14:21:02 -0400207 optimization=opt)
Georg Brandl45438462011-02-07 12:36:54 +0000208 self.assertTrue(os.path.isfile(cached2))
Brett Cannon7822e122013-06-14 23:04:02 -0400209 cached3 = importlib.util.cache_from_source(self.source_path3,
Brett Cannonf299abd2015-04-13 14:21:02 -0400210 optimization=opt)
Georg Brandl45438462011-02-07 12:36:54 +0000211 self.assertTrue(os.path.isfile(cached3))
Georg Brandl8334fd92010-12-04 10:26:46 +0000212
Berker Peksag812a2b62016-10-01 00:54:18 +0300213 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 Peksagd8e97132016-10-01 02:44:37 +0300217 line = stdout.getvalue().splitlines()[0]
218 self.assertRegex(line, r'Listing ([^WindowsPath|PosixPath].*)')
Berker Peksag812a2b62016-10-01 00:54:18 +0300219 self.assertTrue(os.path.isfile(self.bc_path))
220
Dustin Spicuzza1d817e42018-11-23 12:06:55 -0500221 @mock.patch('concurrent.futures.ProcessPoolExecutor')
Brett Cannonf1a8df02014-09-12 10:39:48 -0400222 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 Spicuzza1d817e42018-11-23 12:06:55 -0500231 @mock.patch('concurrent.futures.ProcessPoolExecutor')
Brett Cannonf1a8df02014-09-12 10:39:48 -0400232 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 Spicuzza1d817e42018-11-23 12:06:55 -0500236 @mock.patch('concurrent.futures.ProcessPoolExecutor')
Brett Cannonf1a8df02014-09-12 10:39:48 -0400237 @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 Spicuzza1d817e42018-11-23 12:06:55 -0500243 @mock.patch('concurrent.futures.ProcessPoolExecutor', new=None)
Berker Peksagd86ef052015-04-22 09:39:19 +0300244 @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 Warsaw28a691b2010-04-17 00:19:56 +0000248
Petr Viktorin4267c982019-09-26 11:53:51 +0200249 def test_compile_dir_maxlevels(self):
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200250 # Test the actual impact of maxlevels attr
Petr Viktorin4267c982019-09-26 11:53:51 +0200251 self.create_long_path()
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200252 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 Viktorin4267c982019-09-26 11:53:51 +0200256 quiet=True)
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200257 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 Pranskevichusa6b3ec52018-10-10 12:43:14 -0400367
368class CompileallTestsWithSourceEpoch(CompileallTestsBase,
369 unittest.TestCase,
370 metaclass=SourceDateEpochTestMeta,
371 source_date_epoch=True):
372 pass
373
374
375class CompileallTestsWithoutSourceEpoch(CompileallTestsBase,
376 unittest.TestCase,
377 metaclass=SourceDateEpochTestMeta,
378 source_date_epoch=False):
379 pass
380
381
Martin v. Löwis4b003072010-03-16 13:19:21 +0000382class EncodingTest(unittest.TestCase):
Barry Warsaw28a691b2010-04-17 00:19:56 +0000383 """Issue 6716: compileall should escape source code when printing errors
384 to stdout."""
Martin v. Löwis4b003072010-03-16 13:19:21 +0000385
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 Warsawc8a99de2010-04-29 18:43:10 +0000404
Elvis Pranskevichusa6b3ec52018-10-10 12:43:14 -0400405class CommandLineTestsBase:
R. David Murray650f1472010-11-20 21:18:51 +0000406 """Test compileall's CLI."""
Barry Warsaw28a691b2010-04-17 00:19:56 +0000407
Brett Cannon65ed7502015-10-09 15:09:43 -0700408 @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 Petersona820c7c2012-09-25 11:42:35 -0400435 def _get_run_args(self, args):
Victor Stinner9def2842016-01-18 12:15:08 +0100436 return [*support.optim_args_from_interpreter_flags(),
437 '-S', '-m', 'compileall',
438 *args]
Benjamin Petersona820c7c2012-09-25 11:42:35 -0400439
R. David Murray5317e9c2010-12-16 19:08:51 +0000440 def assertRunOK(self, *args, **env_vars):
441 rc, out, err = script_helper.assert_python_ok(
Benjamin Petersona820c7c2012-09-25 11:42:35 -0400442 *self._get_run_args(args), **env_vars)
R. David Murray95333e32010-12-14 22:32:50 +0000443 self.assertEqual(b'', err)
444 return out
445
R. David Murray5317e9c2010-12-16 19:08:51 +0000446 def assertRunNotOK(self, *args, **env_vars):
R. David Murray95333e32010-12-14 22:32:50 +0000447 rc, out, err = script_helper.assert_python_failure(
Benjamin Petersona820c7c2012-09-25 11:42:35 -0400448 *self._get_run_args(args), **env_vars)
R. David Murray95333e32010-12-14 22:32:50 +0000449 return rc, out, err
450
451 def assertCompiled(self, fn):
Brett Cannon7822e122013-06-14 23:04:02 -0400452 path = importlib.util.cache_from_source(fn)
453 self.assertTrue(os.path.exists(path))
R. David Murray95333e32010-12-14 22:32:50 +0000454
455 def assertNotCompiled(self, fn):
Brett Cannon7822e122013-06-14 23:04:02 -0400456 path = importlib.util.cache_from_source(fn)
457 self.assertFalse(os.path.exists(path))
R. David Murray95333e32010-12-14 22:32:50 +0000458
Barry Warsaw28a691b2010-04-17 00:19:56 +0000459 def setUp(self):
Barry Warsaw28a691b2010-04-17 00:19:56 +0000460 self.directory = tempfile.mkdtemp()
Brett Cannon65ed7502015-10-09 15:09:43 -0700461 self.addCleanup(support.rmtree, self.directory)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000462 self.pkgdir = os.path.join(self.directory, 'foo')
463 os.mkdir(self.pkgdir)
R. David Murray95333e32010-12-14 22:32:50 +0000464 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 Warsaw28a691b2010-04-17 00:19:56 +0000468
R. David Murray5317e9c2010-12-16 19:08:51 +0000469 def test_no_args_compiles_path(self):
470 # Note that -l is implied for the no args case.
Brett Cannon65ed7502015-10-09 15:09:43 -0700471 self._skip_if_sys_path_not_writable()
R. David Murray5317e9c2010-12-16 19:08:51 +0000472 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 Warsaw28a691b2010-04-17 00:19:56 +0000477
Elvis Pranskevichusa6b3ec52018-10-10 12:43:14 -0400478 @without_source_date_epoch # timestamp invalidation test
R David Murray8a1d1e62013-12-15 20:49:38 -0500479 def test_no_args_respects_force_flag(self):
Brett Cannon65ed7502015-10-09 15:09:43 -0700480 self._skip_if_sys_path_not_writable()
R David Murray8a1d1e62013-12-15 20:49:38 -0500481 bazfn = script_helper.make_script(self.directory, 'baz', '')
482 self.assertRunOK(PYTHONPATH=self.directory)
R David Murray755d5ea2013-12-15 20:56:00 -0500483 pycpath = importlib.util.cache_from_source(bazfn)
R David Murray8a1d1e62013-12-15 20:49:38 -0500484 # 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 Cannon65ed7502015-10-09 15:09:43 -0700497 self._skip_if_sys_path_not_writable()
R David Murray8a1d1e62013-12-15 20:49:38 -0500498 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 Brandl1463a3f2010-10-14 07:42:27 +0000504 # Ensure that the default behavior of compileall's CLI is to create
Brett Cannonf299abd2015-04-13 14:21:02 -0400505 # PEP 3147/PEP 488 pyc files.
Georg Brandl1463a3f2010-10-14 07:42:27 +0000506 for name, ext, switch in [
507 ('normal', 'pyc', []),
Brett Cannonf299abd2015-04-13 14:21:02 -0400508 ('optimize', 'opt-1.pyc', ['-O']),
509 ('doubleoptimize', 'opt-2.pyc', ['-OO']),
Georg Brandl1463a3f2010-10-14 07:42:27 +0000510 ]:
511 def f(self, ext=ext, switch=switch):
R. David Murray95333e32010-12-14 22:32:50 +0000512 script_helper.assert_python_ok(*(switch +
513 ['-m', 'compileall', '-q', self.pkgdir]))
Georg Brandl1463a3f2010-10-14 07:42:27 +0000514 # Verify the __pycache__ directory contents.
R. David Murray95333e32010-12-14 22:32:50 +0000515 self.assertTrue(os.path.exists(self.pkgdir_cachedir))
Brett Cannon7822e122013-06-14 23:04:02 -0400516 expected = sorted(base.format(sys.implementation.cache_tag, ext)
517 for base in ('__init__.{}.{}', 'bar.{}.{}'))
R. David Murray95333e32010-12-14 22:32:50 +0000518 self.assertEqual(sorted(os.listdir(self.pkgdir_cachedir)), expected)
Georg Brandl1463a3f2010-10-14 07:42:27 +0000519 # Make sure there are no .pyc files in the source directory.
R. David Murray95333e32010-12-14 22:32:50 +0000520 self.assertFalse([fn for fn in os.listdir(self.pkgdir)
521 if fn.endswith(ext)])
Georg Brandl1463a3f2010-10-14 07:42:27 +0000522 locals()['test_pep3147_paths_' + name] = f
Barry Warsaw28a691b2010-04-17 00:19:56 +0000523
524 def test_legacy_paths(self):
525 # Ensure that with the proper switch, compileall leaves legacy
Brett Cannonf299abd2015-04-13 14:21:02 -0400526 # pyc files, and no __pycache__ directory.
R. David Murray95333e32010-12-14 22:32:50 +0000527 self.assertRunOK('-b', '-q', self.pkgdir)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000528 # Verify the __pycache__ directory contents.
R. David Murray95333e32010-12-14 22:32:50 +0000529 self.assertFalse(os.path.exists(self.pkgdir_cachedir))
Brett Cannonf299abd2015-04-13 14:21:02 -0400530 expected = sorted(['__init__.py', '__init__.pyc', 'bar.py',
531 'bar.pyc'])
Barry Warsaw28a691b2010-04-17 00:19:56 +0000532 self.assertEqual(sorted(os.listdir(self.pkgdir)), expected)
533
Barry Warsawc04317f2010-04-26 15:59:03 +0000534 def test_multiple_runs(self):
535 # Bug 8527 reported that multiple calls produced empty
536 # __pycache__/__pycache__ directories.
R. David Murray95333e32010-12-14 22:32:50 +0000537 self.assertRunOK('-q', self.pkgdir)
Barry Warsawc04317f2010-04-26 15:59:03 +0000538 # Verify the __pycache__ directory contents.
R. David Murray95333e32010-12-14 22:32:50 +0000539 self.assertTrue(os.path.exists(self.pkgdir_cachedir))
540 cachecachedir = os.path.join(self.pkgdir_cachedir, '__pycache__')
Barry Warsawc04317f2010-04-26 15:59:03 +0000541 self.assertFalse(os.path.exists(cachecachedir))
542 # Call compileall again.
R. David Murray95333e32010-12-14 22:32:50 +0000543 self.assertRunOK('-q', self.pkgdir)
544 self.assertTrue(os.path.exists(self.pkgdir_cachedir))
Barry Warsawc04317f2010-04-26 15:59:03 +0000545 self.assertFalse(os.path.exists(cachecachedir))
546
Elvis Pranskevichusa6b3ec52018-10-10 12:43:14 -0400547 @without_source_date_epoch # timestamp invalidation test
R. David Murray650f1472010-11-20 21:18:51 +0000548 def test_force(self):
R. David Murray95333e32010-12-14 22:32:50 +0000549 self.assertRunOK('-q', self.pkgdir)
Brett Cannon7822e122013-06-14 23:04:02 -0400550 pycpath = importlib.util.cache_from_source(self.barfn)
R. David Murray650f1472010-11-20 21:18:51 +0000551 # set atime/mtime backward to avoid file timestamp resolution issues
552 os.utime(pycpath, (time.time()-60,)*2)
R. David Murray95333e32010-12-14 22:32:50 +0000553 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 Murray650f1472010-11-20 21:18:51 +0000562
R. David Murray95333e32010-12-14 22:32:50 +0000563 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 Murray650f1472010-11-20 21:18:51 +0000574
Benjamin Peterson344ff4a2014-08-19 16:13:26 -0500575 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' Balhar8e7bb992019-09-26 08:28:26 +0200609 @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 Murray650f1472010-11-20 21:18:51 +0000623 def test_quiet(self):
R. David Murray95333e32010-12-14 22:32:50 +0000624 noisy = self.assertRunOK(self.pkgdir)
625 quiet = self.assertRunOK('-q', self.pkgdir)
626 self.assertNotEqual(b'', noisy)
627 self.assertEqual(b'', quiet)
R. David Murray650f1472010-11-20 21:18:51 +0000628
Berker Peksag6554b862014-10-15 11:10:57 +0300629 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 Murray650f1472010-11-20 21:18:51 +0000636 def test_regexp(self):
R David Murrayee1a7cb2011-07-01 14:55:43 -0400637 self.assertRunOK('-q', '-x', r'ba[^\\/]*$', self.pkgdir)
R. David Murray95333e32010-12-14 22:32:50 +0000638 self.assertNotCompiled(self.barfn)
639 self.assertCompiled(self.initfn)
R. David Murray650f1472010-11-20 21:18:51 +0000640
R. David Murray95333e32010-12-14 22:32:50 +0000641 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 Murray95333e32010-12-14 22:32:50 +0000652 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 Cannon7822e122013-06-14 23:04:02 -0400661 pyc = importlib.util.cache_from_source(bazfn)
R. David Murray95333e32010-12-14 22:32:50 +0000662 os.rename(pyc, os.path.join(self.pkgdir, 'baz.pyc'))
663 os.remove(bazfn)
Victor Stinnere8785ff2013-10-12 14:44:01 +0200664 rc, out, err = script_helper.assert_python_failure(fn, __isolated=False)
R. David Murray95333e32010-12-14 22:32:50 +0000665 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 Cannon7822e122013-06-14 23:04:02 -0400672 self.assertFalse(os.path.exists(importlib.util.cache_from_source(
R. David Murray95333e32010-12-14 22:32:50 +0000673 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 Petersona820c7c2012-09-25 11:42:35 -0400707 p = script_helper.spawn_python(*(self._get_run_args(()) + ['-i', '-']))
R. David Murray95333e32010-12-14 22:32:50 +0000708 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 Murray5317e9c2010-12-16 19:08:51 +0000719 self.assertRegex(out, b'rror')
R. David Murray95333e32010-12-14 22:32:50 +0000720 self.assertNotCompiled(bingfn)
721 self.assertCompiled(self.initfn)
722 self.assertCompiled(self.barfn)
723
R. David Murray5317e9c2010-12-16 19:08:51 +0000724 def test_invalid_arg_produces_message(self):
725 out = self.assertRunOK('badfilename')
Victor Stinner53071262011-05-11 00:36:28 +0200726 self.assertRegex(out, b"Can't list 'badfilename'")
R. David Murray650f1472010-11-20 21:18:51 +0000727
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800728 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 Cannonf1a8df02014-09-12 10:39:48 -0400741 @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 Pitrou1a2dd822019-05-15 23:45:18 +0200762 self.assertEqual(compile_dir.call_args[-1]['workers'], 0)
Brett Cannonf1a8df02014-09-12 10:39:48 -0400763
Lumír 'Frenzy' Balhar8e7bb992019-09-26 08:28:26 +0200764 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 Warsaw28a691b2010-04-17 00:19:56 +0000832
Min ho Kimc4cacc82019-07-31 08:16:13 +1000833class CommandLineTestsWithSourceEpoch(CommandLineTestsBase,
Elvis Pranskevichusa6b3ec52018-10-10 12:43:14 -0400834 unittest.TestCase,
835 metaclass=SourceDateEpochTestMeta,
836 source_date_epoch=True):
837 pass
838
839
Min ho Kimc4cacc82019-07-31 08:16:13 +1000840class CommandLineTestsNoSourceEpoch(CommandLineTestsBase,
Elvis Pranskevichusa6b3ec52018-10-10 12:43:14 -0400841 unittest.TestCase,
842 metaclass=SourceDateEpochTestMeta,
843 source_date_epoch=False):
844 pass
845
846
847
Brett Cannonbefb14f2009-02-10 02:10:16 +0000848if __name__ == "__main__":
Brett Cannon7822e122013-06-14 23:04:02 -0400849 unittest.main()