Merge pull request #400 from pycontribs/toml-checker

Toml checker
This commit is contained in:
Anthony Sottile 2019-08-05 10:28:00 -07:00 committed by GitHub
commit c245a7c5e5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 69 additions and 0 deletions

View file

@ -70,6 +70,12 @@
entry: check-symlinks
language: python
types: [symlink]
- id: check-toml
name: Check Toml
description: This hook checks toml files for parseable syntax.
entry: check-toml
language: python
types: [toml]
- id: check-vcs-permalinks
name: Check vcs permalinks
description: Ensures that links to vcs websites are permalinks.

View file

@ -42,6 +42,7 @@ Add this to your `.pre-commit-config.yaml`
- `check-json` - Attempts to load all json files to verify syntax.
- `check-merge-conflict` - Check for files that contain merge conflict strings.
- `check-symlinks` - Checks for symlinks which do not point to anything.
- `check-toml` - Attempts to load all TOML files to verify syntax.
- `check-vcs-permalinks` - Ensures that links to vcs websites are permalinks.
- `check-xml` - Attempts to load all xml files to verify syntax.
- `check-yaml` - Attempts to load all yaml files to verify syntax.

View file

@ -0,0 +1,28 @@
from __future__ import print_function
import argparse
import sys
from typing import Optional
from typing import Sequence
import toml
def main(argv=None): # type: (Optional[Sequence[str]]) -> int
parser = argparse.ArgumentParser()
parser.add_argument('filenames', nargs='*', help='Filenames to check.')
args = parser.parse_args(argv)
retval = 0
for filename in args.filenames:
try:
with open(filename) as f:
toml.load(f)
except toml.TomlDecodeError as exc:
print('{}: {}'.format(filename, exc))
retval = 1
return retval
if __name__ == '__main__':
sys.exit(main())

View file

@ -26,6 +26,7 @@ packages = find:
install_requires =
flake8
ruamel.yaml>=0.15
toml
six
typing; python_version<"3.5"
python_requires = >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*
@ -43,6 +44,7 @@ console_scripts =
check-json = pre_commit_hooks.check_json:main
check-merge-conflict = pre_commit_hooks.check_merge_conflict:main
check-symlinks = pre_commit_hooks.check_symlinks:main
check-toml = pre_commit_hooks.check_toml:main
check-vcs-permalinks = pre_commit_hooks.check_vcs_permalinks:main
check-xml = pre_commit_hooks.check_xml:main
check-yaml = pre_commit_hooks.check_yaml:main

32
tests/check_toml_test.py Normal file
View file

@ -0,0 +1,32 @@
from __future__ import absolute_import
from __future__ import unicode_literals
from pre_commit_hooks.check_toml import main
def test_toml_good(tmpdir):
filename = tmpdir.join('f')
filename.write("""
key = # INVALID
= "no key name" # INVALID
""")
ret = main((filename.strpath,))
assert ret == 1
def test_toml_bad(tmpdir):
filename = tmpdir.join('f')
filename.write(
"""
# This is a TOML document.
title = "TOML Example"
[owner]
name = "John"
dob = 1979-05-27T07:32:00-08:00 # First class dates
""",
)
ret = main((filename.strpath,))
assert ret == 0