10000 Support report terminal output in Markdown Table format #1418 by stepeos · Pull Request #1479 · nedbat/coveragepy · GitHub
[go: up one dir, main page]

Skip to content

Support report terminal output in Markdown Table format #1418 #1479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Nov 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
style: adjust the formatting
  • Loading branch information
nedbat committed Nov 4, 2022
commit 87113b403193b67498e1c79e31ecbba301551e49
4 changes: 2 additions & 2 deletions coverage/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ def report(
self, morfs=None, show_missing=None, ignore_errors=None,
file=None, omit=None, include=None, skip_covered=None,
contexts=None, skip_empty=None, precision=None, sort=None,
output_format=None
output_format=None,
):
"""Write a textual summary report to `file`.

Expand Down Expand Up @@ -966,7 +966,7 @@ def report(
ignore_errors=ignore_errors, report_omit=omit, report_include=include,
show_missing=show_missing, skip_covered=skip_covered,
report_contexts=contexts, skip_empty=skip_empty, precision=precision,
sort=sort, output_format=output_format
sort=sort, output_format=output_format,
):
reporter = SummaryReporter(self)
return reporter.report(morfs, outfile=file)
Expand Down
86 changes: 46 additions & 40 deletions coverage/summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,30 @@ def writeout(self, line):

def _report_text(self, header, lines_values, total_line, end_lines):
"""Internal method that prints report data in text format.
`header` is a tuple with captions.

`header` is a tuple with captions.
`lines_values` is list of tuples of sortable values.
`total_line` is a tuple with values of the total line.
`end_lines` is a tuple of ending lines with information about skipped files.
""" # Prepare the formatting strings, header, and column sorting.
max_name = max([len(line[0]) for line in lines_values] + [5]
) + 1
max_n = max(len(total_line[header.index("Cover")]) + 2,
len(" Cover")
) + 1

"""
# Prepare the formatting strings, header, and column sorting.
max_name = max([len(line[0]) for line in lines_values] + [5]) + 1
max_n = max(len(total_line[header.index("Cover")]) + 2, len(" Cover")) + 1
max_n = max([max_n] + [len(line[header.index("Cover")]) + 2 for line in lines_values])
h_form = dict(
Name="{:{name_len}}", Stmts="{:>7}", Miss="{:>7}",
Branch="{:>7}", BrPart="{:>7}", Cover="{:>{n}}",
Missing="{:>10}"
)
Name="{:{name_len}}",
Stmts="{:>7}",
Miss="{:>7}",
Branch="{:>7}",
BrPart="{:>7}",
Cover="{:>{n}}",
Missing="{:>10}",
)
header_items = [
h_form[item].format(item, name_len=max_name, n=max_n)
for item in header]
for item in header
]
header_str = "".join(header_items)
rule = "-" * len(header_str)

Expand All @@ -64,15 +68,17 @@ def _report_text(self, header, lines_values, total_line, end_lines):
# build string with line values
line_items = [
h_form[item].format(str(value),
name_len=max_name, n=max_n-1) for item, value in zip(header, values)]
name_len=max_name, n=max_n-1) for item, value in zip(header, values)
]
text = "".join(line_items)
self.writeout(text)

# Write a TOTAL line
self.writeout(rule)
line_items = [
h_form[item].format(str(value),
name_len=max_name, n=max_n-1) for item, value in zip(header, total_line)]
name_len=max_name, n=max_n-1) for item, value in zip(header, total_line)
]
text = "".join(line_items)
self.writeout(text)

Expand All @@ -81,25 +87,27 @@ def _report_text(self, header, lines_values, total_line, end_lines):

def _report_markdown(self, header, lines_values, total_line, end_lines):
"""Internal method that prints report data in markdown format.
`header` is a tuple with captions.

`header` is a tuple with captions.
`lines_values` is a sorted list of tuples containing coverage information.
`total_line` is a tuple with values of the total line.
`end_lines` is a tuple of ending lines with information about skipped files.

"""
# Prepare the formatting strings, header, and column sorting.
max_name = max([len(line[0].replace("_", "\\_")) for line in lines_values] + [9]
)
max_name = max([len(line[0].replace("_", "\\_")) for line in lines_values] + [9])
max_name += 1
h_form = dict(
Name="| {:{name_len}}|", Stmts="{:>9} |", Miss="{:>9} |",
Branch="{:>9} |", BrPart="{:>9} |", Cover="{:>{n}} |",
Missing="{:>10} |")
max_n = max(len(total_line[header.index("Cover")]) + 6,
len(" Cover ")
Name="| {:{name_len}}|",
Stmts="{:>9} |",
Miss="{:>9} |",
Branch="{:>9} |",
BrPart="{:>9} |",
Cover="{:>{n}} |",
Missing="{:>10} |",
)
header_items = [
h_form[item].format(item, name_len=max_name, n=max_n) for item in header]
max_n = max(len(total_line[header.index("Cover")]) + 6, len(" Cover "))
header_items = [h_form[item].format(item, name_len=max_name, n=max_n) for item in header]
header_str = "".join(header_items)
rule_str = "|" + " ".join(["- |".rjust(len(header_items[0])-1, '-')] +
["-: |".rjust(len(item)-1, '-') for item in header_items[1:]]
Expand All @@ -114,7 +122,8 @@ def _report_markdown(self, header, lines_values, total_line, end_lines):
h_form.update(dict(Cover="{:>{n}}% |"))
line_items = [
h_form[item].format(str(value).replace("_", "\\_"),
name_len=max_name, n=max_n-1) for item, value in zip(header, values)]
name_len=max_name, n=max_n-1) for item, value in zip(header, values)
]
text = "".join(line_items)
self.writeout(text)

Expand All @@ -125,12 +134,10 @@ def _report_markdown(self, header, lines_values, total_line, end_lines):
if value == '':
insert = value
elif item == "Cover":
insert = " **"+str(value)+"%**"
insert = f" **{value}%**"
else:
insert = " **"+str(value)+"**"
total_line_items += h_form[item].format(
insert, name_len=max_name, n=max_n
)
insert = f" **{value}**"
total_line_items += h_form[item].format(insert, name_len=max_name, n=max_n)
total_row_str = "".join(total_line_items)
self.writeout(total_row_str)
for end_line in end_lines:
Expand Down Expand Up @@ -188,11 +195,9 @@ def report(self, morfs, outfile=None):
if sort_idx is None:
raise ConfigError(f"Invalid sorting option: {self.config.sort!r}")
if sort_option == "name":
lines_values.sort(key=lambda tup: (human_key(tup[0]), tup[1]),
reverse=reverse)
lines_values.sort(key=lambda tup: (human_key(tup[0]), tup[1]), reverse=reverse)
else:
lines_values.sort(key=lambda tup: (tup[sort_idx], tup[0]),
reverse=reverse)
lines_values.sort(key=lambda tup: (tup[sort_idx], tup[0]), reverse=reverse)

# calculate total if we had at least one file.
total_line = ("TOTAL", self.total.n_statements, self.total.n_missing)
Expand All @@ -209,20 +214,21 @@ def report(self, morfs, outfile=None):

if self.config.skip_covered and self.skipped_count:
file_suffix = 's' if self.skipped_count>1 else ''
fmt_skip_covered = (f"\n{self.skipped_count} file{file_suffix} skipped due to "
+ "complete coverage."
fmt_skip_covered = (
f"\n{self.skipped_count} file{file_suffix} skipped due to complete coverage."
)
end_lines.append(fmt_skip_covered)
if self.config.skip_empty and self.empty_count:
file_suffix = 's' if self.empty_count>1 else ''
fmt_skip_empty = f"\n{self.empty_count} empty file{file_suffix} skipped."
end_lines.append(fmt_skip_empty)

text_format = self.config.output_format or 'text'
if text_format.lower() == 'markdown':
self._report_markdown(header, lines_values, total_line, end_lines)
text_format = self.config.output_format or "text"
if text_format == "markdown":
formatter = self._report_markdown
else:
self._report_text(header, lines_values, total_line, end_lines)
formatter = self._report_text
formatter(header, lines_values, total_line, end_lines)

return self.total.n_statements and self.total.pc_covered

Expand Down
2 changes: 1 addition & 1 deletion tests/test_cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class BaseCmdLineTest(CoverageTest):
_defaults.Coverage().report(
ignore_errors=None, include=None, omit=None, morfs=[],
show_missing=None, skip_covered=None, contexts=None, skip_empty=None, precision=None,
sort=None, output_format=None
sort=None, output_format=None,
)
_defaults.Coverage().xml_report(
ignore_errors=None, include=None, omit=None, morfs=[], outfile=None,
Expand Down
0