From ac47760d8c53895f8d35388b21b2b179d86ef136 Mon Sep 17 00:00:00 2001 From: Alexander Senier Date: Wed, 5 Jan 2022 16:39:26 +0100 Subject: [PATCH] Make invalid identifiers a fatal error Ref. #892 --- rflx/cli.py | 2 +- rflx/identifier.py | 22 ++++++++-------------- tests/unit/expression_test.py | 4 ++-- tests/unit/generator_test.py | 2 +- tests/unit/identifier_test.py | 22 +++++++++------------- tests/unit/pyrflx_test.py | 6 +++--- 6 files changed, 24 insertions(+), 34 deletions(-) diff --git a/rflx/cli.py b/rflx/cli.py index 75e87eb493..72e65e50ec 100644 --- a/rflx/cli.py +++ b/rflx/cli.py @@ -347,7 +347,7 @@ def validate(args: argparse.Namespace) -> None: try: identifier = ID(args.message_identifier) - except RecordFluxError as e: + except FatalError as e: fail(f"invalid identifier: {e}", Subsystem.CLI) try: diff --git a/rflx/identifier.py b/rflx/identifier.py index 56b0b7838e..c203e73414 100644 --- a/rflx/identifier.py +++ b/rflx/identifier.py @@ -1,7 +1,7 @@ import re from typing import Optional, Sequence, TypeVar, Union -from rflx.error import Location, RecordFluxError, Severity, Subsystem +from rflx.error import Location, RecordFluxError, Severity, Subsystem, fatal_fail Self = TypeVar("Self", bound="ID") @@ -25,23 +25,17 @@ def __init__( error = RecordFluxError() if not self._parts: - error.extend([("empty identifier", Subsystem.ID, Severity.ERROR, location)]) + fatal_fail("empty identifier", Subsystem.ID, Severity.ERROR, location) elif "" in self._parts: - error.extend( - [(f'empty part in identifier "{self}"', Subsystem.ID, Severity.ERROR, location)] - ) + fatal_fail(f'empty part in identifier "{self}"', Subsystem.ID, Severity.ERROR, location) else: for c in [" ", ".", ":"]: if any(c in part for part in self._parts): - error.extend( - [ - ( - f'"{c}" in identifier parts of "{self}"', - Subsystem.ID, - Severity.ERROR, - location, - ) - ], + fatal_fail( + f'"{c}" in identifier parts of "{self}"', + Subsystem.ID, + Severity.ERROR, + location, ) error.propagate() diff --git a/tests/unit/expression_test.py b/tests/unit/expression_test.py index f55d083df5..cf53213698 100644 --- a/tests/unit/expression_test.py +++ b/tests/unit/expression_test.py @@ -6,7 +6,7 @@ import z3 from rflx import ada, typing_ as rty -from rflx.error import Location, RecordFluxError +from rflx.error import FatalError, Location, RecordFluxError from rflx.expression import ( FALSE, TRUE, @@ -719,7 +719,7 @@ def test_term_simplified() -> None: def test_variable_invalid_name() -> None: - with pytest.raises(RecordFluxError): + with pytest.raises(FatalError): Variable("Foo (Bar)") diff --git a/tests/unit/generator_test.py b/tests/unit/generator_test.py index 0468fac54a..642a6ba82f 100644 --- a/tests/unit/generator_test.py +++ b/tests/unit/generator_test.py @@ -68,7 +68,7 @@ def generate(model: Model) -> Generator: def test_invalid_prefix() -> None: - with pytest.raises(RecordFluxError, match=r'^id: error: empty part in identifier "A..B"$'): + with pytest.raises(FatalError, match=r'^id: error: empty part in identifier "A..B"$'): Generator(Model(), Integration(), "A..B") diff --git a/tests/unit/identifier_test.py b/tests/unit/identifier_test.py index 75d2726079..4fb2af0fd0 100644 --- a/tests/unit/identifier_test.py +++ b/tests/unit/identifier_test.py @@ -1,6 +1,6 @@ import pytest -from rflx.error import RecordFluxError +from rflx.error import FatalError from rflx.identifier import ID @@ -20,41 +20,37 @@ def test_id_invalid_type() -> None: def test_id_invalid_empty() -> None: - with pytest.raises(RecordFluxError, match=r"^id: error: empty identifier$"): + with pytest.raises(FatalError, match=r"^id: error: empty identifier$"): ID([]) def test_id_invalid_empty_string() -> None: - with pytest.raises(RecordFluxError, match=r'^id: error: empty part in identifier ""$'): + with pytest.raises(FatalError, match=r'^id: error: empty part in identifier ""$'): ID("") def test_id_invalid_empty_part() -> None: - with pytest.raises(RecordFluxError, match=r'^id: error: empty part in identifier "A::::B"$'): + with pytest.raises(FatalError, match=r'^id: error: empty part in identifier "A::::B"$'): ID("A::::B") def test_id_invalid_empty_first_part() -> None: - with pytest.raises(RecordFluxError, match=r'^id: error: empty part in identifier "::A::B"$'): + with pytest.raises(FatalError, match=r'^id: error: empty part in identifier "::A::B"$'): ID("::A::B") def test_id_invalid_empty_last_part() -> None: - with pytest.raises(RecordFluxError, match=r'^id: error: empty part in identifier "A::B::"$'): + with pytest.raises(FatalError, match=r'^id: error: empty part in identifier "A::B::"$'): ID("A::B::") def test_id_invalid_whitespace() -> None: - with pytest.raises( - RecordFluxError, match=r'^id: error: " " in identifier parts of "A::B C::D"$' - ): + with pytest.raises(FatalError, match=r'^id: error: " " in identifier parts of "A::B C::D"$'): ID("A::B C::D") def test_id_invalid_colon() -> None: - with pytest.raises( - RecordFluxError, match=r'^id: error: ":" in identifier parts of "A::B:C::D"$' - ): + with pytest.raises(FatalError, match=r'^id: error: ":" in identifier parts of "A::B:C::D"$'): ID("A::B:C::D") @@ -107,7 +103,7 @@ def test_id_parent() -> None: def test_id_parent_error() -> None: - with pytest.raises(RecordFluxError, match=r"^id: error: empty identifier$"): + with pytest.raises(FatalError, match=r"^id: error: empty identifier$"): ID("A").parent # pylint: disable=expression-not-assigned diff --git a/tests/unit/pyrflx_test.py b/tests/unit/pyrflx_test.py index 94bd11e701..e929bc112a 100644 --- a/tests/unit/pyrflx_test.py +++ b/tests/unit/pyrflx_test.py @@ -5,6 +5,7 @@ import pytest from rflx import expression as expr +from rflx.error import FatalError from rflx.identifier import ID from rflx.model import ( BOOLEAN, @@ -1215,9 +1216,8 @@ def test_set_checksum_to_pyrflx_invalid_id( pyrflx_checksum: PyRFLX, ) -> None: with pytest.raises( - PyRFLXError, - match='^pyrflx: error: "Refinement_With_Checksum:Message" is not a valid identifier:' - ' id: error: ":" in identifier parts of "Refinement_With_Checksum:Message"$', + FatalError, + match='^id: error: ":" in identifier parts of "Refinement_With_Checksum:Message"$', ): pyrflx_checksum.set_checksum_functions( {