Skip to content

Commit ce5af6f

Browse files
safishamsiclaude
andcommitted
test: guard to_html/sanitize_label against null source_file/label (#1775)
The TypeError reported on 0.1.14 is already fixed on v8: sanitize_label coerces None ('if text is None: return ""') and the source_file call site guards with str(data.get("source_file") or ""). Add regression tests (unit + to_html integration with null label/source_file) so it can't silently regress. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 4c075f9 commit ce5af6f

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

tests/test_export.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,3 +639,18 @@ def test_to_json_proceeds_on_empty_existing(tmp_path):
639639
assert to_json(_mkG(3), {}, str(p), force=False) is True
640640
data = json.loads(p.read_text())
641641
assert len(data["nodes"]) == 3
642+
643+
644+
def test_to_html_handles_null_source_file_and_label(tmp_path):
645+
"""#1775: a node with source_file=None or label=None must not crash to_html
646+
(synthetic/aggregate nodes legitimately carry null source_file; JSON `null`
647+
survives .get()'s default). Regression guard — fixed via sanitize_label's
648+
None-coercion + the str(source_file or "") call-site guard."""
649+
import networkx as nx
650+
G = nx.Graph()
651+
G.add_node("n1", label="Foo", source_file=None, community=0)
652+
G.add_node("n2", label=None, source_file="a.py", community=0)
653+
G.add_node("n3", label=None, source_file=None, community=0)
654+
out = tmp_path / "graph.html"
655+
to_html(G, {0: ["n1", "n2", "n3"]}, str(out))
656+
assert out.exists() and out.stat().st_size > 0

tests/test_security.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,12 @@ def test_sanitize_label_safe_passthrough():
219219
assert sanitize_label("MyClass") == "MyClass"
220220
assert sanitize_label("extract_python") == "extract_python"
221221

222+
def test_sanitize_label_none_returns_empty():
223+
# #1775: a node with source_file=None / label=None (synthetic/aggregate
224+
# nodes, or JSON `null`) must not raise — .get() returns None, not the
225+
# default, when the key is present-but-null.
226+
assert sanitize_label(None) == ""
227+
222228

223229
# ---------------------------------------------------------------------------
224230
# check_graph_file_size_cap (#F4 — graph-load memory bomb protection)

0 commit comments

Comments
 (0)