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