test_tracer.py 1.7 KB

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