Skip to content

Commit

Permalink
Aggregate report: Add support for --output-file flag
Browse files Browse the repository at this point in the history
  • Loading branch information
mirekdlugosz committed Jun 6, 2024
1 parent 8aa39c9 commit b48cb6f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
29 changes: 27 additions & 2 deletions qpc/report/aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
from qpc.clicommand import CliCommand
from qpc.request import GET, request
from qpc.translation import _
from qpc.utils import pretty_format
from qpc.utils import (
pretty_format,
validate_write_file,
write_file,
)

logger = getLogger(__name__)

Expand Down Expand Up @@ -44,11 +48,24 @@ def __init__(self, subparsers):
metavar="REPORT_ID",
help=_(messages.REPORT_REPORT_ID_HELP),
)
self.parser.add_argument(
"--output-file",
dest="path",
metavar="PATH",
help=_(messages.REPORT_PATH_HELP),
)
self.report_id = None

def _validate_args(self):
CliCommand._validate_args(self)

try:
if (args_path := getattr(self.args, "path", None)) is not None:
validate_write_file(args_path, "output-file")
except ValueError as error:
logger.error(error)
sys.exit(1)

if not (report_id := self.args.report_id):
response = request(
parser=self.parser,
Expand Down Expand Up @@ -76,7 +93,15 @@ def _validate_args(self):
def _handle_response_success(self):
json_data = self.response.json()
data = pretty_format(json_data)
print(data)
try:
args_path = getattr(self.args, "path", None)
write_file(args_path, data)
logger.info(_(messages.REPORT_SUCCESSFULLY_WRITTEN))
except EnvironmentError as err:
logger.error(
_(messages.WRITE_FILE_ERROR), {"path": self.args.path, "error": err}
)
sys.exit(1)

def _handle_response_error(self):
if self.args.report_id is None:
Expand Down
49 changes: 49 additions & 0 deletions qpc/tests/report/test_report_aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,52 @@ def test_aggregate_report_but_report_does_not_exist(faker, caplog):
messages.REPORT_NO_AGGREGATE_REPORT_FOR_REPORT_ID % unknown_report_id
)
assert expected_error in caplog.text


def test_aggregate_report_output_directory(caplog):
"""Testing fail because output directory."""
args = Namespace(path="/")
with pytest.raises(SystemExit):
get_command().main(args)
expected_error = messages.REPORT_OUTPUT_IS_A_DIRECTORY % ("output-file", "/")
assert expected_error in caplog.text


def test_aggregate_report_output_directory_not_exist(caplog):
"""Testing fail because output directory does not exist."""
args = Namespace(path="/foo/bar")
with pytest.raises(SystemExit):
get_command().main(args)
expected_error = messages.REPORT_DIRECTORY_DOES_NOT_EXIST % "/foo"
assert expected_error in caplog.text


def test_aggregate_report_output_file_empty(faker, caplog):
"""Testing fail because output file empty."""
report_id = faker.pyint()
report_uri = get_aggregate_report_uri(report_id)
report_json_data = {faker.slug(): faker.slug()}
with requests_mock.Mocker() as mocker:
mocker.get(report_uri, status_code=200, json=report_json_data)
args = Namespace(path="", report_id=report_id)
with pytest.raises(SystemExit):
get_command().main(args)
expected_error = messages.REPORT_OUTPUT_IS_A_DIRECTORY % ("output-file", ".")
assert expected_error in caplog.text


@patch("qpc.report.aggregate.write_file")
def test_aggregate_file_fails_to_write(file, faker, caplog):
"""Testing deployments failure while writing to file."""
file.side_effect = EnvironmentError()
output_file = "./foobar.json"
report_id = faker.pyint()
report_uri = get_aggregate_report_uri(report_id)
report_json_data = {faker.slug(): faker.slug()}
with requests_mock.Mocker() as mocker:
mocker.get(report_uri, status_code=200, json=report_json_data)
args = Namespace(path=output_file, report_id=report_id)
with pytest.raises(SystemExit):
get_command().main(args)
expected_error = messages.WRITE_FILE_ERROR % {"path": output_file, "error": ""}
assert expected_error in caplog.text

0 comments on commit b48cb6f

Please sign in to comment.