diff --git a/README.md b/README.md index 723136d..ffb1e53 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ Add this to your `.pre-commit-config.yaml` - `check-docstring-first` - Checks for a common error of placing code before the docstring. - `check-json` - Attempts to load all json files to verify syntax. +- `check-rubocop` - Attempts to verify syntax / style for ruby files - `check-merge-conflict` - Check for files that contain merge conflict strings. - `check-symlinks` - Checks for symlinks which do not point to anything. - `check-xml` - Attempts to load all xml files to verify syntax. diff --git a/hooks.yaml b/hooks.yaml index bda3f76..010df64 100644 --- a/hooks.yaml +++ b/hooks.yaml @@ -43,6 +43,12 @@ entry: check-json language: python files: \.json$ +- id: check-rubocop + name: Check Rubocop + description: This hook checks ruby files for syntax/style + entry: check-rubocop + language: python + files: \.rb$ - id: pretty-format-json name: Pretty format JSON description: This hook sets a standard for formatting JSON files. diff --git a/pre_commit_hooks/check_rubocop.py b/pre_commit_hooks/check_rubocop.py new file mode 100644 index 0000000..f6f9e86 --- /dev/null +++ b/pre_commit_hooks/check_rubocop.py @@ -0,0 +1,27 @@ +from __future__ import print_function + +import argparse +import subprocess +import sys + + +def check_rubocop(argv=None): + parser = argparse.ArgumentParser() + parser.add_argument('filenames', nargs='*', help='filenames to check.') + args = parser.parse_args(argv) + + retval = 0 + + command = ["rubocop"] + args.filenames + + try: + retval = subprocess.check_call(command, shell=False) + except subprocess.CalledProcessError as err: + print('{0}: rubocop failed ({1})'.format(args.filenames, err)) + retval = 1 + + return retval + + +if __name__ == '__main__': + sys.exit(check_rubocop()) diff --git a/setup.py b/setup.py index 9db61a4..6c615a8 100644 --- a/setup.py +++ b/setup.py @@ -40,6 +40,7 @@ setup( '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-rubocop = pre_commit_hooks.check_rubocop:check_rubocop', 'check-merge-conflict = pre_commit_hooks.check_merge_conflict:detect_merge_conflict', 'check-symlinks = pre_commit_hooks.check_symlinks:check_symlinks', 'check-xml = pre_commit_hooks.check_xml:check_xml', diff --git a/testing/resources/invalid_ruby.rb b/testing/resources/invalid_ruby.rb new file mode 100644 index 0000000..7c62116 --- /dev/null +++ b/testing/resources/invalid_ruby.rb @@ -0,0 +1 @@ +puts 'Hello World! diff --git a/testing/resources/valid_ruby.rb b/testing/resources/valid_ruby.rb new file mode 100644 index 0000000..733c205 --- /dev/null +++ b/testing/resources/valid_ruby.rb @@ -0,0 +1 @@ +puts 'Hello World!' diff --git a/tests/check_rubocop_test.py b/tests/check_rubocop_test.py new file mode 100644 index 0000000..c7c1fbc --- /dev/null +++ b/tests/check_rubocop_test.py @@ -0,0 +1,13 @@ +import pytest + +from pre_commit_hooks.check_rubocop import check_rubocop +from testing.util import get_resource_path + + +@pytest.mark.parametrize(('filename', 'expected_retval'), ( + ('invalid_ruby.rb', 1), + ('valid_ruby.rb', 0), +)) +def test_check_rubocop(filename, expected_retval): + ret = check_rubocop([get_resource_path(filename)]) + assert ret == expected_retval