Skip to content

Commit

Permalink
Add check-xml hook.
Browse files Browse the repository at this point in the history
  • Loading branch information
asottile committed Jan 17, 2015
1 parent fea9b2e commit ddc9208
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ env: # These should match the tox env list
- TOXENV=py33
- TOXENV=py34
- TOXENV=pypy
- TOXENV=pypy3
install: pip install coveralls tox --use-mirrors
script: tox
# Special snowflake. Our tests depend on making real commits.
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Add this to your `.pre-commit-config.yaml`
- `check-case-conflict` - Check for files that would conflict in case-insensitive filesystems.
- `check-docstring-first` - Checks a common error of defining a docstring after code.
- `check-json` - Attempts to load all json files to verify syntax.
- `check-xml` - Attempts to load all xml files to verify syntax.
- `check-yaml` - Attempts to load all yaml files to verify syntax.
- `debug-statements` - Check for pdb / ipdb / pudb statements in code.
- `end-of-file-fixer` - Makes sure files end in a newline and only a newline.
Expand Down
6 changes: 6 additions & 0 deletions hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
entry: check-json
language: python
files: \.json$
- id: check-xml
name: Check Xml
description: This hook checks xml files for parseable syntax.
entry: check-xml
language: python
files: \.xml$
- id: check-yaml
name: Check Yaml
description: This hook checks yaml files for parseable syntax.
Expand Down
28 changes: 28 additions & 0 deletions pre_commit_hooks/check_xml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals

import argparse
import io
import sys
import xml.sax


def check_xml(argv=None):
parser = argparse.ArgumentParser()
parser.add_argument('filenames', nargs='*', help='XML filenames to check.')
args = parser.parse_args(argv)

retval = 0
for filename in args.filenames:
try:
with io.open(filename, 'rb') as xml_file:
xml.sax.parse(xml_file, xml.sax.ContentHandler())
except xml.sax.SAXException as exc:
print('{0}: Failed to xml parse ({1})'.format(filename, exc))
retval = 1
return retval


if __name__ == '__main__':
sys.exit(check_xml())
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
-e .

astroid<1.3.3
coverage
flake8
mock
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
'check-case-conflict = pre_commit_hooks.check_case_conflict:main',
'check-docstring-first = pre_commit_hooks.check_docstring_first:main',
'check-json = pre_commit_hooks.check_json:check_json',
'check-xml = pre_commit_hooks.check_xml:check_xml',
'check-yaml = pre_commit_hooks.check_yaml:check_yaml',
'debug-statement-hook = pre_commit_hooks.debug_statement_hook:debug_statement_hook',
'end-of-file-fixer = pre_commit_hooks.end_of_file_fixer:end_of_file_fixer',
Expand Down
1 change: 1 addition & 0 deletions testing/resources/bad_xml.notxml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<im><not><terminated><lol>
4 changes: 4 additions & 0 deletions testing/resources/ok_xml.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<document>
<hello>Hi</hello>
<world>Earth</world>
</document>
13 changes: 13 additions & 0 deletions tests/check_xml_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import pytest

from pre_commit_hooks.check_xml import check_xml
from testing.util import get_resource_path


@pytest.mark.parametrize(('filename', 'expected_retval'), (
('bad_xml.notxml', 1),
('ok_xml.xml', 0),
))
def test_check_xml(filename, expected_retval):
ret = check_xml([get_resource_path(filename)])
assert ret == expected_retval
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tox]
project = pre_commit_hooks
# These should match the travis env list
envlist = py26,py27,py33,py34,pypy
envlist = py26,py27,py33,py34,pypy,pypy3

[testenv]
install_command = pip install --use-wheel {opts} {packages}
Expand Down

0 comments on commit ddc9208

Please sign in to comment.