Skip to content

Commit

Permalink
Merge pull request #246 from chef/new_cops
Browse files Browse the repository at this point in the history
Add Chef/IncludingOhaiDefaultRecipe Chef/IncludingXMLRubyRecipe & Chef/LegacyYumCookbookRecipes
  • Loading branch information
tas50 authored Aug 27, 2019
2 parents 8e94069 + 4a3c88c commit 455562e
Show file tree
Hide file tree
Showing 8 changed files with 339 additions and 4 deletions.
29 changes: 28 additions & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
## Unreleased

### 9 New Chef Cops

#### Chef/IncludingOhaiDefaultRecipe

#### Chef/IncludingXMLRubyRecipe

#### Chef/LegacyYumCookbookRecipes

#### Chef/DefaultMetadataMaintainer

#### Chef/UsesDeprecatedMixins

#### Chef/MinitestHandlerUsage

#### Chef/WindowsVersionHelper

#### Chef/WindowsZipfileUsage

#### Chef/NodeMethodsInsteadofAttributes

### Other fixes and changes

- Multiple cops will now skip scanning metadata.rb to speed up cookstyle runs
- Chef/MetadataMissingName now supports autocorrecting. The folder containing the metadata.rb will be used as the cookbook name if it's missing from metdata.rb

## Cookstyle 5.3

### New Chef Cops
### 5 New Chef Cops

#### Chef/IncludingAptDefaultRecipe

Expand Down
27 changes: 24 additions & 3 deletions config/cookstyle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,16 @@ Chef/UsesDeprecatedMixins:
Description: Don't use deprecated Mixins no longer included in Chef Infra Client 14 and later.
Enabled: true
VersionAdded: '5.4.0'
# Include:
# - '**/libraries/*.rb'
# - '**/providers/*.rb'
Include:
- '**/libraries/*.rb'
- '**/providers/*.rb'

Chef/LegacyYumCookbookRecipes:
Description: The elrepo, epel, ius, remi, and repoforge recipes were split into their own cookbooks and the yum recipe was renamed to be default with the release of yum cookbook 3.0 (Dec 2013).
Enabled: true
VersionAdded: '5.4.0'
Exclude:
- '**/metadata.rb'

###############################
# Cleaning up Legacy Code
Expand Down Expand Up @@ -395,6 +402,20 @@ Chef/MinitestHandlerUsage:
Include:
- '**/metadata.rb'

Chef/IncludingOhaiDefaultRecipe:
Description: Use the ohai_plugin resource to ship custom Ohai plugins instead of using the ohai::default recipe. If you're not shipping custom Ohai plugins, then you can remove this recipe entirely.
Enabled: true
VersionAdded: '5.4.0'
Exclude:
- '**/metadata.rb'

Chef/IncludingXMLRubyRecipe:
Description: The xml::ruby recipe installs nokogiri which is included in Chef Infra Client 12 and later.
Enabled: true
VersionAdded: '5.4.0'
Exclude:
- '**/metadata.rb'

###############################
# Detecting code that breaks Chef
###############################
Expand Down
49 changes: 49 additions & 0 deletions lib/rubocop/cop/chef/deprecation/legacy_yum_cookbook_recipes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#
# Copyright:: 2019, 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
# The Ohai default recipe previously allowed a user to ship custom Ohai plugins to a system by including them
# in a directory in the Ohai cookbook. This functionality was replaced with the ohai_plugin resource, which
# should be used instead as it doesn't require forking the ohai cookbook.
#
# @example
#
# # bad
# include_recipe 'yum::elrepo'
# include_recipe 'yum::epel'
# include_recipe 'yum::ius'
# include_recipe 'yum::remi'
# include_recipe 'yum::repoforge'
# include_recipe 'yum::yum'
#
class LegacyYumCookbookRecipes < Cop
MSG = 'The elrepo, epel, ius, remi, and repoforge recipes were split into their own cookbooks and the yum recipe was renamed to be default with the release of yum cookbook 3.0 (Dec 2013).'.freeze

def_node_matcher :old_yum_recipe?, <<-PATTERN
(send nil? :include_recipe (str {"yum::elrepo" "yum::epel" "yum::ius" "yum::remi" "yum::repoforge" "yum::yum"}))
PATTERN

def on_send(node)
old_yum_recipe?(node) do
add_offense(node, location: :expression, message: MSG, severity: :refactor)
end
end
end
end
end
end
45 changes: 45 additions & 0 deletions lib/rubocop/cop/chef/modernize/ohai_default_recipe.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#
# Copyright:: 2019, 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
# The Ohai default recipe previously allowed a user to ship custom Ohai plugins to a system by including them
# in a directory in the Ohai cookbook. This functionality was replaced with the ohai_plugin resource, which
# should be used instead as it doesn't require forking the ohai cookbook.
#
# @example
#
# # bad
# include_recipe 'ohai::default'
# include_recipe 'ohai'
#
class IncludingOhaiDefaultRecipe < Cop
MSG = "Use the ohai_plugin resource to ship custom Ohai plugins instead of using the ohai::default recipe. If you're not shipping custom Ohai plugins, then you can remove this recipe entirely".freeze

def_node_matcher :ohai_recipe_usage?, <<-PATTERN
(send nil? :include_recipe (str {"ohai" "ohai::default"}))
PATTERN

def on_send(node)
ohai_recipe_usage?(node) do
add_offense(node, location: :expression, message: MSG, severity: :refactor)
end
end
end
end
end
end
49 changes: 49 additions & 0 deletions lib/rubocop/cop/chef/modernize/xml_ruby_recipe.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#
# Copyright:: 2019, 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
# The xml::ruby recipe was used to install nokogiri into the Chef installation. Nokogiri is included
# in Chef Infra Client 12 and later so this recipe is no longer necessary.
#
# @example
#
# # bad
# include_recipe 'xml::ruby'
#
class IncludingXMLRubyRecipe < Cop
MSG = 'The xml::ruby recipe installs nokogiri which is included in Chef Infra Client 12 and later.'.freeze

def_node_matcher :xml_ruby_recipe?, <<-PATTERN
(send nil? :include_recipe (str "xml::ruby"))
PATTERN

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

def autocorrect(node)
lambda do |corrector|
corrector.remove(node.loc.expression)
end
end
end
end
end
end
41 changes: 41 additions & 0 deletions spec/rubocop/cop/chef/correctness/ohai_default_recipe_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#
# Copyright:: 2019, Chef Software, Inc.
#
# 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::IncludingOhaiDefaultRecipe, :config do
subject(:cop) { described_class.new(config) }

it 'registers an offense when a recipe includes "ohai::default"' do
expect_offense(<<~RUBY)
include_recipe 'ohai::default'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use the ohai_plugin resource to ship custom Ohai plugins instead of using the ohai::default recipe. If you're not shipping custom Ohai plugins, then you can remove this recipe entirely
RUBY
end

it 'registers an offense when a recipe includes "ohai"' do
expect_offense(<<~RUBY)
include_recipe 'ohai'
^^^^^^^^^^^^^^^^^^^^^ Use the ohai_plugin resource to ship custom Ohai plugins instead of using the ohai::default recipe. If you're not shipping custom Ohai plugins, then you can remove this recipe entirely
RUBY
end

it "doesn't register an offense when a recipe includes another recipe" do
expect_no_offenses(<<~RUBY)
include_recipe 'ohai::other_thing'
RUBY
end
end
34 changes: 34 additions & 0 deletions spec/rubocop/cop/chef/correctness/xml_ruby_recipe_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#
# Copyright:: 2019, Chef Software, Inc.
#
# 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::IncludingXMLRubyRecipe, :config do
subject(:cop) { described_class.new(config) }

it 'registers an offense when a recipe includes "xml::ruby"' do
expect_offense(<<~RUBY)
include_recipe 'xml::ruby'
^^^^^^^^^^^^^^^^^^^^^^^^^^ The xml::ruby recipe installs nokogiri which is included in Chef Infra Client 12 and later.
RUBY
end

it "doesn't register an offense when a recipe includes another recipe" do
expect_no_offenses(<<~RUBY)
include_recipe 'xml::other_thing'
RUBY
end
end
69 changes: 69 additions & 0 deletions spec/rubocop/cop/chef/deprecation/legacy_yum_cookbook_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#
# Copyright:: 2019, Chef Software, Inc.
#
# 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::LegacyYumCookbookRecipes, :config do
subject(:cop) { described_class.new(config) }

it 'registers an offense when a cookbook includes "yum::elrepo"' do
expect_offense(<<~RUBY)
include_recipe 'yum::elrepo'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The elrepo, epel, ius, remi, and repoforge recipes were split into their own cookbooks and the yum recipe was renamed to be default with the release of yum cookbook 3.0 (Dec 2013).
RUBY
end

it 'registers an offense when a cookbook includes "yum::epel"' do
expect_offense(<<~RUBY)
include_recipe 'yum::epel'
^^^^^^^^^^^^^^^^^^^^^^^^^^ The elrepo, epel, ius, remi, and repoforge recipes were split into their own cookbooks and the yum recipe was renamed to be default with the release of yum cookbook 3.0 (Dec 2013).
RUBY
end

it 'registers an offense when a cookbook includes "yum::ius"' do
expect_offense(<<~RUBY)
include_recipe 'yum::ius'
^^^^^^^^^^^^^^^^^^^^^^^^^ The elrepo, epel, ius, remi, and repoforge recipes were split into their own cookbooks and the yum recipe was renamed to be default with the release of yum cookbook 3.0 (Dec 2013).
RUBY
end

it 'registers an offense when a cookbook includes "yum::remi"' do
expect_offense(<<~RUBY)
include_recipe 'yum::remi'
^^^^^^^^^^^^^^^^^^^^^^^^^^ The elrepo, epel, ius, remi, and repoforge recipes were split into their own cookbooks and the yum recipe was renamed to be default with the release of yum cookbook 3.0 (Dec 2013).
RUBY
end

it 'registers an offense when a cookbook includes "yum::repoforge"' do
expect_offense(<<~RUBY)
include_recipe 'yum::repoforge'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The elrepo, epel, ius, remi, and repoforge recipes were split into their own cookbooks and the yum recipe was renamed to be default with the release of yum cookbook 3.0 (Dec 2013).
RUBY
end

it 'registers an offense when a cookbook includes "yum::yum"' do
expect_offense(<<~RUBY)
include_recipe 'yum::yum'
^^^^^^^^^^^^^^^^^^^^^^^^^ The elrepo, epel, ius, remi, and repoforge recipes were split into their own cookbooks and the yum recipe was renamed to be default with the release of yum cookbook 3.0 (Dec 2013).
RUBY
end

it "doesn't register an offense when including yum::default" do
expect_no_offenses(<<~RUBY)
include_recipe 'yum::default'
RUBY
end
end

0 comments on commit 455562e

Please sign in to comment.