From 46b0b4bc9fcbe56cb5d8782703f327366455bd93 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Sat, 9 Jan 2010 20:57:37 -0500 Subject: XML output file is configurable in .rc file. --- coverage/cmdline.py | 2 -- coverage/config.py | 7 +++++++ coverage/control.py | 13 ++++++++++--- test/farm/html/run_a_xml.py | 20 -------------------- test/farm/html/run_a_xml_1.py | 20 ++++++++++++++++++++ test/farm/html/run_a_xml_2.py | 20 ++++++++++++++++++++ test/farm/html/src/coverage.xml | 20 ++++++++++++++++++++ test/farm/html/src/run_a_xml_2.ini | 3 +++ test/test_cmdline.py | 2 +- test/test_config.py | 6 ++++++ 10 files changed, 87 insertions(+), 26 deletions(-) delete mode 100644 test/farm/html/run_a_xml.py create mode 100644 test/farm/html/run_a_xml_1.py create mode 100644 test/farm/html/run_a_xml_2.py create mode 100644 test/farm/html/src/coverage.xml create mode 100644 test/farm/html/src/run_a_xml_2.ini diff --git a/coverage/cmdline.py b/coverage/cmdline.py index e82cf27..60f9cdd 100644 --- a/coverage/cmdline.py +++ b/coverage/cmdline.py @@ -506,8 +506,6 @@ class CoverageScript(object): directory=options.directory, **report_args) if 'xml' in options.actions: outfile = options.outfile - if outfile == '-': - outfile = None self.coverage.xml_report(outfile=outfile, **report_args) return OK diff --git a/coverage/config.py b/coverage/config.py index 9307fad..8f508f2 100644 --- a/coverage/config.py +++ b/coverage/config.py @@ -29,6 +29,9 @@ class CoverageConfig(object): # Defaults for [html] self.html_dir = "htmlcov" + # Defaults for [xml] + self.xml_output = "coverage.xml" + def from_environment(self, env_var): """Read configuration from the `env_var` environment variable.""" # Timidity: for nose users, read an environment variable. This is a @@ -86,3 +89,7 @@ class CoverageConfig(object): # [html] if cp.has_option('html', 'directory'): self.html_dir = cp.get('html', 'directory') + + # [xml] + if cp.has_option('xml', 'output'): + self.xml_output = cp.get('xml', 'output') diff --git a/coverage/control.py b/coverage/control.py index 3a1b7f0..5c25380 100644 --- a/coverage/control.py +++ b/coverage/control.py @@ -366,14 +366,21 @@ class coverage(object): """Generate an XML report of coverage results. The report is compatible with Cobertura reports. + + Each module in `morfs` is included in the report. `outfile` is the + path to write the file to, "-" will write to stdout. """ self.config.from_args( ignore_errors=ignore_errors, - omit_prefixes=omit_prefixes + omit_prefixes=omit_prefixes, + xml_output=outfile, ) - if outfile: - outfile = open(outfile, "w") + if self.config.xml_output: + if self.config.xml_output == '-': + outfile = sys.stdout + else: + outfile = open(self.config.xml_output, "w") try: reporter = XmlReporter(self, self.config.ignore_errors) reporter.report( diff --git a/test/farm/html/run_a_xml.py b/test/farm/html/run_a_xml.py deleted file mode 100644 index dabfa61..0000000 --- a/test/farm/html/run_a_xml.py +++ /dev/null @@ -1,20 +0,0 @@ -def html_it(): - """Run coverage and make an XML report for x.""" - import coverage - cov = coverage.coverage() - cov.start() - import a - cov.stop() - cov.xml_report(a, outfile="../xml/coverage.xml") - -import os -if not os.path.exists("xml"): - os.makedirs("xml") - -runfunc(html_it, rundir="src") - -compare("gold_x_xml", "xml", scrubs=[ - (r' timestamp="\d+"', ' timestamp="TIMESTAMP"'), - (r' version="[-.\w]+"', ' version="VERSION"'), - ]) -clean("xml") diff --git a/test/farm/html/run_a_xml_1.py b/test/farm/html/run_a_xml_1.py new file mode 100644 index 0000000..7f4805e --- /dev/null +++ b/test/farm/html/run_a_xml_1.py @@ -0,0 +1,20 @@ +def html_it(): + """Run coverage and make an XML report for a.""" + import coverage + cov = coverage.coverage() + cov.start() + import a + cov.stop() + cov.xml_report(a, outfile="../xml_1/coverage.xml") + +import os +if not os.path.exists("xml_1"): + os.makedirs("xml_1") + +runfunc(html_it, rundir="src") + +compare("gold_x_xml", "xml_1", scrubs=[ + (r' timestamp="\d+"', ' timestamp="TIMESTAMP"'), + (r' version="[-.\w]+"', ' version="VERSION"'), + ]) +clean("xml_1") diff --git a/test/farm/html/run_a_xml_2.py b/test/farm/html/run_a_xml_2.py new file mode 100644 index 0000000..b08d796 --- /dev/null +++ b/test/farm/html/run_a_xml_2.py @@ -0,0 +1,20 @@ +def html_it(): + """Run coverage and make an XML report for a.""" + import coverage + cov = coverage.coverage(config_file="run_a_xml_2.ini") + cov.start() + import a + cov.stop() + cov.xml_report(a) + +import os +if not os.path.exists("xml_2"): + os.makedirs("xml_2") + +runfunc(html_it, rundir="src") + +compare("gold_x_xml", "xml_2", scrubs=[ + (r' timestamp="\d+"', ' timestamp="TIMESTAMP"'), + (r' version="[-.\w]+"', ' version="VERSION"'), + ]) +clean("xml_2") diff --git a/test/farm/html/src/coverage.xml b/test/farm/html/src/coverage.xml new file mode 100644 index 0000000..bc51732 --- /dev/null +++ b/test/farm/html/src/coverage.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + diff --git a/test/farm/html/src/run_a_xml_2.ini b/test/farm/html/src/run_a_xml_2.ini new file mode 100644 index 0000000..8d28f97 --- /dev/null +++ b/test/farm/html/src/run_a_xml_2.ini @@ -0,0 +1,3 @@ +# Put all the XML output in xml_2 +[xml] +output = ../xml_2/coverage.xml diff --git a/test/test_cmdline.py b/test/test_cmdline.py index 9196873..5624268 100644 --- a/test/test_cmdline.py +++ b/test/test_cmdline.py @@ -482,7 +482,7 @@ class NewCmdLineTest(CmdLineTest): """) self.cmd_executes("xml -o -", self.INIT_LOAD + """\ .xml_report(ignore_errors=None, omit_prefixes=None, morfs=[], - outfile=None) + outfile="-") """) self.cmd_executes("xml --omit fooey", self.INIT_LOAD + """\ .xml_report(ignore_errors=None, omit_prefixes=["fooey"], morfs=[], diff --git a/test/test_config.py b/test/test_config.py index 478f115..f4bd65b 100644 --- a/test/test_config.py +++ b/test/test_config.py @@ -93,6 +93,7 @@ class ConfigFileTest(CoverageTest): """Tests of the config file settings in particular.""" def test_config_file_settings(self): + # This sample file tries to use lots of variation of syntax... self.make_file(".coveragerc", """\ # This is a settings file for coverage.py [run] @@ -119,6 +120,9 @@ class ConfigFileTest(CoverageTest): directory = c:\\tricky\\dir.somewhere + [xml] + output=mycov.xml + """) cov = coverage.coverage() @@ -137,3 +141,5 @@ class ConfigFileTest(CoverageTest): ) self.assertEqual(cov.config.html_dir, r"c:\tricky\dir.somewhere") + + self.assertEqual(cov.config.xml_output, "mycov.xml") -- cgit v1.2.1