leapp/0009-Generate-empty-report-correctly.patch
Petr Stodulka 257b6fe19e CTC2 builds
- Bump leapp-framework to 4.0
- Improve the report summary output to make it more visible
- Fix processing data in remediation instructions with non-ascii characters
- Fix creation of Dialog for Component without choices
- Store tracebacks from actors in leapp.db
- Resolves: #2223312
2023-07-17 22:27:22 +02:00

52 lines
2.3 KiB
Diff

From 48b3ec6ab6dc8657facdb5482c0edb6a4872f850 Mon Sep 17 00:00:00 2001
From: Petr Stodulka <pstodulk@redhat.com>
Date: Fri, 5 May 2023 14:41:42 +0200
Subject: [PATCH 09/18] Generate empty report correctly
On Python2, the json.dumps library can produce either
unicode or str output. Nowadays, the unicode is required, however
in case the input for json.dumps function does not contain any
unicode objects (e.g. when no report entry exists), it produces
str type.
Relates: #821
---
leapp/utils/report.py | 5 +++++
tests/scripts/test_reporting.py | 6 ++++++
2 files changed, 11 insertions(+)
diff --git a/leapp/utils/report.py b/leapp/utils/report.py
index 71d47e3..8ffbcd3 100644
--- a/leapp/utils/report.py
+++ b/leapp/utils/report.py
@@ -165,4 +165,9 @@ def generate_report_file(messages_to_report, context, path, report_schema='1.1.0
# because of a py2 bug in the json module that can produce a mix of unicode and str objects that will be
# incompatible with write. https://bugs.python.org/issue13769
data = json.dumps({'entries': messages_to_report, 'leapp_run_id': context}, indent=2, ensure_ascii=False)
+ if isinstance(data, six.binary_type):
+ # Note: if messages_to_report is empty, the produced data is str instead of unicode
+ # at least one string must be unicode for the json.dumps function to produce actually
+ # unicode object.
+ data = data.decode('utf-8')
f.write(data)
diff --git a/tests/scripts/test_reporting.py b/tests/scripts/test_reporting.py
index 4a9a59b..2b0f245 100644
--- a/tests/scripts/test_reporting.py
+++ b/tests/scripts/test_reporting.py
@@ -118,6 +118,12 @@ def test_remediation_with_non_ascii_value(report_suffix):
generate_report_file([report_message], 'leapp-run-id', reportfile.name, '1.1.0')
+@pytest.mark.parametrize("report_suffix", ('.json', '.txt'))
+def test_generation_of_empty_report(report_suffix):
+ with tempfile.NamedTemporaryFile(suffix=report_suffix) as reportfile:
+ generate_report_file([], 'leapp-run-id', reportfile.name, '1.1.0')
+
+
def test_convert_from_error_to_report():
error_dict_no_details = {
'message': 'The system is not registered or subscribed.',
--
2.41.0