From bb514e18af2fbab811f967982351a58049c4207d Mon Sep 17 00:00:00 2001 From: Nicola Coretti Date: Thu, 6 Jun 2024 12:52:25 +0200 Subject: [PATCH] Fix support for single mitigation (#30) --- exasol/error/_parse.py | 9 +++++-- test/unit/regression_test.py | 46 ++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 test/unit/regression_test.py diff --git a/exasol/error/_parse.py b/exasol/error/_parse.py index 7d20086..2cec875 100644 --- a/exasol/error/_parse.py +++ b/exasol/error/_parse.py @@ -197,7 +197,11 @@ def normalize(params): description=None, internalDescription=None, potentialCauses=None, - mitigations=[m.value for m in mitigations.elts] + mitigations=( + [m.value for m in mitigations.elts] + if isinstance(mitigations, ast.List) + else [mitigations.value] + ) if not isinstance(mitigations, str) else [mitigations], sourceFile=self._filename, @@ -229,7 +233,8 @@ def parse_file( else stack.enter_context(open(file, "r")) ) root_node = ast.parse(f.read()) - collector = ErrorCollector(root_node, f.name) + name = f.name if hasattr(f, 'name') else f'<{f.__class__.__name__}>' + collector = ErrorCollector(root_node, name) collector.collect() return collector.error_definitions, collector.warnings, collector.errors diff --git a/test/unit/regression_test.py b/test/unit/regression_test.py new file mode 100644 index 0000000..cd69f92 --- /dev/null +++ b/test/unit/regression_test.py @@ -0,0 +1,46 @@ +import io +from inspect import cleandoc + +from exasol.error._parse import parse_file +from exasol.error._report import ErrorCodeDetails + + +# regression test for GitHub Issue #26 +def test_single_mitigation(): + f = io.StringIO(initial_value=cleandoc( + """ + from exasol import error + from exasol.error import Parameter + + var = input("description: ") + + error1 = error.ExaError( + "E-TEST-1", + "this is an error", + "no mitigation available", + {}, + ) + """ + )) + + expected_definitions = [ + ErrorCodeDetails( + identifier='E-TEST-1', + message='this is an error', + messagePlaceholders=[], + description=None, + internalDescription=None, + potentialCauses=None, + mitigations=['no mitigation available'], + sourceFile='', + sourceLine=6, + contextHash=None), + ] + expected_warnings = [] + expected_errors = [] + + definitions, warnings, errors = parse_file(f) + + assert expected_definitions == definitions + assert expected_warnings == warnings + assert expected_errors == errors