-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #246 from chef/new_cops
Add Chef/IncludingOhaiDefaultRecipe Chef/IncludingXMLRubyRecipe & Chef/LegacyYumCookbookRecipes
- Loading branch information
Showing
8 changed files
with
339 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
lib/rubocop/cop/chef/deprecation/legacy_yum_cookbook_recipes.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
41
spec/rubocop/cop/chef/correctness/ohai_default_recipe_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
69
spec/rubocop/cop/chef/deprecation/legacy_yum_cookbook_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |