Skip to content

Commit

Permalink
RuboCopBear: Fix CI breakage for version > 0.47.1
Browse files Browse the repository at this point in the history
rubocop/rubocop#4226 indicates that `--stdin`
takes filename as an argument which was incorporated. A part of the
fixing was to improve the tests that involved the bear to be run on
files with the `.rb`(ruby) extension because initially the bear was
run on the generated temporary files that was the cause of error.

Fixes #1548
  • Loading branch information
yash-nisar committed Jul 6, 2017
1 parent 012b7d1 commit d35b120
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 49 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Expand Up @@ -4,7 +4,7 @@ ruby '2.2.2'
gem "csvlint"
gem "puppet-lint", "2.1.1"
gem "reek"
gem "rubocop", "0.47.1"
gem "rubocop", "0.49.1"
gem "scss_lint", require: false
gem "sqlint"
gem "travis", "1.8.8"
4 changes: 2 additions & 2 deletions bears/ruby/RuboCopBear.py
Expand Up @@ -19,7 +19,7 @@ class RuboCopBear:
"""

LANGUAGES = {'Ruby'}
REQUIREMENTS = {GemRequirement('rubocop', '0.47.1'),
REQUIREMENTS = {GemRequirement('rubocop', '0.49.1'),
PipRequirement('pyyaml', '3.12')}
AUTHORS = {'The coala developers'}
AUTHORS_EMAILS = {'coala-devel@googlegroups.com'}
Expand All @@ -36,7 +36,7 @@ class RuboCopBear:
def create_arguments(filename, file, config_file, rubocop_config: str=''):
# Need both stdin and filename. Explained in this comment:
# https://github.com/bbatsov/rubocop/pull/2146#issuecomment-131403694
args = (filename, '--stdin', '--format=json')
args = ('--stdin', filename, '--format=json')
if rubocop_config:
args += ('--config', rubocop_config)
else:
Expand Down
146 changes: 100 additions & 46 deletions tests/ruby/RuboCopBearTest.py
@@ -1,57 +1,111 @@
import os
from queue import Queue

from coalib.results.Result import Result
from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY
from coalib.settings.Section import Section
from coalib.testing.BearTestHelper import generate_skip_decorator
from coalib.testing.LocalBearTestHelper import LocalBearTestHelper

from bears.ruby.RuboCopBear import RuboCopBear
from coalib.testing.LocalBearTestHelper import verify_local_bear

good_file = """def good_name
test if something
end
"""

bad_file = """def badName
test if something
end
"""
def get_testfile_path(name):
return os.path.join(os.path.dirname(__file__),
'test_files',
name)


def load_testfile(name):
with open(get_testfile_path(name)) as f:
return f.readlines()


@generate_skip_decorator(RuboCopBear)
class RuboCopBearTest(LocalBearTestHelper):

RuboCopBearTest = verify_local_bear(RuboCopBear,
invalid_files=(bad_file,),
valid_files=(good_file,))
def setUp(self):
self.uut = RuboCopBear(Section('name'), Queue())

# Testing Config
rubocop_config = os.path.join(os.path.dirname(__file__),
'test_files',
'rubocop_config.yaml')
def test_good_file(self):
filename = 'good_file.rb'
file_contents = load_testfile(filename)
self.check_results(
self.uut,
file_contents,
[],
filename=get_testfile_path(filename))

def test_bad_file(self):
filename = 'bad_file.rb'
file_contents = load_testfile(filename)
self.check_results(
self.uut,
file_contents,
[Result.from_values('RuboCopBear (Style/MethodName)',
message='Use snake_case for method names.',
file=get_testfile_path(filename),
line=1,
column=5,
end_line=1,
end_column=12,
severity=RESULT_SEVERITY.INFO)],
filename=get_testfile_path(filename))


# bad file becomes good and vice-versa
RuboCopBearConfigFileTest = verify_local_bear(
RuboCopBear,
valid_files=(bad_file,),
invalid_files=(good_file,),
settings={'rubocop_config': rubocop_config})

# Testing settings
another_good_file = """
def goodindent
# 1 space indent
end
"""

another_bad_file = """
def badindent
# 2 spaces indent
end
"""

RuboCopBearSettingsTest = verify_local_bear(
RuboCopBear,
valid_files=(another_good_file,),
invalid_files=(another_bad_file,),
settings={'indent_size': 1})

RuboCopBearSettingsTest = verify_local_bear(
RuboCopBear,
valid_files=(bad_file,),
invalid_files=(good_file,),
settings={'method_name_case': 'camel'})
def test_bad_config_file(self):
filename = 'good_file.rb'
file_contents = load_testfile(filename)
self.check_results(
self.uut,
file_contents,
[Result.from_values('RuboCopBear (Style/MethodName)',
message='Use camelCase for method names.',
file=get_testfile_path(filename),
line=1,
column=5,
end_line=1,
end_column=14,
severity=RESULT_SEVERITY.INFO)],
filename=get_testfile_path(filename),
settings={'rubocop_config': get_testfile_path(
'rubocop_config.yaml')})

def test_good_config_file(self):
filename = 'bad_file.rb'
file_contents = load_testfile(filename)
self.check_results(
self.uut,
file_contents,
[],
filename=get_testfile_path(filename),
settings={'rubocop_config': get_testfile_path(
'rubocop_config.yaml')})

def test_bad_indent_size(self):
filename = 'indent_file.rb'
file_contents = load_testfile(filename)
self.check_results(
self.uut,
file_contents,
[Result.from_values('RuboCopBear (Layout/CommentIndentation)',
message='Incorrect indentation detected '
'(column 2 instead of 1).',
file=get_testfile_path(filename),
line=2,
column=3,
end_line=2,
end_column=20,
severity=RESULT_SEVERITY.INFO)],
filename=get_testfile_path(filename),
settings={'indent_size': 1})

def test_good_indent_size(self):
filename = 'indent_file.rb'
file_contents = load_testfile(filename)
self.check_results(
self.uut,
file_contents,
[],
filename=get_testfile_path(filename))
3 changes: 3 additions & 0 deletions tests/ruby/test_files/bad_file.rb
@@ -0,0 +1,3 @@
def badName
test if something
end
3 changes: 3 additions & 0 deletions tests/ruby/test_files/good_file.rb
@@ -0,0 +1,3 @@
def good_name
test if something
end
3 changes: 3 additions & 0 deletions tests/ruby/test_files/indent_file.rb
@@ -0,0 +1,3 @@
def badindent
# 2 spaces indent
end

0 comments on commit d35b120

Please sign in to comment.