adding TRANSFORMERS_VERBOSITY env var (#6961)

* introduce TRANSFORMERS_VERBOSITY env var + test + test helpers

* cleanup

* remove helper function
This commit is contained in:
Stas Bekman
2020-09-09 01:08:01 -07:00
committed by GitHub
parent f0fc0aea6b
commit d0963486c1
4 changed files with 163 additions and 4 deletions

View File

@@ -1,4 +1,5 @@
import inspect
import logging
import os
import re
import shutil
@@ -270,6 +271,46 @@ class CaptureStderr(CaptureStd):
super().__init__(out=False)
class CaptureLogger:
"""Context manager to capture `logging` streams
Args:
- logger: 'logging` logger object
Results:
The captured output is available via `self.out`
Example:
from transformers import logging
from transformers.testing_utils import CaptureLogger
msg = "Testing 1, 2, 3"
logging.set_verbosity_info()
logger = logging.get_logger("transformers.tokenization_bart")
with CaptureLogger(logger) as cl:
logger.info(msg)
assert cl.out, msg+"\n"
"""
def __init__(self, logger):
self.logger = logger
self.io = StringIO()
self.sh = logging.StreamHandler(self.io)
self.out = ""
def __enter__(self):
self.logger.addHandler(self.sh)
return self
def __exit__(self, *exc):
self.logger.removeHandler(self.sh)
self.out = self.io.getvalue()
def __repr__(self):
return f"captured: {self.out}\n"
class TestCasePlus(unittest.TestCase):
"""This class extends `unittest.TestCase` with additional features.
@@ -357,3 +398,14 @@ class TestCasePlus(unittest.TestCase):
for path in self.teardown_tmp_dirs:
shutil.rmtree(path, ignore_errors=True)
self.teardown_tmp_dirs = []
def mockenv(**kwargs):
"""this is a convenience wrapper, that allows this:
@mockenv(USE_CUDA=True, USE_TF=False)
def test_something():
use_cuda = os.getenv("USE_CUDA", False)
use_tf = os.getenv("USE_TF", False)
"""
return unittest.mock.patch.dict(os.environ, kwargs)