test_tracer.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. from typing import List
  2. import pytest
  3. from pluggy._tracing import TagTracer
  4. @pytest.fixture
  5. def rootlogger() -> TagTracer:
  6. return TagTracer()
  7. def test_simple(rootlogger: TagTracer) -> None:
  8. log = rootlogger.get("pytest")
  9. log("hello")
  10. out: List[str] = []
  11. rootlogger.setwriter(out.append)
  12. log("world")
  13. assert len(out) == 1
  14. assert out[0] == "world [pytest]\n"
  15. sublog = log.get("collection")
  16. sublog("hello")
  17. assert out[1] == "hello [pytest:collection]\n"
  18. def test_indent(rootlogger: TagTracer) -> None:
  19. log = rootlogger.get("1")
  20. out = []
  21. log.root.setwriter(lambda arg: out.append(arg))
  22. log("hello")
  23. log.root.indent += 1
  24. log("line1")
  25. log("line2")
  26. log.root.indent += 1
  27. log("line3")
  28. log("line4")
  29. log.root.indent -= 1
  30. log("line5")
  31. log.root.indent -= 1
  32. log("last")
  33. assert len(out) == 7
  34. names = [x[: x.rfind(" [")] for x in out]
  35. assert names == [
  36. "hello",
  37. " line1",
  38. " line2",
  39. " line3",
  40. " line4",
  41. " line5",
  42. "last",
  43. ]
  44. def test_readable_output_dictargs(rootlogger: TagTracer) -> None:
  45. out = rootlogger._format_message(["test"], [1])
  46. assert out == "1 [test]\n"
  47. out2 = rootlogger._format_message(["test"], ["test", {"a": 1}])
  48. assert out2 == "test [test]\n a: 1\n"
  49. def test_setprocessor(rootlogger: TagTracer) -> None:
  50. log = rootlogger.get("1")
  51. log2 = log.get("2")
  52. assert log2.tags == tuple("12")
  53. out = []
  54. rootlogger.setprocessor(tuple("12"), lambda *args: out.append(args))
  55. log("not seen")
  56. log2("seen")
  57. assert len(out) == 1
  58. tags, args = out[0]
  59. assert "1" in tags
  60. assert "2" in tags
  61. assert args == ("seen",)
  62. l2 = []
  63. rootlogger.setprocessor("1:2", lambda *args: l2.append(args))
  64. log2("seen")
  65. tags, args = l2[0]
  66. assert args == ("seen",)