blob: 7b307edfbc19af86d49a60314aba14b5289543cc [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 Cannonbefb14f2009-02-10 02:10:16 +00004import os
Brett Cannon89065d92015-10-09 15:09:43 -07005import pathlib
Brett Cannonbefb14f2009-02-10 02:10:16 +00006import py_compile
7import shutil
8import struct
Brett Cannonbefb14f2009-02-10 02:10:16 +00009import tempfile
R. David Murray650f1472010-11-20 21:18:51 +000010import time
Brett Cannonbefb14f2009-02-10 02:10:16 +000011import unittest
Martin v. Löwis4b003072010-03-16 13:19:21 +000012import io
Brett Cannonbefb14f2009-02-10 02:10:16 +000013
R. David Murray95333e32010-12-14 22:32:50 +000014from test import support, script_helper
Brett Cannonbefb14f2009-02-10 02:10:16 +000015
16class CompileallTests(unittest.TestCase):
17
18 def setUp(self):
19 self.directory = tempfile.mkdtemp()
20 self.source_path = os.path.join(self.directory, '_test.py')
Brett Cannon7822e122013-06-14 23:04:02 -040021 self.bc_path = importlib.util.cache_from_source(self.source_path)
Brett Cannonbefb14f2009-02-10 02:10:16 +000022 with open(self.source_path, 'w') as file:
23 file.write('x = 123\n')
Matthias Klosec33b9022010-03-16 00:36:26 +000024 self.source_path2 = os.path.join(self.directory, '_test2.py')
Brett Cannon7822e122013-06-14 23:04:02 -040025 self.bc_path2 = importlib.util.cache_from_source(self.source_path2)
Matthias Klosec33b9022010-03-16 00:36:26 +000026 shutil.copyfile(self.source_path, self.source_path2)
Georg Brandl45438462011-02-07 12:36:54 +000027 self.subdirectory = os.path.join(self.directory, '_subdir')
28 os.mkdir(self.subdirectory)
29 self.source_path3 = os.path.join(self.subdirectory, '_test3.py')
30 shutil.copyfile(self.source_path, self.source_path3)
Brett Cannonbefb14f2009-02-10 02:10:16 +000031
32 def tearDown(self):
33 shutil.rmtree(self.directory)
34
35 def data(self):
36 with open(self.bc_path, 'rb') as file:
37 data = file.read(8)
38 mtime = int(os.stat(self.source_path).st_mtime)
Brett Cannon7822e122013-06-14 23:04:02 -040039 compare = struct.pack('<4sl', importlib.util.MAGIC_NUMBER, mtime)
Brett Cannonbefb14f2009-02-10 02:10:16 +000040 return data, compare
41
Serhiy Storchaka43767632013-11-03 21:31:38 +020042 @unittest.skipUnless(hasattr(os, 'stat'), 'test needs os.stat()')
Brett Cannonbefb14f2009-02-10 02:10:16 +000043 def recreation_check(self, metadata):
44 """Check that compileall recreates bytecode when the new metadata is
45 used."""
Brett Cannonbefb14f2009-02-10 02:10:16 +000046 py_compile.compile(self.source_path)
47 self.assertEqual(*self.data())
48 with open(self.bc_path, 'rb') as file:
49 bc = file.read()[len(metadata):]
50 with open(self.bc_path, 'wb') as file:
51 file.write(metadata)
52 file.write(bc)
53 self.assertNotEqual(*self.data())
54 compileall.compile_dir(self.directory, force=False, quiet=True)
55 self.assertTrue(*self.data())
56
57 def test_mtime(self):
58 # Test a change in mtime leads to a new .pyc.
Brett Cannon7822e122013-06-14 23:04:02 -040059 self.recreation_check(struct.pack('<4sl', importlib.util.MAGIC_NUMBER,
60 1))
Brett Cannonbefb14f2009-02-10 02:10:16 +000061
62 def test_magic_number(self):
63 # Test a change in mtime leads to a new .pyc.
64 self.recreation_check(b'\0\0\0\0')
65
Matthias Klosec33b9022010-03-16 00:36:26 +000066 def test_compile_files(self):
67 # Test compiling a single file, and complete directory
68 for fn in (self.bc_path, self.bc_path2):
69 try:
70 os.unlink(fn)
71 except:
72 pass
73 compileall.compile_file(self.source_path, force=False, quiet=True)
Barry Warsaw28a691b2010-04-17 00:19:56 +000074 self.assertTrue(os.path.isfile(self.bc_path) and
75 not os.path.isfile(self.bc_path2))
Matthias Klosec33b9022010-03-16 00:36:26 +000076 os.unlink(self.bc_path)
77 compileall.compile_dir(self.directory, force=False, quiet=True)
Barry Warsaw28a691b2010-04-17 00:19:56 +000078 self.assertTrue(os.path.isfile(self.bc_path) and
79 os.path.isfile(self.bc_path2))
Matthias Klosec33b9022010-03-16 00:36:26 +000080 os.unlink(self.bc_path)
81 os.unlink(self.bc_path2)
Brett Cannonbefb14f2009-02-10 02:10:16 +000082
Barry Warsawc8a99de2010-04-29 18:43:10 +000083 def test_no_pycache_in_non_package(self):
84 # Bug 8563 reported that __pycache__ directories got created by
85 # compile_file() for non-.py files.
86 data_dir = os.path.join(self.directory, 'data')
87 data_file = os.path.join(data_dir, 'file')
88 os.mkdir(data_dir)
89 # touch data/file
90 with open(data_file, 'w'):
91 pass
92 compileall.compile_file(data_file)
93 self.assertFalse(os.path.exists(os.path.join(data_dir, '__pycache__')))
94
Georg Brandl8334fd92010-12-04 10:26:46 +000095 def test_optimize(self):
96 # make sure compiling with different optimization settings than the
97 # interpreter's creates the correct file names
98 optimize = 1 if __debug__ else 0
99 compileall.compile_dir(self.directory, quiet=True, optimize=optimize)
Brett Cannon7822e122013-06-14 23:04:02 -0400100 cached = importlib.util.cache_from_source(self.source_path,
101 debug_override=not optimize)
Georg Brandl8334fd92010-12-04 10:26:46 +0000102 self.assertTrue(os.path.isfile(cached))
Brett Cannon7822e122013-06-14 23:04:02 -0400103 cached2 = importlib.util.cache_from_source(self.source_path2,
104 debug_override=not optimize)
Georg Brandl45438462011-02-07 12:36:54 +0000105 self.assertTrue(os.path.isfile(cached2))
Brett Cannon7822e122013-06-14 23:04:02 -0400106 cached3 = importlib.util.cache_from_source(self.source_path3,
107 debug_override=not optimize)
Georg Brandl45438462011-02-07 12:36:54 +0000108 self.assertTrue(os.path.isfile(cached3))
Georg Brandl8334fd92010-12-04 10:26:46 +0000109
Barry Warsaw28a691b2010-04-17 00:19:56 +0000110
Martin v. Löwis4b003072010-03-16 13:19:21 +0000111class EncodingTest(unittest.TestCase):
Barry Warsaw28a691b2010-04-17 00:19:56 +0000112 """Issue 6716: compileall should escape source code when printing errors
113 to stdout."""
Martin v. Löwis4b003072010-03-16 13:19:21 +0000114
115 def setUp(self):
116 self.directory = tempfile.mkdtemp()
117 self.source_path = os.path.join(self.directory, '_test.py')
118 with open(self.source_path, 'w', encoding='utf-8') as file:
119 file.write('# -*- coding: utf-8 -*-\n')
120 file.write('print u"\u20ac"\n')
121
122 def tearDown(self):
123 shutil.rmtree(self.directory)
124
125 def test_error(self):
126 try:
127 orig_stdout = sys.stdout
128 sys.stdout = io.TextIOWrapper(io.BytesIO(),encoding='ascii')
129 compileall.compile_dir(self.directory)
130 finally:
131 sys.stdout = orig_stdout
132
Barry Warsawc8a99de2010-04-29 18:43:10 +0000133
Barry Warsaw28a691b2010-04-17 00:19:56 +0000134class CommandLineTests(unittest.TestCase):
R. David Murray650f1472010-11-20 21:18:51 +0000135 """Test compileall's CLI."""
Barry Warsaw28a691b2010-04-17 00:19:56 +0000136
Brett Cannon89065d92015-10-09 15:09:43 -0700137 @classmethod
138 def setUpClass(cls):
139 for path in filter(os.path.isdir, sys.path):
140 directory_created = False
141 directory = pathlib.Path(path) / '__pycache__'
142 path = directory / 'test.try'
143 try:
144 if not directory.is_dir():
145 directory.mkdir()
146 directory_created = True
147 with path.open('w') as file:
148 file.write('# for test_compileall')
149 except OSError:
150 sys_path_writable = False
151 break
152 finally:
153 support.unlink(str(path))
154 if directory_created:
155 directory.rmdir()
156 else:
157 sys_path_writable = True
158 cls._sys_path_writable = sys_path_writable
159
160 def _skip_if_sys_path_not_writable(self):
161 if not self._sys_path_writable:
162 raise unittest.SkipTest('not all entries on sys.path are writable')
163
Benjamin Petersona820c7c2012-09-25 11:42:35 -0400164 def _get_run_args(self, args):
165 interp_args = ['-S']
166 if sys.flags.optimize:
167 interp_args.append({1 : '-O', 2 : '-OO'}[sys.flags.optimize])
168 return interp_args + ['-m', 'compileall'] + list(args)
169
R. David Murray5317e9c2010-12-16 19:08:51 +0000170 def assertRunOK(self, *args, **env_vars):
171 rc, out, err = script_helper.assert_python_ok(
Benjamin Petersona820c7c2012-09-25 11:42:35 -0400172 *self._get_run_args(args), **env_vars)
R. David Murray95333e32010-12-14 22:32:50 +0000173 self.assertEqual(b'', err)
174 return out
175
R. David Murray5317e9c2010-12-16 19:08:51 +0000176 def assertRunNotOK(self, *args, **env_vars):
R. David Murray95333e32010-12-14 22:32:50 +0000177 rc, out, err = script_helper.assert_python_failure(
Benjamin Petersona820c7c2012-09-25 11:42:35 -0400178 *self._get_run_args(args), **env_vars)
R. David Murray95333e32010-12-14 22:32:50 +0000179 return rc, out, err
180
181 def assertCompiled(self, fn):
Brett Cannon7822e122013-06-14 23:04:02 -0400182 path = importlib.util.cache_from_source(fn)
183 self.assertTrue(os.path.exists(path))
R. David Murray95333e32010-12-14 22:32:50 +0000184
185 def assertNotCompiled(self, fn):
Brett Cannon7822e122013-06-14 23:04:02 -0400186 path = importlib.util.cache_from_source(fn)
187 self.assertFalse(os.path.exists(path))
R. David Murray95333e32010-12-14 22:32:50 +0000188
Barry Warsaw28a691b2010-04-17 00:19:56 +0000189 def setUp(self):
Barry Warsaw28a691b2010-04-17 00:19:56 +0000190 self.directory = tempfile.mkdtemp()
Brett Cannon89065d92015-10-09 15:09:43 -0700191 self.addCleanup(support.rmtree, self.directory)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000192 self.pkgdir = os.path.join(self.directory, 'foo')
193 os.mkdir(self.pkgdir)
R. David Murray95333e32010-12-14 22:32:50 +0000194 self.pkgdir_cachedir = os.path.join(self.pkgdir, '__pycache__')
195 # Create the __init__.py and a package module.
196 self.initfn = script_helper.make_script(self.pkgdir, '__init__', '')
197 self.barfn = script_helper.make_script(self.pkgdir, 'bar', '')
Barry Warsaw28a691b2010-04-17 00:19:56 +0000198
R. David Murray5317e9c2010-12-16 19:08:51 +0000199 def test_no_args_compiles_path(self):
200 # Note that -l is implied for the no args case.
Brett Cannon89065d92015-10-09 15:09:43 -0700201 self._skip_if_sys_path_not_writable()
R. David Murray5317e9c2010-12-16 19:08:51 +0000202 bazfn = script_helper.make_script(self.directory, 'baz', '')
203 self.assertRunOK(PYTHONPATH=self.directory)
204 self.assertCompiled(bazfn)
205 self.assertNotCompiled(self.initfn)
206 self.assertNotCompiled(self.barfn)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000207
R David Murray8a1d1e62013-12-15 20:49:38 -0500208 def test_no_args_respects_force_flag(self):
Brett Cannon89065d92015-10-09 15:09:43 -0700209 self._skip_if_sys_path_not_writable()
R David Murray8a1d1e62013-12-15 20:49:38 -0500210 bazfn = script_helper.make_script(self.directory, 'baz', '')
211 self.assertRunOK(PYTHONPATH=self.directory)
R David Murray755d5ea2013-12-15 20:56:00 -0500212 pycpath = importlib.util.cache_from_source(bazfn)
R David Murray8a1d1e62013-12-15 20:49:38 -0500213 # Set atime/mtime backward to avoid file timestamp resolution issues
214 os.utime(pycpath, (time.time()-60,)*2)
215 mtime = os.stat(pycpath).st_mtime
216 # Without force, no recompilation
217 self.assertRunOK(PYTHONPATH=self.directory)
218 mtime2 = os.stat(pycpath).st_mtime
219 self.assertEqual(mtime, mtime2)
220 # Now force it.
221 self.assertRunOK('-f', PYTHONPATH=self.directory)
222 mtime2 = os.stat(pycpath).st_mtime
223 self.assertNotEqual(mtime, mtime2)
224
225 def test_no_args_respects_quiet_flag(self):
Brett Cannon89065d92015-10-09 15:09:43 -0700226 self._skip_if_sys_path_not_writable()
R David Murray8a1d1e62013-12-15 20:49:38 -0500227 script_helper.make_script(self.directory, 'baz', '')
228 noisy = self.assertRunOK(PYTHONPATH=self.directory)
229 self.assertIn(b'Listing ', noisy)
230 quiet = self.assertRunOK('-q', PYTHONPATH=self.directory)
231 self.assertNotIn(b'Listing ', quiet)
232
Georg Brandl1463a3f2010-10-14 07:42:27 +0000233 # Ensure that the default behavior of compileall's CLI is to create
234 # PEP 3147 pyc/pyo files.
235 for name, ext, switch in [
236 ('normal', 'pyc', []),
237 ('optimize', 'pyo', ['-O']),
Éric Araujo31717e82010-11-26 00:39:59 +0000238 ('doubleoptimize', 'pyo', ['-OO']),
Georg Brandl1463a3f2010-10-14 07:42:27 +0000239 ]:
240 def f(self, ext=ext, switch=switch):
R. David Murray95333e32010-12-14 22:32:50 +0000241 script_helper.assert_python_ok(*(switch +
242 ['-m', 'compileall', '-q', self.pkgdir]))
Georg Brandl1463a3f2010-10-14 07:42:27 +0000243 # Verify the __pycache__ directory contents.
R. David Murray95333e32010-12-14 22:32:50 +0000244 self.assertTrue(os.path.exists(self.pkgdir_cachedir))
Brett Cannon7822e122013-06-14 23:04:02 -0400245 expected = sorted(base.format(sys.implementation.cache_tag, ext)
246 for base in ('__init__.{}.{}', 'bar.{}.{}'))
R. David Murray95333e32010-12-14 22:32:50 +0000247 self.assertEqual(sorted(os.listdir(self.pkgdir_cachedir)), expected)
Georg Brandl1463a3f2010-10-14 07:42:27 +0000248 # Make sure there are no .pyc files in the source directory.
R. David Murray95333e32010-12-14 22:32:50 +0000249 self.assertFalse([fn for fn in os.listdir(self.pkgdir)
250 if fn.endswith(ext)])
Georg Brandl1463a3f2010-10-14 07:42:27 +0000251 locals()['test_pep3147_paths_' + name] = f
Barry Warsaw28a691b2010-04-17 00:19:56 +0000252
253 def test_legacy_paths(self):
254 # Ensure that with the proper switch, compileall leaves legacy
255 # pyc/pyo files, and no __pycache__ directory.
R. David Murray95333e32010-12-14 22:32:50 +0000256 self.assertRunOK('-b', '-q', self.pkgdir)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000257 # Verify the __pycache__ directory contents.
R. David Murray95333e32010-12-14 22:32:50 +0000258 self.assertFalse(os.path.exists(self.pkgdir_cachedir))
Benjamin Petersona820c7c2012-09-25 11:42:35 -0400259 opt = 'c' if __debug__ else 'o'
260 expected = sorted(['__init__.py', '__init__.py' + opt, 'bar.py',
261 'bar.py' + opt])
Barry Warsaw28a691b2010-04-17 00:19:56 +0000262 self.assertEqual(sorted(os.listdir(self.pkgdir)), expected)
263
Barry Warsawc04317f2010-04-26 15:59:03 +0000264 def test_multiple_runs(self):
265 # Bug 8527 reported that multiple calls produced empty
266 # __pycache__/__pycache__ directories.
R. David Murray95333e32010-12-14 22:32:50 +0000267 self.assertRunOK('-q', self.pkgdir)
Barry Warsawc04317f2010-04-26 15:59:03 +0000268 # Verify the __pycache__ directory contents.
R. David Murray95333e32010-12-14 22:32:50 +0000269 self.assertTrue(os.path.exists(self.pkgdir_cachedir))
270 cachecachedir = os.path.join(self.pkgdir_cachedir, '__pycache__')
Barry Warsawc04317f2010-04-26 15:59:03 +0000271 self.assertFalse(os.path.exists(cachecachedir))
272 # Call compileall again.
R. David Murray95333e32010-12-14 22:32:50 +0000273 self.assertRunOK('-q', self.pkgdir)
274 self.assertTrue(os.path.exists(self.pkgdir_cachedir))
Barry Warsawc04317f2010-04-26 15:59:03 +0000275 self.assertFalse(os.path.exists(cachecachedir))
276
R. David Murray650f1472010-11-20 21:18:51 +0000277 def test_force(self):
R. David Murray95333e32010-12-14 22:32:50 +0000278 self.assertRunOK('-q', self.pkgdir)
Brett Cannon7822e122013-06-14 23:04:02 -0400279 pycpath = importlib.util.cache_from_source(self.barfn)
R. David Murray650f1472010-11-20 21:18:51 +0000280 # set atime/mtime backward to avoid file timestamp resolution issues
281 os.utime(pycpath, (time.time()-60,)*2)
R. David Murray95333e32010-12-14 22:32:50 +0000282 mtime = os.stat(pycpath).st_mtime
283 # without force, no recompilation
284 self.assertRunOK('-q', self.pkgdir)
285 mtime2 = os.stat(pycpath).st_mtime
286 self.assertEqual(mtime, mtime2)
287 # now force it.
288 self.assertRunOK('-q', '-f', self.pkgdir)
289 mtime2 = os.stat(pycpath).st_mtime
290 self.assertNotEqual(mtime, mtime2)
R. David Murray650f1472010-11-20 21:18:51 +0000291
R. David Murray95333e32010-12-14 22:32:50 +0000292 def test_recursion_control(self):
293 subpackage = os.path.join(self.pkgdir, 'spam')
294 os.mkdir(subpackage)
295 subinitfn = script_helper.make_script(subpackage, '__init__', '')
296 hamfn = script_helper.make_script(subpackage, 'ham', '')
297 self.assertRunOK('-q', '-l', self.pkgdir)
298 self.assertNotCompiled(subinitfn)
299 self.assertFalse(os.path.exists(os.path.join(subpackage, '__pycache__')))
300 self.assertRunOK('-q', self.pkgdir)
301 self.assertCompiled(subinitfn)
302 self.assertCompiled(hamfn)
R. David Murray650f1472010-11-20 21:18:51 +0000303
304 def test_quiet(self):
R. David Murray95333e32010-12-14 22:32:50 +0000305 noisy = self.assertRunOK(self.pkgdir)
306 quiet = self.assertRunOK('-q', self.pkgdir)
307 self.assertNotEqual(b'', noisy)
308 self.assertEqual(b'', quiet)
R. David Murray650f1472010-11-20 21:18:51 +0000309
310 def test_regexp(self):
R David Murrayee1a7cb2011-07-01 14:55:43 -0400311 self.assertRunOK('-q', '-x', r'ba[^\\/]*$', self.pkgdir)
R. David Murray95333e32010-12-14 22:32:50 +0000312 self.assertNotCompiled(self.barfn)
313 self.assertCompiled(self.initfn)
R. David Murray650f1472010-11-20 21:18:51 +0000314
R. David Murray95333e32010-12-14 22:32:50 +0000315 def test_multiple_dirs(self):
316 pkgdir2 = os.path.join(self.directory, 'foo2')
317 os.mkdir(pkgdir2)
318 init2fn = script_helper.make_script(pkgdir2, '__init__', '')
319 bar2fn = script_helper.make_script(pkgdir2, 'bar2', '')
320 self.assertRunOK('-q', self.pkgdir, pkgdir2)
321 self.assertCompiled(self.initfn)
322 self.assertCompiled(self.barfn)
323 self.assertCompiled(init2fn)
324 self.assertCompiled(bar2fn)
325
326 def test_d_takes_exactly_one_dir(self):
327 rc, out, err = self.assertRunNotOK('-d', 'foo')
328 self.assertEqual(out, b'')
329 self.assertRegex(err, b'-d')
330 rc, out, err = self.assertRunNotOK('-d', 'foo', 'bar')
331 self.assertEqual(out, b'')
332 self.assertRegex(err, b'-d')
333
334 def test_d_compile_error(self):
335 script_helper.make_script(self.pkgdir, 'crunchyfrog', 'bad(syntax')
336 rc, out, err = self.assertRunNotOK('-q', '-d', 'dinsdale', self.pkgdir)
337 self.assertRegex(out, b'File "dinsdale')
338
339 def test_d_runtime_error(self):
340 bazfn = script_helper.make_script(self.pkgdir, 'baz', 'raise Exception')
341 self.assertRunOK('-q', '-d', 'dinsdale', self.pkgdir)
342 fn = script_helper.make_script(self.pkgdir, 'bing', 'import baz')
Brett Cannon7822e122013-06-14 23:04:02 -0400343 pyc = importlib.util.cache_from_source(bazfn)
R. David Murray95333e32010-12-14 22:32:50 +0000344 os.rename(pyc, os.path.join(self.pkgdir, 'baz.pyc'))
345 os.remove(bazfn)
Victor Stinnere8785ff2013-10-12 14:44:01 +0200346 rc, out, err = script_helper.assert_python_failure(fn, __isolated=False)
R. David Murray95333e32010-12-14 22:32:50 +0000347 self.assertRegex(err, b'File "dinsdale')
348
349 def test_include_bad_file(self):
350 rc, out, err = self.assertRunNotOK(
351 '-i', os.path.join(self.directory, 'nosuchfile'), self.pkgdir)
352 self.assertRegex(out, b'rror.*nosuchfile')
353 self.assertNotRegex(err, b'Traceback')
Brett Cannon7822e122013-06-14 23:04:02 -0400354 self.assertFalse(os.path.exists(importlib.util.cache_from_source(
R. David Murray95333e32010-12-14 22:32:50 +0000355 self.pkgdir_cachedir)))
356
357 def test_include_file_with_arg(self):
358 f1 = script_helper.make_script(self.pkgdir, 'f1', '')
359 f2 = script_helper.make_script(self.pkgdir, 'f2', '')
360 f3 = script_helper.make_script(self.pkgdir, 'f3', '')
361 f4 = script_helper.make_script(self.pkgdir, 'f4', '')
362 with open(os.path.join(self.directory, 'l1'), 'w') as l1:
363 l1.write(os.path.join(self.pkgdir, 'f1.py')+os.linesep)
364 l1.write(os.path.join(self.pkgdir, 'f2.py')+os.linesep)
365 self.assertRunOK('-i', os.path.join(self.directory, 'l1'), f4)
366 self.assertCompiled(f1)
367 self.assertCompiled(f2)
368 self.assertNotCompiled(f3)
369 self.assertCompiled(f4)
370
371 def test_include_file_no_arg(self):
372 f1 = script_helper.make_script(self.pkgdir, 'f1', '')
373 f2 = script_helper.make_script(self.pkgdir, 'f2', '')
374 f3 = script_helper.make_script(self.pkgdir, 'f3', '')
375 f4 = script_helper.make_script(self.pkgdir, 'f4', '')
376 with open(os.path.join(self.directory, 'l1'), 'w') as l1:
377 l1.write(os.path.join(self.pkgdir, 'f2.py')+os.linesep)
378 self.assertRunOK('-i', os.path.join(self.directory, 'l1'))
379 self.assertNotCompiled(f1)
380 self.assertCompiled(f2)
381 self.assertNotCompiled(f3)
382 self.assertNotCompiled(f4)
383
384 def test_include_on_stdin(self):
385 f1 = script_helper.make_script(self.pkgdir, 'f1', '')
386 f2 = script_helper.make_script(self.pkgdir, 'f2', '')
387 f3 = script_helper.make_script(self.pkgdir, 'f3', '')
388 f4 = script_helper.make_script(self.pkgdir, 'f4', '')
Benjamin Petersona820c7c2012-09-25 11:42:35 -0400389 p = script_helper.spawn_python(*(self._get_run_args(()) + ['-i', '-']))
R. David Murray95333e32010-12-14 22:32:50 +0000390 p.stdin.write((f3+os.linesep).encode('ascii'))
391 script_helper.kill_python(p)
392 self.assertNotCompiled(f1)
393 self.assertNotCompiled(f2)
394 self.assertCompiled(f3)
395 self.assertNotCompiled(f4)
396
397 def test_compiles_as_much_as_possible(self):
398 bingfn = script_helper.make_script(self.pkgdir, 'bing', 'syntax(error')
399 rc, out, err = self.assertRunNotOK('nosuchfile', self.initfn,
400 bingfn, self.barfn)
R. David Murray5317e9c2010-12-16 19:08:51 +0000401 self.assertRegex(out, b'rror')
R. David Murray95333e32010-12-14 22:32:50 +0000402 self.assertNotCompiled(bingfn)
403 self.assertCompiled(self.initfn)
404 self.assertCompiled(self.barfn)
405
R. David Murray5317e9c2010-12-16 19:08:51 +0000406 def test_invalid_arg_produces_message(self):
407 out = self.assertRunOK('badfilename')
Victor Stinner53071262011-05-11 00:36:28 +0200408 self.assertRegex(out, b"Can't list 'badfilename'")
R. David Murray650f1472010-11-20 21:18:51 +0000409
Barry Warsaw28a691b2010-04-17 00:19:56 +0000410
Brett Cannonbefb14f2009-02-10 02:10:16 +0000411if __name__ == "__main__":
Brett Cannon7822e122013-06-14 23:04:02 -0400412 unittest.main()