def run_command(cmd): """Run a command in a sub-process. Returns the exit status code and the combined stdout and stderr. """ if env.PY2 and isinstance(cmd, unicode_class): cmd = cmd.encode(sys.getfilesystemencoding()) # In some strange cases (PyPy3 in a virtualenv!?) the stdout encoding of # the subprocess is set incorrectly to ascii. Use an environment variable # to force the encoding to be the same as ours. sub_env = dict(os.environ) sub_env['PYTHONIOENCODING'] = output_encoding() proc = subprocess.Popen(cmd, shell=True, env=sub_env, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) output, _ = proc.communicate() status = proc.returncode # Get the output, and canonicalize it to strings with newlines. if not isinstance(output, str): output = output.decode(output_encoding()) output = output.replace('\r', '') return status, output
def run_command(cmd): """Run a command in a sub-process. Returns the exit status code and the combined stdout and stderr. """ if env.PY2 and isinstance(cmd, unicode_class): cmd = cmd.encode(sys.getfilesystemencoding()) # In some strange cases (PyPy3 in a virtualenv!?) the stdout encoding of # the subprocess is set incorrectly to ascii. Use an environment variable # to force the encoding to be the same as ours. sub_env = dict(os.environ) sub_env['PYTHONIOENCODING'] = output_encoding() proc = subprocess.Popen( cmd, shell=True, env=sub_env, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT ) output, _ = proc.communicate() status = proc.returncode # Get the output, and canonicalize it to strings with newlines. if not isinstance(output, str): output = output.decode(output_encoding()) output = output.replace('\r', '') return status, output
def run_command(cmd): """Run a command in a sub-process. Returns the exit status code and the combined stdout and stderr. """ # Subprocesses are expensive, but convenient, and so may be over-used in # the test suite. Use these lines to get a list of the tests using them: if 0: # pragma: debugging with open("/tmp/processes.txt", "a") as proctxt: print(os.environ.get("PYTEST_CURRENT_TEST", "unknown"), file=proctxt, flush=True) # In some strange cases (PyPy3 in a virtualenv!?) the stdout encoding of # the subprocess is set incorrectly to ascii. Use an environment variable # to force the encoding to be the same as ours. sub_env = dict(os.environ) sub_env['PYTHONIOENCODING'] = output_encoding() proc = subprocess.Popen(cmd, shell=True, env=sub_env, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) output, _ = proc.communicate() status = proc.returncode # Get the output, and canonicalize it to strings with newlines. output = output.decode(output_encoding()).replace("\r", "") return status, output
def test_accenteddotpy_not_python(self): # We run a .py file with a non-ascii name, and when reporting, we can't # parse it as Python. We should get an error message in the report. self.make_file(u"accented\xe2.py", "print('accented')") self.run_command(u"coverage run accented\xe2.py") self.make_file(u"accented\xe2.py", "This isn't python at all!") report = self.report_from_command(u"coverage report accented\xe2.py") # xxxx NotPython: Couldn't parse '...' as Python source: 'invalid syntax' at line 1 # Name Stmts Miss Cover # ---------------------------- # No data to report. errmsg = self.squeezed_lines(report)[0] # The actual file name varies run to run. errmsg = re.sub(r"parse '.*(accented.*?\.py)", r"parse '\1", errmsg) # The actual error message varies version to version errmsg = re.sub(r": '.*' at", ": 'error' at", errmsg) expected = ( u"accented\xe2.py NotPython: " u"Couldn't parse 'accented\xe2.py' as Python source: 'error' at line 1" ) if env.PY2: # pylint: disable=redefined-variable-type expected = expected.encode(output_encoding()) self.assertEqual(errmsg, expected)
def test_accented_dot_py(self): # Make a file with a non-ascii character in the filename. self.make_file(u"h\xe2t.py", "print('accented')") out = self.run_command(u"coverage run h\xe2t.py") self.assertEqual(out, "accented\n") # The HTML report uses ascii-encoded HTML entities. out = self.run_command("coverage html") self.assertEqual(out, "") self.assert_exists(u"htmlcov/h\xe2t_py.html") with open("htmlcov/index.html") as indexf: index = indexf.read() self.assertIn('<a href="hât_py.html">hât.py</a>', index) # The XML report is always UTF8-encoded. out = self.run_command("coverage xml") self.assertEqual(out, "") with open("coverage.xml", "rb") as xmlf: xml = xmlf.read() self.assertIn(u' filename="h\xe2t.py"'.encode('utf8'), xml) self.assertIn(u' name="h\xe2t.py"'.encode('utf8'), xml) report_expected = (u"Name Stmts Miss Cover\n" u"----------------------------\n" u"h\xe2t.py 1 0 100%\n") if env.PY2: report_expected = report_expected.encode(output_encoding()) out = self.run_command("coverage report") self.assertEqual(out, report_expected)
def main(argv=None): """The main entry point to coverage.py. This is installed as the script entry point. """ if argv is None: argv = sys.argv[1:] try: status = CoverageScript().command_line(argv) except ExceptionDuringRun as err: # An exception was caught while running the product code. The # sys.exc_info() return tuple is packed into an ExceptionDuringRun # exception. traceback.print_exception(*err.args) # pylint: disable=no-value-for-parameter status = ERR except BaseCoverageException as err: # A controlled error inside coverage.py: print the message to the user. msg = err.args[0] if env.PY2: msg = msg.encode(output_encoding()) print(msg) status = ERR except SystemExit as err: # The user called `sys.exit()`. Exit with their argument, if any. if err.args: status = err.args[0] else: status = None return status
def test_accenteddotpy_not_python(self): if env.JYTHON: self.skipTest("Jython doesn't like accented file names") # We run a .py file with a non-ascii name, and when reporting, we can't # parse it as Python. We should get an error message in the report. self.make_file(u"accented\xe2.py", "print('accented')") self.run_command(u"coverage run accented\xe2.py") self.make_file(u"accented\xe2.py", "This isn't python at all!") report = self.report_from_command(u"coverage report accented\xe2.py") # xxxx NotPython: Couldn't parse '...' as Python source: 'invalid syntax' at line 1 # Name Stmts Miss Cover # ---------------------------- # No data to report. errmsg = self.squeezed_lines(report)[0] # The actual file name varies run to run. errmsg = re.sub(r"parse '.*(accented.*?\.py)", r"parse '\1", errmsg) # The actual error message varies version to version errmsg = re.sub(r": '.*' at", ": 'error' at", errmsg) expected = ( u"accented\xe2.py NotPython: " u"Couldn't parse 'accented\xe2.py' as Python source: 'error' at line 1" ) if env.PY2: expected = expected.encode(output_encoding()) self.assertEqual(expected, errmsg)
def test_accented_dot_py(self): # Make a file with a non-ascii character in the filename. self.make_file(u"h\xe2t.py", "print('accented')") out = self.run_command(u"coverage run h\xe2t.py") self.assertEqual(out, "accented\n") # The HTML report uses ascii-encoded HTML entities. out = self.run_command("coverage html") self.assertEqual(out, "") self.assert_exists(u"htmlcov/h\xe2t_py.html") with open("htmlcov/index.html") as indexf: index = indexf.read() self.assertIn('<a href="hât_py.html">hât.py</a>', index) # The XML report is always UTF8-encoded. out = self.run_command("coverage xml") self.assertEqual(out, "") with open("coverage.xml", "rb") as xmlf: xml = xmlf.read() self.assertIn(u' filename="h\xe2t.py"'.encode('utf8'), xml) self.assertIn(u' name="h\xe2t.py"'.encode('utf8'), xml) report_expected = ( u"Name Stmts Miss Cover\n" u"----------------------------\n" u"h\xe2t.py 1 0 100%\n" ) if env.PY2: report_expected = report_expected.encode(output_encoding()) out = self.run_command("coverage report") self.assertEqual(out, report_expected)
def test_accenteddotpy_not_python(self): # We run a .py file with a non-ascii name, and when reporting, we can't # parse it as Python. We should get an error message in the report. self.make_file(u"accented\xe2.py", "print('accented')") self.run_command(u"coverage run accented\xe2.py") self.make_file(u"accented\xe2.py", "This isn't python at all!") report = self.report_from_command(u"coverage report accented\xe2.py") # Name Stmts Miss Cover # ---------------------------- # xxxx NotPython: Couldn't parse '...' as Python source: 'invalid syntax' at line 1 # No data to report. last = self.squeezed_lines(report)[-2] # The actual file name varies run to run. last = re.sub(r"parse '.*(accented.*?\.py)", r"parse '\1", last) # The actual error message varies version to version last = re.sub(r": '.*' at", ": 'error' at", last) expected = ( u"accented\xe2.py NotPython: " u"Couldn't parse 'accented\xe2.py' as Python source: 'error' at line 1" ) if env.PY2: # pylint: disable=redefined-variable-type expected = expected.encode(output_encoding()) self.assertEqual(last, expected)
def test_accenteddotpy_not_python(self): if env.JYTHON: self.skipTest("Jython doesn't like accented file names") # We run a .py file with a non-ascii name, and when reporting, we can't # parse it as Python. We should get an error message in the report. self.make_file(u"accented\xe2.py", "print('accented')") self.run_command(u"coverage run accented\xe2.py") self.make_file(u"accented\xe2.py", "This isn't python at all!") report = self.report_from_command(u"coverage report accented\xe2.py") # Couldn't parse '...' as Python source: 'invalid syntax' at line 1 # Name Stmts Miss Cover # ---------------------------- # No data to report. errmsg = self.squeezed_lines(report)[0] # The actual file name varies run to run. errmsg = re.sub(r"parse '.*(accented.*?\.py)", r"parse '\1", errmsg) # The actual error message varies version to version errmsg = re.sub(r": '.*' at", ": 'error' at", errmsg) expected = u"Couldn't parse 'accented\xe2.py' as Python source: 'error' at line 1" if env.PY2: expected = expected.encode(output_encoding()) assert expected == errmsg
def test_accented_directory(self): # Make a file with a non-ascii character in the directory name. self.make_file(u"\xe2/accented.py", "print('accented')") out = self.run_command(u"coverage run \xe2/accented.py") self.assertEqual(out, "accented\n") # The HTML report uses ascii-encoded HTML entities. out = self.run_command("coverage html") self.assertEqual(out, "") self.assert_exists(u"htmlcov/\xe2_accented_py.html") with open("htmlcov/index.html") as indexf: index = indexf.read() self.assertIn('<a href="â_accented_py.html">â%saccented.py</a>' % os.sep, index) # The XML report is always UTF8-encoded. out = self.run_command("coverage xml") self.assertEqual(out, "") with open("coverage.xml", "rb") as xmlf: xml = xmlf.read() self.assertIn(u' filename="\xe2/accented.py"'.encode('utf8'), xml) self.assertIn(u' name="accented.py"'.encode('utf8'), xml) self.assertIn( u'<package branch-rate="0" complexity="0" line-rate="1" name="\xe2">'.encode('utf8'), xml ) report_expected = ( u"Name Stmts Miss Cover\n" u"-----------------------------------\n" u"\xe2%saccented.py 1 0 100%%\n" % os.sep ) if env.PY2: # pylint: disable=redefined-variable-type report_expected = report_expected.encode(output_encoding()) out = self.run_command("coverage report") self.assertEqual(out, report_expected)
def writeout(line): """Write a line to the output, adding a newline.""" if env.PY2: line = line.encode(output_encoding()) outfile.write(line.rstrip()) outfile.write("\n")
def report(self, morfs, outfile=None): """Writes a report summarizing coverage statistics per module. `outfile` is a file object to write the summary to. It must be opened for native strings (bytes on Python 2, Unicode on Python 3). """ self.find_file_reporters(morfs) # Prepare the formatting strings max_name = max([len(fr.relative_filename()) for fr in self.file_reporters] + [5]) fmt_name = u"%%- %ds " % max_name fmt_err = u"%s %s: %s\n" fmt_skip_covered = u"\n%s file%s skipped due to complete coverage.\n" header = (fmt_name % "Name") + u" Stmts Miss" fmt_coverage = fmt_name + u"%6d %6d" if self.branches: header += u" Branch BrPart" fmt_coverage += u" %6d %6d" width100 = Numbers.pc_str_width() header += u"%*s" % (width100+4, "Cover") fmt_coverage += u"%%%ds%%%%" % (width100+3,) if self.config.show_missing: header += u" Missing" fmt_coverage += u" %s" rule = u"-" * len(header) + u"\n" header += u"\n" fmt_coverage += u"\n" if outfile is None: outfile = sys.stdout if env.PY2: writeout = lambda u: outfile.write(u.encode(output_encoding())) else: writeout = outfile.write # Write the header writeout(header) writeout(rule) total = Numbers() skipped_count = 0 for fr in self.file_reporters: try: analysis = self.coverage._analyze(fr) nums = analysis.numbers total += nums if self.config.skip_covered: # Don't report on 100% files. no_missing_lines = (nums.n_missing == 0) no_missing_branches = (nums.n_partial_branches == 0) if no_missing_lines and no_missing_branches: skipped_count += 1 continue args = (fr.relative_filename(), nums.n_statements, nums.n_missing) if self.branches: args += (nums.n_branches, nums.n_partial_branches) args += (nums.pc_covered_str,) if self.config.show_missing: missing_fmtd = analysis.missing_formatted() if self.branches: branches_fmtd = analysis.arcs_missing_formatted() if branches_fmtd: if missing_fmtd: missing_fmtd += ", " missing_fmtd += branches_fmtd args += (missing_fmtd,) writeout(fmt_coverage % args) except Exception: report_it = not self.config.ignore_errors if report_it: typ, msg = sys.exc_info()[:2] # NotPython is only raised by PythonFileReporter, which has a # should_be_python() method. if typ is NotPython and not fr.should_be_python(): report_it = False if report_it: writeout(fmt_err % (fr.relative_filename(), typ.__name__, msg)) if total.n_files > 1: writeout(rule) args = ("TOTAL", total.n_statements, total.n_missing) if self.branches: args += (total.n_branches, total.n_partial_branches) args += (total.pc_covered_str,) if self.config.show_missing: args += ("",) writeout(fmt_coverage % args) if not total.n_files and not skipped_count: raise CoverageException("No data to report.") if self.config.skip_covered and skipped_count: writeout(fmt_skip_covered % (skipped_count, 's' if skipped_count > 1 else '')) return total.n_statements and total.pc_covered