Skip to content

Commit

Permalink
Merge pull request #1292 from buildtesters/issue_1276
Browse files Browse the repository at this point in the history
Colorize output for all terse formats
  • Loading branch information
shahzebsiddiqui authored Nov 7, 2022
2 parents b364cc6 + d30d3bc commit 82fb79b
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 84 deletions.
70 changes: 30 additions & 40 deletions buildtest/cli/buildspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,10 +592,10 @@ def print_buildspecfiles(self, terse=None, header=None):
if self.terse:

if not self.header:
print("buildspec")
console.print("buildspec", style=self.color)

for buildspec in self.cache["buildspecs"].keys():
print(buildspec)
console.print(f"[{self.color}]{buildspec}")

return

Expand Down Expand Up @@ -623,10 +623,10 @@ def print_tags(self):
# if --terse option specified print list of all tags in machine readable format
if self.terse:
if not self.header:
print("tag")
console.print("tag", style=self.color)

for tag in self.cache["unique_tags"]:
print(tag)
console.print(f"[{self.color}]{tag}")

return

Expand All @@ -652,10 +652,10 @@ def print_executors(self):
if self.terse:

if not self.header:
print("executor")
console.print("executor", style=self.color)

for executor in self.cache["unique_executors"]:
print(executor)
console.print(f"[{self.color}]{executor}")

return

Expand All @@ -681,13 +681,15 @@ def print_by_executors(self):
if self.terse:

if not self.header:
print("executor|name|description")
console.print("executor|name|description", style=self.color)

for executor_name in self.cache["executor"].keys():
for test_name, description in self.cache["executor"][
executor_name
].items():
print(f"{executor_name}|{test_name}|{description}")
console.print(
f"[{self.color}]{executor_name}|{test_name}|{description}"
)
return

table = Table(title="Tests by Executors", header_style="blue", show_lines=True)
Expand All @@ -712,12 +714,11 @@ def print_by_tags(self):
if self.terse:

if not self.header:
print("tags|name|description")
console.print("tags|name|description", style=self.color)

for tagname in self.cache["tags"].keys():
for test_name, description in self.cache["tags"][tagname].items():

print(f"{tagname}|{test_name}|{description}")
console.print(f"[{self.color}]{tagname}|{test_name}|{description}")
return

table = Table(title="Tests by Tags", header_style="blue", show_lines=True)
Expand Down Expand Up @@ -781,7 +782,7 @@ def print_buildspecs(self, terse=None, header=None, quiet=None):
# print terse output

if not self.header:
print("|".join(self.table.keys()))
console.print("|".join(self.table.keys()), style=self.color)

for row in t:

Expand All @@ -790,7 +791,8 @@ def print_buildspecs(self, terse=None, header=None, quiet=None):

# if any entry contains None type we convert to empty string
row = ["" if item is None else item for item in row]
console.print("|".join(row))
join_string = "|".join(row)
console.print(f"[{self.color}]{join_string}")

return

Expand All @@ -805,29 +807,23 @@ def list_maintainers(self):
"""Return a list of maintainers"""
return self.cache["maintainers"]

def print_maintainer(self, color=None):
"""This method prints maintainers from buildspec cache file which implements ``buildtest buildspec maintainers --list`` command.
Args:
color (bool, optional): Print table output of ``buildtest buildspec maintainers --list`` with selected color
"""

consoleColor = checkColor(color)
def print_maintainer(self):
"""This method prints maintainers from buildspec cache file which implements ``buildtest buildspec maintainers --list`` command."""

if self.terse:
if not self.header:
print("maintainers")
console.print("maintainers", style=self.color)

for maintainer in self.cache["maintainers"]:
print(maintainer)
console.print(f"[{self.color}]{maintainer}")

return

table = Table(
Column("Maintainers", overflow="fold"),
header_style="blue",
title_style="red",
row_styles=[consoleColor],
row_styles=[self.color],
)

for maintainer in self.cache["maintainers"].keys():
Expand All @@ -853,21 +849,15 @@ def print_maintainers_find(self, name):
for file in self.cache["maintainers"][name]:
console.print(file)

def print_maintainers_by_buildspecs(self, color=None):
"""This method prints maintainers breakdown by buildspecs. This method implements ``buildtest buildspec maintainers --breakdown``
Args:
color (bool, optional): Print table output of ``buildtest buildspec maintainers --breakdown`` with selected color
"""

consoleColor = checkColor(color)
def print_maintainers_by_buildspecs(self):
"""This method prints maintainers breakdown by buildspecs. This method implements ``buildtest buildspec maintainers --breakdown``."""

if self.terse:
if not self.header:
print("maintainers|buildspec")
console.print("maintainers|buildspec", style=self.color)

for maintainer, buildspecs in self.cache["maintainers"].items():
print(f"{maintainer}|{':'.join(buildspecs)}")
console.print(f"[{self.color}]{maintainer}|{':'.join(buildspecs)}")
return

table = Table(
Expand All @@ -877,7 +867,7 @@ def print_maintainers_by_buildspecs(self, color=None):
header_style="blue",
style="cyan",
title_style="red",
row_styles=[consoleColor],
row_styles=[self.color],
show_lines=True,
)

Expand Down Expand Up @@ -1299,15 +1289,15 @@ def buildspec_maintainers(
name (str, optional): List all buildspecs corresponding to maintainer name. This command is specified via ``buildtest buildspec maintainers find <name>``
"""

consoleColor = checkColor(color)

cache = BuildspecCache(configuration=configuration, terse=terse, header=header)
cache = BuildspecCache(
configuration=configuration, terse=terse, header=header, color=color
)

if list_maintainers:
cache.print_maintainer(color=consoleColor)
cache.print_maintainer()

if breakdown:
cache.print_maintainers_by_buildspecs(color=consoleColor)
cache.print_maintainers_by_buildspecs()

if name:
cache.print_maintainers_find(name=name)
Expand Down
40 changes: 11 additions & 29 deletions buildtest/cli/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,38 +102,20 @@ def list_build_history(no_header=None, terse=None, pager=None, color=None):

if terse:

row_entry = []

for key in table.keys():
row_entry.append(table[key])

transpose_list = [list(i) for i in zip(*row_entry)]

# We print the table columns if --no-header is not specified
if not no_header:
console.print("|".join(table.keys()))
console.print("|".join(table.keys()), style=consoleColor)

for (
build_id,
hostname,
user,
system,
date,
pass_test,
fail_tests,
total_tests,
pass_rate,
fail_rate,
command,
) in zip(
table["id"],
table["hostname"],
table["user"],
table["system"],
table["date"],
table["pass_tests"],
table["fail_tests"],
table["total_tests"],
table["pass_rate"],
table["fail_rate"],
table["command"],
):
console.print(
f"{build_id}|{hostname}|{user}|{date}|{pass_test}|{fail_tests}|{total_tests}|{pass_rate}|{fail_rate}|{command}"
)
for row in transpose_list:
line = "|".join(row)
console.print(f"[{consoleColor}]{line}")
return

history_table = Table(
Expand Down
21 changes: 11 additions & 10 deletions buildtest/cli/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,32 +578,33 @@ def print_report(
"""
consoleColor = checkColor(color)
if terse:
join_list = []
row_entry = []

for key in self.display_table.keys():
join_list.append(self.display_table[key])
row_entry.append(self.display_table[key])

t = [list(i) for i in zip(*join_list)]
transpose_list = [list(i) for i in zip(*row_entry)]

# limited number of rows to be printed in terse mode
if count:
t = t[:count]
transpose_list = transpose_list[:count]

if not noheader:
print("|".join(self.display_table.keys()))
console.print("|".join(self.display_table.keys()), style=consoleColor)

for i in t:
print("|".join(i))
for row in transpose_list:
line = "|".join(row)
console.print(f"[{consoleColor}]{line}")

return

join_list = []
row_entry = []
title = title or f"Report File: {self.reportfile()}"
table = Table(title=title, show_lines=True, expand=True)
for field in self.display_table.keys():
table.add_column(field, overflow="fold", style=consoleColor)
join_list.append(self.display_table[field])
transpose_list = [list(i) for i in zip(*join_list)]
row_entry.append(self.display_table[field])
transpose_list = [list(i) for i in zip(*row_entry)]

# limited number of rows to be printed
if count:
Expand Down
7 changes: 6 additions & 1 deletion tests/cli/test_buildspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,12 @@ def test_func_buildspec_find():
@pytest.mark.cli
def test_buildspec_find_terse():

cache = BuildspecCache(configuration=configuration, terse=True, header=False)
cache = BuildspecCache(
configuration=configuration,
terse=True,
header=False,
color=Color.default().name,
)
cache.print_buildspecs()
cache.print_tags()
cache.print_executors()
Expand Down
6 changes: 4 additions & 2 deletions tests/cli/test_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ def test_build_history_list():
# test with pager support: buildtest history list --pager
list_build_history(terse=False, no_header=False, pager=True)

# test with terse mode: buildtest history list --terse
list_build_history(terse=True, no_header=False, pager=False)
# test with terse mode and with color: buildtest --color <Color> history list --terse
list_build_history(
terse=True, no_header=False, pager=False, color=Color.default().name
)

# test with terse and no header: buildtest history list --terse --no-header
list_build_history(terse=True, no_header=True, pager=False)
Expand Down
5 changes: 3 additions & 2 deletions tests/cli/test_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from buildtest.cli.report import Report, report_cmd, report_summary
from buildtest.defaults import BUILD_REPORT, BUILDTEST_REPORTS, BUILDTEST_ROOT
from buildtest.exceptions import BuildTestError
from rich.color import Color


@pytest.mark.cli
Expand All @@ -19,8 +20,8 @@ def test_report():

result.print_report()

# run 'buildtest report --format name,state,returncode,buildspec --terse'
result.print_report(terse=True)
# run 'buildtest --color <Color> report --format name,state,returncode,buildspec --terse'
result.print_report(terse=True, color=Color.default().name)

result.print_report(row_count=True)

Expand Down

0 comments on commit 82fb79b

Please sign in to comment.