forked from Ikalus1988/MisakaNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_add_provenance.py
More file actions
105 lines (83 loc) · 2.65 KB
/
Copy pathtest_add_provenance.py
File metadata and controls
105 lines (83 loc) · 2.65 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
"""Tests for add_provenance.py (Issue #1219)."""
from __future__ import annotations
import tempfile
from pathlib import Path
from scripts.add_provenance import (
parse_frontmatter,
has_provenance_section,
add_provenance,
extract_domain_from_path,
guess_contributor,
)
def test_parse_frontmatter():
"""Test frontmatter parsing."""
content = """---
title: "Test Lesson"
domain: "devops"
tags:
- ci
- github
status: "published"
---
# Content here
"""
frontmatter, body = parse_frontmatter(content)
assert frontmatter.get("title") == '"Test Lesson"'
assert frontmatter.get("domain") == '"devops"'
def test_has_provenance_section():
"""Test provenance detection."""
with_provenance = """---
title: "Test"
provenance:
source: "internal"
---
"""
without_provenance = """---
title: "Test"
---
"""
assert has_provenance_section(with_provenance) is True
assert has_provenance_section(without_provenance) is False
def test_add_provenance():
"""Test adding provenance to content."""
content = """---
title: "Test Lesson"
---
# Content
"""
result = add_provenance(content, "TestContributor", "internal")
assert "provenance:" in result
assert "TestContributor" in result
assert "internal" in result
assert "title: \"Test Lesson\"" in result
def test_add_provenance_no_frontmatter():
"""Test adding provenance to content without frontmatter."""
content = "# Simple Content\n\nSome text."
result = add_provenance(content, "Contributor", "community")
assert result.startswith("---")
assert "provenance:" in result
assert "Simple Content" in result
def test_extract_domain():
"""Test domain extraction from path."""
from scripts.add_provenance import LESSONS_DIR
path = LESSONS_DIR / "contrib" / "lesson.md"
assert extract_domain_from_path(path) == "contrib"
path = LESSONS_DIR / "devops" / "lesson.md"
assert extract_domain_from_path(path) == "devops"
def test_guess_contributor():
"""Test contributor guessing."""
from scripts.add_provenance import LESSONS_DIR
# From contributor field
assert guess_contributor({"contributor": "TestNode"}, Path("test.md")) == "TestNode"
# From title pattern
assert guess_contributor({"title": "Misaka10018 fix"}, Path("test.md")) == "Misaka10018"
# Default for contrib
assert guess_contributor({}, LESSONS_DIR / "contrib" / "test.md") == "Community"
if __name__ == "__main__":
test_parse_frontmatter()
test_has_provenance_section()
test_add_provenance()
test_add_provenance_no_frontmatter()
test_extract_domain()
test_guess_contributor()
print("All tests passed ✓")