Skip to content

Commit

Permalink
XMLBear: Support for style specifications
Browse files Browse the repository at this point in the history
A format can be specified in the arguments or in the configuration.
The valid args are c14n, c14n11, exc-c14n and oldxml10.
If a style is specified, style issues have a MAJOR severity.

Closes #1098
  • Loading branch information
namanyadav12 committed Jan 3, 2017
1 parent 00103fe commit 54f9232
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
33 changes: 27 additions & 6 deletions bears/xml2/XMLBear.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import itertools
import re
import logging

from coalib.bearlib.abstractions.Linter import linter
from dependency_management.requirements.DistributionRequirement import (
Expand All @@ -11,7 +12,6 @@
def path_or_url(xml_dtd):
"""
Converts the setting value to url or path.
:param xml_dtd: Setting key.
:return: Returns a converted setting value.
"""
Expand All @@ -21,13 +21,27 @@ def path_or_url(xml_dtd):
return path(xml_dtd)


def xml_style_valid(xml_style):
"""
Checks if xml_style is valid.
:param xml_style: Setting key.
:return: Returns the value if valid else returns none.
"""
style = str(xml_style)
if style not in XMLBear._styles:
logging.warn('Unrecognised style ' + style + '. Valid xml'
' styles are c14n, c14n11, exc-c14n and oldxml10.'
' Running XMLBear without any xml_style argument.')
return None
return '--' + style


@linter(executable='xmllint',
use_stdout=True,
use_stderr=True)
class XMLBear:
"""
Checks the code with ``xmllint``.
See http://xmlsoft.org/xmllint.html
"""
LANGUAGES = {'XML'}
Expand All @@ -40,21 +54,28 @@ class XMLBear:
_output_regex = re.compile(
r'.*:(?P<line>\d+):.*(?P<severity>error|warning)\s?: '
r'(?P<message>.*)\n.*\n.*')
_diff_severity = RESULT_SEVERITY.INFO
_styles = ('c14n', 'c14n11', 'exc-c14n', 'oldxml10')

@staticmethod
def create_arguments(filename, file, config_file,
def create_arguments(self, filename, file, config_file,
xml_schema: path='',
xml_dtd: path_or_url=''):
xml_dtd: path_or_url='',
xml_style: xml_style_valid=None):
"""
:param xml_schema: ``W3C XML Schema`` file used for validation.
:param xml_dtd: ``Document type Definition (DTD)`` file or
url used for validation.
:param xml_style: ``XML Style Specification`` Relevant args are
c14n, c14n11, exc-c14n and oldxml10.
"""
args = (filename,)
if xml_schema:
args += ('-schema', xml_schema)
if xml_dtd:
args += ('-dtdvalid', xml_dtd)
if xml_style:
args += (xml_style,)
self._diff_severity = RESULT_SEVERITY.MAJOR

return args

Expand All @@ -68,7 +89,7 @@ def process_output(self, output, filename, file):
output_regex=self._output_regex),
self.process_output_corrected(
stdout, filename, file,
diff_severity=RESULT_SEVERITY.INFO,
diff_severity=self._diff_severity,
result_message='XML can be formatted better.'))
else:
# Return issues from stderr if stdout is empty
Expand Down
31 changes: 31 additions & 0 deletions tests/xml2/XMLBearTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY
from coalib.settings.Section import Section
from coala_utils.ContextManagers import prepare_file
from coalib.settings.Setting import Setting


def load_testdata(filename):
Expand Down Expand Up @@ -36,6 +37,10 @@ def load_testdata(filename):
<a>hey and hi</a>
"""

invalid_xml_file_c14n = """<?xml version="1.0"?>
<a/>
"""

dtd_file = os.path.join(os.path.dirname(__file__),
'test_files',
'note.dtd')
Expand Down Expand Up @@ -98,3 +103,29 @@ def test_errors(self):
with prepare_file(content, None) as (file, fname):
with execute_bear(self.uut, fname, file) as results:
self.assertEqual(results[0].severity, RESULT_SEVERITY.MAJOR)


@generate_skip_decorator(XMLBear)
class XMLBearStyleTest(unittest.TestCase):

def setUp(self):
self.section = Section('name')
self.uut = XMLBear(self.section, Queue())

def test_xml_style_errors(self):
self.section.append(Setting('xml_style', 'c14n'))
content = invalid_xml_file_c14n.splitlines()
with prepare_file(content, None) as (file, fname):
with execute_bear(self.uut, fname, file) as results:
self.assertEqual(results[0].message,
'XML can be formatted better.')
self.assertEqual(results[0].severity, RESULT_SEVERITY.MAJOR)

def test_unrecognised_xml_style_name(self):
self.section.append(Setting('xml_style', 'wrong-args'))
content = invalid_xml_file.splitlines()
with prepare_file(content, None) as (file, fname):
with execute_bear(self.uut, fname, file) as results:
self.assertEqual(results[0].message,
'XML can be formatted better.')
self.assertEqual(results[0].severity, RESULT_SEVERITY.INFO)

0 comments on commit 54f9232

Please sign in to comment.