Skip to content

Commit

Permalink
fix caching issue with Chef/Deprecations/Delivery
Browse files Browse the repository at this point in the history
Signed-off-by: Doug Knight <[email protected]>
  • Loading branch information
karmix committed Feb 23, 2022
1 parent a0694a0 commit 45bd2a7
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 9 deletions.
17 changes: 12 additions & 5 deletions config/cookstyle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ AllCops:
SuggestExtensions: false
TargetRubyVersion: 2.5
TargetChefVersion: ~
inherit_mode:
merge:
- Include
Include:
- '**/.delivery/project.toml' # required by Chef/Deprecations/Delivery
- '**/.delivery/config.json' # required by Chef/Deprecations/Delivery
Exclude:
- '**/files/**/*'
- '**/vendor/**/*'
Expand Down Expand Up @@ -1265,12 +1271,13 @@ Chef/Deprecations/PolicyfileCommunitySource:
- '**/Policyfile.rb'

Chef/Deprecations/Delivery:
Description: Do not include a `.delivery` directory for the `delivery` command in your cookbooks. Chef Delivery (Workflow) went EOL Dec 31st 2021 and the delivery command was removed from Chef Workstation Feb 2022.
Description: Do not include Chef Delivery (Workflow) configuration in your cookbooks. Chef Delivery (Workflow) went EOL Dec 31st 2021 and the delivery command was removed from Chef Workstation Feb 2022.
StyleGuide: 'chef_deprecations_delivery'
Enabled: true
VersionAdded: '7.31.0'
Include:
- '**/metadata.rb'
- '**/.delivery/project.toml'
- '**/.delivery/config.json'

Chef/Deprecations/DeprecatedSudoActions:
Description: The `sudo` resource in the sudo cookbook 5.0 (2018) or Chef Infra Client 14 and later have replaced the existing `:install` and `:remove` actions with `:create` and `:delete` actions to better match other resources in Chef Infra.
Expand Down Expand Up @@ -1954,7 +1961,7 @@ Chef/Modernize/UseChefLanguageCloudHelpers:
- '**/resources/*.rb'
- '**/providers/*.rb'
- '**/recipes/*.rb'

Chef/Modernize/ClassEvalActionClass:
Description: In Chef Infra Client 12.9 and later it is no longer necessary to call the class_eval method on the action class block.
StyleGuide: 'chef_modernize_classevalactionclass'
Expand Down Expand Up @@ -2332,7 +2339,7 @@ Chef/Security/SshPrivateKey:
- '**/recipes/*.rb'
- '**/attributes/*.rb'
- '**/definitions/*.rb'

#### The base rubocop 0.37 enabled.yml file we started with ####

Layout/AccessModifierIndentation:
Expand Down Expand Up @@ -3094,4 +3101,4 @@ Style/FileRead:

# reduce file write complexity
Style/FileWrite:
Enabled: true
Enabled: true
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ examples:
version_added: 7.31.0
enabled: true
included_file_paths:
- "**/metadata.rb"
- "**/.delivery/project.toml"
10 changes: 7 additions & 3 deletions lib/rubocop/cop/chef/deprecation/delivery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,20 @@ module Deprecations
class Delivery < Base
include RangeHelp

MSG = 'Do not include a `.delivery` directory for the `delivery` command in your cookbooks. Chef Delivery (Workflow) went EOL Dec 31st 2021 and the delivery command was removed from Chef Workstation Feb 2022.'
MSG = 'Do not include Chef Delivery (Workflow) configuration in your cookbooks. It went EOL Dec 31st 2021 and the delivery command was removed from Chef Workstation Feb 2022.'

def on_new_investigation
return unless File.exist?(File.join(File.dirname(processed_source.path) + '/.delivery'))
def on_other_file
return unless processed_source.path.end_with?('/.delivery/project.toml', '/.delivery/config.json')

# Using range similar to RuboCop::Cop::Naming::Filename (file_name.rb)
range = source_range(processed_source.buffer, 1, 0)

add_offense(range, severity: :warning)
end

# An empty / simple TOML file can also be syntatically valid Ruby, so
# RuboCop may start an investigation instead of calling on_other_file.
alias_method :on_new_investigation, :on_other_file
end
end
end
Expand Down
28 changes: 28 additions & 0 deletions lib/rubocop/monkey_patches/allow_invalid_ruby.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true
require 'rubocop/rspec/expect_offense'
# rubocop:disable Lint/DuplicateMethods

module RuboCop
module RSpec
module ExpectOffense
# Yields to a block with `parse_processed_source` patched to not raise an
# exception.
#
# RSpec's `expect_offense` helper calls a method called
# `parse_processed_source` that parses source code and raises an exception
# if it is not valid Ruby. Raising an exception prevents RuboCop from
# calling the cop's `on_other_file` method for checking non-Ruby files.
def allow_invalid_ruby(&block)
alias :parse_processed_source :_parse_invalid_source
yield block
alias :parse_processed_source :_orig_parse_processed_source
end

alias :_orig_parse_processed_source :parse_processed_source

def _parse_invalid_source(source, file = nil)
parse_source(source, file)
end
end
end
end
39 changes: 39 additions & 0 deletions spec/rubocop/cop/chef/deprecation/delivery_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true
#
# 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'
require 'rubocop/monkey_patches/allow_invalid_ruby'

describe RuboCop::Cop::Chef::Deprecations::Delivery, :config do
subject(:cop) { described_class.new(config) }

it 'registers an offense when cookbook includes .delivery/project.toml' do
expect_offense(<<~'TOML', 'cookbook/.delivery/project.toml')
[delivery_local]
^ Do not include Chef Delivery (Workflow) configuration [...]
TOML
end

it 'registers an offense when .delivery/config.json is not valid Ruby' do
allow_invalid_ruby do
expect_offense(<<~TOML, 'cookbook/.delivery/config.json')
This isn't parsable Ruby.
^ Do not include Chef Delivery (Workflow) configuration [...]
TOML
end
end
end

0 comments on commit 45bd2a7

Please sign in to comment.