From ae70f7e3c7619e024548b73bda56d08a7626983a Mon Sep 17 00:00:00 2001 From: Semyon Maryasin Date: Thu, 28 Mar 2019 01:13:45 +0300 Subject: [PATCH 1/4] Add check-toml hook --- .pre-commit-hooks.yaml | 6 ++++++ pre_commit_hooks/check_toml.py | 28 ++++++++++++++++++++++++++++ setup.cfg | 2 ++ 3 files changed, 36 insertions(+) create mode 100644 pre_commit_hooks/check_toml.py diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 9396d55..7a3b380 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -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. diff --git a/pre_commit_hooks/check_toml.py b/pre_commit_hooks/check_toml.py new file mode 100644 index 0000000..5f3b820 --- /dev/null +++ b/pre_commit_hooks/check_toml.py @@ -0,0 +1,28 @@ +from __future__ import print_function + +import argparse +import sys +from typing import Optional +from typing import Sequence + +import pytoml + + +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: + pytoml.load(f) + except pytoml.TomlError as exc: + print(exc) + retval = 1 + return retval + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/setup.cfg b/setup.cfg index 96d5f5a..742707b 100644 --- a/setup.cfg +++ b/setup.cfg @@ -26,6 +26,7 @@ packages = find: install_requires = flake8 ruamel.yaml>=0.15 + pytoml 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 From 771156fb5e9cfc894f78e2369f217f5dd5f7b35a Mon Sep 17 00:00:00 2001 From: Semyon Maryasin Date: Thu, 28 Mar 2019 01:22:57 +0300 Subject: [PATCH 2/4] Use toml rather than pytoml Pytoml has an issue with error messages: https://github.com/avakar/pytoml/issues/41 --- pre_commit_hooks/check_toml.py | 6 +++--- setup.cfg | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pre_commit_hooks/check_toml.py b/pre_commit_hooks/check_toml.py index 5f3b820..370830c 100644 --- a/pre_commit_hooks/check_toml.py +++ b/pre_commit_hooks/check_toml.py @@ -5,7 +5,7 @@ import sys from typing import Optional from typing import Sequence -import pytoml +import toml def main(argv=None): # type: (Optional[Sequence[str]]) -> int @@ -17,8 +17,8 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int for filename in args.filenames: try: with open(filename) as f: - pytoml.load(f) - except pytoml.TomlError as exc: + toml.load(f) + except toml.TomlDecodeError as exc: print(exc) retval = 1 return retval diff --git a/setup.cfg b/setup.cfg index 742707b..18d3492 100644 --- a/setup.cfg +++ b/setup.cfg @@ -26,7 +26,7 @@ packages = find: install_requires = flake8 ruamel.yaml>=0.15 - pytoml + toml six typing; python_version<"3.5" python_requires = >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.* From 8d7d40c7cc93c1e5b2d12a36a4ea97926269e678 Mon Sep 17 00:00:00 2001 From: Semyon Maryasin Date: Thu, 28 Mar 2019 01:50:06 +0300 Subject: [PATCH 3/4] Add README entry for check-toml hook --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1a152de..3614ea9 100644 --- a/README.md +++ b/README.md @@ -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. From 317aef49610c5a9bb47838aa9a9863cb8090d829 Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Sat, 3 Aug 2019 11:36:34 +0100 Subject: [PATCH 4/4] Added tests for check_toml Also assures we print filename when error occurs. --- pre_commit_hooks/check_toml.py | 2 +- tests/check_toml_test.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 tests/check_toml_test.py diff --git a/pre_commit_hooks/check_toml.py b/pre_commit_hooks/check_toml.py index 370830c..e16e17c 100644 --- a/pre_commit_hooks/check_toml.py +++ b/pre_commit_hooks/check_toml.py @@ -19,7 +19,7 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int with open(filename) as f: toml.load(f) except toml.TomlDecodeError as exc: - print(exc) + print('{}: {}'.format(filename, exc)) retval = 1 return retval diff --git a/tests/check_toml_test.py b/tests/check_toml_test.py new file mode 100644 index 0000000..1172c40 --- /dev/null +++ b/tests/check_toml_test.py @@ -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