Skip to content

Commit

Permalink
Fix false negatives in Rails/NotNullColumn cop (rubocop#3508)
Browse files Browse the repository at this point in the history
  • Loading branch information
pocke authored and Neodelf committed Oct 15, 2016
1 parent aa27c62 commit 7027681
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
* [#3450](https://github.com/bbatsov/rubocop/issues/3450): Prevent `Style/TernaryParentheses` cop from making unsafe corrections. ([@drenmi][])
* [#3460](https://github.com/bbatsov/rubocop/issues/3460): Fix false positives in `Style/InlineComment` cop. ([@drenmi][])
* [#3485](https://github.com/bbatsov/rubocop/issues/3485): Make OneLineConditional cop not register offense for empty else. ([@tejasbubane][])
* [#3508](https://github.com/bbatsov/rubocop/pull/3508): Fix false negatives in `Rails/NotNullColumn`. ([@pocke][])

### Changes

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rails/not_null_column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class NotNullColumn < Cop
PATTERN

def_node_matcher :has_default?, <<-PATTERN
(pair (sym :default) _)
(pair (sym :default) !(:nil))
PATTERN

def on_send(node)
Expand Down
30 changes: 16 additions & 14 deletions spec/rubocop/cop/rails/not_null_column_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,29 @@
let(:source) do
'add_column :users, :name, :string, null: false, default: ""'
end
it 'accepts' do
expect(cop.offenses).to be_empty
include_examples 'accepts'
end

context 'with null: false and default: nil' do
let(:source) do
'add_column :users, :name, :string, null: false, default: nil'
end
it 'reports an offense' do
expect(cop.offenses.size).to eq(1)
expect(cop.messages).to eq(
['Do not add a NOT NULL column without a default value']
)
end
end

context 'with null: true' do
let(:source) { 'add_column :users, :name, :string, null: true' }
it 'accepts' do
expect(cop.offenses).to be_empty
end
include_examples 'accepts'
end

context 'without any options' do
let(:source) { 'add_column :users, :name, :string' }
it 'accepts' do
expect(cop.offenses).to be_empty
end
include_examples 'accepts'
end
end

Expand All @@ -54,9 +60,7 @@
'change_column :users, :name, :string, null: false'
]
end
it 'accepts' do
expect(cop.offenses).to be_empty
end
include_examples 'accepts'
end

context 'with create_table call' do
Expand All @@ -70,8 +74,6 @@
' end',
'end']
end
it 'accepts' do
expect(cop.offenses).to be_empty
end
include_examples 'accepts'
end
end

0 comments on commit 7027681

Please sign in to comment.