Skip to content

Commit a4da2c6

Browse files
committed
refactor: more clean up of report table code
1 parent 3b3cc69 commit a4da2c6

File tree

1 file changed

+38
-41
lines changed

1 file changed

+38
-41
lines changed

coverage/summary.py

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ def write_items(self, items):
3737
def _report_text(self, header, lines_values, total_line, end_lines):
3838
"""Internal method that prints report data in text format.
3939
40-
`header` is a tuple with captions.
41-
`lines_values` is list of tuples of sortable values.
42-
`total_line` is a tuple with values of the total line.
43-
`end_lines` is a tuple of ending lines with information about skipped files.
40+
`header` is a list with captions.
41+
`lines_values` is list of lists of sortable values.
42+
`total_line` is a list with values of the total line.
43+
`end_lines` is a list of ending lines with information about skipped files.
4444
4545
"""
4646
# Prepare the formatting strings, header, and column sorting.
@@ -90,15 +90,15 @@ def _report_text(self, header, lines_values, total_line, end_lines):
9090
def _report_markdown(self, header, lines_values, total_line, end_lines):
9191
"""Internal method that prints report data in markdown format.
9292
93-
`header` is a tuple with captions.
94-
`lines_values` is a sorted list of tuples containing coverage information.
95-
`total_line` is a tuple with values of the total line.
96-
`end_lines` is a tuple of ending lines with information about skipped files.
93+
`header` is a list with captions.
94+
`lines_values` is a sorted list of lists containing coverage information.
95+
`total_line` is a list with values of the total line.
96+
`end_lines` is a list of ending lines with information about skipped files.
9797
9898
"""
9999
# Prepare the formatting strings, header, and column sorting.
100-
max_name = max([len(line[0].replace("_", "\\_")) for line in lines_values] + [9])
101-
max_name += 1
100+
max_name = max((len(line[0].replace("_", "\\_")) for line in lines_values), default=0)
101+
max_name = max(max_name, len("**TOTAL**")) + 1
102102
formats = dict(
103103
Name="| {:{name_len}}|",
104104
Stmts="{:>9} |",
@@ -123,24 +123,23 @@ def _report_markdown(self, header, lines_values, total_line, end_lines):
123123
# build string with line values
124124
formats.update(dict(Cover="{:>{n}}% |"))
125125
line_items = [
126-
formats[item].format(str(value).replace("_", "\\_"),
127-
name_len=max_name, n=max_n-1) for item, value in zip(header, values)
126+
formats[item].format(str(value).replace("_", "\\_"), name_len=max_name, n=max_n-1)
127+
for item, value in zip(header, values)
128128
]
129129
self.write_items(line_items)
130130

131131
# Write the TOTAL line
132132
formats.update(dict(Name="|{:>{name_len}} |", Cover="{:>{n}} |"))
133133
total_line_items = []
134134
for item, value in zip(header, total_line):
135-
if value == '':
135+
if value == "":
136136
insert = value
137137
elif item == "Cover":
138138
insert = f" **{value}%**"
139139
else:
140140
insert = f" **{value}**"
141141
total_line_items += formats[item].format(insert, name_len=max_name, n=max_n)
142-
total_row_str = "".join(total_line_items)
143-
self.write(total_row_str)
142+
self.write_items(total_line_items)
144143
for end_line in end_lines:
145144
self.write(end_line)
146145

@@ -157,34 +156,37 @@ def report(self, morfs, outfile=None):
157156
for fr, analysis in get_analysis_to_report(self.coverage, morfs):
158157
self.report_one_file(fr, analysis)
159158

160-
# Prepare the formatting strings, header, and column sorting.
161-
header = ("Name", "Stmts", "Miss",)
159+
if not self.total.n_files and not self.skipped_count:
160+
raise NoDataError("No data to report.")
161+
162+
# Prepare the header line and column sorting.
163+
header = ["Name", "Stmts", "Miss"]
162164
if self.branches:
163-
header += ("Branch", "BrPart",)
164-
header += ("Cover",)
165+
header += ["Branch", "BrPart"]
166+
header += ["Cover"]
165167
if self.config.show_missing:
166-
header += ("Missing",)
168+
header += ["Missing"]
167169

168170
column_order = dict(name=0, stmts=1, miss=2, cover=-1)
169171
if self.branches:
170172
column_order.update(dict(branch=3, brpart=4))
171173

172-
# `lines_values` is list of tuples of sortable values.
174+
# `lines_values` is list of lists of sortable values.
173175
lines_values = []
174176

175177
for (fr, analysis) in self.fr_analysis:
176178
nums = analysis.numbers
177179

178-
args = (fr.relative_filename(), nums.n_statements, nums.n_missing)
180+
args = [fr.relative_filename(), nums.n_statements, nums.n_missing]
179181
if self.branches:
180-
args += (nums.n_branches, nums.n_partial_branches)
181-
args += (nums.pc_covered_str,)
182+
args += [nums.n_branches, nums.n_partial_branches]
183+
args += [nums.pc_covered_str]
182184
if self.config.show_missing:
183-
args += (analysis.missing_formatted(branches=True),)
184-
args += (nums.pc_covered,)
185+
args += [analysis.missing_formatted(branches=True)]
186+
args += [nums.pc_covered]
185187
lines_values.append(args)
186188

187-
# line-sorting.
189+
# Line sorting.
188190
sort_option = (self.config.sort or "name").lower()
189191
reverse = False
190192
if sort_option[0] == '-':
@@ -200,29 +202,24 @@ def report(self, morfs, outfile=None):
200202
else:
201203
lines_values.sort(key=lambda tup: (tup[sort_idx], tup[0]), reverse=reverse)
202204

203-
# calculate total if we had at least one file.
204-
total_line = ("TOTAL", self.total.n_statements, self.total.n_missing)
205+
# Calculate total if we had at least one file.
206+
total_line = ["TOTAL", self.total.n_statements, self.total.n_missing]
205207
if self.branches:
206-
total_line += (self.total.n_branches, self.total.n_partial_branches)
207-
total_line += (self.total.pc_covered_str,)
208+
total_line += [self.total.n_branches, self.total.n_partial_branches]
209+
total_line += [self.total.pc_covered_str]
208210
if self.config.show_missing:
209-
total_line += ("",)
211+
total_line += [""]
210212

211-
# create other final lines
213+
# Create other final lines.
212214
end_lines = []
213-
if not self.total.n_files and not self.skipped_count:
214-
raise NoDataError("No data to report.")
215-
216215
if self.config.skip_covered and self.skipped_count:
217216
file_suffix = 's' if self.skipped_count>1 else ''
218-
fmt_skip_covered = (
217+
end_lines.append(
219218
f"\n{self.skipped_count} file{file_suffix} skipped due to complete coverage."
220219
)
221-
end_lines.append(fmt_skip_covered)
222220
if self.config.skip_empty and self.empty_count:
223-
file_suffix = 's' if self.empty_count>1 else ''
224-
fmt_skip_empty = f"\n{self.empty_count} empty file{file_suffix} skipped."
225-
end_lines.append(fmt_skip_empty)
221+
file_suffix = 's' if self.empty_count > 1 else ''
222+
end_lines.append(f"\n{self.empty_count} empty file{file_suffix} skipped.")
226223

227224
text_format = self.config.format or "text"
228225
if text_format == "markdown":

0 commit comments

Comments
 (0)