ยปCore Development>Code coverage>Lib/test/test_compileall.py

Python code coverage for Lib/test/test_compileall.py

#countcontent
1n/aimport sys
2n/aimport compileall
3n/aimport importlib.util
4n/aimport test.test_importlib.util
5n/aimport os
6n/aimport pathlib
7n/aimport py_compile
8n/aimport shutil
9n/aimport struct
10n/aimport tempfile
11n/aimport time
12n/aimport unittest
13n/aimport io
14n/a
15n/afrom unittest import mock, skipUnless
16n/atry:
17n/a from concurrent.futures import ProcessPoolExecutor
18n/a _have_multiprocessing = True
19n/aexcept ImportError:
20n/a _have_multiprocessing = False
21n/a
22n/afrom test import support
23n/afrom test.support import script_helper
24n/a
25n/aclass CompileallTests(unittest.TestCase):
26n/a
27n/a def setUp(self):
28n/a self.directory = tempfile.mkdtemp()
29n/a self.source_path = os.path.join(self.directory, '_test.py')
30n/a self.bc_path = importlib.util.cache_from_source(self.source_path)
31n/a with open(self.source_path, 'w') as file:
32n/a file.write('x = 123\n')
33n/a self.source_path2 = os.path.join(self.directory, '_test2.py')
34n/a self.bc_path2 = importlib.util.cache_from_source(self.source_path2)
35n/a shutil.copyfile(self.source_path, self.source_path2)
36n/a self.subdirectory = os.path.join(self.directory, '_subdir')
37n/a os.mkdir(self.subdirectory)
38n/a self.source_path3 = os.path.join(self.subdirectory, '_test3.py')
39n/a shutil.copyfile(self.source_path, self.source_path3)
40n/a
41n/a def tearDown(self):
42n/a shutil.rmtree(self.directory)
43n/a
44n/a def add_bad_source_file(self):
45n/a self.bad_source_path = os.path.join(self.directory, '_test_bad.py')
46n/a with open(self.bad_source_path, 'w') as file:
47n/a file.write('x (\n')
48n/a
49n/a def data(self):
50n/a with open(self.bc_path, 'rb') as file:
51n/a data = file.read(8)
52n/a mtime = int(os.stat(self.source_path).st_mtime)
53n/a compare = struct.pack('<4sl', importlib.util.MAGIC_NUMBER, mtime)
54n/a return data, compare
55n/a
56n/a @unittest.skipUnless(hasattr(os, 'stat'), 'test needs os.stat()')
57n/a def recreation_check(self, metadata):
58n/a """Check that compileall recreates bytecode when the new metadata is
59n/a used."""
60n/a py_compile.compile(self.source_path)
61n/a self.assertEqual(*self.data())
62n/a with open(self.bc_path, 'rb') as file:
63n/a bc = file.read()[len(metadata):]
64n/a with open(self.bc_path, 'wb') as file:
65n/a file.write(metadata)
66n/a file.write(bc)
67n/a self.assertNotEqual(*self.data())
68n/a compileall.compile_dir(self.directory, force=False, quiet=True)
69n/a self.assertTrue(*self.data())
70n/a
71n/a def test_mtime(self):
72n/a # Test a change in mtime leads to a new .pyc.
73n/a self.recreation_check(struct.pack('<4sl', importlib.util.MAGIC_NUMBER,
74n/a 1))
75n/a
76n/a def test_magic_number(self):
77n/a # Test a change in mtime leads to a new .pyc.
78n/a self.recreation_check(b'\0\0\0\0')
79n/a
80n/a def test_compile_files(self):
81n/a # Test compiling a single file, and complete directory
82n/a for fn in (self.bc_path, self.bc_path2):
83n/a try:
84n/a os.unlink(fn)
85n/a except:
86n/a pass
87n/a self.assertTrue(compileall.compile_file(self.source_path,
88n/a force=False, quiet=True))
89n/a self.assertTrue(os.path.isfile(self.bc_path) and
90n/a not os.path.isfile(self.bc_path2))
91n/a os.unlink(self.bc_path)
92n/a self.assertTrue(compileall.compile_dir(self.directory, force=False,
93n/a quiet=True))
94n/a self.assertTrue(os.path.isfile(self.bc_path) and
95n/a os.path.isfile(self.bc_path2))
96n/a os.unlink(self.bc_path)
97n/a os.unlink(self.bc_path2)
98n/a # Test against bad files
99n/a self.add_bad_source_file()
100n/a self.assertFalse(compileall.compile_file(self.bad_source_path,
101n/a force=False, quiet=2))
102n/a self.assertFalse(compileall.compile_dir(self.directory,
103n/a force=False, quiet=2))
104n/a
105n/a def test_compile_file_pathlike(self):
106n/a self.assertFalse(os.path.isfile(self.bc_path))
107n/a # we should also test the output
108n/a with support.captured_stdout() as stdout:
109n/a self.assertTrue(compileall.compile_file(pathlib.Path(self.source_path)))
110n/a self.assertRegex(stdout.getvalue(), r'Compiling ([^WindowsPath|PosixPath].*)')
111n/a self.assertTrue(os.path.isfile(self.bc_path))
112n/a
113n/a def test_compile_file_pathlike_ddir(self):
114n/a self.assertFalse(os.path.isfile(self.bc_path))
115n/a self.assertTrue(compileall.compile_file(pathlib.Path(self.source_path),
116n/a ddir=pathlib.Path('ddir_path'),
117n/a quiet=2))
118n/a self.assertTrue(os.path.isfile(self.bc_path))
119n/a
120n/a def test_compile_path(self):
121n/a with test.test_importlib.util.import_state(path=[self.directory]):
122n/a self.assertTrue(compileall.compile_path(quiet=2))
123n/a
124n/a with test.test_importlib.util.import_state(path=[self.directory]):
125n/a self.add_bad_source_file()
126n/a self.assertFalse(compileall.compile_path(skip_curdir=False,
127n/a force=True, quiet=2))
128n/a
129n/a def test_no_pycache_in_non_package(self):
130n/a # Bug 8563 reported that __pycache__ directories got created by
131n/a # compile_file() for non-.py files.
132n/a data_dir = os.path.join(self.directory, 'data')
133n/a data_file = os.path.join(data_dir, 'file')
134n/a os.mkdir(data_dir)
135n/a # touch data/file
136n/a with open(data_file, 'w'):
137n/a pass
138n/a compileall.compile_file(data_file)
139n/a self.assertFalse(os.path.exists(os.path.join(data_dir, '__pycache__')))
140n/a
141n/a def test_optimize(self):
142n/a # make sure compiling with different optimization settings than the
143n/a # interpreter's creates the correct file names
144n/a optimize, opt = (1, 1) if __debug__ else (0, '')
145n/a compileall.compile_dir(self.directory, quiet=True, optimize=optimize)
146n/a cached = importlib.util.cache_from_source(self.source_path,
147n/a optimization=opt)
148n/a self.assertTrue(os.path.isfile(cached))
149n/a cached2 = importlib.util.cache_from_source(self.source_path2,
150n/a optimization=opt)
151n/a self.assertTrue(os.path.isfile(cached2))
152n/a cached3 = importlib.util.cache_from_source(self.source_path3,
153n/a optimization=opt)
154n/a self.assertTrue(os.path.isfile(cached3))
155n/a
156n/a def test_compile_dir_pathlike(self):
157n/a self.assertFalse(os.path.isfile(self.bc_path))
158n/a with support.captured_stdout() as stdout:
159n/a compileall.compile_dir(pathlib.Path(self.directory))
160n/a line = stdout.getvalue().splitlines()[0]
161n/a self.assertRegex(line, r'Listing ([^WindowsPath|PosixPath].*)')
162n/a self.assertTrue(os.path.isfile(self.bc_path))
163n/a
164n/a @mock.patch('compileall.ProcessPoolExecutor')
165n/a def test_compile_pool_called(self, pool_mock):
166n/a compileall.compile_dir(self.directory, quiet=True, workers=5)
167n/a self.assertTrue(pool_mock.called)
168n/a
169n/a def test_compile_workers_non_positive(self):
170n/a with self.assertRaisesRegex(ValueError,
171n/a "workers must be greater or equal to 0"):
172n/a compileall.compile_dir(self.directory, workers=-1)
173n/a
174n/a @mock.patch('compileall.ProcessPoolExecutor')
175n/a def test_compile_workers_cpu_count(self, pool_mock):
176n/a compileall.compile_dir(self.directory, quiet=True, workers=0)
177n/a self.assertEqual(pool_mock.call_args[1]['max_workers'], None)
178n/a
179n/a @mock.patch('compileall.ProcessPoolExecutor')
180n/a @mock.patch('compileall.compile_file')
181n/a def test_compile_one_worker(self, compile_file_mock, pool_mock):
182n/a compileall.compile_dir(self.directory, quiet=True)
183n/a self.assertFalse(pool_mock.called)
184n/a self.assertTrue(compile_file_mock.called)
185n/a
186n/a @mock.patch('compileall.ProcessPoolExecutor', new=None)
187n/a @mock.patch('compileall.compile_file')
188n/a def test_compile_missing_multiprocessing(self, compile_file_mock):
189n/a compileall.compile_dir(self.directory, quiet=True, workers=5)
190n/a self.assertTrue(compile_file_mock.called)
191n/a
192n/aclass EncodingTest(unittest.TestCase):
193n/a """Issue 6716: compileall should escape source code when printing errors
194n/a to stdout."""
195n/a
196n/a def setUp(self):
197n/a self.directory = tempfile.mkdtemp()
198n/a self.source_path = os.path.join(self.directory, '_test.py')
199n/a with open(self.source_path, 'w', encoding='utf-8') as file:
200n/a file.write('# -*- coding: utf-8 -*-\n')
201n/a file.write('print u"\u20ac"\n')
202n/a
203n/a def tearDown(self):
204n/a shutil.rmtree(self.directory)
205n/a
206n/a def test_error(self):
207n/a try:
208n/a orig_stdout = sys.stdout
209n/a sys.stdout = io.TextIOWrapper(io.BytesIO(),encoding='ascii')
210n/a compileall.compile_dir(self.directory)
211n/a finally:
212n/a sys.stdout = orig_stdout
213n/a
214n/a
215n/aclass CommandLineTests(unittest.TestCase):
216n/a """Test compileall's CLI."""
217n/a
218n/a @classmethod
219n/a def setUpClass(cls):
220n/a for path in filter(os.path.isdir, sys.path):
221n/a directory_created = False
222n/a directory = pathlib.Path(path) / '__pycache__'
223n/a path = directory / 'test.try'
224n/a try:
225n/a if not directory.is_dir():
226n/a directory.mkdir()
227n/a directory_created = True
228n/a with path.open('w') as file:
229n/a file.write('# for test_compileall')
230n/a except OSError:
231n/a sys_path_writable = False
232n/a break
233n/a finally:
234n/a support.unlink(str(path))
235n/a if directory_created:
236n/a directory.rmdir()
237n/a else:
238n/a sys_path_writable = True
239n/a cls._sys_path_writable = sys_path_writable
240n/a
241n/a def _skip_if_sys_path_not_writable(self):
242n/a if not self._sys_path_writable:
243n/a raise unittest.SkipTest('not all entries on sys.path are writable')
244n/a
245n/a def _get_run_args(self, args):
246n/a return [*support.optim_args_from_interpreter_flags(),
247n/a '-S', '-m', 'compileall',
248n/a *args]
249n/a
250n/a def assertRunOK(self, *args, **env_vars):
251n/a rc, out, err = script_helper.assert_python_ok(
252n/a *self._get_run_args(args), **env_vars)
253n/a self.assertEqual(b'', err)
254n/a return out
255n/a
256n/a def assertRunNotOK(self, *args, **env_vars):
257n/a rc, out, err = script_helper.assert_python_failure(
258n/a *self._get_run_args(args), **env_vars)
259n/a return rc, out, err
260n/a
261n/a def assertCompiled(self, fn):
262n/a path = importlib.util.cache_from_source(fn)
263n/a self.assertTrue(os.path.exists(path))
264n/a
265n/a def assertNotCompiled(self, fn):
266n/a path = importlib.util.cache_from_source(fn)
267n/a self.assertFalse(os.path.exists(path))
268n/a
269n/a def setUp(self):
270n/a self.directory = tempfile.mkdtemp()
271n/a self.addCleanup(support.rmtree, self.directory)
272n/a self.pkgdir = os.path.join(self.directory, 'foo')
273n/a os.mkdir(self.pkgdir)
274n/a self.pkgdir_cachedir = os.path.join(self.pkgdir, '__pycache__')
275n/a # Create the __init__.py and a package module.
276n/a self.initfn = script_helper.make_script(self.pkgdir, '__init__', '')
277n/a self.barfn = script_helper.make_script(self.pkgdir, 'bar', '')
278n/a
279n/a def test_no_args_compiles_path(self):
280n/a # Note that -l is implied for the no args case.
281n/a self._skip_if_sys_path_not_writable()
282n/a bazfn = script_helper.make_script(self.directory, 'baz', '')
283n/a self.assertRunOK(PYTHONPATH=self.directory)
284n/a self.assertCompiled(bazfn)
285n/a self.assertNotCompiled(self.initfn)
286n/a self.assertNotCompiled(self.barfn)
287n/a
288n/a def test_no_args_respects_force_flag(self):
289n/a self._skip_if_sys_path_not_writable()
290n/a bazfn = script_helper.make_script(self.directory, 'baz', '')
291n/a self.assertRunOK(PYTHONPATH=self.directory)
292n/a pycpath = importlib.util.cache_from_source(bazfn)
293n/a # Set atime/mtime backward to avoid file timestamp resolution issues
294n/a os.utime(pycpath, (time.time()-60,)*2)
295n/a mtime = os.stat(pycpath).st_mtime
296n/a # Without force, no recompilation
297n/a self.assertRunOK(PYTHONPATH=self.directory)
298n/a mtime2 = os.stat(pycpath).st_mtime
299n/a self.assertEqual(mtime, mtime2)
300n/a # Now force it.
301n/a self.assertRunOK('-f', PYTHONPATH=self.directory)
302n/a mtime2 = os.stat(pycpath).st_mtime
303n/a self.assertNotEqual(mtime, mtime2)
304n/a
305n/a def test_no_args_respects_quiet_flag(self):
306n/a self._skip_if_sys_path_not_writable()
307n/a script_helper.make_script(self.directory, 'baz', '')
308n/a noisy = self.assertRunOK(PYTHONPATH=self.directory)
309n/a self.assertIn(b'Listing ', noisy)
310n/a quiet = self.assertRunOK('-q', PYTHONPATH=self.directory)
311n/a self.assertNotIn(b'Listing ', quiet)
312n/a
313n/a # Ensure that the default behavior of compileall's CLI is to create
314n/a # PEP 3147/PEP 488 pyc files.
315n/a for name, ext, switch in [
316n/a ('normal', 'pyc', []),
317n/a ('optimize', 'opt-1.pyc', ['-O']),
318n/a ('doubleoptimize', 'opt-2.pyc', ['-OO']),
319n/a ]:
320n/a def f(self, ext=ext, switch=switch):
321n/a script_helper.assert_python_ok(*(switch +
322n/a ['-m', 'compileall', '-q', self.pkgdir]))
323n/a # Verify the __pycache__ directory contents.
324n/a self.assertTrue(os.path.exists(self.pkgdir_cachedir))
325n/a expected = sorted(base.format(sys.implementation.cache_tag, ext)
326n/a for base in ('__init__.{}.{}', 'bar.{}.{}'))
327n/a self.assertEqual(sorted(os.listdir(self.pkgdir_cachedir)), expected)
328n/a # Make sure there are no .pyc files in the source directory.
329n/a self.assertFalse([fn for fn in os.listdir(self.pkgdir)
330n/a if fn.endswith(ext)])
331n/a locals()['test_pep3147_paths_' + name] = f
332n/a
333n/a def test_legacy_paths(self):
334n/a # Ensure that with the proper switch, compileall leaves legacy
335n/a # pyc files, and no __pycache__ directory.
336n/a self.assertRunOK('-b', '-q', self.pkgdir)
337n/a # Verify the __pycache__ directory contents.
338n/a self.assertFalse(os.path.exists(self.pkgdir_cachedir))
339n/a expected = sorted(['__init__.py', '__init__.pyc', 'bar.py',
340n/a 'bar.pyc'])
341n/a self.assertEqual(sorted(os.listdir(self.pkgdir)), expected)
342n/a
343n/a def test_multiple_runs(self):
344n/a # Bug 8527 reported that multiple calls produced empty
345n/a # __pycache__/__pycache__ directories.
346n/a self.assertRunOK('-q', self.pkgdir)
347n/a # Verify the __pycache__ directory contents.
348n/a self.assertTrue(os.path.exists(self.pkgdir_cachedir))
349n/a cachecachedir = os.path.join(self.pkgdir_cachedir, '__pycache__')
350n/a self.assertFalse(os.path.exists(cachecachedir))
351n/a # Call compileall again.
352n/a self.assertRunOK('-q', self.pkgdir)
353n/a self.assertTrue(os.path.exists(self.pkgdir_cachedir))
354n/a self.assertFalse(os.path.exists(cachecachedir))
355n/a
356n/a def test_force(self):
357n/a self.assertRunOK('-q', self.pkgdir)
358n/a pycpath = importlib.util.cache_from_source(self.barfn)
359n/a # set atime/mtime backward to avoid file timestamp resolution issues
360n/a os.utime(pycpath, (time.time()-60,)*2)
361n/a mtime = os.stat(pycpath).st_mtime
362n/a # without force, no recompilation
363n/a self.assertRunOK('-q', self.pkgdir)
364n/a mtime2 = os.stat(pycpath).st_mtime
365n/a self.assertEqual(mtime, mtime2)
366n/a # now force it.
367n/a self.assertRunOK('-q', '-f', self.pkgdir)
368n/a mtime2 = os.stat(pycpath).st_mtime
369n/a self.assertNotEqual(mtime, mtime2)
370n/a
371n/a def test_recursion_control(self):
372n/a subpackage = os.path.join(self.pkgdir, 'spam')
373n/a os.mkdir(subpackage)
374n/a subinitfn = script_helper.make_script(subpackage, '__init__', '')
375n/a hamfn = script_helper.make_script(subpackage, 'ham', '')
376n/a self.assertRunOK('-q', '-l', self.pkgdir)
377n/a self.assertNotCompiled(subinitfn)
378n/a self.assertFalse(os.path.exists(os.path.join(subpackage, '__pycache__')))
379n/a self.assertRunOK('-q', self.pkgdir)
380n/a self.assertCompiled(subinitfn)
381n/a self.assertCompiled(hamfn)
382n/a
383n/a def test_recursion_limit(self):
384n/a subpackage = os.path.join(self.pkgdir, 'spam')
385n/a subpackage2 = os.path.join(subpackage, 'ham')
386n/a subpackage3 = os.path.join(subpackage2, 'eggs')
387n/a for pkg in (subpackage, subpackage2, subpackage3):
388n/a script_helper.make_pkg(pkg)
389n/a
390n/a subinitfn = os.path.join(subpackage, '__init__.py')
391n/a hamfn = script_helper.make_script(subpackage, 'ham', '')
392n/a spamfn = script_helper.make_script(subpackage2, 'spam', '')
393n/a eggfn = script_helper.make_script(subpackage3, 'egg', '')
394n/a
395n/a self.assertRunOK('-q', '-r 0', self.pkgdir)
396n/a self.assertNotCompiled(subinitfn)
397n/a self.assertFalse(
398n/a os.path.exists(os.path.join(subpackage, '__pycache__')))
399n/a
400n/a self.assertRunOK('-q', '-r 1', self.pkgdir)
401n/a self.assertCompiled(subinitfn)
402n/a self.assertCompiled(hamfn)
403n/a self.assertNotCompiled(spamfn)
404n/a
405n/a self.assertRunOK('-q', '-r 2', self.pkgdir)
406n/a self.assertCompiled(subinitfn)
407n/a self.assertCompiled(hamfn)
408n/a self.assertCompiled(spamfn)
409n/a self.assertNotCompiled(eggfn)
410n/a
411n/a self.assertRunOK('-q', '-r 5', self.pkgdir)
412n/a self.assertCompiled(subinitfn)
413n/a self.assertCompiled(hamfn)
414n/a self.assertCompiled(spamfn)
415n/a self.assertCompiled(eggfn)
416n/a
417n/a def test_quiet(self):
418n/a noisy = self.assertRunOK(self.pkgdir)
419n/a quiet = self.assertRunOK('-q', self.pkgdir)
420n/a self.assertNotEqual(b'', noisy)
421n/a self.assertEqual(b'', quiet)
422n/a
423n/a def test_silent(self):
424n/a script_helper.make_script(self.pkgdir, 'crunchyfrog', 'bad(syntax')
425n/a _, quiet, _ = self.assertRunNotOK('-q', self.pkgdir)
426n/a _, silent, _ = self.assertRunNotOK('-qq', self.pkgdir)
427n/a self.assertNotEqual(b'', quiet)
428n/a self.assertEqual(b'', silent)
429n/a
430n/a def test_regexp(self):
431n/a self.assertRunOK('-q', '-x', r'ba[^\\/]*$', self.pkgdir)
432n/a self.assertNotCompiled(self.barfn)
433n/a self.assertCompiled(self.initfn)
434n/a
435n/a def test_multiple_dirs(self):
436n/a pkgdir2 = os.path.join(self.directory, 'foo2')
437n/a os.mkdir(pkgdir2)
438n/a init2fn = script_helper.make_script(pkgdir2, '__init__', '')
439n/a bar2fn = script_helper.make_script(pkgdir2, 'bar2', '')
440n/a self.assertRunOK('-q', self.pkgdir, pkgdir2)
441n/a self.assertCompiled(self.initfn)
442n/a self.assertCompiled(self.barfn)
443n/a self.assertCompiled(init2fn)
444n/a self.assertCompiled(bar2fn)
445n/a
446n/a def test_d_compile_error(self):
447n/a script_helper.make_script(self.pkgdir, 'crunchyfrog', 'bad(syntax')
448n/a rc, out, err = self.assertRunNotOK('-q', '-d', 'dinsdale', self.pkgdir)
449n/a self.assertRegex(out, b'File "dinsdale')
450n/a
451n/a def test_d_runtime_error(self):
452n/a bazfn = script_helper.make_script(self.pkgdir, 'baz', 'raise Exception')
453n/a self.assertRunOK('-q', '-d', 'dinsdale', self.pkgdir)
454n/a fn = script_helper.make_script(self.pkgdir, 'bing', 'import baz')
455n/a pyc = importlib.util.cache_from_source(bazfn)
456n/a os.rename(pyc, os.path.join(self.pkgdir, 'baz.pyc'))
457n/a os.remove(bazfn)
458n/a rc, out, err = script_helper.assert_python_failure(fn, __isolated=False)
459n/a self.assertRegex(err, b'File "dinsdale')
460n/a
461n/a def test_include_bad_file(self):
462n/a rc, out, err = self.assertRunNotOK(
463n/a '-i', os.path.join(self.directory, 'nosuchfile'), self.pkgdir)
464n/a self.assertRegex(out, b'rror.*nosuchfile')
465n/a self.assertNotRegex(err, b'Traceback')
466n/a self.assertFalse(os.path.exists(importlib.util.cache_from_source(
467n/a self.pkgdir_cachedir)))
468n/a
469n/a def test_include_file_with_arg(self):
470n/a f1 = script_helper.make_script(self.pkgdir, 'f1', '')
471n/a f2 = script_helper.make_script(self.pkgdir, 'f2', '')
472n/a f3 = script_helper.make_script(self.pkgdir, 'f3', '')
473n/a f4 = script_helper.make_script(self.pkgdir, 'f4', '')
474n/a with open(os.path.join(self.directory, 'l1'), 'w') as l1:
475n/a l1.write(os.path.join(self.pkgdir, 'f1.py')+os.linesep)
476n/a l1.write(os.path.join(self.pkgdir, 'f2.py')+os.linesep)
477n/a self.assertRunOK('-i', os.path.join(self.directory, 'l1'), f4)
478n/a self.assertCompiled(f1)
479n/a self.assertCompiled(f2)
480n/a self.assertNotCompiled(f3)
481n/a self.assertCompiled(f4)
482n/a
483n/a def test_include_file_no_arg(self):
484n/a f1 = script_helper.make_script(self.pkgdir, 'f1', '')
485n/a f2 = script_helper.make_script(self.pkgdir, 'f2', '')
486n/a f3 = script_helper.make_script(self.pkgdir, 'f3', '')
487n/a f4 = script_helper.make_script(self.pkgdir, 'f4', '')
488n/a with open(os.path.join(self.directory, 'l1'), 'w') as l1:
489n/a l1.write(os.path.join(self.pkgdir, 'f2.py')+os.linesep)
490n/a self.assertRunOK('-i', os.path.join(self.directory, 'l1'))
491n/a self.assertNotCompiled(f1)
492n/a self.assertCompiled(f2)
493n/a self.assertNotCompiled(f3)
494n/a self.assertNotCompiled(f4)
495n/a
496n/a def test_include_on_stdin(self):
497n/a f1 = script_helper.make_script(self.pkgdir, 'f1', '')
498n/a f2 = script_helper.make_script(self.pkgdir, 'f2', '')
499n/a f3 = script_helper.make_script(self.pkgdir, 'f3', '')
500n/a f4 = script_helper.make_script(self.pkgdir, 'f4', '')
501n/a p = script_helper.spawn_python(*(self._get_run_args(()) + ['-i', '-']))
502n/a p.stdin.write((f3+os.linesep).encode('ascii'))
503n/a script_helper.kill_python(p)
504n/a self.assertNotCompiled(f1)
505n/a self.assertNotCompiled(f2)
506n/a self.assertCompiled(f3)
507n/a self.assertNotCompiled(f4)
508n/a
509n/a def test_compiles_as_much_as_possible(self):
510n/a bingfn = script_helper.make_script(self.pkgdir, 'bing', 'syntax(error')
511n/a rc, out, err = self.assertRunNotOK('nosuchfile', self.initfn,
512n/a bingfn, self.barfn)
513n/a self.assertRegex(out, b'rror')
514n/a self.assertNotCompiled(bingfn)
515n/a self.assertCompiled(self.initfn)
516n/a self.assertCompiled(self.barfn)
517n/a
518n/a def test_invalid_arg_produces_message(self):
519n/a out = self.assertRunOK('badfilename')
520n/a self.assertRegex(out, b"Can't list 'badfilename'")
521n/a
522n/a @skipUnless(_have_multiprocessing, "requires multiprocessing")
523n/a def test_workers(self):
524n/a bar2fn = script_helper.make_script(self.directory, 'bar2', '')
525n/a files = []
526n/a for suffix in range(5):
527n/a pkgdir = os.path.join(self.directory, 'foo{}'.format(suffix))
528n/a os.mkdir(pkgdir)
529n/a fn = script_helper.make_script(pkgdir, '__init__', '')
530n/a files.append(script_helper.make_script(pkgdir, 'bar2', ''))
531n/a
532n/a self.assertRunOK(self.directory, '-j', '0')
533n/a self.assertCompiled(bar2fn)
534n/a for file in files:
535n/a self.assertCompiled(file)
536n/a
537n/a @mock.patch('compileall.compile_dir')
538n/a def test_workers_available_cores(self, compile_dir):
539n/a with mock.patch("sys.argv",
540n/a new=[sys.executable, self.directory, "-j0"]):
541n/a compileall.main()
542n/a self.assertTrue(compile_dir.called)
543n/a self.assertEqual(compile_dir.call_args[-1]['workers'], None)
544n/a
545n/a
546n/aif __name__ == "__main__":
547n/a unittest.main()