Skip to content

Commit

Permalink
Make invalid identifiers a fatal error
Browse files Browse the repository at this point in the history
Ref. #892
  • Loading branch information
Alexander Senier committed Jan 6, 2022
1 parent 8dc0e4f commit a873cac
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 33 deletions.
2 changes: 1 addition & 1 deletion rflx/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
22 changes: 8 additions & 14 deletions rflx/identifier.py
Original file line number Diff line number Diff line change
@@ -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")

Expand All @@ -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()

Expand Down
4 changes: 2 additions & 2 deletions rflx/pyrflx/pyrflx.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from pathlib import Path
from typing import Dict, Iterable, Iterator, Union

from rflx.error import RecordFluxError
from rflx.error import FatalError, RecordFluxError
from rflx.identifier import ID, StrID
from rflx.model import Model
from rflx.pyrflx.typevalue import MessageValue, RefinementValue
Expand Down Expand Up @@ -61,7 +61,7 @@ def set_checksum_functions(self, functions: Dict[StrID, Dict[str, ChecksumFuncti
identifier_str = str(identifier_str)
try:
message_identifier = ID(identifier_str)
except RecordFluxError as e:
except FatalError as e:
raise PyRFLXError(f'"{identifier_str}" is not a valid identifier: {e}') from e

if len(message_identifier.parts) < 2:
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/expression_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)")


Expand Down
2 changes: 1 addition & 1 deletion tests/unit/generator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")


Expand Down
22 changes: 9 additions & 13 deletions tests/unit/identifier_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from rflx.error import RecordFluxError
from rflx.error import FatalError
from rflx.identifier import ID


Expand All @@ -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")


Expand Down Expand Up @@ -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


Expand Down

0 comments on commit a873cac

Please sign in to comment.