Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_pc_covered_str(self):
# Numbers._precision is a global, which is bad.
Numbers.set_precision(0)
n0 = Numbers(n_files=1, n_statements=1000, n_missing=0)
n1 = Numbers(n_files=1, n_statements=1000, n_missing=1)
n999 = Numbers(n_files=1, n_statements=1000, n_missing=999)
n1000 = Numbers(n_files=1, n_statements=1000, n_missing=1000)
self.assertEqual(n0.pc_covered_str, "100")
self.assertEqual(n1.pc_covered_str, "99")
self.assertEqual(n999.pc_covered_str, "1")
self.assertEqual(n1000.pc_covered_str, "0")
def report(self, morfs, outfile=None):
if hasattr(self, 'find_code_units'):
self.find_code_units(morfs)
else:
self.find_file_reporters(morfs)
total = Numbers()
if hasattr(self, 'code_units'):
units = self.code_units
else:
units = self.file_reporters
for cu in units:
try:
analysis = self.coverage._analyze(cu)
nums = analysis.numbers
total += nums
except KeyboardInterrupt:
raise
except Exception:
if self.config.ignore_errors:
continue
def test_covered_ratio(self):
n = Numbers(n_files=1, n_statements=200, n_missing=47)
self.assertEqual(n.ratio_covered, (153, 200))
n = Numbers(
n_files=1, n_statements=200, n_missing=47,
n_branches=10, n_missing_branches=3, n_partial_branches=1000,
)
self.assertEqual(n.ratio_covered, (160, 210))
retry = True
while not loaded and retry:
try:
# Load the new coverage data
cov = coverage.coverage()
cov.load()
# Override precision for coverage reporting.
coverage.results.Numbers.set_precision(1)
if cov.data.measured_files():
self.coverage_data = {
'lines': {},
'missing': {},
}
totals = coverage.results.Numbers()
# Update the coverage display of every file mentioned in the file.
for filename in cov.data.measured_files():
filename = os.path.normcase(filename)
node = nodify(filename)
dirname, basename = os.path.split(filename)
# If the normalized version of the filename is the same as the
# filename, then the file *isn't* under the project root.
if filename == self.filename_normalizer(filename):
file_tree = self.global_file_tree
else:
file_tree = self.project_file_tree
try:
# # Make sure the file exists on the tree.
status_file = os.path.join(self.directory, self.STATUS_FILE)
with open(status_file) as fstatus:
status = json.load(fstatus)
except (IOError, ValueError):
usable = False
else:
usable = True
if status['format'] != self.STATUS_FORMAT:
usable = False
elif status['version'] != coverage.__version__:
usable = False
if usable:
self.files = {}
for filename, fileinfo in iitems(status['files']):
fileinfo['index']['nums'] = Numbers(*fileinfo['index']['nums'])
self.files[filename] = fileinfo
self.globals = status['globals']
else:
self.reset()
if self.coverage.data.has_arcs():
self.no_branch = self.parser.lines_matching(
join_regex(self.coverage.config.partial_list),
join_regex(self.coverage.config.partial_always_list)
)
n_branches = self.total_branches()
mba = self.missing_branch_arcs()
n_missing_branches = sum(
[len(v) for k,v in mba.items() if k not in self.missing]
)
else:
n_branches = n_missing_branches = 0
self.no_branch = set()
self.numbers = Numbers(
n_files=1,
n_statements=len(self.statements),
n_excluded=len(self.excluded),
n_missing=len(self.missing),
n_branches=n_branches,
n_missing_branches=n_missing_branches,
)
self.coverage.get_data().set_query_contexts(self.config.report_contexts)
for fr, analysis in get_analysis_to_report(self.coverage, morfs):
self.report_one_file(fr, analysis)
# Prepare the formatting strings, header, and column sorting.
max_name = max([len(fr.relative_filename()) for (fr, analysis) in self.fr_analysis] + [5])
fmt_name = u"%%- %ds " % max_name
fmt_skip_covered = u"\n%s file%s skipped due to complete coverage."
fmt_skip_empty = u"\n%s empty file%s skipped."
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)
column_order = dict(name=0, stmts=1, miss=2, cover=-1)
if self.branches:
column_order.update(dict(branch=3, brpart=4))
# Write the header
self.writeout(header)
self.writeout(rule)
# `lines` is a list of pairs, (line text, line values). The line text
def __init__(self, coverage):
self.coverage = coverage
self.config = self.coverage.config
self.branches = coverage.get_data().has_arcs()
self.outfile = None
self.fr_analysis = []
self.skipped_count = 0
self.empty_count = 0
self.total = Numbers()
self.fmt_err = u"%s %s: %s"
header2 += " Missing"
fmt_coverage += " %s"
rule = "-" * len(header1) + "\n"
header1 += "\n"
header2 += "\n"
fmt_coverage += "\n"
if not outfile:
outfile = sys.stdout
# Write the header
outfile.write(header1)
outfile.write(header2)
outfile.write(rule)
total = Numbers()
total_uncovered = 0
lines = []
for cu in self.code_units:
try:
analysis = self.coverage._analyze(cu)
nums = analysis.numbers
uncovered = nums.n_statements - nums.n_executed
total_uncovered += uncovered
args = (cu.name, uncovered, nums.n_executed)
if self.branches:
args += (nums.n_branches, nums.n_executed_branches)
args += (nums.pc_covered,)
if self.show_missing:
args += (analysis.missing_formatted(),)
if sortby == "covered":