Skip to content

Commit 685dec5

Browse files
authored
fix sonarcloud-reported bug in xmlwriter, add test (#1951)
sonarcloud detected a bug: “Remove 1 unexpected arguments; 'join' expects 1 positional arguments.” in xmlwriter: ```py return ":".join(pre, uri[len(ns) :]) ``` Added containing brackets apparently omitted by original author. ```py return ":".join([pre, uri[len(ns) :]]) ``` Added test for surety, took the opportunity of adding two tests to take coverage of `xmlwriter` to 100%.
1 parent 32979d1 commit 685dec5

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

rdflib/plugins/serializers/xmlwriter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def qname(self, uri):
100100
for pre, ns in self.extra_ns.items():
101101
if uri.startswith(ns):
102102
if pre != "":
103-
return ":".join(pre, uri[len(ns) :])
103+
return ":".join([pre, uri[len(ns) :]])
104104
else:
105105
return uri[len(ns) :]
106106

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import tempfile
2+
3+
import pytest
4+
5+
import rdflib
6+
from rdflib.plugins.serializers.xmlwriter import XMLWriter
7+
8+
EXNS = rdflib.Namespace("https://example.org/ns/")
9+
TRIXNS = rdflib.Namespace("http://www.w3.org/2004/03/trix/trix-1/")
10+
11+
12+
def test_xmlwriter_namespaces():
13+
14+
g = rdflib.Graph()
15+
16+
with tempfile.TemporaryFile() as fp:
17+
18+
xmlwr = XMLWriter(fp, g.namespace_manager, extra_ns={"": TRIXNS, "ex": EXNS})
19+
20+
xmlwr.namespaces()
21+
22+
fp.seek(0)
23+
24+
assert fp.readlines() == [
25+
b'<?xml version="1.0" encoding="utf-8"?>\n',
26+
b' xmlns:owl="http://www.w3.org/2002/07/owl#"\n',
27+
b' xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"\n',
28+
b' xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"\n',
29+
b' xmlns:xsd="http://www.w3.org/2001/XMLSchema#"\n',
30+
b' xmlns:xml="http://www.w3.org/XML/1998/namespace"\n',
31+
b' xmlns="http://www.w3.org/2004/03/trix/trix-1/"\n',
32+
b' xmlns:ex="https://example.org/ns/"\n',
33+
]
34+
35+
36+
def test_xmlwriter_decl():
37+
38+
g = rdflib.Graph()
39+
40+
with tempfile.TemporaryFile() as fp:
41+
42+
xmlwr = XMLWriter(fp, g.namespace_manager, decl=0, extra_ns={"": TRIXNS})
43+
44+
xmlwr.namespaces()
45+
46+
fp.seek(0)
47+
assert fp.readlines() == [
48+
b"\n",
49+
b' xmlns:owl="http://www.w3.org/2002/07/owl#"\n',
50+
b' xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"\n',
51+
b' xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"\n',
52+
b' xmlns:xsd="http://www.w3.org/2001/XMLSchema#"\n',
53+
b' xmlns:xml="http://www.w3.org/XML/1998/namespace"\n',
54+
b' xmlns="http://www.w3.org/2004/03/trix/trix-1/"\n',
55+
]
56+
57+
58+
@pytest.mark.parametrize(
59+
"uri",
60+
[
61+
# NS bound to “ex”, so “ex:foo”
62+
["https://example.org/ns/foo", "ex:foo"],
63+
# NS bound to "", so “graph”
64+
["http://www.w3.org/2004/03/trix/trix-1/graph", "graph"],
65+
# NS not in extra_ns, use ns<int> idiom
66+
["https://example.org/foo", "ns1:foo"],
67+
],
68+
)
69+
def test_xmlwriter_qname(uri):
70+
71+
g = rdflib.Graph()
72+
g.bind("", TRIXNS)
73+
g.bind("ex", EXNS)
74+
75+
fp = tempfile.TemporaryFile()
76+
77+
xmlwr = XMLWriter(fp, g.namespace_manager, extra_ns={"": TRIXNS, "ex": EXNS})
78+
79+
assert xmlwr.qname(uri[0]) == uri[1]

0 commit comments

Comments
 (0)