From 5f16bf411c0ac64c10c5a01f618df58139b205e2 Mon Sep 17 00:00:00 2001 From: Kenta Murata Date: Fri, 17 May 2024 12:41:28 +0900 Subject: [PATCH] Make FutureWarning error --- test/helper.rb | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/test/helper.rb b/test/helper.rb index c20b58f8..1cccc4e5 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -40,7 +40,67 @@ end module Charty + module PythonTestHelpers + def define_python_warning_check + PyCall.exec(< None: + super().__init__(io.BytesIO(), encoding="UTF-8", newline="", write_through=True) + + def getvalue(self) -> str: + assert isinstance(self.buffer, io.BytesIO) + return self.buffer.getvalue().decode("UTF-8") + +class WarningCheckIO(CaptureIO): + def __init__(self, pattern: str) -> None: + super().__init__() + self.pattern = pattern + + def write(self, s: str) -> int: + assert re.search(self.pattern, s) is None + return super().write(s) + +@contextlib.contextmanager +def warning_check(pattern: str): + old = sys.stderr + setattr(sys, "stderr", WarningCheckIO(pattern)) + try: + yield + finally: + setattr(sys, "stderr", old) +PYTHON + end + + def python_warning_check(pattern) + f = PyCall.eval("warning_check") + f.(pattern) + rescue PyCall::PyError + define_python_warning_check + python_warning_check(pattern) + end + + def check_python_warning(pattern, &block) + PyCall.with(python_warning_check(pattern), &block) + end + end + module TestHelpers + include PythonTestHelpers + + def setup + super + if pandas_available? + check_python_warning("FutureWarning") do + yield + end + end + end + module_function def arrow_available? defined?(::Arrow::Table) and Arrow::Version::MAJOR >= 6 end