Skip to content

Commit

Permalink
Ignore non string arguments for file path cop
Browse files Browse the repository at this point in the history
  • Loading branch information
geniou committed Mar 26, 2015
1 parent d1be39f commit 1f8ac1f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
9 changes: 4 additions & 5 deletions lib/rubocop/cop/rspec/file_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ def on_top_level_describe(node, args)
object = const_name(args.first)
return unless object

method = args[1]
return unless method.nil? || method.type == :str

path_matcher = matcher(object, method)
path_matcher = matcher(object, args[1])
return if source_filename =~ regexp_from_glob(path_matcher)

add_offense(node, :expression, format(MESSAGE, path_matcher))
Expand All @@ -38,7 +35,9 @@ def on_top_level_describe(node, args)

def matcher(object, method)
path = File.join(parts(object))
path += '*' + method.children.first.gsub(/\W+/, '') if method
if method && method.type == :str
path += '*' + method.children.first.gsub(/\W+/, '')
end

"#{path}*_spec.rb"
end
Expand Down
12 changes: 8 additions & 4 deletions spec/rubocop/cop/rspec/file_path_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,14 @@
expect(cop.offenses).to be_empty
end

it 'skips specs not having a string second argument' do
inspect_source(cop, 'describe Some::Class, :config do; end')

expect(cop.offenses).to be_empty
it 'ignores second argument if is not a string' do
inspect_source(cop,
'describe MyClass, :foo do; end',
'wrong_class_spec.rb')
expect(cop.offenses.size).to eq(1)
expect(cop.offenses.map(&:line).sort).to eq([1])
expect(cop.messages)
.to eq(['Spec path should end with `my_class*_spec.rb`'])
end

it 'checks class specs' do
Expand Down

0 comments on commit 1f8ac1f

Please sign in to comment.