|
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. |
@@ -91,6 +91,108 @@ def test_format_params(value, expected): |
91 | 91 | assert Logfmter.format_params(value) == expected |
92 | 92 |
|
93 | 93 |
|
| 94 | +@pytest.mark.parametrize( |
| 95 | + "value", |
| 96 | + [ |
| 97 | + {"at": "INFO"}, |
| 98 | + {"levelname": "INFO", "a": "1"}, |
| 99 | + {"at": "INFO", "msg": "test", "a": "1"}, |
| 100 | + {"at": "INFO", "msg": "="}, |
| 101 | + {"at": "INFO", "msg": "alpha", "exc_info": 'exc\n"info"\ntb'}, |
| 102 | + {"a": " "}, |
| 103 | + {"_": " "}, |
| 104 | + {"a": '"'}, |
| 105 | + {"a": "\\"}, |
| 106 | + {"a": "\\", "b": "c"}, |
| 107 | + {"a": "\n"}, |
| 108 | + {"a": "", "b": "c"}, |
| 109 | + {"a": "1", "b": "2"}, |
| 110 | + {"a": "1.2", "b": "2"}, |
| 111 | + {"a": "1.2.3", "b": "0.2", "c": "1.0", "d": ".", "e": "1..2"}, |
| 112 | + {"foo.bar": "baz", "foo.blah": "blub"}, |
| 113 | + ], |
| 114 | +) |
| 115 | +def test_parse_logfmt_round_trip(value): |
| 116 | + assert parse_logfmt(Logfmter.format_params(value)) == value |
| 117 | + |
| 118 | + |
| 119 | +@pytest.mark.parametrize( |
| 120 | + "value", |
| 121 | + [ |
| 122 | + {"a": 1}, |
| 123 | + {"a": 1, "b": 2}, |
| 124 | + {"a": 1.2, "b": 2}, |
| 125 | + {"a": "1.2.3", "b": 0.2, "c": 1.0, "d": ".", "e": "1..2"}, |
| 126 | + ], |
| 127 | +) |
| 128 | +def test_parse_logfmt_numeric_round_trip(value): |
| 129 | + assert parse_logfmt(Logfmter.format_params(value), convert_numeric=True) == value |
| 130 | + |
| 131 | + |
| 132 | +@pytest.mark.parametrize( |
| 133 | + "value,expected,kwargs", |
| 134 | + [ |
| 135 | + ("at=INFO", {"at": "INFO"}, {}), |
| 136 | + ('at="INFO"', {"at": "INFO"}, {"reverse": False}), |
| 137 | + ( |
| 138 | + "at=INFO a=1", |
| 139 | + {"levelname": "INFO", "a": "1"}, |
| 140 | + {"aliases": {"at": "levelname"}, "reverse": False}, |
| 141 | + ), |
| 142 | + ("at=INFO msg=test a=1", {"at": "INFO", "msg": "test", "a": "1"}, {}), |
| 143 | + ('at=INFO msg="="', {"at": "INFO", "msg": "="}, {}), |
| 144 | + ( |
| 145 | + "at=INFO first_name=josh", |
| 146 | + {"at": "INFO", "first name": "josh"}, |
| 147 | + {"aliases": {"first_name": "first name"}, "reverse": False}, |
| 148 | + ), |
| 149 | + ( |
| 150 | + r'at=INFO msg=alpha exc_info="exc\n\"info\"\ntb"', |
| 151 | + {"at": "INFO", "msg": "alpha", "exc_info": 'exc\n"info"\ntb'}, |
| 152 | + {}, |
| 153 | + ), |
| 154 | + ('a=" "', {"a": " "}, {}), |
| 155 | + ('_=" "', {"_": " "}, {}), |
| 156 | + ('a="\\""', {"a": '"'}, {}), |
| 157 | + (r'a="\\"', {"a": "\\"}, {"reverse": False}), |
| 158 | + (r'a="\n"', {"a": "\n"}, {}), |
| 159 | + (r"a=\n", {"a": "n"}, {"reverse": False}), |
| 160 | + ("a= b=c", {"a": "", "b": "c"}, {}), |
| 161 | + ("a b=c", {"a": "", "b": "c"}, {"reverse": False}), |
| 162 | + ("a=1", {"a": 1}, {"convert_numeric": True}), |
| 163 | + ("a=1 b=2", {"a": "1", "b": "2"}, {}), |
| 164 | + ("a=1 b=2", {"a": 1, "b": 2}, {"convert_numeric": True}), |
| 165 | + ("a=1.2 b=2", {"a": "1.2", "b": "2"}, {}), |
| 166 | + ("a=1.2 b=2", {"a": 1.2, "b": 2}, {"convert_numeric": True}), |
| 167 | + ( |
| 168 | + "a=1.2.3 b=.2 c=1. d=. e=1..2", |
| 169 | + {"a": "1.2.3", "b": 0.2, "c": 1.0, "d": ".", "e": "1..2"}, |
| 170 | + {"convert_numeric": True, "reverse": False}, |
| 171 | + ), |
| 172 | + ("foo.bar=baz foo.blah=blub", {"foo.bar": "baz", "foo.blah": "blub"}, {}), |
| 173 | + ("\\n=foo", {"n": "foo"}, {"reverse": False}), |
| 174 | + ("a=' '", {"'": "", "a": "'"}, {"reverse": False}), |
| 175 | + ], |
| 176 | +) |
| 177 | +def test_parse_logfmt(value, kwargs, expected): |
| 178 | + reverse = kwargs.pop("reverse", True) |
| 179 | + assert parse_logfmt(value, **kwargs) == expected |
| 180 | + if reverse: |
| 181 | + assert Logfmter.format_params(expected) == value |
| 182 | + |
| 183 | + |
| 184 | +@pytest.mark.parametrize( |
| 185 | + "value", |
| 186 | + [ |
| 187 | + "a=\n", |
| 188 | + pytest.param("a = b", marks=pytest.mark.xfail()), |
| 189 | + ], |
| 190 | +) |
| 191 | +def test_parse_invalid_logfmt(value): |
| 192 | + with pytest.raises(ValueError): |
| 193 | + print(parse_logfmt(value)) |
| 194 | + |
| 195 | + |
94 | 196 | @pytest.mark.parametrize( |
95 | 197 | "value,expected", |
96 | 198 | [ |
|
0 commit comments