Skip to content
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

global option for --row-count #1463

Merged
merged 26 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 5 additions & 5 deletions bash_completion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ _failed_tests()

_avail_maintainers()
{
buildtest buildspec maintainers --terse -l --no-header | sort
buildtest buildspec maintainers --terse -l --no-header --row-count | sort
}

# list of filterfields
Expand Down Expand Up @@ -278,7 +278,7 @@ _buildtest ()
# case statement to handle completion for buildtest inspect [name|id|list] command
case "${COMP_WORDS[2+offset]}" in
list|l)
local opts="--builder --help --no-header --pager --terse -b -h -n -t"
local opts="--builder --help --no-header --pager --row-count --terse -b -h -n -t"
COMPREPLY=( $( compgen -W "${opts}" -- $cur ) );;
name|n)
COMPREPLY=( $( compgen -W "$(_test_name)" -- $cur ) )
Expand Down Expand Up @@ -320,7 +320,7 @@ _buildtest ()
case ${COMP_WORDS[3+offset]} in
# completion for 'buildtest buildspec find invalid'
invalid)
local opts="--error --help -e -h"
local opts="--error --help --row-count -e -h"
COMPREPLY=( $( compgen -W "${opts}" -- $cur ) );;
# completion for rest of arguments
*)
Expand Down Expand Up @@ -378,7 +378,7 @@ _buildtest ()
esac
;;
maintainers|m)
local opts="--breakdown --list --help --terse --no-header -b -h -l -n find"
local opts="--breakdown --help --list --row-count --terse --no-header -b -h -l -n find"
COMPREPLY=( $( compgen -W "${opts}" -- $cur ) )

case ${COMP_WORDS[3+offset]} in
Expand Down Expand Up @@ -408,7 +408,7 @@ _buildtest ()

case ${COMP_WORDS[2+offset]} in
list)
local opts="--help --no-header --terse -h -n -t"
local opts="--help --no-header --row-count --terse -h -n -t"
COMPREPLY=( $( compgen -W "${opts}" -- $cur ) )
;;
query)
Expand Down
32 changes: 16 additions & 16 deletions buildtest/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,11 @@ def get_parent_parser():
parent_parser["file"].add_argument(
"--file", help="Write configuration file to a new file"
)

parent_parser["row-count"] = argparse.ArgumentParser(add_help=False)
parent_parser["row-count"].add_argument(
"--row-count", action="store_true", help="Show row count as a global option"
)
return parent_parser

parent_parser = get_parent_parser()
Expand Down Expand Up @@ -441,7 +446,9 @@ def history_menu(subparsers, parent_parser):
)

list_parser = history_subparser.add_parser(
"list", help="List a summary of all builds", parents=[parent_parser["pager"]]
"list",
help="List a summary of all builds",
parents=[parent_parser["pager"], parent_parser["row-count"]],
)
list_parser.add_argument(
"-n",
Expand Down Expand Up @@ -681,12 +688,15 @@ def buildspec_menu(subparsers, parent_parser):
"find",
aliases=["f"],
help="Query information from buildspecs cache",
parents=[parent_parser["pager"]],
parents=[parent_parser["pager"], parent_parser["row-count"]],
)

# buildtest buildspec maintainers
buildspec_maintainers = subparsers_buildspec.add_parser(
"maintainers", aliases=["m"], help="Query maintainers from buildspecs cache"
"maintainers",
aliases=["m"],
help="Query maintainers from buildspecs cache",
parents=[parent_parser["row-count"]],
)

subparsers_maintainers = buildspec_maintainers.add_subparsers()
Expand Down Expand Up @@ -730,7 +740,7 @@ def buildspec_menu(subparsers, parent_parser):
metavar="", dest="buildspec_find_subcommand"
)
invalid_buildspecs = subparsers_invalid.add_parser(
"invalid", help="Show invalid buildspecs"
"invalid", help="Show invalid buildspecs", parents=[parent_parser["row-count"]]
)

# buildtest buildspec find invalid options
Expand Down Expand Up @@ -813,11 +823,6 @@ def buildspec_menu(subparsers, parent_parser):
type=positive_number,
help="Limit number of entries queried in output",
)
buildspec_find.add_argument(
"--row-count",
action="store_true",
help="Print total count of records from the table.",
)
buildspec_find.add_argument(
"-r",
"--rebuild",
Expand Down Expand Up @@ -1050,7 +1055,7 @@ def report_menu(subparsers, parent_parser):
"report",
aliases=["rt"],
help="Query test report",
parents=[parent_parser["pager"]],
parents=[parent_parser["pager"], parent_parser["row-count"]],
)
subparsers = parser_report.add_subparsers(
description="Fetch test results from report file and print them in table format",
Expand Down Expand Up @@ -1154,11 +1159,6 @@ def report_menu(subparsers, parent_parser):
action="store_true",
help="Print output in machine readable format",
)
parser_report.add_argument(
"--row-count",
action="store_true",
help="Print total count of records from the table.",
)
parser_report_summary.add_argument(
"--detailed", "-d", action="store_true", help="Enable a more detailed report"
)
Expand Down Expand Up @@ -1215,7 +1215,7 @@ def inspect_menu(subparsers, parent_parser):
"list",
aliases=["l"],
help="List all test names, ids, and corresponding buildspecs",
parents=[parent_parser["pager"]],
parents=[parent_parser["pager"], parent_parser["row-count"]],
)
inspect_list.add_argument(
"-n",
Expand Down
17 changes: 15 additions & 2 deletions buildtest/cli/buildspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -914,13 +914,16 @@ def print_maintainers_by_buildspecs(self):

console.print(table)

def print_invalid_buildspecs(self, error=None, terse=None, header=None):
def print_invalid_buildspecs(
self, error=None, terse=None, header=None, row_count=None
):
"""Print invalid buildspecs from cache file. This method implements command ``buildtest buildspec find invalid``

Args:
error (bool, optional): Display error messages for invalid buildspecs. Default is ``False`` where we only print list of invalid buildspecs
terse (bool, optional): Display output in machine readable format.
header (bool, optional): Determine whether to print header column in machine readable format.
row_count (bool, optional): Display row count of invalid buildspces table
"""

terse = terse or self.terse
Expand All @@ -934,6 +937,10 @@ def print_invalid_buildspecs(self, error=None, terse=None, header=None):
console.print("There are no invalid buildspecs in cache")
return

if row_count:
print(len(self.cache["invalids"].keys()))
return

# implementation for machine readable format specified via --terse
if terse:
if not header:
Expand Down Expand Up @@ -1320,6 +1327,7 @@ def buildspec_maintainers(
header=None,
color=None,
name=None,
row_count=None,
):
"""Entry point for ``buildtest buildspec maintainers`` command.

Expand All @@ -1330,12 +1338,17 @@ def buildspec_maintainers(
header (bool, optional): If True disable printing of headers
color (bool, optional): Print output of table with selected color
name (str, optional): List all buildspecs corresponding to maintainer name. This command is specified via ``buildtest buildspec maintainers find <name>``
row_count (bool, opotional): Print row count of the maintainer table. This command is specified via ``buildtest --row-count buildspec maintainers -l``
"""

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

if row_count:
print(len(cache.list_maintainers()))
return

if list_maintainers:
cache.print_maintainer()

Expand Down Expand Up @@ -1368,7 +1381,7 @@ def buildspec_find(args, configuration):
)

if args.buildspec_find_subcommand == "invalid":
cache.print_invalid_buildspecs(error=args.error)
cache.print_invalid_buildspecs(error=args.error, row_count=args.row_count)
return

# buildtest buildspec find --tags
Expand Down
16 changes: 16 additions & 0 deletions buildtest/cli/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ def print_buildspec_help():
"buildtest buildspec find invalid --error",
"Show invalid buildspecs with error messages",
)
table.add_row(
"buildtest buildspec find invalid --row-count",
"Print total count of records from the table",
)
console.print(table)

table = Table(title="Validating Buildspecs", show_lines=False)
Expand Down Expand Up @@ -226,6 +230,10 @@ def print_buildspec_help():
"buildtest buildspec maintainers --list",
"List all maintainers from buildspec cache",
)
table.add_row(
"buildtest buildspec maintainers --list --row-count",
"Print total count of records from the table",
)
table.add_row(
"buildtest buildspec maintainers --list --terse --no-header",
"List all maintainers in machine readable format without header",
Expand Down Expand Up @@ -311,6 +319,10 @@ def print_inspect_help():
)
table.add_row("buildtest inspect list -t", "Show output in terse format")
table.add_row("buildtest inspect list --pager", "Paginate output of inspect list")
table.add_row(
"buildtest inspect list --row-count",
"Print total count of records from the table",
)
table.add_row(
"buildtest inspect name hello", "Display last run for test name 'hello'"
)
Expand Down Expand Up @@ -406,6 +418,10 @@ def print_history_help():

table.add_row("buildtest history list", "List all build history files")
table.add_row("buildtest history list --terse", "Print output in terse format")
table.add_row(
"buildtest history list --row-count",
"Print total count of records from the table",
)
table.add_row(
"buildtest history list --pager", "Paginate output of the history list"
)
Expand Down
10 changes: 9 additions & 1 deletion buildtest/cli/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def build_history(args):
terse=args.terse,
pager=args.pager,
color=args.color,
row_count=args.row_count,
)

if args.history == "query":
Expand Down Expand Up @@ -70,7 +71,9 @@ def print_terse(table, no_header=None, consoleColor=None):
console.print(f"[{consoleColor}]{line}")


def list_build_history(no_header=None, terse=None, pager=None, color=None):
def list_build_history(
no_header=None, terse=None, pager=None, color=None, row_count=None
):
"""This method is entry point for ``buildtest history list`` which prints all previous builds
stored in **BUILD_HISTORY_DIR**. Each directory has a ``build.json`` file that stores content
of each build that was run by ``buildtest build``.
Expand All @@ -80,6 +83,7 @@ def list_build_history(no_header=None, terse=None, pager=None, color=None):
terse (bool, optional): Print output in terse format
pager (bool, optional): Print output in paging format
color (bool, optional): Select desired color when displaying results
row_count (bool, optional): Print row count of all previous builds
"""

consoleColor = checkColor(color)
Expand All @@ -90,6 +94,10 @@ def list_build_history(no_header=None, terse=None, pager=None, color=None):
# only filter filters that are 'build.json'
history_files = [f for f in history_files if os.path.basename(f) == "build.json"]

if row_count:
print(len(history_files))
return

# sort all files alpha-numerically
history_files = sorted_alphanumeric(history_files)

Expand Down
14 changes: 13 additions & 1 deletion buildtest/cli/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def inspect_cmd(args, configuration, report_file=None):
builder=args.builder,
color=args.color,
pager=args.pager,
row_count=args.row_count,
)
return

Expand Down Expand Up @@ -127,7 +128,13 @@ def print_terse(table, no_header=None, console_color=None):


def inspect_list(
report, terse=None, no_header=None, builder=None, color=None, pager=None
report,
terse=None,
no_header=None,
builder=None,
color=None,
pager=None,
row_count=None,
):
"""This method list an output of test id, name, and buildspec file from the report cache. The default
behavior is to display output in table format though this can be changed with terse format which will
Expand All @@ -140,11 +147,16 @@ def inspect_list(
builder (bool, optional): Print output in builder format which can be done via ``buildtest inspect list --builder``
color (bool, optional): Print table output of ``buildtest inspect list`` with selected color
pager (bool, optional): Print output in paging format
row_count (bool, optional): Print total number of test runs
"""
consoleColor = checkColor(color)

test_ids = report._testid_lookup()

if row_count:
print(len(test_ids))
return

# implement command 'buildtest inspect list --builder'
if builder:
if pager:
Expand Down
1 change: 1 addition & 0 deletions buildtest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ def main():
header=args.no_header,
color=args.color,
name=name,
row_count=args.row_count,
)

elif args.buildspecs_subcommand in ["validate", "val"]:
Expand Down
29 changes: 11 additions & 18 deletions tests/cli/test_buildspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ def test_buildspec_maintainers():
header=True,
color=Color.default().name,
)
buildspec_maintainers(configuration=configuration, row_count=True)
buildspec_maintainers(configuration=configuration, name="@shahzebsiddiqui")


Expand All @@ -201,35 +202,27 @@ def test_buildspec_find_invalid():
cache = BuildspecCache(configuration=configuration)

# testing buildtest buildspec find invalid. This will assert SystemExit exception raised by sys.exit
try:
with pytest.raises(SystemExit):
cache.print_invalid_buildspecs(error=True)
except SystemExit:
pass

try:
with pytest.raises(SystemExit):
cache.print_invalid_buildspecs(error=False)
except SystemExit:
pass

try:
with pytest.raises(SystemExit):
cache.print_invalid_buildspecs(error=True, terse=True)
except SystemExit:
pass

try:
with pytest.raises(SystemExit):
cache.print_invalid_buildspecs(error=False, terse=True)
except SystemExit:
pass

try:
with pytest.raises(SystemExit):
cache.print_invalid_buildspecs(error=True, terse=True, header=True)
except SystemExit:
pass

try:
with pytest.raises(SystemExit):
cache.print_invalid_buildspecs(error=False, terse=True, header=True)
except SystemExit:
pass

cache.print_invalid_buildspecs(
error=False, terse=False, header=False, row_count=True
)


@pytest.mark.cli
Expand Down
3 changes: 3 additions & 0 deletions tests/cli/test_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ def test_build_history_list():
# buildtest history list --pager
list_build_history(terse=False, no_header=False, pager=True)

# buildtest history list --row-count
list_build_history(terse=False, no_header=False, pager=False, row_count=True)

# buildtest --color <Color> history list --terse --pager
list_build_history(
terse=True, no_header=False, pager=True, color=Color.default().name
Expand Down
Loading