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

Aggregate report: Add support for --output-file flag #341

Merged
merged 1 commit into from
Jun 6, 2024
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
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:
mirekdlugosz marked this conversation as resolved.
Show resolved Hide resolved
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
Loading