summaryrefslogtreecommitdiff
path: root/tests/test_plugins.py
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2014-11-27 19:28:02 -0500
committerNed Batchelder <ned@nedbatchelder.com>2014-11-27 19:28:02 -0500
commit82d7b2bec95db01265eb68707e807d89bb0b1677 (patch)
tree1dfd923a8bd2baa69527154713039ce6d85b4848 /tests/test_plugins.py
parent0d8e1f7783171c9c4f666f5b759322c5f27ebeb0 (diff)
downloadpython-coveragepy-git-82d7b2bec95db01265eb68707e807d89bb0b1677.tar.gz
Finished the plugin2 test of a dynamic tracing plugin.
Diffstat (limited to 'tests/test_plugins.py')
-rw-r--r--tests/test_plugins.py126
1 files changed, 70 insertions, 56 deletions
diff --git a/tests/test_plugins.py b/tests/test_plugins.py
index f2658998..b94d0776 100644
--- a/tests/test_plugins.py
+++ b/tests/test_plugins.py
@@ -1,6 +1,6 @@
"""Tests for plugins."""
-import sys
+import os, sys
from nose.plugins.skip import SkipTest
@@ -11,6 +11,9 @@ import coverage.plugin
from tests.coveragetest import CoverageTest
+# Are we running with the C tracer or not?
+C_TRACER = os.getenv('COVERAGE_TEST_TRACER', 'c') == 'c'
+
class FakeConfig(object):
"""A fake config for use in tests."""
@@ -138,62 +141,73 @@ class PluginTest(CoverageTest):
cov.stop()
-class FileTracerTest(CoverageTest):
- """Tests of plugins that implement file_tracer."""
-
- def test_plugin1(self):
- if sys.platform == 'win32':
- raise SkipTest("Plugin stuff is jank on windows.. fixing soon...")
-
- self.make_file("simple.py", """\
- import try_xyz
- a = 1
- b = 2
- """)
- self.make_file("try_xyz.py", """\
- c = 3
- d = 4
- """)
-
- cov = coverage.Coverage()
- cov.config["run:plugins"] = ["tests.plugin1"]
-
- # Import the python file, executing it.
- self.start_import_stop(cov, "simple")
-
- _, statements, missing, _ = cov.analysis("simple.py")
- self.assertEqual(statements, [1,2,3])
- self.assertEqual(missing, [])
- _, statements, _, _ = cov.analysis("/src/try_ABC.zz")
- self.assertEqual(statements, [105, 106, 107, 205, 206, 207])
-
- def test_plugin2(self):
- self.make_file("render.py", """\
- def render(filename, linenum):
- fiddle_around = 1 # vamp until ready
- return "[{0} @ {1}]".format(filename, linenum)
- """)
- self.make_file("caller.py", """\
- from render import render
-
- assert render("foo.html", 17) == "[foo.html @ 17]"
- assert render("bar.html", 23) == "[bar.html @ 23]"
- """)
+if not C_TRACER:
+ class FileTracerTest(CoverageTest):
+ """Tests of plugins that implement file_tracer."""
- cov = coverage.Coverage()
- cov.config["run:plugins"] = ["tests.plugin2"]
- cov.config["run:debug"] = ["trace"]
+ def test_plugin1(self):
+ if sys.platform == 'win32':
+ raise SkipTest("Plugin stuff is jank on windows.. fixing soon...")
- self.start_import_stop(cov, "caller")
+ self.make_file("simple.py", """\
+ import try_xyz
+ a = 1
+ b = 2
+ """)
+ self.make_file("try_xyz.py", """\
+ c = 3
+ d = 4
+ """)
- print(self.stderr())
- cov._harvest_data()
- print(cov.data.line_data())
-
- return # TODO: finish this test
+ cov = coverage.Coverage()
+ cov.config["run:plugins"] = ["tests.plugin1"]
+
+ # Import the python file, executing it.
+ self.start_import_stop(cov, "simple")
+
+ _, statements, missing, _ = cov.analysis("simple.py")
+ self.assertEqual(statements, [1,2,3])
+ self.assertEqual(missing, [])
+ _, statements, _, _ = cov.analysis("/src/try_ABC.zz")
+ self.assertEqual(statements, [105, 106, 107, 205, 206, 207])
+
+ def test_plugin2(self):
+ # plugin2 emulates a dynamic tracing plugin: the caller's locals
+ # are examined to determine the source file and line number.
+ # The plugin is in tests/plugin2.py.
+ self.make_file("render.py", """\
+ def render(filename, linenum):
+ # This function emulates a template renderer. The plugin
+ # will examine the `filename` and `linenum` locals to
+ # determine the source file and line number.
+ fiddle_around = 1 # not used, just chaff.
+ return "[{0} @ {1}]".format(filename, linenum)
+
+ def helper(x):
+ # This function is here just to show that not all code in
+ # this file will be part of the dynamic tracing.
+ return x+1
+ """)
+ self.make_file("caller.py", """\
+ from render import helper, render
+
+ assert render("foo_7.html", 4) == "[foo_7.html @ 4]"
+ assert helper(42) == 43
+ assert render("bar_4.html", 2) == "[bar_4.html @ 2]"
+ assert helper(76) == 77
+ """)
- _, statements, missing, _ = cov.analysis("simple.py")
- self.assertEqual(statements, [1,2,3])
- self.assertEqual(missing, [])
- _, statements, _, _ = cov.analysis("/src/try_ABC.zz")
- self.assertEqual(statements, [105, 106, 107, 205, 206, 207])
+ cov = coverage.Coverage()
+ cov.config["run:plugins"] = ["tests.plugin2"]
+
+ self.start_import_stop(cov, "caller")
+
+ # The way plugin2 works, a file named foo_7.html will be claimed to
+ # have 7 lines in it. If render() was called with line number 4,
+ # then the plugin will claim that lines 4 and 5 were executed.
+ _, statements, missing, _ = cov.analysis("foo_7.html")
+ self.assertEqual(statements, [1,2,3,4,5,6,7])
+ self.assertEqual(missing, [1,2,3,6,7])
+ _, statements, missing, _ = cov.analysis("bar_4.html")
+ self.assertEqual(statements, [1,2,3,4])
+ self.assertEqual(missing, [1,4])