Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ChefCorrectness/InvalidDefaultAction cop #685

Merged
merged 3 commits into from
Jul 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions config/cookstyle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,15 @@ ChefCorrectness/InvalidPlatformInCase:
- '**/metadata.rb'
- '**/Berksfile'

ChefCorrectness/InvalidDefaultAction:
Description: Default actions in resources should be a symbol or an array of symbols.
StyleGuide: '#chefcorrectnessinvaliddefaultaction'
Enabled: true
VersionAdded: '6.13.0'
Include:
- '**/libraries/*.rb'
- '**/resources/*.rb'

###############################
# ChefSharing: Issues that prevent sharing code with other teams or with the Chef community in general
###############################
Expand Down
48 changes: 48 additions & 0 deletions lib/rubocop/cop/chef/correctness/invalid_default_action.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true
#
# Copyright:: 2020, Chef Software Inc.
# Author:: Tim Smith (<tsmith@chef.io>)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

module RuboCop
module Cop
module Chef
# Default actions in resources should be symbols or an array of symbols.
#
# @example
#
# # bad
# default_action 'create'
#
# # good
# default_action :create
#
module ChefCorrectness
class InvalidDefaultAction < Cop
MSG = 'Default actions in resources should be symbols or an array of symbols.'

def_node_matcher :default_action?, '(send nil? :default_action $_)'

def on_send(node)
default_action?(node) do |match|
return if %i(send sym array).include?(match.type)
add_offense(node, location: :expression, message: MSG, severity: :refactor)
end
end
end
end
end
end
end
42 changes: 42 additions & 0 deletions spec/rubocop/cop/chef/correctness/invalid_default_action_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true
#
# Copyright:: Copyright 2020, Chef Software Inc.
# Author:: Tim Smith (<tsmith@chef.io>)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'spec_helper'

describe RuboCop::Cop::Chef::ChefCorrectness::InvalidDefaultAction do
subject(:cop) { described_class.new }

it 'registers an offense when default action is a string' do
expect_offense(<<~RUBY)
default_action 'create'
^^^^^^^^^^^^^^^^^^^^^^^ Default actions in resources should be symbols or an array of symbols.
RUBY
end

it 'does not register an offense with a symbol' do
expect_no_offenses('default_action :create')
end

it 'does not register an offense with a variable' do
expect_no_offenses('default_action foo')
end

it 'does not register an offense with an array of symbols' do
expect_no_offenses('default_action [:create, :enable]')
end
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might add a test for an array of non-symbols.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now I just sorta pass on detecting that. Any array is fine. I saw my first case of someone using that while writing this and I don't think anyone really realizes it's even an option.