diff --git a/README.md b/README.md index 65e8617..f1b60b7 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,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-xml` - Attempts to load all xml files to verify syntax. - `check-yaml` - Attempts to load all yaml files to verify syntax. diff --git a/hooks.yaml b/hooks.yaml index 8fcbe78..0547404 100644 --- a/hooks.yaml +++ b/hooks.yaml @@ -37,6 +37,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 4fefeaa..8e85b8d 100644 --- a/setup.py +++ b/setup.py @@ -42,6 +42,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-xml = pre_commit_hooks.check_xml:check_xml', 'check-yaml = pre_commit_hooks.check_yaml:check_yaml', 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