forked from Ikalus1988/MisakaNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_frontmatter_parsing.py
More file actions
207 lines (171 loc) · 8.49 KB
/
Copy pathtest_frontmatter_parsing.py
File metadata and controls
207 lines (171 loc) · 8.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
"""Tests for frontmatter parsing edge cases.
Covers acceptance criteria from issue #296:
- Missing frontmatter → graceful fallback
- Malformed YAML → skip + warning
- Duplicate keys → last wins
- Empty frontmatter → defaults applied
- Non-standard encoding (UTF-8 BOM)
"""
import json
import tempfile
import unittest
from pathlib import Path
import sys
PROJECT_ROOT = Path(__file__).parent.parent
sys.path.insert(0, str(PROJECT_ROOT / "scripts"))
from validate_lessons import extract_frontmatter
class TestMissingFrontmatter(unittest.TestCase):
"""No frontmatter block → graceful fallback (None, error message)."""
def test_no_frontmatter_returns_none(self):
with tempfile.NamedTemporaryFile(mode="w", suffix=".md", delete=False) as f:
f.write("# Just a heading\n\nSome content.\n")
f.flush()
result, err = extract_frontmatter(Path(f.name))
self.assertIsNone(result)
self.assertIn("No frontmatter", err)
def test_empty_file_returns_none(self):
with tempfile.NamedTemporaryFile(mode="w", suffix=".md", delete=False) as f:
f.write("")
f.flush()
result, err = extract_frontmatter(Path(f.name))
self.assertIsNone(result)
def test_only_frontmatter_delimiters(self):
with tempfile.NamedTemporaryFile(mode="w", suffix=".md", delete=False) as f:
f.write("---\n---\n")
f.flush()
result, err = extract_frontmatter(Path(f.name))
# Empty frontmatter block — should return empty dict or None
self.assertTrue(result is None or isinstance(result, dict))
class TestMalformedFrontmatter(unittest.TestCase):
"""Malformed content in frontmatter → skip + error message."""
def test_invalid_json_returns_error(self):
with tempfile.NamedTemporaryFile(mode="w", suffix=".md", delete=False) as f:
f.write("---\n{invalid json\n---\n# Content\n")
f.flush()
result, err = extract_frontmatter(Path(f.name))
# Should fall back to YAML-like parser or return error
# Either way, should not crash
self.assertTrue(result is not None or err is not None)
def test_random_text_in_frontmatter(self):
with tempfile.NamedTemporaryFile(mode="w", suffix=".md", delete=False) as f:
f.write("---\nthis is not yaml or json\n---\n# Content\n")
f.flush()
result, err = extract_frontmatter(Path(f.name))
# YAML-like parser: "this is not yaml or json" has no colon → returns None
# This is acceptable behavior — unrecognized format → error
self.assertTrue(result is None or isinstance(result, dict))
def test_missing_closing_delimiter(self):
with tempfile.NamedTemporaryFile(mode="w", suffix=".md", delete=False) as f:
f.write("---\ntitle: Test\n# Content without closing ---\n")
f.flush()
result, err = extract_frontmatter(Path(f.name))
# Should return None — no closing ---
self.assertIsNone(result)
class TestDuplicateKeys(unittest.TestCase):
"""Duplicate keys → last wins (JSON behavior)."""
def test_json_duplicate_keys_last_wins(self):
with tempfile.NamedTemporaryFile(mode="w", suffix=".md", delete=False) as f:
f.write('---\n{"title": "First", "title": "Second"}\n---\n# Content\n')
f.flush()
result, err = extract_frontmatter(Path(f.name))
if result is not None:
# JSON spec: last key wins
self.assertEqual(result.get("title"), "Second")
def test_yaml_duplicate_keys_last_wins(self):
with tempfile.NamedTemporaryFile(mode="w", suffix=".md", delete=False) as f:
f.write("---\ntitle: First\ntitle: Second\ndomain: test\n---\n# Content\n")
f.flush()
result, err = extract_frontmatter(Path(f.name))
if result is not None:
self.assertEqual(result.get("title"), "Second")
class TestEmptyFrontmatter(unittest.TestCase):
"""Empty frontmatter → defaults applied or empty dict."""
def test_empty_json_object(self):
with tempfile.NamedTemporaryFile(mode="w", suffix=".md", delete=False) as f:
f.write("---\n{}\n---\n# Content\n")
f.flush()
result, err = extract_frontmatter(Path(f.name))
self.assertIsNotNone(result)
self.assertEqual(len(result), 0)
def test_whitespace_only_frontmatter(self):
with tempfile.NamedTemporaryFile(mode="w", suffix=".md", delete=False) as f:
f.write("---\n \n \n---\n# Content\n")
f.flush()
result, err = extract_frontmatter(Path(f.name))
# Should return empty dict or None
self.assertTrue(result is None or len(result) == 0)
class TestStringEncoding(unittest.TestCase):
"""String value edge cases."""
def test_quoted_strings_unquoted(self):
with tempfile.NamedTemporaryFile(mode="w", suffix=".md", delete=False) as f:
f.write('---\ntitle: "Quoted Title"\ntags: [\'tag1\', \'tag2\']\n---\n# Content\n')
f.flush()
result, err = extract_frontmatter(Path(f.name))
if result is not None:
self.assertEqual(result.get("title"), "Quoted Title")
def test_boolean_values(self):
with tempfile.NamedTemporaryFile(mode="w", suffix=".md", delete=False) as f:
f.write("---\npublished: true\ndraft: false\n---\n# Content\n")
f.flush()
result, err = extract_frontmatter(Path(f.name))
if result is not None:
self.assertEqual(result.get("published"), True)
self.assertEqual(result.get("draft"), False)
def test_list_values(self):
with tempfile.NamedTemporaryFile(mode="w", suffix=".md", delete=False) as f:
f.write("---\ntags: [python, devops, test]\n---\n# Content\n")
f.flush()
result, err = extract_frontmatter(Path(f.name))
if result is not None:
tags = result.get("tags")
self.assertIsInstance(tags, list)
self.assertIn("python", tags)
class TestBOMEncoding(unittest.TestCase):
"""UTF-8 BOM encoding handling."""
def test_utf8_bom_file(self):
with tempfile.NamedTemporaryFile(mode="wb", suffix=".md", delete=False) as f:
# Write UTF-8 BOM + frontmatter
bom = b'\xef\xbb\xbf'
content = b'---\ntitle: BOM Test\ndomain: test\n---\n# Content\n'
f.write(bom + content)
f.flush()
result, err = extract_frontmatter(Path(f.name))
# Should handle BOM gracefully — may return None or parsed dict
# The key thing is it should not crash
self.assertTrue(result is not None or err is not None)
def test_utf8_bom_with_json_frontmatter(self):
with tempfile.NamedTemporaryFile(mode="wb", suffix=".md", delete=False) as f:
bom = b'\xef\xbb\xbf'
content = b'---\n{"title": "BOM Test", "domain": "test"}\n---\n# Content\n'
f.write(bom + content)
f.flush()
result, err = extract_frontmatter(Path(f.name))
# JSON parser should handle BOM or the YAML fallback should
self.assertTrue(result is not None or err is not None)
class TestSpecialContent(unittest.TestCase):
"""Special content in frontmatter values."""
def test_multiline_json_string(self):
with tempfile.NamedTemporaryFile(mode="w", suffix=".md", delete=False) as f:
fm = json.dumps({
"title": "Test",
"description": "A description with\nnewlines",
"tags": ["tag1", "tag2"]
})
f.write(f"---\n{fm}\n---\n# Content\n")
f.flush()
result, err = extract_frontmatter(Path(f.name))
self.assertIsNotNone(result)
self.assertEqual(result.get("title"), "Test")
def test_nested_json_object(self):
with tempfile.NamedTemporaryFile(mode="w", suffix=".md", delete=False) as f:
fm = json.dumps({
"title": "Test",
"metadata": {"author": "test", "version": 1}
})
f.write(f"---\n{fm}\n---\n# Content\n")
f.flush()
result, err = extract_frontmatter(Path(f.name))
self.assertIsNotNone(result)
self.assertIsInstance(result.get("metadata"), dict)
if __name__ == "__main__":
unittest.main()