Skip to content

Commit c245a7c

Browse files
authored
Merge pull request #400 from pycontribs/toml-checker
Toml checker
2 parents 696c403 + 317aef4 commit c245a7c

File tree

5 files changed

+69
-0
lines changed

5 files changed

+69
-0
lines changed

.pre-commit-hooks.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@
7070
entry: check-symlinks
7171
language: python
7272
types: [symlink]
73+
- id: check-toml
74+
name: Check Toml
75+
description: This hook checks toml files for parseable syntax.
76+
entry: check-toml
77+
language: python
78+
types: [toml]
7379
- id: check-vcs-permalinks
7480
name: Check vcs permalinks
7581
description: Ensures that links to vcs websites are permalinks.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Add this to your `.pre-commit-config.yaml`
4242
- `check-json` - Attempts to load all json files to verify syntax.
4343
- `check-merge-conflict` - Check for files that contain merge conflict strings.
4444
- `check-symlinks` - Checks for symlinks which do not point to anything.
45+
- `check-toml` - Attempts to load all TOML files to verify syntax.
4546
- `check-vcs-permalinks` - Ensures that links to vcs websites are permalinks.
4647
- `check-xml` - Attempts to load all xml files to verify syntax.
4748
- `check-yaml` - Attempts to load all yaml files to verify syntax.

pre_commit_hooks/check_toml.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from __future__ import print_function
2+
3+
import argparse
4+
import sys
5+
from typing import Optional
6+
from typing import Sequence
7+
8+
import toml
9+
10+
11+
def main(argv=None): # type: (Optional[Sequence[str]]) -> int
12+
parser = argparse.ArgumentParser()
13+
parser.add_argument('filenames', nargs='*', help='Filenames to check.')
14+
args = parser.parse_args(argv)
15+
16+
retval = 0
17+
for filename in args.filenames:
18+
try:
19+
with open(filename) as f:
20+
toml.load(f)
21+
except toml.TomlDecodeError as exc:
22+
print('{}: {}'.format(filename, exc))
23+
retval = 1
24+
return retval
25+
26+
27+
if __name__ == '__main__':
28+
sys.exit(main())

setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ packages = find:
2626
install_requires =
2727
flake8
2828
ruamel.yaml>=0.15
29+
toml
2930
six
3031
typing; python_version<"3.5"
3132
python_requires = >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*
@@ -43,6 +44,7 @@ console_scripts =
4344
check-json = pre_commit_hooks.check_json:main
4445
check-merge-conflict = pre_commit_hooks.check_merge_conflict:main
4546
check-symlinks = pre_commit_hooks.check_symlinks:main
47+
check-toml = pre_commit_hooks.check_toml:main
4648
check-vcs-permalinks = pre_commit_hooks.check_vcs_permalinks:main
4749
check-xml = pre_commit_hooks.check_xml:main
4850
check-yaml = pre_commit_hooks.check_yaml:main

tests/check_toml_test.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from __future__ import absolute_import
2+
from __future__ import unicode_literals
3+
4+
from pre_commit_hooks.check_toml import main
5+
6+
7+
def test_toml_good(tmpdir):
8+
filename = tmpdir.join('f')
9+
filename.write("""
10+
key = # INVALID
11+
12+
= "no key name" # INVALID
13+
""")
14+
ret = main((filename.strpath,))
15+
assert ret == 1
16+
17+
18+
def test_toml_bad(tmpdir):
19+
filename = tmpdir.join('f')
20+
filename.write(
21+
"""
22+
# This is a TOML document.
23+
24+
title = "TOML Example"
25+
26+
[owner]
27+
name = "John"
28+
dob = 1979-05-27T07:32:00-08:00 # First class dates
29+
""",
30+
)
31+
ret = main((filename.strpath,))
32+
assert ret == 0

0 commit comments

Comments
 (0)