diff --git a/buildtest/cli/report.py b/buildtest/cli/report.py index 13145d2a3..64fd8cc2a 100644 --- a/buildtest/cli/report.py +++ b/buildtest/cli/report.py @@ -771,6 +771,37 @@ def fetch_records_by_ids(self, testids): return records +def list_report(): + """This method will list all report files. This method will implement ``buildtest report list`` command.""" + if not is_file(BUILDTEST_REPORTS): + sys.exit( + console.print( + "There are no report files, please run 'buildtest build' to generate a report file." + ) + ) + + content = load_json(BUILDTEST_REPORTS) + for fname in content: + console.print(fname) + + +def clear_report(): + """This method will clear all report files. We read file BUILDTEST_REPORTS and remove all report files and also remove content of BUILDTEST_REPORTS. + This method will implement ``buildtest report clear`` command.""" + if not is_file(BUILDTEST_REPORTS): + sys.exit("There is no report file to delete") + + reports = load_json(BUILDTEST_REPORTS) + for report in reports: + console.print(f"Removing report file: {report}") + try: + os.remove(report) + except OSError: + continue + + os.remove(BUILDTEST_REPORTS) + + def report_cmd(args, configuration, report_file=None): """Entry point for ``buildtest report`` command""" @@ -778,32 +809,11 @@ def report_cmd(args, configuration, report_file=None): pager = args.pager or configuration.target_config.get("pager") if args.report_subcommand in ["clear", "c"]: - # if BUILDTEST_REPORTS file is not present then we have no report files to delete since it tracks all report files that are created - if not is_file(BUILDTEST_REPORTS): - sys.exit("There is no report file to delete") - - reports = load_json(BUILDTEST_REPORTS) - for report in reports: - console.print(f"Removing report file: {report}") - try: - os.remove(report) - except OSError: - continue - - os.remove(BUILDTEST_REPORTS) + clear_report() return if args.report_subcommand in ["list", "l"]: - if not is_file(BUILDTEST_REPORTS): - sys.exit( - console.print( - "There are no report files, please run 'buildtest build' to generate a report file." - ) - ) - - content = load_json(BUILDTEST_REPORTS) - for fname in content: - console.print(fname) + list_report() return results = Report( diff --git a/pyproject.toml b/pyproject.toml index b46c68a50..795867e60 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -88,6 +88,7 @@ omit = [ 'buildtest/cli/cd.py', 'buildtest/tools/docs.py', 'buildtest/tools/unittests.py', + 'buildtest/tools/tutorialexamples.py', ] [tool.coverage.report] @@ -112,6 +113,7 @@ exclude_lines = [ # buildtest methods that should not be coverred by regression tests 'buildspec_find', 'config_cmd', + 'report_cmd', 'build_history', ] ignore_errors = true diff --git a/tests/cli/test_report.py b/tests/cli/test_report.py index 4d241e487..49062e2f9 100644 --- a/tests/cli/test_report.py +++ b/tests/cli/test_report.py @@ -6,7 +6,13 @@ import pytest from rich.color import Color -from buildtest.cli.report import Report, report_cmd, report_summary +from buildtest.cli.report import ( + Report, + clear_report, + list_report, + report_cmd, + report_summary, +) from buildtest.config import SiteConfiguration from buildtest.defaults import BUILD_REPORT, BUILDTEST_REPORTS, BUILDTEST_ROOT from buildtest.exceptions import BuildTestError @@ -144,13 +150,13 @@ def test_report_oldest_and_latest(): @pytest.mark.cli def test_report_failure(): - # buildtest report --filter tags=tutorials --failure + # buildtest report --filter tags=tutorials --fail Report(configuration=configuration, filter_args={"tags": "tutorials"}, failure=True) @pytest.mark.cli def test_report_passed(): - # buildtest report --filter tags=tutorials --passed + # buildtest report --filter tags=tutorials --pass Report(configuration=configuration, filter_args={"tags": "tutorials"}, passed=True) @@ -272,20 +278,7 @@ def test_report_summary(): @pytest.mark.cli def test_report_list(): - class args: - helpformat = False - helpfilter = False - filter = None - format = None - oldest = False - latest = False - report_subcommand = "list" - terse = None - color = None - pager = None - - report_cmd(args, configuration=configuration) - + list_report() backupfile = tempfile.NamedTemporaryFile() shutil.copy2(BUILDTEST_REPORTS, backupfile.name) @@ -293,7 +286,7 @@ class args: os.remove(BUILDTEST_REPORTS) with pytest.raises(SystemExit): - report_cmd(args, configuration=configuration) + list_report() # move back the removed BUILDTEST_REPORTS file shutil.move(backupfile.name, BUILDTEST_REPORTS) @@ -302,30 +295,17 @@ class args: @pytest.mark.cli def test_report_clear(): - class args: - helpformat = False - helpfilter = False - filter = None - format = None - oldest = False - latest = False - report_subcommand = "clear" - terse = None - no_header = None - color = None - pager = None - backupfile_report = tempfile.NamedTemporaryFile() shutil.copy2(BUILD_REPORT, backupfile_report.name) backupfile_list_report = tempfile.NamedTemporaryFile() shutil.copy2(BUILDTEST_REPORTS, backupfile_list_report.name) - report_cmd(args, configuration=configuration) + clear_report() # buildtest report clear will raise an error since file doesn't exist with pytest.raises(SystemExit): - report_cmd(args, configuration=configuration) + clear_report() assert not is_file(BUILD_REPORT) # move back the backe-up files since report_cmd() function removes the files BUILD_REPORT and BUILDTEST_REPORTS