Skip to content

Commit

Permalink
Implement limit on output errors
Browse files Browse the repository at this point in the history
Ref. #748
  • Loading branch information
Alexander Senier committed Aug 24, 2021
1 parent b35483c commit dc1406e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
15 changes: 12 additions & 3 deletions rflx/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from pkg_resources import get_distribution

from rflx import __version__
from rflx.error import RecordFluxError, Severity, Subsystem, fail
from rflx.error import FAIL_AFTER_VALUE, RecordFluxError, Severity, Subsystem, fail
from rflx.generator import Generator
from rflx.graph import Graph
from rflx.model import Message, Model, Session
Expand All @@ -28,6 +28,13 @@ def main(argv: List[str]) -> Union[int, str]:
"-q", "--quiet", action="store_true", help="disable logging to standard output"
)
parser.add_argument("--version", action="store_true")
parser.add_argument(
"--max-errors",
type=int,
default=0,
metavar=("NUM"),
help="exit after at most NUM errors",
)

subparsers = parser.add_subparsers(dest="subcommand")

Expand All @@ -42,8 +49,8 @@ def main(argv: List[str]) -> Union[int, str]:
"-p",
"--prefix",
type=str,
default="RFLX",
help=("add prefix to generated packages " f"(default: {DEFAULT_PREFIX})"),
default=DEFAULT_PREFIX,
help="add prefix to generated packages (default: %(default)s)",
)
parser_generate.add_argument(
"-n", "--no-library", help="omit generating library files", action="store_true"
Expand Down Expand Up @@ -99,6 +106,8 @@ def main(argv: List[str]) -> Union[int, str]:
if args.quiet:
logging.disable(logging.CRITICAL)

FAIL_AFTER_VALUE.v = args.max_errors

try:
args.func(args)
except RecordFluxError as e:
Expand Down
15 changes: 15 additions & 0 deletions rflx/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
from collections import deque
from enum import Enum, auto
from pathlib import Path
from threading import local
from typing import Deque, List, NoReturn, Optional, Tuple, TypeVar, Union

from rflx.common import Base, verbose_repr

FAIL_AFTER_VALUE = local()
FAIL_AFTER_VALUE.v = 0


class Location(Base):
def __init__(
Expand Down Expand Up @@ -138,21 +142,32 @@ def append(
self, message: str, subsystem: Subsystem, severity: Severity, location: Location = None
) -> None:
self.__errors.append(BaseError.Entry(message, subsystem, severity, location))
if get_fail_after() > 0 and len(self.__errors) >= get_fail_after():
raise self

def appendleft(
self, message: str, subsystem: Subsystem, severity: Severity, location: Location = None
) -> None:
self.__errors.appendleft(BaseError.Entry(message, subsystem, severity, location))
if get_fail_after() > 0 and len(self.__errors) >= get_fail_after():
raise self

def extend(
self,
entries: Union[List[Tuple[str, Subsystem, Severity, Optional[Location]]], "BaseError"],
) -> None:
# pylint: disable = global-statement
global FAIL_AFTER_VALUE
if isinstance(entries, BaseError):
self.__errors.extend(entries.errors)
else:
for message, subsystem, severity, location in entries:
self.__errors.append(BaseError.Entry(message, subsystem, severity, location))
num_errors = len(
list(e for e in self.__errors if e.severity in (Severity.WARNING, Severity.ERROR))
)
if 0 < FAIL_AFTER_VALUE.v <= num_errors:
raise self

def check(self) -> bool:
return len(self.__errors) > 0
Expand Down
12 changes: 12 additions & 0 deletions tests/data/specs/multiple_errors.rflx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package Multiple_Errors is
type INVALID is mod 2**8;
type INVALID is mod 2**8;
type INVALID is mod 2**8;
type INVALID is mod 2**8;
type INVALID is mod 2**8;
type INVALID is mod 2**8;
type INVALID is mod 2**8;
type INVALID is mod 2**8;
type INVALID is mod 2**8;
type INVALID is mod 2**8;
end Multiple_Errors;
19 changes: 19 additions & 0 deletions tests/unit/cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,22 @@ def test_main_unexpected_exception(monkeypatch: Any, tmp_path: Path) -> None:
str(cli.main(["rflx", "generate", "-d", str(tmp_path), SPEC_FILE])),
re.DOTALL,
)


def test_fail_fast() -> None:
assert (
len(
str(
cli.main(
[
"rflx",
"--max-errors",
"5",
"check",
str(SPEC_DIR / "multiple_errors.rflx"),
]
)
).split("\n")
)
== 10
)

0 comments on commit dc1406e

Please sign in to comment.