Martin v. Löwis | 4b00307 | 2010-03-16 13:19:21 +0000 | [diff] [blame] | 1 | import sys |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 2 | import compileall |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 3 | import importlib.util |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 4 | import os |
| 5 | import py_compile |
| 6 | import shutil |
| 7 | import struct |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 8 | import tempfile |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 9 | import time |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 10 | import unittest |
Martin v. Löwis | 4b00307 | 2010-03-16 13:19:21 +0000 | [diff] [blame] | 11 | import io |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 12 | |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 13 | from test import support, script_helper |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 14 | |
| 15 | class CompileallTests(unittest.TestCase): |
| 16 | |
| 17 | def setUp(self): |
| 18 | self.directory = tempfile.mkdtemp() |
| 19 | self.source_path = os.path.join(self.directory, '_test.py') |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 20 | self.bc_path = importlib.util.cache_from_source(self.source_path) |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 21 | with open(self.source_path, 'w') as file: |
| 22 | file.write('x = 123\n') |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 23 | self.source_path2 = os.path.join(self.directory, '_test2.py') |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 24 | self.bc_path2 = importlib.util.cache_from_source(self.source_path2) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 25 | shutil.copyfile(self.source_path, self.source_path2) |
Georg Brandl | 4543846 | 2011-02-07 12:36:54 +0000 | [diff] [blame] | 26 | self.subdirectory = os.path.join(self.directory, '_subdir') |
| 27 | os.mkdir(self.subdirectory) |
| 28 | self.source_path3 = os.path.join(self.subdirectory, '_test3.py') |
| 29 | shutil.copyfile(self.source_path, self.source_path3) |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 30 | |
| 31 | def tearDown(self): |
| 32 | shutil.rmtree(self.directory) |
| 33 | |
| 34 | def data(self): |
| 35 | with open(self.bc_path, 'rb') as file: |
| 36 | data = file.read(8) |
| 37 | mtime = int(os.stat(self.source_path).st_mtime) |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 38 | compare = struct.pack('<4sl', importlib.util.MAGIC_NUMBER, mtime) |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 39 | return data, compare |
| 40 | |
Serhiy Storchaka | 4376763 | 2013-11-03 21:31:38 +0200 | [diff] [blame] | 41 | @unittest.skipUnless(hasattr(os, 'stat'), 'test needs os.stat()') |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 42 | def recreation_check(self, metadata): |
| 43 | """Check that compileall recreates bytecode when the new metadata is |
| 44 | used.""" |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 45 | py_compile.compile(self.source_path) |
| 46 | self.assertEqual(*self.data()) |
| 47 | with open(self.bc_path, 'rb') as file: |
| 48 | bc = file.read()[len(metadata):] |
| 49 | with open(self.bc_path, 'wb') as file: |
| 50 | file.write(metadata) |
| 51 | file.write(bc) |
| 52 | self.assertNotEqual(*self.data()) |
| 53 | compileall.compile_dir(self.directory, force=False, quiet=True) |
| 54 | self.assertTrue(*self.data()) |
| 55 | |
| 56 | def test_mtime(self): |
| 57 | # Test a change in mtime leads to a new .pyc. |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 58 | self.recreation_check(struct.pack('<4sl', importlib.util.MAGIC_NUMBER, |
| 59 | 1)) |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 60 | |
| 61 | def test_magic_number(self): |
| 62 | # Test a change in mtime leads to a new .pyc. |
| 63 | self.recreation_check(b'\0\0\0\0') |
| 64 | |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 65 | def test_compile_files(self): |
| 66 | # Test compiling a single file, and complete directory |
| 67 | for fn in (self.bc_path, self.bc_path2): |
| 68 | try: |
| 69 | os.unlink(fn) |
| 70 | except: |
| 71 | pass |
| 72 | compileall.compile_file(self.source_path, force=False, quiet=True) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 73 | self.assertTrue(os.path.isfile(self.bc_path) and |
| 74 | not os.path.isfile(self.bc_path2)) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 75 | os.unlink(self.bc_path) |
| 76 | compileall.compile_dir(self.directory, force=False, quiet=True) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 77 | self.assertTrue(os.path.isfile(self.bc_path) and |
| 78 | os.path.isfile(self.bc_path2)) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 79 | os.unlink(self.bc_path) |
| 80 | os.unlink(self.bc_path2) |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 81 | |
Barry Warsaw | c8a99de | 2010-04-29 18:43:10 +0000 | [diff] [blame] | 82 | def test_no_pycache_in_non_package(self): |
| 83 | # Bug 8563 reported that __pycache__ directories got created by |
| 84 | # compile_file() for non-.py files. |
| 85 | data_dir = os.path.join(self.directory, 'data') |
| 86 | data_file = os.path.join(data_dir, 'file') |
| 87 | os.mkdir(data_dir) |
| 88 | # touch data/file |
| 89 | with open(data_file, 'w'): |
| 90 | pass |
| 91 | compileall.compile_file(data_file) |
| 92 | self.assertFalse(os.path.exists(os.path.join(data_dir, '__pycache__'))) |
| 93 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 94 | def test_optimize(self): |
| 95 | # make sure compiling with different optimization settings than the |
| 96 | # interpreter's creates the correct file names |
| 97 | optimize = 1 if __debug__ else 0 |
| 98 | compileall.compile_dir(self.directory, quiet=True, optimize=optimize) |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 99 | cached = importlib.util.cache_from_source(self.source_path, |
| 100 | debug_override=not optimize) |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 101 | self.assertTrue(os.path.isfile(cached)) |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 102 | cached2 = importlib.util.cache_from_source(self.source_path2, |
| 103 | debug_override=not optimize) |
Georg Brandl | 4543846 | 2011-02-07 12:36:54 +0000 | [diff] [blame] | 104 | self.assertTrue(os.path.isfile(cached2)) |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 105 | cached3 = importlib.util.cache_from_source(self.source_path3, |
| 106 | debug_override=not optimize) |
Georg Brandl | 4543846 | 2011-02-07 12:36:54 +0000 | [diff] [blame] | 107 | self.assertTrue(os.path.isfile(cached3)) |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 108 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 109 | |
Martin v. Löwis | 4b00307 | 2010-03-16 13:19:21 +0000 | [diff] [blame] | 110 | class EncodingTest(unittest.TestCase): |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 111 | """Issue 6716: compileall should escape source code when printing errors |
| 112 | to stdout.""" |
Martin v. Löwis | 4b00307 | 2010-03-16 13:19:21 +0000 | [diff] [blame] | 113 | |
| 114 | def setUp(self): |
| 115 | self.directory = tempfile.mkdtemp() |
| 116 | self.source_path = os.path.join(self.directory, '_test.py') |
| 117 | with open(self.source_path, 'w', encoding='utf-8') as file: |
| 118 | file.write('# -*- coding: utf-8 -*-\n') |
| 119 | file.write('print u"\u20ac"\n') |
| 120 | |
| 121 | def tearDown(self): |
| 122 | shutil.rmtree(self.directory) |
| 123 | |
| 124 | def test_error(self): |
| 125 | try: |
| 126 | orig_stdout = sys.stdout |
| 127 | sys.stdout = io.TextIOWrapper(io.BytesIO(),encoding='ascii') |
| 128 | compileall.compile_dir(self.directory) |
| 129 | finally: |
| 130 | sys.stdout = orig_stdout |
| 131 | |
Barry Warsaw | c8a99de | 2010-04-29 18:43:10 +0000 | [diff] [blame] | 132 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 133 | class CommandLineTests(unittest.TestCase): |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 134 | """Test compileall's CLI.""" |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 135 | |
Benjamin Peterson | a820c7c | 2012-09-25 11:42:35 -0400 | [diff] [blame] | 136 | def _get_run_args(self, args): |
| 137 | interp_args = ['-S'] |
| 138 | if sys.flags.optimize: |
| 139 | interp_args.append({1 : '-O', 2 : '-OO'}[sys.flags.optimize]) |
| 140 | return interp_args + ['-m', 'compileall'] + list(args) |
| 141 | |
R. David Murray | 5317e9c | 2010-12-16 19:08:51 +0000 | [diff] [blame] | 142 | def assertRunOK(self, *args, **env_vars): |
| 143 | rc, out, err = script_helper.assert_python_ok( |
Benjamin Peterson | a820c7c | 2012-09-25 11:42:35 -0400 | [diff] [blame] | 144 | *self._get_run_args(args), **env_vars) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 145 | self.assertEqual(b'', err) |
| 146 | return out |
| 147 | |
R. David Murray | 5317e9c | 2010-12-16 19:08:51 +0000 | [diff] [blame] | 148 | def assertRunNotOK(self, *args, **env_vars): |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 149 | rc, out, err = script_helper.assert_python_failure( |
Benjamin Peterson | a820c7c | 2012-09-25 11:42:35 -0400 | [diff] [blame] | 150 | *self._get_run_args(args), **env_vars) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 151 | return rc, out, err |
| 152 | |
| 153 | def assertCompiled(self, fn): |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 154 | path = importlib.util.cache_from_source(fn) |
| 155 | self.assertTrue(os.path.exists(path)) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 156 | |
| 157 | def assertNotCompiled(self, fn): |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 158 | path = importlib.util.cache_from_source(fn) |
| 159 | self.assertFalse(os.path.exists(path)) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 160 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 161 | def setUp(self): |
| 162 | self.addCleanup(self._cleanup) |
| 163 | self.directory = tempfile.mkdtemp() |
| 164 | self.pkgdir = os.path.join(self.directory, 'foo') |
| 165 | os.mkdir(self.pkgdir) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 166 | self.pkgdir_cachedir = os.path.join(self.pkgdir, '__pycache__') |
| 167 | # Create the __init__.py and a package module. |
| 168 | self.initfn = script_helper.make_script(self.pkgdir, '__init__', '') |
| 169 | self.barfn = script_helper.make_script(self.pkgdir, 'bar', '') |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 170 | |
| 171 | def _cleanup(self): |
| 172 | support.rmtree(self.directory) |
R. David Murray | 5317e9c | 2010-12-16 19:08:51 +0000 | [diff] [blame] | 173 | |
| 174 | def test_no_args_compiles_path(self): |
| 175 | # Note that -l is implied for the no args case. |
| 176 | bazfn = script_helper.make_script(self.directory, 'baz', '') |
| 177 | self.assertRunOK(PYTHONPATH=self.directory) |
| 178 | self.assertCompiled(bazfn) |
| 179 | self.assertNotCompiled(self.initfn) |
| 180 | self.assertNotCompiled(self.barfn) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 181 | |
R David Murray | 8a1d1e6 | 2013-12-15 20:49:38 -0500 | [diff] [blame] | 182 | def test_no_args_respects_force_flag(self): |
| 183 | bazfn = script_helper.make_script(self.directory, 'baz', '') |
| 184 | self.assertRunOK(PYTHONPATH=self.directory) |
R David Murray | 755d5ea | 2013-12-15 20:56:00 -0500 | [diff] [blame] | 185 | pycpath = importlib.util.cache_from_source(bazfn) |
R David Murray | 8a1d1e6 | 2013-12-15 20:49:38 -0500 | [diff] [blame] | 186 | # Set atime/mtime backward to avoid file timestamp resolution issues |
| 187 | os.utime(pycpath, (time.time()-60,)*2) |
| 188 | mtime = os.stat(pycpath).st_mtime |
| 189 | # Without force, no recompilation |
| 190 | self.assertRunOK(PYTHONPATH=self.directory) |
| 191 | mtime2 = os.stat(pycpath).st_mtime |
| 192 | self.assertEqual(mtime, mtime2) |
| 193 | # Now force it. |
| 194 | self.assertRunOK('-f', PYTHONPATH=self.directory) |
| 195 | mtime2 = os.stat(pycpath).st_mtime |
| 196 | self.assertNotEqual(mtime, mtime2) |
| 197 | |
| 198 | def test_no_args_respects_quiet_flag(self): |
| 199 | script_helper.make_script(self.directory, 'baz', '') |
| 200 | noisy = self.assertRunOK(PYTHONPATH=self.directory) |
| 201 | self.assertIn(b'Listing ', noisy) |
| 202 | quiet = self.assertRunOK('-q', PYTHONPATH=self.directory) |
| 203 | self.assertNotIn(b'Listing ', quiet) |
| 204 | |
Georg Brandl | 1463a3f | 2010-10-14 07:42:27 +0000 | [diff] [blame] | 205 | # Ensure that the default behavior of compileall's CLI is to create |
| 206 | # PEP 3147 pyc/pyo files. |
| 207 | for name, ext, switch in [ |
| 208 | ('normal', 'pyc', []), |
| 209 | ('optimize', 'pyo', ['-O']), |
Éric Araujo | 31717e8 | 2010-11-26 00:39:59 +0000 | [diff] [blame] | 210 | ('doubleoptimize', 'pyo', ['-OO']), |
Georg Brandl | 1463a3f | 2010-10-14 07:42:27 +0000 | [diff] [blame] | 211 | ]: |
| 212 | def f(self, ext=ext, switch=switch): |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 213 | script_helper.assert_python_ok(*(switch + |
| 214 | ['-m', 'compileall', '-q', self.pkgdir])) |
Georg Brandl | 1463a3f | 2010-10-14 07:42:27 +0000 | [diff] [blame] | 215 | # Verify the __pycache__ directory contents. |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 216 | self.assertTrue(os.path.exists(self.pkgdir_cachedir)) |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 217 | expected = sorted(base.format(sys.implementation.cache_tag, ext) |
| 218 | for base in ('__init__.{}.{}', 'bar.{}.{}')) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 219 | self.assertEqual(sorted(os.listdir(self.pkgdir_cachedir)), expected) |
Georg Brandl | 1463a3f | 2010-10-14 07:42:27 +0000 | [diff] [blame] | 220 | # Make sure there are no .pyc files in the source directory. |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 221 | self.assertFalse([fn for fn in os.listdir(self.pkgdir) |
| 222 | if fn.endswith(ext)]) |
Georg Brandl | 1463a3f | 2010-10-14 07:42:27 +0000 | [diff] [blame] | 223 | locals()['test_pep3147_paths_' + name] = f |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 224 | |
| 225 | def test_legacy_paths(self): |
| 226 | # Ensure that with the proper switch, compileall leaves legacy |
| 227 | # pyc/pyo files, and no __pycache__ directory. |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 228 | self.assertRunOK('-b', '-q', self.pkgdir) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 229 | # Verify the __pycache__ directory contents. |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 230 | self.assertFalse(os.path.exists(self.pkgdir_cachedir)) |
Benjamin Peterson | a820c7c | 2012-09-25 11:42:35 -0400 | [diff] [blame] | 231 | opt = 'c' if __debug__ else 'o' |
| 232 | expected = sorted(['__init__.py', '__init__.py' + opt, 'bar.py', |
| 233 | 'bar.py' + opt]) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 234 | self.assertEqual(sorted(os.listdir(self.pkgdir)), expected) |
| 235 | |
Barry Warsaw | c04317f | 2010-04-26 15:59:03 +0000 | [diff] [blame] | 236 | def test_multiple_runs(self): |
| 237 | # Bug 8527 reported that multiple calls produced empty |
| 238 | # __pycache__/__pycache__ directories. |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 239 | self.assertRunOK('-q', self.pkgdir) |
Barry Warsaw | c04317f | 2010-04-26 15:59:03 +0000 | [diff] [blame] | 240 | # Verify the __pycache__ directory contents. |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 241 | self.assertTrue(os.path.exists(self.pkgdir_cachedir)) |
| 242 | cachecachedir = os.path.join(self.pkgdir_cachedir, '__pycache__') |
Barry Warsaw | c04317f | 2010-04-26 15:59:03 +0000 | [diff] [blame] | 243 | self.assertFalse(os.path.exists(cachecachedir)) |
| 244 | # Call compileall again. |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 245 | self.assertRunOK('-q', self.pkgdir) |
| 246 | self.assertTrue(os.path.exists(self.pkgdir_cachedir)) |
Barry Warsaw | c04317f | 2010-04-26 15:59:03 +0000 | [diff] [blame] | 247 | self.assertFalse(os.path.exists(cachecachedir)) |
| 248 | |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 249 | def test_force(self): |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 250 | self.assertRunOK('-q', self.pkgdir) |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 251 | pycpath = importlib.util.cache_from_source(self.barfn) |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 252 | # set atime/mtime backward to avoid file timestamp resolution issues |
| 253 | os.utime(pycpath, (time.time()-60,)*2) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 254 | mtime = os.stat(pycpath).st_mtime |
| 255 | # without force, no recompilation |
| 256 | self.assertRunOK('-q', self.pkgdir) |
| 257 | mtime2 = os.stat(pycpath).st_mtime |
| 258 | self.assertEqual(mtime, mtime2) |
| 259 | # now force it. |
| 260 | self.assertRunOK('-q', '-f', self.pkgdir) |
| 261 | mtime2 = os.stat(pycpath).st_mtime |
| 262 | self.assertNotEqual(mtime, mtime2) |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 263 | |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 264 | def test_recursion_control(self): |
| 265 | subpackage = os.path.join(self.pkgdir, 'spam') |
| 266 | os.mkdir(subpackage) |
| 267 | subinitfn = script_helper.make_script(subpackage, '__init__', '') |
| 268 | hamfn = script_helper.make_script(subpackage, 'ham', '') |
| 269 | self.assertRunOK('-q', '-l', self.pkgdir) |
| 270 | self.assertNotCompiled(subinitfn) |
| 271 | self.assertFalse(os.path.exists(os.path.join(subpackage, '__pycache__'))) |
| 272 | self.assertRunOK('-q', self.pkgdir) |
| 273 | self.assertCompiled(subinitfn) |
| 274 | self.assertCompiled(hamfn) |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 275 | |
| 276 | def test_quiet(self): |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 277 | noisy = self.assertRunOK(self.pkgdir) |
| 278 | quiet = self.assertRunOK('-q', self.pkgdir) |
| 279 | self.assertNotEqual(b'', noisy) |
| 280 | self.assertEqual(b'', quiet) |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 281 | |
| 282 | def test_regexp(self): |
R David Murray | ee1a7cb | 2011-07-01 14:55:43 -0400 | [diff] [blame] | 283 | self.assertRunOK('-q', '-x', r'ba[^\\/]*$', self.pkgdir) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 284 | self.assertNotCompiled(self.barfn) |
| 285 | self.assertCompiled(self.initfn) |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 286 | |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 287 | def test_multiple_dirs(self): |
| 288 | pkgdir2 = os.path.join(self.directory, 'foo2') |
| 289 | os.mkdir(pkgdir2) |
| 290 | init2fn = script_helper.make_script(pkgdir2, '__init__', '') |
| 291 | bar2fn = script_helper.make_script(pkgdir2, 'bar2', '') |
| 292 | self.assertRunOK('-q', self.pkgdir, pkgdir2) |
| 293 | self.assertCompiled(self.initfn) |
| 294 | self.assertCompiled(self.barfn) |
| 295 | self.assertCompiled(init2fn) |
| 296 | self.assertCompiled(bar2fn) |
| 297 | |
| 298 | def test_d_takes_exactly_one_dir(self): |
| 299 | rc, out, err = self.assertRunNotOK('-d', 'foo') |
| 300 | self.assertEqual(out, b'') |
| 301 | self.assertRegex(err, b'-d') |
| 302 | rc, out, err = self.assertRunNotOK('-d', 'foo', 'bar') |
| 303 | self.assertEqual(out, b'') |
| 304 | self.assertRegex(err, b'-d') |
| 305 | |
| 306 | def test_d_compile_error(self): |
| 307 | script_helper.make_script(self.pkgdir, 'crunchyfrog', 'bad(syntax') |
| 308 | rc, out, err = self.assertRunNotOK('-q', '-d', 'dinsdale', self.pkgdir) |
| 309 | self.assertRegex(out, b'File "dinsdale') |
| 310 | |
| 311 | def test_d_runtime_error(self): |
| 312 | bazfn = script_helper.make_script(self.pkgdir, 'baz', 'raise Exception') |
| 313 | self.assertRunOK('-q', '-d', 'dinsdale', self.pkgdir) |
| 314 | fn = script_helper.make_script(self.pkgdir, 'bing', 'import baz') |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 315 | pyc = importlib.util.cache_from_source(bazfn) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 316 | os.rename(pyc, os.path.join(self.pkgdir, 'baz.pyc')) |
| 317 | os.remove(bazfn) |
Victor Stinner | e8785ff | 2013-10-12 14:44:01 +0200 | [diff] [blame] | 318 | rc, out, err = script_helper.assert_python_failure(fn, __isolated=False) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 319 | self.assertRegex(err, b'File "dinsdale') |
| 320 | |
| 321 | def test_include_bad_file(self): |
| 322 | rc, out, err = self.assertRunNotOK( |
| 323 | '-i', os.path.join(self.directory, 'nosuchfile'), self.pkgdir) |
| 324 | self.assertRegex(out, b'rror.*nosuchfile') |
| 325 | self.assertNotRegex(err, b'Traceback') |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 326 | self.assertFalse(os.path.exists(importlib.util.cache_from_source( |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 327 | self.pkgdir_cachedir))) |
| 328 | |
| 329 | def test_include_file_with_arg(self): |
| 330 | f1 = script_helper.make_script(self.pkgdir, 'f1', '') |
| 331 | f2 = script_helper.make_script(self.pkgdir, 'f2', '') |
| 332 | f3 = script_helper.make_script(self.pkgdir, 'f3', '') |
| 333 | f4 = script_helper.make_script(self.pkgdir, 'f4', '') |
| 334 | with open(os.path.join(self.directory, 'l1'), 'w') as l1: |
| 335 | l1.write(os.path.join(self.pkgdir, 'f1.py')+os.linesep) |
| 336 | l1.write(os.path.join(self.pkgdir, 'f2.py')+os.linesep) |
| 337 | self.assertRunOK('-i', os.path.join(self.directory, 'l1'), f4) |
| 338 | self.assertCompiled(f1) |
| 339 | self.assertCompiled(f2) |
| 340 | self.assertNotCompiled(f3) |
| 341 | self.assertCompiled(f4) |
| 342 | |
| 343 | def test_include_file_no_arg(self): |
| 344 | f1 = script_helper.make_script(self.pkgdir, 'f1', '') |
| 345 | f2 = script_helper.make_script(self.pkgdir, 'f2', '') |
| 346 | f3 = script_helper.make_script(self.pkgdir, 'f3', '') |
| 347 | f4 = script_helper.make_script(self.pkgdir, 'f4', '') |
| 348 | with open(os.path.join(self.directory, 'l1'), 'w') as l1: |
| 349 | l1.write(os.path.join(self.pkgdir, 'f2.py')+os.linesep) |
| 350 | self.assertRunOK('-i', os.path.join(self.directory, 'l1')) |
| 351 | self.assertNotCompiled(f1) |
| 352 | self.assertCompiled(f2) |
| 353 | self.assertNotCompiled(f3) |
| 354 | self.assertNotCompiled(f4) |
| 355 | |
| 356 | def test_include_on_stdin(self): |
| 357 | f1 = script_helper.make_script(self.pkgdir, 'f1', '') |
| 358 | f2 = script_helper.make_script(self.pkgdir, 'f2', '') |
| 359 | f3 = script_helper.make_script(self.pkgdir, 'f3', '') |
| 360 | f4 = script_helper.make_script(self.pkgdir, 'f4', '') |
Benjamin Peterson | a820c7c | 2012-09-25 11:42:35 -0400 | [diff] [blame] | 361 | p = script_helper.spawn_python(*(self._get_run_args(()) + ['-i', '-'])) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 362 | p.stdin.write((f3+os.linesep).encode('ascii')) |
| 363 | script_helper.kill_python(p) |
| 364 | self.assertNotCompiled(f1) |
| 365 | self.assertNotCompiled(f2) |
| 366 | self.assertCompiled(f3) |
| 367 | self.assertNotCompiled(f4) |
| 368 | |
| 369 | def test_compiles_as_much_as_possible(self): |
| 370 | bingfn = script_helper.make_script(self.pkgdir, 'bing', 'syntax(error') |
| 371 | rc, out, err = self.assertRunNotOK('nosuchfile', self.initfn, |
| 372 | bingfn, self.barfn) |
R. David Murray | 5317e9c | 2010-12-16 19:08:51 +0000 | [diff] [blame] | 373 | self.assertRegex(out, b'rror') |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 374 | self.assertNotCompiled(bingfn) |
| 375 | self.assertCompiled(self.initfn) |
| 376 | self.assertCompiled(self.barfn) |
| 377 | |
R. David Murray | 5317e9c | 2010-12-16 19:08:51 +0000 | [diff] [blame] | 378 | def test_invalid_arg_produces_message(self): |
| 379 | out = self.assertRunOK('badfilename') |
Victor Stinner | 5307126 | 2011-05-11 00:36:28 +0200 | [diff] [blame] | 380 | self.assertRegex(out, b"Can't list 'badfilename'") |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 381 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 382 | |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 383 | if __name__ == "__main__": |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 384 | unittest.main() |