diff options
Diffstat (limited to 'coverage/annotate.py')
-rw-r--r-- | coverage/annotate.py | 38 |
1 files changed, 22 insertions, 16 deletions
diff --git a/coverage/annotate.py b/coverage/annotate.py index 6e68d4a3..4060450f 100644 --- a/coverage/annotate.py +++ b/coverage/annotate.py @@ -1,10 +1,19 @@ -"""Source file annotation for Coverage.""" +# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 +# For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt -import os, re +"""Source file annotation for coverage.py.""" -from coverage import env +import io +import os +import re + +from coverage.files import flat_rootname +from coverage.misc import isolate_module from coverage.report import Reporter +os = isolate_module(os) + + class AnnotateReporter(Reporter): """Generate annotated source files showing line coverage. @@ -53,14 +62,14 @@ class AnnotateReporter(Reporter): excluded = sorted(analysis.excluded) if self.directory: - dest_file = os.path.join(self.directory, fr.flat_rootname()) + dest_file = os.path.join(self.directory, flat_rootname(fr.relative_filename())) if dest_file.endswith("_py"): dest_file = dest_file[:-3] + ".py" dest_file += ",cover" else: dest_file = fr.filename + ",cover" - with open(dest_file, 'w') as dest: + with io.open(dest_file, 'w', encoding='utf8') as dest: i = 0 j = 0 covered = True @@ -73,25 +82,22 @@ class AnnotateReporter(Reporter): if i < len(statements) and statements[i] == lineno: covered = j >= len(missing) or missing[j] > lineno if self.blank_re.match(line): - dest.write(' ') + dest.write(u' ') elif self.else_re.match(line): # Special logic for lines containing only 'else:'. if i >= len(statements) and j >= len(missing): - dest.write('! ') + dest.write(u'! ') elif i >= len(statements) or j >= len(missing): - dest.write('> ') + dest.write(u'> ') elif statements[i] == missing[j]: - dest.write('! ') + dest.write(u'! ') else: - dest.write('> ') + dest.write(u'> ') elif lineno in excluded: - dest.write('- ') + dest.write(u'- ') elif covered: - dest.write('> ') + dest.write(u'> ') else: - dest.write('! ') - - if env.PY2: - line = line.encode('utf-8') + dest.write(u'! ') dest.write(line) |