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 3 new cops #522

Merged
merged 1 commit into from
Feb 19, 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
25 changes: 25 additions & 0 deletions config/cookstyle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,15 @@ ChefModernize/ExecuteSysctl:
- '**/attributes/*.rb'
- '**/Berksfile'

ChefModernize/SimplifyAptPpaSetup:
Description: The apt_repository resource allows setting up PPAs without using the full URL to ppa.launchpad.net.
Enabled: true
VersionAdded: '5.21.0'
Exclude:
- '**/metadata.rb'
- '**/attributes/*.rb'
- '**/Berksfile'

###############################
# ChefRedundantCode: Cleanup unnecessary code in your cookbooks regardless of Chef Infra Client release
###############################
Expand Down Expand Up @@ -1247,6 +1256,22 @@ ChefRedundantCode/GroupingMetadata:
Include:
- '**/metadata.rb'

ChefRedundantCode/StringPropertyWithNilDefault:
Description: 'Properties have a nil value by default so there is no need to set the default value to nil.'
Enabled: true
VersionAdded: '5.21.0'
Include:
- '**/resources/*.rb'
- '**/libraries/*.rb'

ChefRedundantCode/PropertySplatRegex:
Description: 'There is no need to validate the input of properties in resources using a regex value that will always pass.'
Enabled: true
VersionAdded: '5.21.0'
Include:
- '**/resources/*.rb'
- '**/libraries/*.rb'

###############################
# Migrating to new patterns
###############################
Expand Down
67 changes: 67 additions & 0 deletions lib/rubocop/cop/chef/modernize/simplify_apt_ppa_setup.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#
# 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
module ChefModernize
# The apt_repository resource allows setting up PPAs without using the full URL to ppa.launchpad.net, which should be used to simplify the resource code in your cookbooks.
#
# @example
#
# # bad
# apt_repository 'atom-ppa' do
# uri 'http://ppa.launchpad.net/webupd8team/atom/ubuntu'
# components ['main']
# keyserver 'keyserver.ubuntu.com'
# key 'C2518248EEA14886'
# end
#
# # good
# apt_repository 'atom-ppa' do
# uri 'ppa:webupd8team/atom'
# components ['main']
# keyserver 'keyserver.ubuntu.com'
# key 'C2518248EEA14886'
# end
#
class SimplifyAptPpaSetup < Cop
include RangeHelp
include RuboCop::Chef::CookbookHelpers

MSG = 'The apt_repository resource allows setting up PPAs without using the full URL to ppa.launchpad.net.'.freeze

def on_block(node)
match_property_in_resource?(:apt_repository, 'uri', node) do |uri|
if %r{http(s)*://ppa.launchpad.net/(.*)/ubuntu$}.match?(uri.arguments&.first&.str_content)
add_offense(uri, location: :expression, message: MSG, severity: :refactor)
end
end
end

def autocorrect(node)
if (replacement_val = %r{http(s)*://ppa.launchpad.net/(.*)/ubuntu}.match(node.source)[2])
lambda do |corrector|
corrector.replace(node.loc.expression, "uri 'ppa:#{replacement_val}'")
end
end
end
end
end
end
end
end
58 changes: 58 additions & 0 deletions lib/rubocop/cop/chef/redundant/property_splat_regex.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#
# 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.
#
module RuboCop
module Cop
module Chef
module ChefRedundantCode
# When a property has a type of String it can accept any string. There is no need to also validate string inputs against a regex that accept all values.
#
# @example
#
# # bad
# property :config_file, String, regex: /.*/
# attribute :config_file, String, regex: /.*/
#
# # good
# property :config_file, String
# attribute :config_file, String
#
class PropertySplatRegex < Cop
include RangeHelp

MSG = 'There is no need to validate the input of properties in resources using a regex value that will always pass.'.freeze

def_node_matcher :property_with_regex_splat?, <<-PATTERN
(send nil? {:property :attribute} (sym _) ... (hash <$(pair (sym :regex) (regexp (str ".*") (regopt))) ...>))
PATTERN

def on_send(node)
property_with_regex_splat?(node) do |splat|
add_offense(splat, location: :expression, message: MSG, severity: :refactor)
end
end

def autocorrect(node)
lambda do |corrector|
range = range_with_surrounding_comma(range_with_surrounding_space(range: node.loc.expression, side: :left), :left)
corrector.remove(range)
end
end
end
end
end
end
end
65 changes: 65 additions & 0 deletions lib/rubocop/cop/chef/redundant/string_property_with_nil_default.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#
# Copyright:: Copyright 2019-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
module ChefRedundantCode
# Properties have a nil value by default so there is no need to set the default value to nil.
#
# @example
#
# # bad
# property :config_file, String, default: nil
# property :config_file, [String, NilClass], default: nil
#
# # good
# property :config_file, String
# property :config_file, [String, NilClass]
#
class StringPropertyWithNilDefault < Cop
include RangeHelp

MSG = 'Properties have a nil value by default so there is no need to set the default value to nil.'.freeze

def_node_matcher :string_property_with_nil_default?, <<-PATTERN
(send nil? :property (sym _)
{(const nil? :String) #string_and_nil_like?}
(hash <$(pair (sym :default) (nil)) ...>))
PATTERN

# An array of types that includes String & either NilClass or nil
def_node_matcher :string_and_nil_like?, <<-PATTERN
(array <(const nil? :String) {(const nil? :NilClass) (nil)}>)
PATTERN

def on_send(node)
string_property_with_nil_default?(node) do |nil_default|
add_offense(nil_default, location: :expression, message: MSG, severity: :refactor)
end
end

def autocorrect(node)
lambda do |corrector|
range = range_with_surrounding_comma(range_with_surrounding_space(range: node.loc.expression, side: :left), :left)
corrector.remove(range)
end
end
end
end
end
end
end
54 changes: 54 additions & 0 deletions spec/rubocop/cop/chef/modernize/simplify_apt_ppa_setup_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#
# 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::ChefModernize::SimplifyAptPpaSetup, :config do
subject(:cop) { described_class.new(config) }

it 'registers an offense and apt_repository contains a PPA repo in http(s) form' do
expect_offense(<<~RUBY)
apt_repository 'atom-ppa' do
uri 'http://ppa.launchpad.net/webupd8team/atom/ubuntu'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The apt_repository resource allows setting up PPAs without using the full URL to ppa.launchpad.net.
components ['main']
keyserver 'keyserver.ubuntu.com'
key 'C2518248EEA14886'
end
RUBY
end

it "doesn't register an offense when apt_repository sets a non-PPA uri" do
expect_no_offenses(<<~RUBY)
apt_repository 'atom-ppa' do
uri 'http://foo.bar.com/ubuntu'
components ['main']
end
RUBY
end

it "doesn't register an offense when apt_repository sets a non-PPA uri" do
expect_no_offenses(<<~RUBY)
apt_repository 'atom-ppa' do
uri 'ppa:webupd8team/atom'
components ['main']
keyserver 'keyserver.ubuntu.com'
key 'C2518248EEA14886'
end
RUBY
end
end
44 changes: 44 additions & 0 deletions spec/rubocop/cop/chef/redundant/property_splat_regex_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#
# 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::ChefRedundantCode:: PropertySplatRegex, :config do
subject(:cop) { described_class.new(config) }

it 'registers an offense when a property includes regex: /.*/' do
expect_offense(<<~RUBY)
property :foo, String, regex: /.*/
^^^^^^^^^^^ There is no need to validate the input of properties in resources using a regex value that will always pass.
RUBY

expect_correction("property :foo, String\n")
end

it 'registers an offense when an attribute includes regex: /.*/' do
expect_offense(<<~RUBY)
attribute :foo, String, regex: /.*/
^^^^^^^^^^^ There is no need to validate the input of properties in resources using a regex value that will always pass.
RUBY

expect_correction("attribute :foo, String\n")
end

it "doesn't register an offense when a property sets desired_state: false" do
expect_no_offenses('property :foo, String, desired_state: false')
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#
# 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::ChefRedundantCode::StringPropertyWithNilDefault, :config do
subject(:cop) { described_class.new(config) }

it 'registers an offense when a String property has a nil default' do
expect_offense(<<~RUBY)
property :config_file, String, default: nil
^^^^^^^^^^^^ Properties have a nil value by default so there is no need to set the default value to nil.
RUBY

expect_correction("property :config_file, String\n")
end

it 'registers an offense when a String/NilClass property has a nil default' do
expect_offense(<<~RUBY)
property :config_file, [String, NilClass], default: nil
^^^^^^^^^^^^ Properties have a nil value by default so there is no need to set the default value to nil.
RUBY

expect_correction("property :config_file, [String, NilClass]\n")
end

it 'registers an offense when a String/nil property has a nil default' do
expect_offense(<<~RUBY)
property :config_file, [String, nil], default: nil
^^^^^^^^^^^^ Properties have a nil value by default so there is no need to set the default value to nil.
RUBY

expect_correction("property :config_file, [String, nil]\n")
end

it "doesn't register an offense when a property has a non-nil default" do
expect_no_offenses(<<~RUBY)
property :bob, String, default: 'foo'
RUBY
end
end