test_log.py 868 B

12345678910111213141516171819202122232425262728293031323334
  1. import io
  2. import re
  3. from contextlib import redirect_stdout
  4. import pytest
  5. from numpy.distutils import log
  6. def setup_module():
  7. f = io.StringIO() # changing verbosity also logs here, capture that
  8. with redirect_stdout(f):
  9. log.set_verbosity(2, force=True) # i.e. DEBUG
  10. def teardown_module():
  11. log.set_verbosity(0, force=True) # the default
  12. r_ansi = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])")
  13. @pytest.mark.parametrize("func_name", ["error", "warn", "info", "debug"])
  14. def test_log_prefix(func_name):
  15. func = getattr(log, func_name)
  16. msg = f"{func_name} message"
  17. f = io.StringIO()
  18. with redirect_stdout(f):
  19. func(msg)
  20. out = f.getvalue()
  21. assert out # sanity check
  22. clean_out = r_ansi.sub("", out)
  23. line = next(line for line in clean_out.splitlines())
  24. assert line == f"{func_name.upper()}: {msg}"