Repository: enowars/enochecker

Issue:
Do not return OK when previous putflag/putnoise wasn't successful
See https://github.com/enowars/enochecker/blob/master/src/enochecker/enochecker.py#L416


Comments:

Patch:
diff --git a/src/enochecker/enochecker.py b/src/enochecker/enochecker.py
index 831cb5b..fd98452 100644
--- a/src/enochecker/enochecker.py
+++ b/src/enochecker/enochecker.py
@@ -395,9 +395,6 @@ class BaseChecker(metaclass=_CheckerMeta):
             1,
         )
 
-    # def __format_internal_db_entry(name):
-    #     return f"__Checker-Internals:{name}__"
-
     # ---- Basic checker functionality ---- #
 
     def _run_method(self, method: Optional[str] = None) -> Optional[Result]:
@@ -413,22 +410,6 @@ class BaseChecker(metaclass=_CheckerMeta):
                 "Method {} not supported! Supported: {}".format(method, CHECKER_METHODS)
             )
 
-        # handle the cases where the original putflag/putnoise wasn't successful
-        if method == "getflag":
-            key = f"__Checker-internals-RESULT:putflag,{self.flag_round},{self.flag_idx}__"
-            if key not in self.team_db or self.team_db[key] != "OK":
-                self.info(
-                    f"original putflag did not return successfully -- ignoring getflag for flag_round:{self.flag_round}, index: {self.flag_idx}"
-                )
-                return Result.OK
-        elif method == "getnoise":
-            key = f"__Checker-internals-RESULT:putnoise,{self.flag_round},{self.flag_idx}__"
-            if key not in self.team_db or self.team_db[key] != "OK":
-                self.info(
-                    f"original putnoise did not return successfully -- ignoring getnoise for flag_round:{self.flag_round}, index: {self.flag_idx}"
-                )
-                return Result.OK
-
         return getattr(self, snake_caseify(method))()
 
     def run(self, method: Optional[str] = None) -> CheckerResult:
@@ -455,17 +436,16 @@ class BaseChecker(metaclass=_CheckerMeta):
 
                 # Better wrap this, in case somebody returns raw ints (?)
                 ret = Result(ret)
+                if ret != Result.OK:
+                    warnings.warn(
+                        "Returning a non-ok status code is not recommended and will be removed in the future. Raise an Exception with additional text instead.",
+                        DeprecationWarning,
+                    )
                 self.info("Checker [{}] resulted in {}".format(self.method, ret.name))
-                self.team_db[
-                    f"__Checker-internals-RESULT:{str(method)},{self.flag_round},{self.flag_idx}__"
-                ] = ret.name
                 return CheckerResult(ret)
 
             # Returned Normally
             self.info("Checker [{}] executed successfully!".format(self.method))
-            self.team_db[
-                f"__Checker-internals-RESULT:{str(method)},{self.flag_round},{self.flag_idx}__"
-            ] = "OK"
             return CheckerResult(Result.OK)
 
         except EnoException as eno:


Test patch:
diff --git a/tests/test_run_method.py b/tests/test_run_method.py
index d059e30..047b3df 100644
--- a/tests/test_run_method.py
+++ b/tests/test_run_method.py
@@ -87,54 +87,21 @@ def test_run_return_status(method, result, checker_cls):
     def meth(self):
         return result
 
-    def ok(self):
-        return Result.OK
-
-    if method == "getflag":
-        setattr(checker_cls, "putflag", ok)
-        c = checker_cls("putflag")
-        c.run()
-    elif method == "getnoise":
-        setattr(checker_cls, "putnoise", ok)
-        c = checker_cls("putnoise")
-        c.run()
-
     setattr(checker_cls, method, meth)
     c = checker_cls(method)
-    res = c.run()
+    with pytest.warns(
+        None
+    ):  # ignore warnings caused by the deprecation of returning Results
+        res = c.run()
     assert isinstance(res, CheckerResult)
     assert res.result == result
 
 
-@pytest.mark.parametrize("method", ["getflag", "getnoise"])
-def test_run_get_original_run_failed(method, checker_cls):
-    def meth(self):
-        assert False  # this function should never be called
-
-    setattr(checker_cls, method, meth)
-    c = checker_cls(method)
-    res = c.run()
-    assert isinstance(res, CheckerResult)
-    assert res.result == Result.OK
-
-
 @pytest.mark.parametrize("method", CHECKER_METHODS)
 def test_raise_broken_service_exception(method, checker_cls):
     def meth(self):
         raise BrokenServiceException("msg123")
 
-    def ok(self):
-        return Result.OK
-
-    if method == "getflag":
-        setattr(checker_cls, "putflag", ok)
-        c = checker_cls("putflag")
-        c.run()
-    elif method == "getnoise":
-        setattr(checker_cls, "putnoise", ok)
-        c = checker_cls("putnoise")
-        c.run()
-
     setattr(checker_cls, method, meth)
     c = checker_cls(method)
     res = c.run()
@@ -148,18 +115,6 @@ def test_raise_offline_exception(method, checker_cls):
     def meth(self):
         raise OfflineException("msg123")
 
-    def ok(self):
-        return Result.OK
-
-    if method == "getflag":
-        setattr(checker_cls, "putflag", ok)
-        c = checker_cls("putflag")
-        c.run()
-    elif method == "getnoise":
-        setattr(checker_cls, "putnoise", ok)
-        c = checker_cls("putnoise")
-        c.run()
-
     setattr(checker_cls, method, meth)
     c = checker_cls(method)
     res = c.run()
@@ -173,18 +128,6 @@ def test_raise_unhandled_exception(method, checker_cls):
     def meth(self):
         raise Exception("msg123")
 
-    def ok(self):
-        return Result.OK
-
-    if method == "getflag":
-        setattr(checker_cls, "putflag", ok)
-        c = checker_cls("putflag")
-        c.run()
-    elif method == "getnoise":
-        setattr(checker_cls, "putnoise", ok)
-        c = checker_cls("putnoise")
-        c.run()
-
     setattr(checker_cls, method, meth)
     c = checker_cls(method)
     res = c.run()
@@ -200,18 +143,6 @@ def test_invalid_return(method, checker_cls):
     def meth(self):
         return "lolthisisinvalid"
 
-    def ok(self):
-        return Result.OK
-
-    if method == "getflag":
-        setattr(checker_cls, "putflag", ok)
-        c = checker_cls("putflag")
-        c.run()
-    elif method == "getnoise":
-        setattr(checker_cls, "putnoise", ok)
-        c = checker_cls("putnoise")
-        c.run()
-
     setattr(checker_cls, method, meth)
     c = checker_cls(method)
     res = c.run()
@@ -236,18 +167,6 @@ def test_requests_mumble(method, exc, checker_cls):
     def meth(self):
         raise exc()
 
-    def ok(self):
-        return Result.OK
-
-    if method == "getflag":
-        setattr(checker_cls, "putflag", ok)
-        c = checker_cls("putflag")
-        c.run()
-    elif method == "getnoise":
-        setattr(checker_cls, "putnoise", ok)
-        c = checker_cls("putnoise")
-        c.run()
-
     setattr(checker_cls, method, meth)
     c = checker_cls(method)
     res = c.run()
@@ -275,6 +194,21 @@ def test_offline_exceptions(method, exc, checker_cls):
     def meth(self):
         raise exc()
 
+    setattr(checker_cls, method, meth)
+    c = checker_cls(method)
+    res = c.run()
+    assert isinstance(res, CheckerResult)
+    assert res.result == Result.OFFLINE
+    assert res.message
+
+
+@pytest.mark.parametrize(
+    "method", CHECKER_METHODS,
+)
+def test_no_warn_deprecated_return_ok(method, checker_cls):
+    def meth(self):
+        return Result.OK
+
     def ok(self):
         return Result.OK
 
@@ -289,7 +223,36 @@ def test_offline_exceptions(method, exc, checker_cls):
 
     setattr(checker_cls, method, meth)
     c = checker_cls(method)
-    res = c.run()
+    with pytest.warns(None) as record:
+        res = c.run()
     assert isinstance(res, CheckerResult)
-    assert res.result == Result.OFFLINE
-    assert res.message
+    assert res.result == Result.OK
+    assert len(record) == 0
+
+
+@pytest.mark.parametrize(
+    "method, ret",
+    product(CHECKER_METHODS, [Result.MUMBLE, Result.OFFLINE, Result.INTERNAL_ERROR]),
+)
+def test_warn_deprecated_return_value(method, ret, checker_cls):
+    def meth(self):
+        return ret
+
+    def ok(self):
+        return Result.OK
+
+    if method == "getflag":
+        setattr(checker_cls, "putflag", ok)
+        c = checker_cls("putflag")
+        c.run()
+    elif method == "getnoise":
+        setattr(checker_cls, "putnoise", ok)
+        c = checker_cls("putnoise")
+        c.run()
+
+    setattr(checker_cls, method, meth)
+    c = checker_cls(method)
+    with pytest.deprecated_call():
+        res = c.run()
+    assert isinstance(res, CheckerResult)
+    assert res.result == ret