Skip to content

Commit b66a6f5

Browse files
committed
fix: use context managers for file handles in tests
Replace bare open() calls with context managers to prevent resource leaks. Also simplify TESTS_DIR to Path(__file__).parent and remove unused os import. Closes #25
1 parent 44e50e5 commit b66a6f5

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

tests/test_l9format.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import json
2-
import os
32
from pathlib import Path
43

54
from l9format import L9Event
65

7-
TESTS_DIR = Path(os.path.dirname(__file__))
6+
TESTS_DIR = Path(__file__).parent
87

98
IP4SCOUT_FILES = [
109
f
@@ -15,13 +14,15 @@
1514

1615
def test_l9event_json_from_reference_repository():
1716
path = TESTS_DIR / "l9event.json"
18-
c = json.load(open(str(path), "r"))
17+
with open(path) as f:
18+
c = json.load(f)
1919
L9Event.from_dict(c)
2020

2121

2222
def test_l9events_from_ip4scout():
2323
for path in IP4SCOUT_FILES:
24-
c = json.load(open(str(path), "r"))
24+
with open(path) as f:
25+
c = json.load(f)
2526
L9Event.from_dict(c)
2627

2728

@@ -33,7 +34,8 @@ def test_iso8601_nanosecond_parsing():
3334
correctly in Python 3.11+.
3435
"""
3536
path = TESTS_DIR / "l9event.json"
36-
c = json.load(open(str(path), "r"))
37+
with open(path) as f:
38+
c = json.load(f)
3739
# Use a timestamp with nanosecond precision (9 decimal places)
3840
c["time"] = "2023-10-05T23:30:36.823867784Z"
3941
event = L9Event.from_dict(c)

0 commit comments

Comments
 (0)