Skip to content

Commit 7f99537

Browse files
committed
InvalidLinkBear: XML Namespaces
Reduce the severity of XML Namespace results to INFO. Related to #1239
1 parent bbd5d33 commit 7f99537

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

bears/general/InvalidLinkBear.py

+26-7
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,25 @@ def extract_links_from_file(file, link_ignore_regex, link_ignore_list):
8080
# Unbalanced parenthesis
8181
(?<!\.)(?<!,) # Exclude trailing `.` or `,` from URL
8282
""", re.VERBOSE)
83-
83+
file_context = {}
8484
for line_number, line in enumerate(file):
85+
xmlns_regex = re.compile(r'xmlns:?\w*="(.*)"')
8586
for match in re.findall(regex, line):
8687
link = match[0]
87-
context = enum(
88-
pip_vcs_url=False)
89-
if link.startswith(('hg+', 'bzr+', 'git+', 'svn+')):
90-
context.pip_vcs_url = True
88+
link_context = file_context.get(link)
89+
if not link_context:
90+
link_context = enum(
91+
xml_namespace=False,
92+
pip_vcs_url=False)
93+
xmlns_match = xmlns_regex.search(line)
94+
if xmlns_match and link in xmlns_match.groups():
95+
link_context.xml_namespace = True
96+
if link.startswith(('hg+', 'bzr+', 'git+', 'svn+')):
97+
link_context.pip_vcs_url = True
98+
file_context[link] = link_context
9199
if not (link_ignore_regex.search(link) or
92100
fnmatch(link, link_ignore_list)):
93-
yield link, line_number, context
101+
yield link, line_number, link_context
94102

95103
def analyze_links_in_file(self, file, network_timeout, link_ignore_regex,
96104
link_ignore_list):
@@ -149,7 +157,18 @@ def run(self, filename, file,
149157

150158
for line_number, link, code, context in self.analyze_links_in_file(
151159
file, network_timeout, link_ignore_regex, link_ignore_list):
152-
if code is None:
160+
if context.xml_namespace:
161+
if code and 200 <= code < 300:
162+
pass
163+
else:
164+
yield Result.from_values(
165+
origin=self,
166+
message=('XML Namespace - '
167+
'{url}').format(url=link),
168+
file=filename,
169+
line=line_number,
170+
severity=RESULT_SEVERITY.INFO)
171+
elif code is None:
153172
yield Result.from_values(
154173
origin=self,
155174
message=('Broken link - unable to connect to '

tests/general/InvalidLinkBearTest.py

+22
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,28 @@ def test_pip_vcs_url(self):
230230
for line in brokenlink_at_hash.splitlines():
231231
self.assertResult(invalid_file=[line])
232232

233+
def test_xml_namespaces(self):
234+
valid_file = """
235+
#Namespace and also a valid link
236+
<ruleset name="test" xmlns="http://httpbin.org/status/200">
237+
238+
# xml where xmlns: and xsi:schema are valid links
239+
<ruleset name="test" xmlns="http://xmlnamespace.org/status/200"
240+
xmlns:xsi="http://xmlnamespace.org/status/200"
241+
xsi:schemaLocation="http://xmlnamespace.org/status/200">
242+
""".splitlines()
243+
244+
self.assertResult(valid_file=valid_file)
245+
246+
invalid_file = """
247+
<ruleset name="test" xmlns="http://this.isa.namespace/ruleset/7.0.0"
248+
xmlns:xsi="http://this.is.another/kindof/namespace"
249+
xsi:schemaLocation="http://this.namespace.dosent/exists/7.0.0"
250+
xsi:schemaLocation="http://httpbin.com/404">""".splitlines()
251+
252+
for line in invalid_file[1:]:
253+
self.assertResult(invalid_file=[line])
254+
233255
def test_links_to_ignore(self):
234256
valid_file = """http://httpbin.org/status/200
235257
http://httpbin.org/status/201

0 commit comments

Comments
 (0)