summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2013-09-28 11:07:41 -0400
committerNed Batchelder <ned@nedbatchelder.com>2013-09-28 11:07:41 -0400
commit1d179a1077819aec18b960a3fa24ea3490c884a5 (patch)
tree82cbfa11032e782578faa1eeaaca3dd6ae9c9b80 /tests
parenta9739749d841818a31ae956fe02e0b3f03a82a31 (diff)
downloadpython-coveragepy-1d179a1077819aec18b960a3fa24ea3490c884a5.tar.gz
Now we can run .pyc files directly. Closes #264.
Diffstat (limited to 'tests')
-rw-r--r--tests/test_execfile.py59
1 files changed, 57 insertions, 2 deletions
diff --git a/tests/test_execfile.py b/tests/test_execfile.py
index 7da2854..24c521b 100644
--- a/tests/test_execfile.py
+++ b/tests/test_execfile.py
@@ -1,9 +1,10 @@
"""Tests for coverage.execfile"""
-import os, sys
+import compileall, os, re, sys
+from coverage.backward import binary_bytes
from coverage.execfile import run_python_file, run_python_module
-from coverage.misc import NoSource
+from coverage.misc import NoCode, NoSource
from tests.coveragetest import CoverageTest
@@ -77,6 +78,60 @@ class RunFileTest(CoverageTest):
self.assertRaises(NoSource, run_python_file, "xyzzy.py", [])
+class RunPycFileTest(CoverageTest):
+ """Test cases for `run_python_file`."""
+
+ def make_pyc(self):
+ """Create a .pyc file, and return the relative path to it."""
+ self.make_file("compiled.py", """\
+ def doit():
+ print("I am here!")
+
+ doit()
+ """)
+ compileall.compile_dir(".", quiet=True)
+ os.remove("compiled.py")
+
+ # Find the .pyc file!
+ for there, _, files in os.walk("."):
+ for f in files:
+ if f.endswith(".pyc"):
+ return os.path.join(there, f)
+
+ def test_running_pyc(self):
+ pycfile = self.make_pyc()
+ run_python_file(pycfile, [pycfile])
+ self.assertEqual(self.stdout(), "I am here!\n")
+
+ def test_running_pyo(self):
+ pycfile = self.make_pyc()
+ pyofile = re.sub(r"[.]pyc$", ".pyo", pycfile)
+ self.assertNotEqual(pycfile, pyofile)
+ os.rename(pycfile, pyofile)
+ run_python_file(pyofile, [pyofile])
+ self.assertEqual(self.stdout(), "I am here!\n")
+
+ def test_running_pyc_from_wrong_python(self):
+ pycfile = self.make_pyc()
+
+ # Jam Python 2.1 magic number into the .pyc file.
+ fpyc = open(pycfile, "r+b")
+ fpyc.seek(0)
+ fpyc.write(binary_bytes([0x2a, 0xeb, 0x0d, 0x0a]))
+ fpyc.close()
+
+ self.assertRaisesRegexp(
+ NoCode, "Bad magic number in .pyc file",
+ run_python_file, pycfile, [pycfile]
+ )
+
+ def test_no_such_pyc_file(self):
+ self.assertRaisesRegexp(
+ NoCode, "No file to run: 'xyzzy.pyc'",
+ run_python_file, "xyzzy.pyc", []
+ )
+
+
class RunModuleTest(CoverageTest):
"""Test run_python_module."""