summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2011-02-06 23:02:12 -0500
committerNed Batchelder <ned@nedbatchelder.com>2011-02-06 23:02:12 -0500
commit2afc485b2724f496c67e0660c9c3683c52634515 (patch)
treedf91bb766f3836f428e533a9a6907569793e45b4 /test
parent50bc61cd0b0a66ec6cc497ea6870765d627fb9be (diff)
downloadpython-coveragepy-git-2afc485b2724f496c67e0660c9c3683c52634515.tar.gz
Add tests and doc for Brandon's -m flag.
Diffstat (limited to 'test')
-rw-r--r--test/modules/pkg1/__main__.py3
-rw-r--r--test/modules/pkg1/runmod2.py3
-rw-r--r--test/modules/pkg1/sub/__main__.py3
-rw-r--r--test/modules/pkg1/sub/runmod3.py3
-rw-r--r--test/modules/runmod1.py3
-rw-r--r--test/test_cmdline.py30
-rw-r--r--test/test_execfile.py43
7 files changed, 85 insertions, 3 deletions
diff --git a/test/modules/pkg1/__main__.py b/test/modules/pkg1/__main__.py
new file mode 100644
index 00000000..66ce5956
--- /dev/null
+++ b/test/modules/pkg1/__main__.py
@@ -0,0 +1,3 @@
+# Used in the tests for run_python_module
+import sys
+print("pkg1.__main__: passed %s" % sys.argv[1])
diff --git a/test/modules/pkg1/runmod2.py b/test/modules/pkg1/runmod2.py
new file mode 100644
index 00000000..b52964cb
--- /dev/null
+++ b/test/modules/pkg1/runmod2.py
@@ -0,0 +1,3 @@
+# Used in the tests for run_python_module
+import sys
+print("runmod2: passed %s" % sys.argv[1])
diff --git a/test/modules/pkg1/sub/__main__.py b/test/modules/pkg1/sub/__main__.py
new file mode 100644
index 00000000..b5be9f1c
--- /dev/null
+++ b/test/modules/pkg1/sub/__main__.py
@@ -0,0 +1,3 @@
+# Used in the tests for run_python_module
+import sys
+print("pkg1.sub.__main__: passed %s" % sys.argv[1])
diff --git a/test/modules/pkg1/sub/runmod3.py b/test/modules/pkg1/sub/runmod3.py
new file mode 100644
index 00000000..3a1ad155
--- /dev/null
+++ b/test/modules/pkg1/sub/runmod3.py
@@ -0,0 +1,3 @@
+# Used in the tests for run_python_module
+import sys
+print("runmod3: passed %s" % sys.argv[1])
diff --git a/test/modules/runmod1.py b/test/modules/runmod1.py
new file mode 100644
index 00000000..671d81ef
--- /dev/null
+++ b/test/modules/runmod1.py
@@ -0,0 +1,3 @@
+# Used in the tests for run_python_module
+import sys
+print("runmod1: passed %s" % sys.argv[1])
diff --git a/test/test_cmdline.py b/test/test_cmdline.py
index a9858d51..d4cc763d 100644
--- a/test/test_cmdline.py
+++ b/test/test_cmdline.py
@@ -33,7 +33,8 @@ class CmdLineTest(CoverageTest):
"""
m = self.model_object()
ret = coverage.CoverageScript(
- _covpkg=m, _run_python_file=m.run_python_file, _help_fn=m.help_fn
+ _covpkg=m, _run_python_file=m.run_python_file,
+ _run_python_module=m.run_python_module, _help_fn=m.help_fn
).command_line(shlex.split(args))
return m, ret
@@ -521,6 +522,33 @@ class NewCmdLineTest(CmdLineTest):
.save()
""")
+ def test_run_module(self):
+ self.cmd_executes("run -m mymodule", """\
+ .coverage(cover_pylib=None, data_suffix=None, timid=None, branch=None, config_file=True, source=None, include=None, omit=None)
+ .erase()
+ .start()
+ .run_python_module('mymodule', ['mymodule'])
+ .stop()
+ .save()
+ """)
+ self.cmd_executes("run -m mymodule -qq arg1 arg2", """\
+ .coverage(cover_pylib=None, data_suffix=None, timid=None, branch=None, config_file=True, source=None, include=None, omit=None)
+ .erase()
+ .start()
+ .run_python_module('mymodule', ['mymodule', '-qq', 'arg1', 'arg2'])
+ .stop()
+ .save()
+ """)
+ self.cmd_executes("run --branch -m mymodule", """\
+ .coverage(cover_pylib=None, data_suffix=None, timid=None, branch=True, config_file=True, source=None, include=None, omit=None)
+ .erase()
+ .start()
+ .run_python_module('mymodule', ['mymodule'])
+ .stop()
+ .save()
+ """)
+ self.cmd_executes_same("run -m mymodule", "run --module mymodule")
+
def test_xml(self):
# coverage xml [-i] [--omit DIR,...] [FILE1 FILE2 ...]
self.cmd_executes("xml", self.INIT_LOAD + """\
diff --git a/test/test_execfile.py b/test/test_execfile.py
index f6e4dd7f..1c5b8024 100644
--- a/test/test_execfile.py
+++ b/test/test_execfile.py
@@ -2,7 +2,7 @@
import os, sys
-from coverage.execfile import run_python_file
+from coverage.execfile import run_python_file, run_python_module
from coverage.misc import NoSource
sys.path.insert(0, os.path.split(__file__)[0]) # Force relative import for Py3k
@@ -10,7 +10,7 @@ from coveragetest import CoverageTest
here = os.path.dirname(__file__)
-class RunTest(CoverageTest):
+class RunFileTest(CoverageTest):
"""Test cases for `run_python_file`."""
def test_run_python_file(self):
@@ -76,3 +76,42 @@ class RunTest(CoverageTest):
def test_no_such_file(self):
self.assertRaises(NoSource, run_python_file, "xyzzy.py", [])
+
+
+class RunModuleTest(CoverageTest):
+ """Test run_python_module."""
+
+ run_in_temp_dir = False
+
+ def setUp(self):
+ super(RunModuleTest, self).setUp()
+ # Parent class saves and restores sys.path, we can just modify it.
+ sys.path.append(self.nice_file(os.path.dirname(__file__), 'modules'))
+
+ def test_runmod1(self):
+ run_python_module("runmod1", ["runmod1", "hello"])
+ self.assertEqual(self.stdout(), "runmod1: passed hello\n")
+
+ def test_runmod2(self):
+ run_python_module("pkg1.runmod2", ["runmod2", "hello"])
+ self.assertEqual(self.stdout(), "runmod2: passed hello\n")
+
+ def test_runmod3(self):
+ run_python_module("pkg1.sub.runmod3", ["runmod3", "hello"])
+ self.assertEqual(self.stdout(), "runmod3: passed hello\n")
+
+ def test_pkg1_main(self):
+ run_python_module("pkg1", ["pkg1", "hello"])
+ self.assertEqual(self.stdout(), "pkg1.__main__: passed hello\n")
+
+ def test_pkg1_sub_main(self):
+ run_python_module("pkg1.sub", ["pkg1.sub", "hello"])
+ self.assertEqual(self.stdout(), "pkg1.sub.__main__: passed hello\n")
+
+ def test_no_such_module(self):
+ self.assertRaises(NoSource, run_python_module, "i_dont_exist", [])
+ self.assertRaises(NoSource, run_python_module, "i.dont_exist", [])
+ self.assertRaises(NoSource, run_python_module, "i.dont.exist", [])
+
+ def test_no_main(self):
+ self.assertRaises(NoSource, run_python_module, "pkg2", ["pkg2", "hi"])