Skip to content

Commit

Permalink
Merge pull request #1561 from buildtesters/refactor_regression_test_f…
Browse files Browse the repository at this point in the history
…or_buildtest_report

Refactor regression test for buildtest report and exclude coverage for some files
  • Loading branch information
shahzebsiddiqui authored Jul 17, 2023
2 parents bec8564 + 6556bb8 commit 83ef133
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 56 deletions.
56 changes: 33 additions & 23 deletions buildtest/cli/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,39 +771,49 @@ 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"""

consoleColor = checkColor(args.color)
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(
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ omit = [
'buildtest/cli/cd.py',
'buildtest/tools/docs.py',
'buildtest/tools/unittests.py',
'buildtest/tools/tutorialexamples.py',
]

[tool.coverage.report]
Expand All @@ -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
Expand Down
46 changes: 13 additions & 33 deletions tests/cli/test_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)


Expand Down Expand Up @@ -272,28 +278,15 @@ 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)

# now removing report summary it should print a message
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)
Expand All @@ -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
Expand Down

0 comments on commit 83ef133

Please sign in to comment.