-
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 #362 from chef/new_cops
Add new poise_archive cop and definition cop
- Loading branch information
Showing
7 changed files
with
186 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
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,59 @@ | ||
# | ||
# 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 | ||
module ChefDeprecations | ||
# The poise_archive resource in the deprecated poise-archive should be replaced with the archive_file resource found in Chef Infra Client 15+. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# poise_archive 'https://example.com/myapp.tgz' do | ||
# destination '/opt/myapp' | ||
# end | ||
# | ||
# # good | ||
# archive_file 'https://example.com/myapp.tgz' do | ||
# destination '/opt/myapp' | ||
# end | ||
# | ||
class PoiseArchiveUsage < Cop | ||
include RuboCop::Chef::CookbookHelpers | ||
|
||
MSG = 'The poise_archive resource in the deprecated poise-archive should be replaced with the archive_file resource found in Chef Infra Client 15+'.freeze | ||
|
||
def_node_matcher :depends_poise_archive?, <<-PATTERN | ||
(send nil? :depends (str "poise-archive")) | ||
PATTERN | ||
|
||
def on_send(node) | ||
depends_poise_archive?(node) do | ||
add_offense(node, location: :expression, message: MSG, severity: :refactor) | ||
end | ||
end | ||
|
||
def on_block(node) | ||
match_resource_type?(:poise_archive, node) do | ||
add_offense(node, location: :expression, message: MSG, severity: :refactor) | ||
end | ||
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,35 @@ | ||
# | ||
# 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 | ||
module ChefModernize | ||
# In 2016 with Chef Infra Client 12.5 Custom Resources were introduced as a way of writing reusable resource code that could be shipped in cookbooks. Custom Resources offer many advantages of legacy Definitions including unit testing with ChefSpec, input validation, actions, commmon properties like not_if/only_if, and resource reporting. | ||
# | ||
class Definitions < Cop | ||
include RuboCop::Chef::CookbookHelpers | ||
|
||
MSG = 'Legacy Chef Infra definitions should be rewritten as custom resources to take full advantage of the Chef Infra feature set.'.freeze | ||
|
||
def on_block(node) | ||
add_offense(node, location: :expression, message: MSG, severity: :refactor) if node.respond_to?(:method_name) && node.method_name == :define | ||
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,47 @@ | ||
# | ||
# 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::ChefDeprecations::PoiseArchiveUsage, :config do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
it 'registers an offense when using the poise_archive resource' do | ||
expect_offense(<<~RUBY) | ||
poise_archive 'Precompiled.zip' do | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The poise_archive resource in the deprecated poise-archive should be replaced with the archive_file resource found in Chef Infra Client 15+ | ||
path "foo/bar.zip" | ||
extract_to "/foo/bar" | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when depending on poise-archive cookbook' do | ||
expect_offense(<<~RUBY) | ||
depends 'poise-archive' | ||
^^^^^^^^^^^^^^^^^^^^^^^ The poise_archive resource in the deprecated poise-archive should be replaced with the archive_file resource found in Chef Infra Client 15+ | ||
RUBY | ||
end | ||
|
||
it "doesn't register an offense when using the archive_file resource" do | ||
expect_no_offenses(<<~RUBY) | ||
archive_file 'Precompiled.zip' do | ||
path '/tmp/Precompiled.zip' | ||
destination '/srv/files' | ||
end | ||
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,30 @@ | ||
# | ||
# 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::ChefModernize::Definitions, :config do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
it 'registers an offense when defining a definition' do | ||
expect_offense(<<~RUBY) | ||
define :foo, :variables => {}, :config_subdir => true do | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Legacy Chef Infra definitions should be rewritten as custom resources to take full advantage of the Chef Infra feature set. | ||
apt_update | ||
end | ||
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