diff options
| author | Ned Batchelder <ned@nedbatchelder.com> | 2014-10-20 18:37:46 -0400 |
|---|---|---|
| committer | Ned Batchelder <ned@nedbatchelder.com> | 2014-10-20 18:37:46 -0400 |
| commit | 17c94a9f94916ba892f7ef0518881776d6b55d66 (patch) | |
| tree | 51a7eda6cf8d9e61adcb3ca791f9917065125085 /tests | |
| parent | ad4c7f3a5194f6966454d534f02e6b02633fa370 (diff) | |
| parent | cd015c45c278aca757263746ed2e64c46d578ddd (diff) | |
| download | python-coveragepy-git-17c94a9f94916ba892f7ef0518881776d6b55d66.tar.gz | |
Merged pull request #18 manually
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/coveragetest.py | 14 | ||||
| -rw-r--r-- | tests/farm/run/run_timid.py | 20 | ||||
| -rw-r--r-- | tests/test_cmdline.py | 503 | ||||
| -rw-r--r-- | tests/test_collector.py | 5 | ||||
| -rw-r--r-- | tests/test_concurrency.py (renamed from tests/test_coroutine.py) | 34 | ||||
| -rw-r--r-- | tests/test_config.py | 43 | ||||
| -rw-r--r-- | tests/test_debug.py | 3 | ||||
| -rw-r--r-- | tests/test_execfile.py | 4 | ||||
| -rw-r--r-- | tests/test_farm.py | 7 | ||||
| -rw-r--r-- | tests/test_oddball.py | 2 | ||||
| -rw-r--r-- | tests/test_plugins.py | 57 | ||||
| -rw-r--r-- | tests/test_process.py | 2 | ||||
| -rw-r--r-- | tests/test_templite.py | 6 | ||||
| -rw-r--r-- | tests/test_testing.py | 4 | ||||
| -rw-r--r-- | tests/try_execfile.py | 7 |
15 files changed, 313 insertions, 398 deletions
diff --git a/tests/coveragetest.py b/tests/coveragetest.py index 4053059f..a680ed46 100644 --- a/tests/coveragetest.py +++ b/tests/coveragetest.py @@ -8,8 +8,7 @@ from coverage.backward import StringIO, import_local_file from coverage.backward import importlib # pylint: disable=unused-import from coverage.control import _TEST_NAME_FILE from coverage.test_helpers import ( - ModuleAwareMixin, SysPathAwareMixin, EnvironmentAwareMixin, - StdStreamCapturingMixin, TempDirMixin, + EnvironmentAwareMixin, StdStreamCapturingMixin, TempDirMixin, ) from tests.backtest import run_command @@ -19,8 +18,6 @@ from tests.backtest import run_command OK, ERR = 0, 1 class CoverageTest( - ModuleAwareMixin, - SysPathAwareMixin, EnvironmentAwareMixin, StdStreamCapturingMixin, TempDirMixin, @@ -37,10 +34,11 @@ class CoverageTest( def setUp(self): super(CoverageTest, self).setUp() - if _TEST_NAME_FILE: - f = open(_TEST_NAME_FILE, "w") - f.write("%s_%s" % (self.__class__.__name__, self._testMethodName)) - f.close() + if _TEST_NAME_FILE: # pragma: debugging + with open(_TEST_NAME_FILE, "w") as f: + f.write("%s_%s" % ( + self.__class__.__name__, self._testMethodName, + )) def clean_local_file_imports(self): """Clean up the results of calls to `import_local_file`. diff --git a/tests/farm/run/run_timid.py b/tests/farm/run/run_timid.py index d4e69a46..99155b85 100644 --- a/tests/farm/run/run_timid.py +++ b/tests/farm/run/run_timid.py @@ -37,24 +37,4 @@ else: # also show the Python function. contains("out/showtraceout.txt", "regular PyTracer") -# Try the environment variable. -old_opts = os.environ.get('COVERAGE_OPTIONS') -os.environ['COVERAGE_OPTIONS'] = '--timid' - -run(""" - coverage run showtrace.py regular - coverage run --timid showtrace.py timid - """, rundir="out", outfile="showtraceout.txt") - -contains("out/showtraceout.txt", - "none None", - "timid PyTracer", - "regular PyTracer", - ) - -if old_opts: - os.environ['COVERAGE_OPTIONS'] = old_opts -else: - del os.environ['COVERAGE_OPTIONS'] - clean("out") diff --git a/tests/test_cmdline.py b/tests/test_cmdline.py index 08f7937a..695c3bec 100644 --- a/tests/test_cmdline.py +++ b/tests/test_cmdline.py @@ -9,7 +9,7 @@ from coverage.misc import ExceptionDuringRun from tests.coveragetest import CoverageTest, OK, ERR -class CmdLineTest(CoverageTest): +class BaseCmdLineTest(CoverageTest): """Tests of execution paths through the command line interpreter.""" run_in_temp_dir = False @@ -20,7 +20,7 @@ class CmdLineTest(CoverageTest): defaults.coverage( cover_pylib=None, data_suffix=None, timid=None, branch=None, config_file=True, source=None, include=None, omit=None, debug=None, - coroutine=None, + concurrency=None, ) defaults.annotate( directory=None, ignore_errors=None, include=None, omit=None, morfs=[], @@ -122,326 +122,92 @@ class CmdLineTest(CoverageTest): ) -class CmdLineTestTest(CmdLineTest): - """Tests that our CmdLineTest helpers work.""" +class BaseCmdLineTestTest(BaseCmdLineTest): + """Tests that our BaseCmdLineTest helpers work.""" def test_assert_same_method_calls(self): # All the other tests here use self.cmd_executes_same in successful # ways, so here we just check that it fails. with self.assertRaises(AssertionError): - self.cmd_executes_same("-e", "-c") + self.cmd_executes_same("run", "debug") -class ClassicCmdLineTest(CmdLineTest): - """Tests of the classic coverage.py command line.""" +class FakeCoverageForDebugData(object): + """Just enough of a fake coverage package for the 'debug data' tests.""" + def __init__(self, summary): + self._summary = summary + self.filename = "FILENAME" + self.data = self - def test_erase(self): - # coverage -e - self.cmd_executes("-e", """\ - .coverage() - .erase() - """) - self.cmd_executes_same("-e", "--erase") + # package members + def coverage(self, *unused_args, **unused_kwargs): + """The coverage class in the package.""" + return self - def test_execute(self): - # coverage -x [-p] [-L] [--timid] MODULE.py [ARG1 ARG2 ...] + # coverage methods + def load(self): + """Fake coverage().load()""" + pass - # -x calls coverage.load first. - self.cmd_executes("-x foo.py", """\ - .coverage() - .load() - .start() - .run_python_file('foo.py', ['foo.py']) - .stop() - .save() - """) - # -e -x calls coverage.erase first. - self.cmd_executes("-e -x foo.py", """\ - .coverage() - .erase() - .start() - .run_python_file('foo.py', ['foo.py']) - .stop() - .save() - """) - # --timid sets a flag, and program arguments get passed through. - self.cmd_executes("-x --timid foo.py abc 123", """\ - .coverage(timid=True) - .load() - .start() - .run_python_file('foo.py', ['foo.py', 'abc', '123']) - .stop() - .save() - """) - # -L sets a flag, and flags for the program don't confuse us. - self.cmd_executes("-x -p -L foo.py -a -b", """\ - .coverage(cover_pylib=True, data_suffix=True) - .load() - .start() - .run_python_file('foo.py', ['foo.py', '-a', '-b']) - .stop() - .save() - """) + # data methods + def has_arcs(self): + """Fake coverage().data.has_arcs()""" + return False - # Check that long forms of flags do the same thing as short forms. - self.cmd_executes_same("-x f.py", "--execute f.py") - self.cmd_executes_same("-e -x f.py", "--erase --execute f.py") - self.cmd_executes_same("-x -p f.py", "-x --parallel-mode f.py") - self.cmd_executes_same("-x -L f.py", "-x --pylib f.py") + def summary(self, fullpath): # pylint: disable=W0613 + """Fake coverage().data.summary()""" + return self._summary - def test_combine(self): - # coverage -c - self.cmd_executes("-c", """\ - .coverage() - .load() - .combine() - .save() - """) - self.cmd_executes_same("-c", "--combine") - def test_report(self): - # coverage -r [-m] [-i] [-o DIR,...] [FILE1 FILE2 ...] - self.cmd_executes("-r", """\ - .coverage() - .load() - .report(show_missing=None) - """) - self.cmd_executes("-r -i", """\ - .coverage() - .load() - .report(ignore_errors=True) - """) - self.cmd_executes("-r -m", """\ - .coverage() - .load() - .report(show_missing=True) - """) - self.cmd_executes("-r -o fooey", """\ - .coverage(omit=["fooey"]) - .load() - .report(omit=["fooey"]) - """) - self.cmd_executes("-r -o fooey,booey", """\ - .coverage(omit=["fooey", "booey"]) - .load() - .report(omit=["fooey", "booey"]) - """) - self.cmd_executes("-r mod1", """\ - .coverage() - .load() - .report(morfs=["mod1"]) - """) - self.cmd_executes("-r mod1 mod2 mod3", """\ - .coverage() - .load() - .report(morfs=["mod1", "mod2", "mod3"]) - """) - - self.cmd_executes_same("-r", "--report") - self.cmd_executes_same("-r -i", "-r --ignore-errors") - self.cmd_executes_same("-r -m", "-r --show-missing") - self.cmd_executes_same("-r -o f", "-r --omit=f") - self.cmd_executes_same("-r -o f", "-r --omit f") - self.cmd_executes_same("-r -o f,b", "-r --omit=f,b") - self.cmd_executes_same("-r -o f,b", "-r --omit f,b") - self.cmd_executes_same("-r -of", "-r --omit=f") - self.cmd_executes_same("-r -of,b", "-r --omit=f,b") +class CmdLineTest(BaseCmdLineTest): + """Tests of the coverage.py command line.""" def test_annotate(self): - # coverage -a [-d DIR] [-i] [-o DIR,...] [FILE1 FILE2 ...] - self.cmd_executes("-a", """\ + # coverage annotate [-d DIR] [-i] [--omit DIR,...] [FILE1 FILE2 ...] + self.cmd_executes("annotate", """\ .coverage() .load() .annotate() """) - self.cmd_executes("-a -d dir1", """\ + self.cmd_executes("annotate -d dir1", """\ .coverage() .load() .annotate(directory="dir1") """) - self.cmd_executes("-a -i", """\ + self.cmd_executes("annotate -i", """\ .coverage() .load() .annotate(ignore_errors=True) """) - self.cmd_executes("-a -o fooey", """\ + self.cmd_executes("annotate --omit fooey", """\ .coverage(omit=["fooey"]) .load() .annotate(omit=["fooey"]) """) - self.cmd_executes("-a -o fooey,booey", """\ + self.cmd_executes("annotate --omit fooey,booey", """\ .coverage(omit=["fooey", "booey"]) .load() .annotate(omit=["fooey", "booey"]) """) - self.cmd_executes("-a mod1", """\ + self.cmd_executes("annotate mod1", """\ .coverage() .load() .annotate(morfs=["mod1"]) """) - self.cmd_executes("-a mod1 mod2 mod3", """\ + self.cmd_executes("annotate mod1 mod2 mod3", """\ .coverage() .load() .annotate(morfs=["mod1", "mod2", "mod3"]) """) - self.cmd_executes_same("-a", "--annotate") - self.cmd_executes_same("-a -d d1", "-a --directory=d1") - self.cmd_executes_same("-a -i", "-a --ignore-errors") - self.cmd_executes_same("-a -o f", "-a --omit=f") - self.cmd_executes_same("-a -o f", "-a --omit f") - self.cmd_executes_same("-a -o f,b", "-a --omit=f,b") - self.cmd_executes_same("-a -o f,b", "-a --omit f,b") - self.cmd_executes_same("-a -of", "-a --omit=f") - self.cmd_executes_same("-a -of,b", "-a --omit=f,b") - - def test_html_report(self): - # coverage -b -d DIR [-i] [-o DIR,...] [FILE1 FILE2 ...] - self.cmd_executes("-b", """\ - .coverage() - .load() - .html_report() - """) - self.cmd_executes("-b -d dir1", """\ - .coverage() - .load() - .html_report(directory="dir1") - """) - self.cmd_executes("-b -i", """\ - .coverage() - .load() - .html_report(ignore_errors=True) - """) - self.cmd_executes("-b -o fooey", """\ - .coverage(omit=["fooey"]) - .load() - .html_report(omit=["fooey"]) - """) - self.cmd_executes("-b -o fooey,booey", """\ - .coverage(omit=["fooey", "booey"]) - .load() - .html_report(omit=["fooey", "booey"]) - """) - self.cmd_executes("-b mod1", """\ - .coverage() - .load() - .html_report(morfs=["mod1"]) - """) - self.cmd_executes("-b mod1 mod2 mod3", """\ + def test_combine(self): + # coverage combine + self.cmd_executes("combine", """\ .coverage() .load() - .html_report(morfs=["mod1", "mod2", "mod3"]) + .combine() + .save() """) - self.cmd_executes_same("-b", "--html") - self.cmd_executes_same("-b -d d1", "-b --directory=d1") - self.cmd_executes_same("-b -i", "-b --ignore-errors") - self.cmd_executes_same("-b -o f", "-b --omit=f") - self.cmd_executes_same("-b -o f,b", "-b --omit=f,b") - self.cmd_executes_same("-b -of", "-b --omit=f") - self.cmd_executes_same("-b -of,b", "-b --omit=f,b") - - def test_help(self): - # coverage -h - self.cmd_help("-h", topic="help", ret=OK) - self.cmd_help("--help", topic="help", ret=OK) - - def test_version(self): - # coverage --version - self.cmd_help("--version", topic="version", ret=OK) - - ## Error cases - - def test_argless_actions(self): - self.cmd_help("-e foo bar", "Unexpected arguments: foo bar") - self.cmd_help("-c baz quux", "Unexpected arguments: baz quux") - - def test_need_action(self): - self.cmd_help("-p", "You must specify at least one of " - "-e, -x, -c, -r, -a, or -b.") - - def test_bad_action_combinations(self): - self.cmd_help('-e -a', - "You can't specify the 'erase' and 'annotate' " - "options at the same time." - ) - self.cmd_help('-e -r', - "You can't specify the 'erase' and 'report' " - "options at the same time." - ) - self.cmd_help('-e -b', - "You can't specify the 'erase' and 'html' " - "options at the same time." - ) - self.cmd_help('-e -c', - "You can't specify the 'erase' and 'combine' " - "options at the same time." - ) - self.cmd_help('-x -a', - "You can't specify the 'execute' and 'annotate' " - "options at the same time." - ) - self.cmd_help('-x -r', - "You can't specify the 'execute' and 'report' " - "options at the same time." - ) - self.cmd_help('-x -b', - "You can't specify the 'execute' and 'html' " - "options at the same time." - ) - self.cmd_help('-x -c', - "You can't specify the 'execute' and 'combine' " - "options at the same time." - ) - - def test_nothing_to_do(self): - self.cmd_help("-x", "Nothing to do.") - - def test_unknown_option(self): - self.cmd_help("-z", "no such option: -z") - - -class FakeCoverageForDebugData(object): - """Just enough of a fake coverage package for the 'debug data' tests.""" - def __init__(self, summary): - self._summary = summary - self.filename = "FILENAME" - self.data = self - - # package members - def coverage(self, *unused_args, **unused_kwargs): - """The coverage class in the package.""" - return self - - # coverage methods - def load(self): - """Fake coverage().load()""" - pass - - # data methods - def has_arcs(self): - """Fake coverage().data.has_arcs()""" - return False - - def summary(self, fullpath): # pylint: disable=W0613 - """Fake coverage().data.summary()""" - return self._summary - - -class NewCmdLineTest(CmdLineTest): - """Tests of the coverage.py command line.""" - - def test_annotate(self): - self.cmd_executes_same("annotate", "-a") - self.cmd_executes_same("annotate -i", "-a -i") - self.cmd_executes_same("annotate -d d1", "-a -d d1") - self.cmd_executes_same("annotate --omit f", "-a --omit f") - self.cmd_executes_same("annotate --omit f,b", "-a --omit f,b") - self.cmd_executes_same("annotate m1", "-a m1") - self.cmd_executes_same("annotate m1 m2 m3", "-a m1 m2 m3") - - def test_combine(self): - self.cmd_executes_same("combine", "-c") - def test_debug(self): self.cmd_help("debug", "What information would you like: data, sys?") self.cmd_help("debug foo", "Don't know what you mean by 'foo'") @@ -474,13 +240,26 @@ class NewCmdLineTest(CmdLineTest): def test_debug_sys(self): self.command_line("debug sys") out = self.stdout() - assert "version:" in out - assert "data_path:" in out + self.assertIn("version:", out) + self.assertIn("data_path:", out) def test_erase(self): - self.cmd_executes_same("erase", "-e") + # coverage erase + self.cmd_executes("erase", """\ + .coverage() + .erase() + """) - def test_help(self): + def test_version(self): + # coverage --version + self.cmd_help("--version", topic="version", ret=OK) + + def test_help_option(self): + # coverage -h + self.cmd_help("-h", topic="help", ret=OK) + self.cmd_help("--help", topic="help", ret=OK) + + def test_help_command(self): self.cmd_executes("help", ".help_fn(topic='help')") def test_cmd_help(self): @@ -489,18 +268,42 @@ class NewCmdLineTest(CmdLineTest): self.cmd_executes_same("help run", "run --help") def test_html(self): - self.cmd_executes_same("html", "-b") - self.cmd_executes_same("html -i", "-b -i") - self.cmd_executes_same("html -d d1", "-b -d d1") - self.cmd_executes_same("html --omit f", "-b --omit f") - self.cmd_executes_same("html --omit f,b", "-b --omit f,b") - self.cmd_executes_same("html m1", "-b m1") - self.cmd_executes_same("html m1 m2 m3", "-b m1 m2 m3") + # coverage html -d DIR [-i] [--omit DIR,...] [FILE1 FILE2 ...] self.cmd_executes("html", """\ .coverage() .load() .html_report() """) + self.cmd_executes("html -d dir1", """\ + .coverage() + .load() + .html_report(directory="dir1") + """) + self.cmd_executes("html -i", """\ + .coverage() + .load() + .html_report(ignore_errors=True) + """) + self.cmd_executes("html --omit fooey", """\ + .coverage(omit=["fooey"]) + .load() + .html_report(omit=["fooey"]) + """) + self.cmd_executes("html --omit fooey,booey", """\ + .coverage(omit=["fooey", "booey"]) + .load() + .html_report(omit=["fooey", "booey"]) + """) + self.cmd_executes("html mod1", """\ + .coverage() + .load() + .html_report(morfs=["mod1"]) + """) + self.cmd_executes("html mod1 mod2 mod3", """\ + .coverage() + .load() + .html_report(morfs=["mod1", "mod2", "mod3"]) + """) self.cmd_executes("html --title=Hello_there", """\ .coverage() .load() @@ -508,22 +311,82 @@ class NewCmdLineTest(CmdLineTest): """) def test_report(self): - self.cmd_executes_same("report", "-r") - self.cmd_executes_same("report -i", "-r -i") - self.cmd_executes_same("report -m", "-r -m") - self.cmd_executes_same("report --omit f", "-r --omit f") - self.cmd_executes_same("report --omit f,b", "-r --omit f,b") - self.cmd_executes_same("report m1", "-r m1") - self.cmd_executes_same("report m1 m2 m3", "-r m1 m2 m3") + # coverage report [-m] [-i] [-o DIR,...] [FILE1 FILE2 ...] + self.cmd_executes("report", """\ + .coverage() + .load() + .report(show_missing=None) + """) + self.cmd_executes("report -i", """\ + .coverage() + .load() + .report(ignore_errors=True) + """) + self.cmd_executes("report -m", """\ + .coverage() + .load() + .report(show_missing=True) + """) + self.cmd_executes("report --omit fooey", """\ + .coverage(omit=["fooey"]) + .load() + .report(omit=["fooey"]) + """) + self.cmd_executes("report --omit fooey,booey", """\ + .coverage(omit=["fooey", "booey"]) + .load() + .report(omit=["fooey", "booey"]) + """) + self.cmd_executes("report mod1", """\ + .coverage() + .load() + .report(morfs=["mod1"]) + """) + self.cmd_executes("report mod1 mod2 mod3", """\ + .coverage() + .load() + .report(morfs=["mod1", "mod2", "mod3"]) + """) def test_run(self): - self.cmd_executes_same("run f.py", "-e -x f.py") - self.cmd_executes_same("run f.py -a arg -z", "-e -x f.py -a arg -z") - self.cmd_executes_same("run -a f.py", "-x f.py") - self.cmd_executes_same("run -p f.py", "-e -x -p f.py") - self.cmd_executes_same("run -L f.py", "-e -x -L f.py") - self.cmd_executes_same("run --timid f.py", "-e -x --timid f.py") - self.cmd_executes_same("run", "-x") + # coverage run [-p] [-L] [--timid] MODULE.py [ARG1 ARG2 ...] + + # run calls coverage.erase first. + self.cmd_executes("run foo.py", """\ + .coverage() + .erase() + .start() + .run_python_file('foo.py', ['foo.py']) + .stop() + .save() + """) + # run -a calls coverage.load first without erasing. + self.cmd_executes("run -a foo.py", """\ + .coverage() + .load() + .start() + .run_python_file('foo.py', ['foo.py']) + .stop() + .save() + """) + # --timid sets a flag, and program arguments get passed through. + self.cmd_executes("run --timid foo.py abc 123", """\ + .coverage(timid=True) + .erase() + .start() + .run_python_file('foo.py', ['foo.py', 'abc', '123']) + .stop() + .save() + """) + # -L sets a flag, and flags for the program don't confuse us. + self.cmd_executes("run -p -L foo.py -a -b", """\ + .coverage(cover_pylib=True, data_suffix=True) + .erase() + .start() + .run_python_file('foo.py', ['foo.py', '-a', '-b']) + .stop() + .save() + """) self.cmd_executes("run --branch foo.py", """\ .coverage(branch=True) .erase() @@ -565,8 +428,7 @@ class NewCmdLineTest(CmdLineTest): .stop() .save() """) - self.cmd_executes("run --source=quux,hi.there,/home/bar foo.py", - """\ + self.cmd_executes("run --source=quux,hi.there,/home/bar foo.py", """\ .coverage(source=["quux", "hi.there", "/home/bar"]) .erase() .start() @@ -574,6 +436,19 @@ class NewCmdLineTest(CmdLineTest): .stop() .save() """) + self.cmd_executes("run --concurrency=gevent foo.py", """\ + .coverage(concurrency='gevent') + .erase() + .start() + .run_python_file('foo.py', ['foo.py']) + .stop() + .save() + """) + + def test_bad_concurrency(self): + self.command_line("run --concurrency=nothing", ret=ERR) + out = self.stdout() + self.assertIn("option --concurrency: invalid choice: 'nothing'", out) def test_run_debug(self): self.cmd_executes("run --debug=opt1 foo.py", """\ @@ -670,39 +545,39 @@ class NewCmdLineTest(CmdLineTest): self.cmd_help("xyzzy", "Unknown command: 'xyzzy'") -class CmdLineStdoutTest(CmdLineTest): +class CmdLineStdoutTest(BaseCmdLineTest): """Test the command line with real stdout output.""" def test_minimum_help(self): self.command_line("") out = self.stdout() - assert "Code coverage for Python." in out - assert out.count("\n") < 4 + self.assertIn("Code coverage for Python.", out) + self.assertLess(out.count("\n"), 4) def test_version(self): self.command_line("--version") out = self.stdout() - assert "ersion " in out - assert out.count("\n") < 4 + self.assertIn("ersion ", out) + self.assertLess(out.count("\n"), 4) def test_help(self): self.command_line("help") out = self.stdout() - assert "nedbatchelder.com" in out - assert out.count("\n") > 10 + self.assertIn("nedbatchelder.com", out) + self.assertGreater(out.count("\n"), 10) def test_cmd_help(self): self.command_line("help run") out = self.stdout() - assert "<pyfile>" in out - assert "--timid" in out - assert out.count("\n") > 10 + self.assertIn("<pyfile>", out) + self.assertIn("--timid", out) + self.assertGreater(out.count("\n"), 10) def test_error(self): self.command_line("fooey kablooey", ret=ERR) out = self.stdout() - assert "fooey" in out - assert "help" in out + self.assertIn("fooey", out) + self.assertIn("help", out) class CmdMainTest(CoverageTest): diff --git a/tests/test_collector.py b/tests/test_collector.py index 7bd4bebb..5f931385 100644 --- a/tests/test_collector.py +++ b/tests/test_collector.py @@ -37,9 +37,8 @@ class CollectorTest(CoverageTest): # Trace one file, but not the other, and get the debug output. debug_out = StringIO() - cov = coverage.coverage( - include=["f1.py"], debug=['trace'], debug_file=debug_out - ) + cov = coverage.coverage(include=["f1.py"], debug=['trace']) + cov._debug_file = debug_out # Import the python file, executing it. self.start_import_stop(cov, "f2") diff --git a/tests/test_coroutine.py b/tests/test_concurrency.py index 4abdd6f6..5ea756f6 100644 --- a/tests/test_coroutine.py +++ b/tests/test_concurrency.py @@ -1,4 +1,4 @@ -"""Tests for coroutining.""" +"""Tests for concurrency libraries.""" import os, os.path, sys, threading @@ -36,8 +36,8 @@ def line_count(s): return sum(1 for l in s.splitlines() if code_line(l)) -class CoroutineTest(CoverageTest): - """Tests of the coroutine support in coverage.py.""" +class ConcurrencyTest(CoverageTest): + """Tests of the concurrency support in coverage.py.""" LIMIT = 1000 @@ -103,7 +103,7 @@ class CoroutineTest(CoverageTest): import gevent.queue as queue """ + COMMON - # Uncomplicated code that doesn't use any of the coroutining stuff, to test + # Uncomplicated code that doesn't use any of the concurrency stuff, to test # the simple case under each of the regimes. SIMPLE = """\ total = 0 @@ -112,32 +112,32 @@ class CoroutineTest(CoverageTest): print(total) """.format(LIMIT=LIMIT) - def try_some_code(self, code, coroutine, the_module, expected_out=None): - """Run some coroutine testing code and see that it was all covered. + def try_some_code(self, code, concurrency, the_module, expected_out=None): + """Run some concurrency testing code and see that it was all covered. - `code` is the Python code to execute. `coroutine` is the name of the - coroutine regime to test it under. `the_module` is the imported module - that must be available for this to work at all. `expected_out` is the - text we expect the code to produce. + `code` is the Python code to execute. `concurrency` is the name of + the concurrency regime to test it under. `the_module` is the imported + module that must be available for this to work at all. `expected_out` + is the text we expect the code to produce. """ self.make_file("try_it.py", code) - cmd = "coverage run --coroutine=%s try_it.py" % coroutine + cmd = "coverage run --concurrency=%s try_it.py" % concurrency out = self.run_command(cmd) if not the_module: # We don't even have the underlying module installed, we expect # coverage to alert us to this fact. expected_out = ( - "Couldn't trace with coroutine=%s, " - "the module isn't installed.\n" % coroutine + "Couldn't trace with concurrency=%s, " + "the module isn't installed.\n" % concurrency ) self.assertEqual(out, expected_out) - elif C_TRACER or coroutine == "thread": + elif C_TRACER or concurrency == "thread": # We can fully measure the code if we are using the C tracer, which - # can support all the coroutining, or if we are using threads. + # can support all the concurrency, or if we are using threads. if expected_out is None: expected_out = "%d\n" % (sum(range(self.LIMIT))) self.assertEqual(out, expected_out) @@ -157,8 +157,8 @@ class CoroutineTest(CoverageTest): self.assertEqual(data.summary()['try_it.py'], lines) else: expected_out = ( - "Can't support coroutine=%s with PyTracer, " - "only threads are supported\n" % coroutine + "Can't support concurrency=%s with PyTracer, " + "only threads are supported\n" % concurrency ) self.assertEqual(out, expected_out) diff --git a/tests/test_config.py b/tests/test_config.py index bf84423d..232d2289 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -123,6 +123,45 @@ class ConfigTest(CoverageTest): ["the_$one", "anotherZZZ", "xZZZy", "xy", "huh${X}what"] ) + def test_tweaks_after_constructor(self): + # Arguments to the constructor are applied to the configuation. + cov = coverage.coverage(timid=True, data_file="fooey.dat") + cov.config["run:timid"] = False + + self.assertFalse(cov.config.timid) + self.assertFalse(cov.config.branch) + self.assertEqual(cov.config.data_file, "fooey.dat") + + self.assertFalse(cov.config["run:timid"]) + self.assertFalse(cov.config["run:branch"]) + self.assertEqual(cov.config["run:data_file"], "fooey.dat") + + def test_tweak_error_checking(self): + # Trying to set an unknown config value raises an error. + cov = coverage.coverage() + with self.assertRaises(CoverageException): + cov.config["run:xyzzy"] = 12 + with self.assertRaises(CoverageException): + cov.config["xyzzy:foo"] = 12 + with self.assertRaises(CoverageException): + _ = cov.config["run:xyzzy"] + with self.assertRaises(CoverageException): + _ = cov.config["xyzzy:foo"] + + def test_tweak_plugin_options(self): + # Plugin options have a more flexible syntax. + cov = coverage.coverage() + cov.config["run:plugins"] = ["fooey.plugin", "xyzzy.coverage.plugin"] + cov.config["fooey.plugin:xyzzy"] = 17 + cov.config["xyzzy.coverage.plugin:plugh"] = ["a", "b"] + with self.assertRaises(CoverageException): + cov.config["no_such.plugin:foo"] = 23 + + self.assertEqual(cov.config["fooey.plugin:xyzzy"], 17) + self.assertEqual(cov.config["xyzzy.coverage.plugin:plugh"], ["a", "b"]) + with self.assertRaises(CoverageException): + _ = cov.config["no_such.plugin:foo"] + class ConfigFileTest(CoverageTest): """Tests of the config file settings in particular.""" @@ -144,6 +183,7 @@ class ConfigFileTest(CoverageTest): cover_pylib = TRUE parallel = on include = a/ , b/ + concurrency = thread plugins = plugins.a_plugin plugins.another @@ -210,6 +250,7 @@ class ConfigFileTest(CoverageTest): self.assertTrue(cov.config.branch) self.assertTrue(cov.config.cover_pylib) self.assertTrue(cov.config.parallel) + self.assertEqual(cov.config.concurrency, "thread") self.assertEqual(cov.get_exclude_list(), ["if 0:", r"pragma:?\s+no cover", "another_tab"] @@ -274,7 +315,7 @@ class ConfigFileTest(CoverageTest): self.assertEqual(cov.config.omit, None) self.assertEqual(cov.config.branch, False) - def test_one(self): + def test_non_ascii(self): self.make_file(".coveragerc", """\ [html] title = tabblo & «ταБЬℓσ» # numbers diff --git a/tests/test_debug.py b/tests/test_debug.py index 985da40f..e8a8e950 100644 --- a/tests/test_debug.py +++ b/tests/test_debug.py @@ -47,7 +47,8 @@ class DebugTraceTest(CoverageTest): """) debug_out = StringIO() - cov = coverage.coverage(debug=debug, debug_file=debug_out) + cov = coverage.coverage(debug=debug) + cov._debug_file = debug_out self.start_import_stop(cov, "f1") out_lines = debug_out.getvalue().splitlines() diff --git a/tests/test_execfile.py b/tests/test_execfile.py index 69616e84..3a92ff76 100644 --- a/tests/test_execfile.py +++ b/tests/test_execfile.py @@ -91,9 +91,9 @@ class RunPycFileTest(CoverageTest): os.remove("compiled.py") # Find the .pyc file! - for there, _, files in os.walk("."): + for there, _, files in os.walk("."): # pragma: part covered for f in files: - if f.endswith(".pyc"): + if f.endswith(".pyc"): # pragma: part covered return os.path.join(there, f) def test_running_pyc(self): diff --git a/tests/test_farm.py b/tests/test_farm.py index 47f9b7b7..d0f0a72a 100644 --- a/tests/test_farm.py +++ b/tests/test_farm.py @@ -70,10 +70,9 @@ class FarmTestCase(object): """Execute the test from the run.py file. """ - if _TEST_NAME_FILE: - f = open(_TEST_NAME_FILE, "w") - f.write(self.description.replace("/", "_")) - f.close() + if _TEST_NAME_FILE: # pragma: debugging + with open(_TEST_NAME_FILE, "w") as f: + f.write(self.description.replace("/", "_")) cwd = self.cd(self.dir) diff --git a/tests/test_oddball.py b/tests/test_oddball.py index 47f492f6..3a013b04 100644 --- a/tests/test_oddball.py +++ b/tests/test_oddball.py @@ -173,7 +173,7 @@ class MemoryLeakTest(CoverageTest): if ram_growth > 100000: fails += 1 - if fails > 8: + if fails > 8: # pragma: only failure self.fail("RAM grew by %d" % (ram_growth)) diff --git a/tests/test_plugins.py b/tests/test_plugins.py index 9c5a037d..5039082e 100644 --- a/tests/test_plugins.py +++ b/tests/test_plugins.py @@ -1,12 +1,17 @@ """Tests for plugins.""" import os.path +import sys + +from nose.plugins.skip import SkipTest import coverage from coverage.codeunit import CodeUnit from coverage.parser import CodeParser from coverage.plugin import Plugins, overrides +import coverage.plugin + from tests.coveragetest import CoverageTest @@ -98,7 +103,10 @@ class PluginTest(CoverageTest): """) self.assert_doesnt_exist("evidence.out") - _ = coverage.Coverage(plugins=["my_plugin"]) + cov = coverage.Coverage() + cov.config["run:plugins"] = ["my_plugin"] + cov.start() + cov.stop() with open("evidence.out") as f: self.assertEqual(f.read(), "we are here!") @@ -106,8 +114,10 @@ class PluginTest(CoverageTest): def test_missing_plugin_raises_import_error(self): # Prove that a missing plugin will raise an ImportError. with self.assertRaises(ImportError): - cov = coverage.Coverage(plugins=["does_not_exist_woijwoicweo"]) + cov = coverage.Coverage() + cov.config["run:plugins"] = ["does_not_exist_woijwoicweo"] cov.start() + cov.stop() def test_bad_plugin_isnt_hidden(self): # Prove that a plugin with an error in it will raise the error. @@ -115,9 +125,15 @@ class PluginTest(CoverageTest): 1/0 """) with self.assertRaises(ZeroDivisionError): - _ = coverage.Coverage(plugins=["plugin_over_zero"]) + cov = coverage.Coverage() + cov.config["run:plugins"] = ["plugin_over_zero"] + cov.start() + cov.stop() def test_importing_myself(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 @@ -128,7 +144,8 @@ class PluginTest(CoverageTest): d = 4 """) - cov = coverage.Coverage(plugins=["tests.test_plugins"]) + cov = coverage.Coverage() + cov.config["run:plugins"] = ["tests.test_plugins"] # Import the python file, executing it. self.start_import_stop(cov, "simple") @@ -141,24 +158,32 @@ class PluginTest(CoverageTest): class Plugin(coverage.CoveragePlugin): - def trace_judge(self, disp): - if "xyz.py" in disp.original_filename: - disp.trace = True - disp.source_filename = os.path.join( - "/src", - os.path.basename( - disp.original_filename.replace("xyz.py", "ABC.zz") - ) - ) + def file_tracer(self, filename): + if "xyz.py" in filename: + file_tracer = FileTracer(filename) + return file_tracer + + def file_reporter(self, filename): + return FileReporter(filename) + + +class FileTracer(coverage.plugin.FileTracer): + def __init__(self, filename): + self._filename = filename + self._source_filename = os.path.join( + "/src", + os.path.basename(filename.replace("xyz.py", "ABC.zz")) + ) + + def source_filename(self): + return self._source_filename def line_number_range(self, frame): lineno = frame.f_lineno return lineno*100+5, lineno*100+7 - def code_unit_class(self, filename): - return PluginCodeUnit -class PluginCodeUnit(CodeUnit): +class FileReporter(coverage.plugin.FileReporter): def get_parser(self, exclude=None): return PluginParser() diff --git a/tests/test_process.py b/tests/test_process.py index 3a0980dc..4669a62f 100644 --- a/tests/test_process.py +++ b/tests/test_process.py @@ -559,7 +559,7 @@ class PydocTest(CoverageTest): self.assert_pydoc_ok("coverage", coverage) def test_pydoc_coverage_coverage(self): - self.assert_pydoc_ok("coverage.coverage", coverage.coverage) + self.assert_pydoc_ok("coverage.Coverage", coverage.Coverage) class FailUnderTest(CoverageTest): diff --git a/tests/test_templite.py b/tests/test_templite.py index a4667a62..aa697b78 100644 --- a/tests/test_templite.py +++ b/tests/test_templite.py @@ -31,8 +31,10 @@ class TempliteTest(CoverageTest): an exception and never get to the result comparison. """ actual = Templite(text).render(ctx or {}) - if result: - self.assertEqual(actual, result) + # If result is None, then an exception should have prevented us getting + # to here. + assert result is not None + self.assertEqual(actual, result) def assertSynErr(self, msg): """Assert that a `TempliteSyntaxError` will happen. diff --git a/tests/test_testing.py b/tests/test_testing.py index 4a19098f..05db7298 100644 --- a/tests/test_testing.py +++ b/tests/test_testing.py @@ -114,7 +114,7 @@ def same_python_executable(e1, e2): e2 = os.path.abspath(os.path.realpath(e2)) if os.path.dirname(e1) != os.path.dirname(e2): - return False + return False # pragma: only failure e1 = os.path.basename(e1) e2 = os.path.basename(e2) @@ -126,4 +126,4 @@ def same_python_executable(e1, e2): # python2.3 and python2.3: ok return True - return False + return False # pragma: only failure diff --git a/tests/try_execfile.py b/tests/try_execfile.py index 9bbabd1a..825fdb70 100644 --- a/tests/try_execfile.py +++ b/tests/try_execfile.py @@ -12,18 +12,13 @@ def my_function(a): FN_VAL = my_function("fooey") -try: - pkg = __package__ -except NameError: - pkg = "*No __package__*" - globals_to_check = { '__name__': __name__, '__file__': __file__, '__doc__': __doc__, '__builtins__.has_open': hasattr(__builtins__, 'open'), '__builtins__.dir': dir(__builtins__), - '__package__': pkg, + '__package__': __package__, 'DATA': DATA, 'FN_VAL': FN_VAL, '__main__.DATA': getattr(__main__, "DATA", "nothing"), |
