Skip to content

Commit

Permalink
envoy.base.checker: Implement no-error-on-warn (#150)
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Northey <ryan@synca.io>
  • Loading branch information
phlax authored Jan 4, 2022
1 parent e6e2f7a commit 2e7807b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ pypi: https://pypi.org/project/envoy.abstract.command

#### [envoy.base.checker](envoy.base.checker)

version: 0.0.3.dev0
version: 0.0.3

pypi: https://pypi.org/project/envoy.base.checker

Expand Down
2 changes: 1 addition & 1 deletion envoy.base.checker/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.3-dev
0.0.3
10 changes: 8 additions & 2 deletions envoy.base.checker/envoy/base/checker/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ def error_count(self) -> int:
def exiting(self):
return "exiting" in self.errors

@property
def fail_on_warn(self) -> bool:
"""Return failure when warnings are generated."""
return self.args.warning == "error"

@property
def failed(self) -> dict:
"""Dictionary of errors per check."""
Expand All @@ -56,8 +61,9 @@ def fix(self) -> bool:
@property
def has_failed(self) -> bool:
"""Shows whether there are any failures."""
# add logic for warn/error
return bool(self.failed or self.warned)
return bool(
self.failed
or (self.warned and self.fail_on_warn))

@cached_property
def path(self) -> pathlib.Path:
Expand Down
24 changes: 21 additions & 3 deletions envoy.base.checker/tests/test_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ def test_checker_error_count():
assert "error_count" not in checker.__dict__


@pytest.mark.parametrize("warning", [True, False, "cabbage", "error"])
def test_checker_fail_on_warn(patches, warning):
checker = Checker("path1", "path2", "path3")
patched = patches(
("Checker.args", dict(new_callable=PropertyMock)),
prefix="envoy.base.checker.checker")

with patched as (m_args, ):
m_args.return_value.warning = warning
assert (
checker.fail_on_warn
== (warning == "error"))
assert "fail_on_warn" not in checker.__dict__


def test_checker_failed():
checker = Checker("path1", "path2", "path3")
checker.errors = dict(foo=["err"] * 3, bar=["err"] * 5, baz=["err"] * 7)
Expand All @@ -97,19 +112,22 @@ def test_checker_fix():

@pytest.mark.parametrize("failed", [True, False])
@pytest.mark.parametrize("warned", [True, False])
def test_checker_has_failed(patches, failed, warned):
@pytest.mark.parametrize("fail_on_warn", [True, False])
def test_checker_has_failed(patches, failed, warned, fail_on_warn):
checker = Checker("path1", "path2", "path3")
patched = patches(
("Checker.fail_on_warn", dict(new_callable=PropertyMock)),
("Checker.failed", dict(new_callable=PropertyMock)),
("Checker.warned", dict(new_callable=PropertyMock)),
prefix="envoy.base.checker.checker")

with patched as (m_failed, m_warned):
with patched as (m_fail_warn, m_failed, m_warned):
m_fail_warn.return_value = fail_on_warn
m_failed.return_value = failed
m_warned.return_value = warned
result = checker.has_failed

if failed or warned:
if failed or (warned and fail_on_warn):
assert result is True
else:
assert result is False
Expand Down

0 comments on commit 2e7807b

Please sign in to comment.