|
7 | 7 |
|
8 | 8 | import pytest |
9 | 9 |
|
10 | | -from logfmter.formatter import Logfmter |
| 10 | +from logfmter.formatter import Logfmter, parse_logfmt |
11 | 11 |
|
12 | 12 | STRING_ESCAPE_RULES = [ |
13 | 13 | # If the string contains a space, then it must be quoted. |
@@ -88,6 +88,57 @@ def test_format_params(value, expected): |
88 | 88 | assert Logfmter.format_params(value) == expected |
89 | 89 |
|
90 | 90 |
|
| 91 | +@pytest.mark.parametrize( |
| 92 | + "value,expected,kwargs", |
| 93 | + [ |
| 94 | + ("at=INFO", {"at": "INFO"}, {}), |
| 95 | + ('at="INFO"', {"at": "INFO"}, {"reverse": False}), |
| 96 | + ( |
| 97 | + "at=INFO a=1", |
| 98 | + {"levelname": "INFO", "a": "1"}, |
| 99 | + {"aliases": {"at": "levelname"}, "reverse": False}, |
| 100 | + ), |
| 101 | + ("at=INFO msg=test a=1", {"at": "INFO", "msg": "test", "a": "1"}, {}), |
| 102 | + ('at=INFO msg="="', {"at": "INFO", "msg": "="}, {}), |
| 103 | + ( |
| 104 | + "at=INFO first_name=josh", |
| 105 | + {"at": "INFO", "first name": "josh"}, |
| 106 | + {"aliases": {"first_name": "first name"}, "reverse": False}, |
| 107 | + ), |
| 108 | + ( |
| 109 | + r'at=INFO msg=alpha exc_info="exc\n\"info\"\ntb"', |
| 110 | + {"at": "INFO", "msg": "alpha", "exc_info": 'exc\n"info"\ntb'}, |
| 111 | + {}, |
| 112 | + ), |
| 113 | + ('a=" "', {"a": " "}, {}), |
| 114 | + ('_=" "', {"_": " "}, {}), |
| 115 | + ('a="\\""', {"a": '"'}, {}), |
| 116 | + (r'a="\\"', {"a": "\\"}, {"reverse": False}), |
| 117 | + (r'a="\n"', {"a": "\n"}, {}), |
| 118 | + (r"a=\n", {"a": "n"}, {"reverse": False}), |
| 119 | + ("a= b=c", {"a": "", "b": "c"}, {}), |
| 120 | + ("a b=c", {"a": "", "b": "c"}, {"reverse": False}), |
| 121 | + ("a=1", {"a": 1}, {"convert_numeric": True}), |
| 122 | + ("a=1 b=2", {"a": "1", "b": "2"}, {}), |
| 123 | + ("a=1 b=2", {"a": 1, "b": 2}, {"convert_numeric": True}), |
| 124 | + ("a=1.2 b=2", {"a": "1.2", "b": "2"}, {}), |
| 125 | + ("a=1.2 b=2", {"a": 1.2, "b": 2}, {"convert_numeric": True}), |
| 126 | + ( |
| 127 | + "a=1.2.3 b=.2 c=1. d=. e=1..2", |
| 128 | + {"a": "1.2.3", "b": 0.2, "c": 1.0, "d": ".", "e": "1..2"}, |
| 129 | + {"convert_numeric": True, "reverse": False}, |
| 130 | + ), |
| 131 | + ("foo.bar=baz foo.blah=blub", {"foo.bar": "baz", "foo.blah": "blub"}, {}), |
| 132 | + ("\\n=foo", {"n": "foo"}, {"reverse": False}), |
| 133 | + ], |
| 134 | +) |
| 135 | +def test_parse_logfmt(value, kwargs, expected): |
| 136 | + reverse = kwargs.pop("reverse", True) |
| 137 | + assert parse_logfmt(value, **kwargs) == expected |
| 138 | + if reverse: |
| 139 | + assert Logfmter.format_params(expected) == value |
| 140 | + |
| 141 | + |
91 | 142 | @pytest.mark.parametrize( |
92 | 143 | "value,expected", |
93 | 144 | [ |
|
0 commit comments