mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-05 03:26:53 +00:00
Add check_rubocop
Uses rubocop to check syntax / style of ruby files.
This commit is contained in:
parent
616c1ebd18
commit
a2d690a0ab
7 changed files with 50 additions and 0 deletions
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
27
pre_commit_hooks/check_rubocop.py
Normal file
27
pre_commit_hooks/check_rubocop.py
Normal file
|
|
@ -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())
|
||||
1
setup.py
1
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',
|
||||
|
|
|
|||
1
testing/resources/invalid_ruby.rb
Normal file
1
testing/resources/invalid_ruby.rb
Normal file
|
|
@ -0,0 +1 @@
|
|||
puts 'Hello World!
|
||||
1
testing/resources/valid_ruby.rb
Normal file
1
testing/resources/valid_ruby.rb
Normal file
|
|
@ -0,0 +1 @@
|
|||
puts 'Hello World!'
|
||||
13
tests/check_rubocop_test.py
Normal file
13
tests/check_rubocop_test.py
Normal file
|
|
@ -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
|
||||
Loading…
Add table
Add a link
Reference in a new issue