Skip to content

Commit

Permalink
Remove SyntaxHighlighter newline, release 0.4.2
Browse files Browse the repository at this point in the history
  • Loading branch information
underyx committed Jul 15, 2024
1 parent dd340b9 commit f7ce40a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## 0.4.2 (2024-07-15)

- Fix typing import unavailable on 3.8
- Remove trailing newline added by SyntaxHighlighter

## 0.4.1 (2024-06-11)

- Export PathPrettifier via top-level package
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

setup(
name="structlog-pretty",
version="0.4.1",
version="0.4.2",
url="https://github.com/underyx/structlog-pretty",
author="Bence Nagy",
author_email="[email protected]",
Expand Down
5 changes: 3 additions & 2 deletions structlog_pretty/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
except ImportError:
fast_json_available = False

from typing import Optional
from xml.dom.minidom import parseString as parse_xml_string

try:
Expand Down Expand Up @@ -179,7 +180,7 @@ def __call__(self, _, __, event_dict):
continue
if not code:
continue
event_dict[field] = highlight(code, lexer, TerminalFormatter())
event_dict[field] = highlight(code, lexer, TerminalFormatter()).rstrip()

return event_dict

Expand Down Expand Up @@ -223,7 +224,7 @@ class PathPrettifier:
Note that working directory is determined when configuring structlog.
"""

def __init__(self, base_dir: Path | None = None):
def __init__(self, base_dir: Optional[Path] = None):
self.base_dir = base_dir or Path.cwd()

def __call__(self, _, __, event_dict):
Expand Down
31 changes: 17 additions & 14 deletions test/test_SyntaxHighlighter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,29 @@


def test_json():
processor = uut(field_map={'body': 'json'})
event_dict = processor(None, None, {'body': '{"ping": true}'})
assert '\x1b[' in event_dict['body'], 'should have at least one ANSI escape code'
processor = uut(field_map={"body": "json"})
event_dict = processor(None, None, {"body": '{"ping": true}'})
assert "\x1b[" in event_dict["body"], "should have at least one ANSI escape code"
assert not event_dict["body"].endswith(
"\n"
), "should not have trailing newline added"


def test_missing_json():
processor = uut(field_map={'body': 'json'})
event_dict = processor(None, None, {'not_body': '{"ping": true}'})
assert event_dict['not_body'] == '{"ping": true}'
processor = uut(field_map={"body": "json"})
event_dict = processor(None, None, {"not_body": '{"ping": true}'})
assert event_dict["not_body"] == '{"ping": true}'


def test_multiple_fields():
processor = uut(field_map={'body': 'json', 'body_2': 'json'})
event_dict = processor(None, None, {'body': 'null', 'body_2': 'null'})
assert '\x1b[' in event_dict['body'], 'should have at least one ANSI escape code'
assert '\x1b[' in event_dict['body_2'], 'should have at least one ANSI escape code'
processor = uut(field_map={"body": "json", "body_2": "json"})
event_dict = processor(None, None, {"body": "null", "body_2": "null"})
assert "\x1b[" in event_dict["body"], "should have at least one ANSI escape code"
assert "\x1b[" in event_dict["body_2"], "should have at least one ANSI escape code"


def test_multiple_languages():
processor = uut(field_map={'body': 'json', 'body_2': 'xml'})
event_dict = processor(None, None, {'body': 'null', 'body_2': '<null/>'})
assert '\x1b[' in event_dict['body'], 'should have at least one ANSI escape code'
assert '\x1b[' in event_dict['body_2'], 'should have at least one ANSI escape code'
processor = uut(field_map={"body": "json", "body_2": "xml"})
event_dict = processor(None, None, {"body": "null", "body_2": "<null/>"})
assert "\x1b[" in event_dict["body"], "should have at least one ANSI escape code"
assert "\x1b[" in event_dict["body_2"], "should have at least one ANSI escape code"

0 comments on commit f7ce40a

Please sign in to comment.