Skip to content

Commit

Permalink
Merge pull request #362 from chef/new_cops
Browse files Browse the repository at this point in the history
Add new poise_archive cop and definition cop
  • Loading branch information
tas50 authored Nov 5, 2019
2 parents 0bc0c97 + 9f88074 commit 956b9ec
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 4 deletions.
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Cookstyle Changelog

<!-- latest_release 5.10.15 -->
## [v5.10.15](https://github.com/chef/cookstyle/tree/v5.10.15) (2019-11-04)
<!-- latest_release unreleased -->
## Unreleased

#### Merged Pull Requests
- Fix docs updater script to work with Rubocop 0.75+ [#361](https://github.com/chef/cookstyle/pull/361) ([tas50](https://github.com/tas50))
- Update to Rubocop 0.75.1 [#359](https://github.com/chef/cookstyle/pull/359) ([tas50](https://github.com/tas50))
<!-- latest_release -->

<!-- release_rollup since=5.10.13 -->
Expand Down
12 changes: 12 additions & 0 deletions config/cookstyle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,11 @@ ChefDeprecations/VerifyPropertyUsesFileExpansion:
Exclude:
- '**/metadata.rb'

ChefDeprecations/PoiseArchiveUsage:
Description: The poise_archive resource in the deprecated poise-archive should be replaced with the archive_file resource found in Chef Infra Client 15+.
Enabled: true
VersionAdded: '5.11.0'

###############################
# ChefModernize: Cleaning up legacy code and using new built-in resources
###############################
Expand Down Expand Up @@ -823,6 +828,13 @@ ChefModernize/ResourceNameFromInitialize:
- '**/providers/*.rb'
- '**/libraries/*.rb'

ChefModernize/Definitions:
Description: Legacy Chef Infra definitions should be rewritten as custom resources to take full advantage of the Chef Infra feature set.
Enabled: true
VersionAdded: '5.11.0'
Include:
- '**/definitions/*.rb'

###############################
# Migrating to new patterns
###############################
Expand Down
59 changes: 59 additions & 0 deletions lib/rubocop/cop/chef/deprecation/poise_archive.rb
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
35 changes: 35 additions & 0 deletions lib/rubocop/cop/chef/modernize/definitions.rb
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
47 changes: 47 additions & 0 deletions spec/rubocop/cop/chef/deprecation/poise_archive_spec.rb
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
30 changes: 30 additions & 0 deletions spec/rubocop/cop/chef/modernize/definitions_spec.rb
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
1 change: 0 additions & 1 deletion tasks/cops_documentation.rake
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ begin
end

def references(config, cop)
cop_config = config.for_cop(cop)
urls = RuboCop::Cop::MessageAnnotator.new(config, cop.name, config.for_cop(cop), {}).urls
return '' if urls.empty?

Expand Down

0 comments on commit 956b9ec

Please sign in to comment.