Skip to content

Commit

Permalink
Fix rake new_cop problem that doesn't add require line (#4722)
Browse files Browse the repository at this point in the history
  • Loading branch information
koic authored and bbatsov committed Sep 12, 2017
1 parent 04087f9 commit eb42226
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
* [#4643](https:/bbatsov/rubocop/issues/4643): Modify `Style/InverseMethods` to not register a separate offense for an inverse method nested inside of the block of an inverse method offense. ([@rrosenblum][])
* [#4593](https:/bbatsov/rubocop/issues/4593): Fix false positive in `Rails/SaveBang` when `save/update_attribute` is used with a `case` statement. ([@theRealNG][])
* [#4322](https:/bbatsov/rubocop/issues/4322): Fix Style/MultilineMemoization from autocorrecting to invalid ruby. ([@dpostorivo][])
* [#4722](https:/bbatsov/rubocop/pull/4722): Fix `rake new_cop` problem that doesn't add `require` line. ([@koic][])

### Changes

Expand Down
9 changes: 7 additions & 2 deletions lib/rubocop/cop/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,19 @@ def updated_directives

def target_line
@target_line ||= begin
in_the_same_department = false
inject_parts = require_path_fragments(injectable_require_directive)

require_entries.find.with_index do |entry, index|
current_entry_parts = require_path_fragments(entry)

next unless inject_parts[0..-2] == current_entry_parts[0..-2]
if inject_parts[0..-2] == current_entry_parts[0..-2]
in_the_same_department = true

break index if inject_parts.last < current_entry_parts.last
break index if inject_parts.last < current_entry_parts.last
elsif in_the_same_department
break index
end
end
end
end
Expand Down
59 changes: 59 additions & 0 deletions spec/rubocop/cop/generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,65 @@ def on_send(node)
end
end

context 'when a cop of style department already exists' do
let(:cop_identifier) { 'Style/TheEndOfStyle' }

before do
allow(File)
.to receive(:readlines).with('lib/rubocop.rb')
.and_return(<<-RUBY.strip_indent.lines)
# frozen_string_literal: true
require 'parser'
require 'rainbow'
require 'English'
require 'set'
require 'forwardable'
require 'rubocop/version'
require 'rubocop/cop/style/end_block'
require 'rubocop/cop/style/even_odd'
require 'rubocop/cop/style/file_name'
require 'rubocop/cop/style/flip_flop'
require 'rubocop/cop/rails/action_filter'
require 'rubocop/cop/team'
RUBY
end

it 'injects a `require` statement on the end of style department' do
generated_source = <<-RUBY.strip_indent
# frozen_string_literal: true
require 'parser'
require 'rainbow'
require 'English'
require 'set'
require 'forwardable'
require 'rubocop/version'
require 'rubocop/cop/style/end_block'
require 'rubocop/cop/style/even_odd'
require 'rubocop/cop/style/file_name'
require 'rubocop/cop/style/flip_flop'
require 'rubocop/cop/style/the_end_of_style'
require 'rubocop/cop/rails/action_filter'
require 'rubocop/cop/team'
RUBY

generator.inject_require
expect(File)
.to have_received(:write).with('lib/rubocop.rb', generated_source)
end
end

context 'when a `require` entry already exists' do
before do
allow(File)
Expand Down

0 comments on commit eb42226

Please sign in to comment.