cookstyle 5.19.9 → 5.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
 - data/config/cookstyle.yml +12 -0
 - data/lib/cookstyle.rb +1 -1
 - data/lib/cookstyle/version.rb +1 -1
 - data/lib/rubocop/cop/chef/deprecation/chef_handler_supports.rb +3 -1
 - data/lib/rubocop/cop/chef/deprecation/deprecated_chefspec_platform.rb +128 -0
 - data/lib/rubocop/cop/chef/modernize/execute_sysctl.rb +2 -5
 - data/lib/rubocop/cop/chef/modernize/libarchive_file.rb +22 -3
 - data/lib/rubocop/cop/chef/style/chef_whaaat.rb +50 -0
 - data/lib/rubocop/cop/chef/style/unnecessary_platform_case_statement.rb +28 -15
 - metadata +4 -2
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA256:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: 5c8af7267fe94ef01b9da1d3b84c221101cdb7e8976217cbca2bd8f43c475858
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 3f6196cac78246fc8b36a013f4bbb335470824f3ef23974869596087c8082925
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: ef274262f9b3e41204d9ee5153c9c4022958659b69a6a4d781848a523d6bb37336101fafe5c30ece1d072118b13422803bfa629ba97605c75bd9588c8ef21e36
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 6fbff49a1400321bee3bec46011d0b3193d8460a702c9749a84e11c9992c5642e75b2e3857ab1c84461c23a31b0e5b4c4dcd186e9ea4a0cc588974fd3e28d4b7
         
     | 
    
        data/config/cookstyle.yml
    CHANGED
    
    | 
         @@ -122,6 +122,11 @@ ChefStyle/OverlyComplexSupportsDependsMetadata: 
     | 
|
| 
       122 
122 
     | 
    
         
             
              Include:
         
     | 
| 
       123 
123 
     | 
    
         
             
                - '**/metadata.rb'
         
     | 
| 
       124 
124 
     | 
    
         | 
| 
      
 125 
     | 
    
         
            +
            ChefStyle/ChefWhaaat:
         
     | 
| 
      
 126 
     | 
    
         
            +
              Description: Do you mean Chef (the company) or a Chef product (e.g. Chef Infra, Chef InSpec, etc)?
         
     | 
| 
      
 127 
     | 
    
         
            +
              Enabled: false
         
     | 
| 
      
 128 
     | 
    
         
            +
              VersionAdded: '5.20.0'
         
     | 
| 
      
 129 
     | 
    
         
            +
             
     | 
| 
       125 
130 
     | 
    
         
             
            ###############################
         
     | 
| 
       126 
131 
     | 
    
         
             
            # ChefCorrectness: Avoiding potential problems
         
     | 
| 
       127 
132 
     | 
    
         
             
            ###############################
         
     | 
| 
         @@ -700,6 +705,13 @@ ChefDeprecations/DeprecatedPlatformMethods: 
     | 
|
| 
       700 
705 
     | 
    
         
             
                - '**/resources/*.rb'
         
     | 
| 
       701 
706 
     | 
    
         
             
                - '**/providers/*.rb'
         
     | 
| 
       702 
707 
     | 
    
         | 
| 
      
 708 
     | 
    
         
            +
            ChefDeprecations/DeprecatedChefSpecPlatform:
         
     | 
| 
      
 709 
     | 
    
         
            +
              Description: Use currently supported platforms in ChefSpec listed at https://github.com/chefspec/fauxhai/blob/master/PLATFORMS.md. Fauxhai / ChefSpec will perform fuzzy matching on platform version so it's always best to be less specific ie. 10 instead of 10.3
         
     | 
| 
      
 710 
     | 
    
         
            +
              Enabled: true
         
     | 
| 
      
 711 
     | 
    
         
            +
              VersionAdded: '5.20.0'
         
     | 
| 
      
 712 
     | 
    
         
            +
              Include:
         
     | 
| 
      
 713 
     | 
    
         
            +
                - '**/spec/**/*.rb'
         
     | 
| 
      
 714 
     | 
    
         
            +
             
     | 
| 
       703 
715 
     | 
    
         
             
            ###############################
         
     | 
| 
       704 
716 
     | 
    
         
             
            # ChefModernize: Cleaning up legacy code and using new built-in resources
         
     | 
| 
       705 
717 
     | 
    
         
             
            ###############################
         
     | 
    
        data/lib/cookstyle.rb
    CHANGED
    
    | 
         @@ -43,7 +43,7 @@ require_relative 'rubocop/chef/platform_helpers' 
     | 
|
| 
       43 
43 
     | 
    
         
             
            require_relative 'rubocop/chef/cookbook_only'
         
     | 
| 
       44 
44 
     | 
    
         
             
            require_relative 'rubocop/cop/target_chef_version'
         
     | 
| 
       45 
45 
     | 
    
         | 
| 
       46 
     | 
    
         
            -
            # Chef specific cops
         
     | 
| 
      
 46 
     | 
    
         
            +
            # Chef Infra specific cops
         
     | 
| 
       47 
47 
     | 
    
         
             
            Dir.glob(File.dirname(__FILE__) + '/rubocop/cop/chef/**/*.rb') do |file|
         
     | 
| 
       48 
48 
     | 
    
         
             
              next if File.directory?(file)
         
     | 
| 
       49 
49 
     | 
    
         | 
    
        data/lib/cookstyle/version.rb
    CHANGED
    
    
| 
         @@ -46,7 +46,9 @@ module RuboCop 
     | 
|
| 
       46 
46 
     | 
    
         | 
| 
       47 
47 
     | 
    
         
             
                      def autocorrect(node)
         
     | 
| 
       48 
48 
     | 
    
         
             
                        lambda do |corrector|
         
     | 
| 
       49 
     | 
    
         
            -
                           
     | 
| 
      
 49 
     | 
    
         
            +
                          # make sure to delete leading and trailing {}s that would create invalid ruby syntax
         
     | 
| 
      
 50 
     | 
    
         
            +
                          extracted_val = node.arguments.first.source.gsub(/{|}/, '')
         
     | 
| 
      
 51 
     | 
    
         
            +
                          corrector.replace(node.loc.expression, "type #{extracted_val}")
         
     | 
| 
       50 
52 
     | 
    
         
             
                        end
         
     | 
| 
       51 
53 
     | 
    
         
             
                      end
         
     | 
| 
       52 
54 
     | 
    
         
             
                    end
         
     | 
| 
         @@ -0,0 +1,128 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            #
         
     | 
| 
      
 2 
     | 
    
         
            +
            # Copyright:: 2020, Chef Software Inc.
         
     | 
| 
      
 3 
     | 
    
         
            +
            # Author:: Tim Smith (<tsmith@chef.io>)
         
     | 
| 
      
 4 
     | 
    
         
            +
            #
         
     | 
| 
      
 5 
     | 
    
         
            +
            # Licensed under the Apache License, Version 2.0 (the "License");
         
     | 
| 
      
 6 
     | 
    
         
            +
            # you may not use this file except in compliance with the License.
         
     | 
| 
      
 7 
     | 
    
         
            +
            # You may obtain a copy of the License at
         
     | 
| 
      
 8 
     | 
    
         
            +
            #
         
     | 
| 
      
 9 
     | 
    
         
            +
            # http://www.apache.org/licenses/LICENSE-2.0
         
     | 
| 
      
 10 
     | 
    
         
            +
            #
         
     | 
| 
      
 11 
     | 
    
         
            +
            # Unless required by applicable law or agreed to in writing, software
         
     | 
| 
      
 12 
     | 
    
         
            +
            # distributed under the License is distributed on an "AS IS" BASIS,
         
     | 
| 
      
 13 
     | 
    
         
            +
            # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
         
     | 
| 
      
 14 
     | 
    
         
            +
            # See the License for the specific language governing permissions and
         
     | 
| 
      
 15 
     | 
    
         
            +
            # limitations under the License.
         
     | 
| 
      
 16 
     | 
    
         
            +
            #
         
     | 
| 
      
 17 
     | 
    
         
            +
             
     | 
| 
      
 18 
     | 
    
         
            +
            module RuboCop
         
     | 
| 
      
 19 
     | 
    
         
            +
              module Cop
         
     | 
| 
      
 20 
     | 
    
         
            +
                module Chef
         
     | 
| 
      
 21 
     | 
    
         
            +
                  module ChefDeprecations
         
     | 
| 
      
 22 
     | 
    
         
            +
                    # Use currently supported platforms in ChefSpec listed at https://github.com/chefspec/fauxhai/blob/master/PLATFORMS.md. Fauxhai / ChefSpec will perform fuzzy matching on platform version so it's always best to be less specific ie. 10 instead of 10.3
         
     | 
| 
      
 23 
     | 
    
         
            +
                    #
         
     | 
| 
      
 24 
     | 
    
         
            +
                    # @example
         
     | 
| 
      
 25 
     | 
    
         
            +
                    #
         
     | 
| 
      
 26 
     | 
    
         
            +
                    #   let(:chef_run) { ChefSpec::ServerRunner.new(platform: 'ubuntu', version: '14.04') }
         
     | 
| 
      
 27 
     | 
    
         
            +
                    #
         
     | 
| 
      
 28 
     | 
    
         
            +
                    class DeprecatedChefSpecPlatform < Cop
         
     | 
| 
      
 29 
     | 
    
         
            +
                      include RuboCop::Chef::CookbookHelpers
         
     | 
| 
      
 30 
     | 
    
         
            +
             
     | 
| 
      
 31 
     | 
    
         
            +
                      MSG = "Use currently supported platforms in ChefSpec listed at https://github.com/chefspec/fauxhai/blob/master/PLATFORMS.md. Fauxhai / ChefSpec will perform fuzzy matching on platform version so it's always best to be less specific ie. 10 instead of 10.3".freeze
         
     | 
| 
      
 32 
     | 
    
         
            +
             
     | 
| 
      
 33 
     | 
    
         
            +
                      Gem::Dependency.new('', ['~> 1.4.5', '>= 1.4.6']).match?('', '1.4.6')
         
     | 
| 
      
 34 
     | 
    
         
            +
             
     | 
| 
      
 35 
     | 
    
         
            +
                      DEPRECATED_MAPPING = {
         
     | 
| 
      
 36 
     | 
    
         
            +
                        'amazon' => {
         
     | 
| 
      
 37 
     | 
    
         
            +
                          '2017.12' => '2',
         
     | 
| 
      
 38 
     | 
    
         
            +
                        },
         
     | 
| 
      
 39 
     | 
    
         
            +
                        'aix' => {
         
     | 
| 
      
 40 
     | 
    
         
            +
                          '~> 6' => true,
         
     | 
| 
      
 41 
     | 
    
         
            +
                        },
         
     | 
| 
      
 42 
     | 
    
         
            +
                        'smartos' => {
         
     | 
| 
      
 43 
     | 
    
         
            +
                          '5.10' => true,
         
     | 
| 
      
 44 
     | 
    
         
            +
                        },
         
     | 
| 
      
 45 
     | 
    
         
            +
                        'ubuntu' => {
         
     | 
| 
      
 46 
     | 
    
         
            +
                          '< 16.04' => true,
         
     | 
| 
      
 47 
     | 
    
         
            +
                          '> 16.04, < 18.04' => true,
         
     | 
| 
      
 48 
     | 
    
         
            +
                        },
         
     | 
| 
      
 49 
     | 
    
         
            +
                        'fedora' => {
         
     | 
| 
      
 50 
     | 
    
         
            +
                          '< 30' => '30',
         
     | 
| 
      
 51 
     | 
    
         
            +
                        },
         
     | 
| 
      
 52 
     | 
    
         
            +
                        'freebsd' => {
         
     | 
| 
      
 53 
     | 
    
         
            +
                          '< 11' => '12',
         
     | 
| 
      
 54 
     | 
    
         
            +
                        },
         
     | 
| 
      
 55 
     | 
    
         
            +
                        'mac_os_x' => {
         
     | 
| 
      
 56 
     | 
    
         
            +
                          '< 10.12' => '10.15',
         
     | 
| 
      
 57 
     | 
    
         
            +
                        },
         
     | 
| 
      
 58 
     | 
    
         
            +
                        'opensuse' => {
         
     | 
| 
      
 59 
     | 
    
         
            +
                          '< 14' => true,
         
     | 
| 
      
 60 
     | 
    
         
            +
                          '~> 42.0' => true,
         
     | 
| 
      
 61 
     | 
    
         
            +
                        },
         
     | 
| 
      
 62 
     | 
    
         
            +
                        'debian' => {
         
     | 
| 
      
 63 
     | 
    
         
            +
                          '< 8' => true,
         
     | 
| 
      
 64 
     | 
    
         
            +
                          '> 8.0, < 8.9' => '8',
         
     | 
| 
      
 65 
     | 
    
         
            +
                          '> 9.0, < 9.8' => '9',
         
     | 
| 
      
 66 
     | 
    
         
            +
                        },
         
     | 
| 
      
 67 
     | 
    
         
            +
                        'centos' => {
         
     | 
| 
      
 68 
     | 
    
         
            +
                          '< 6.0' => true,
         
     | 
| 
      
 69 
     | 
    
         
            +
                          '~> 6.0, < 6.8' => '6',
         
     | 
| 
      
 70 
     | 
    
         
            +
                          '~> 7.0, < 7.6 ' => '7',
         
     | 
| 
      
 71 
     | 
    
         
            +
                        },
         
     | 
| 
      
 72 
     | 
    
         
            +
                        'redhat' => {
         
     | 
| 
      
 73 
     | 
    
         
            +
                          '< 6.0' => true,
         
     | 
| 
      
 74 
     | 
    
         
            +
                          '~> 6.0, < 6.8' => '6',
         
     | 
| 
      
 75 
     | 
    
         
            +
                          '~> 7.0, < 7.6' => '7',
         
     | 
| 
      
 76 
     | 
    
         
            +
                        },
         
     | 
| 
      
 77 
     | 
    
         
            +
                        'oracle' => {
         
     | 
| 
      
 78 
     | 
    
         
            +
                          '< 6.0' => true,
         
     | 
| 
      
 79 
     | 
    
         
            +
                          '~> 6.0, < 6.8' => '6',
         
     | 
| 
      
 80 
     | 
    
         
            +
                          '~> 7.0, < 7.6 ' => '7',
         
     | 
| 
      
 81 
     | 
    
         
            +
                        },
         
     | 
| 
      
 82 
     | 
    
         
            +
                      }.freeze
         
     | 
| 
      
 83 
     | 
    
         
            +
             
     | 
| 
      
 84 
     | 
    
         
            +
                      def_node_matcher :chefspec_definition?, <<-PATTERN
         
     | 
| 
      
 85 
     | 
    
         
            +
                        (send (const (const nil? :ChefSpec) ... ) :new (hash <(pair (sym :platform) $(str ... )) (pair (sym :version) $(str ... )) ... >))
         
     | 
| 
      
 86 
     | 
    
         
            +
                      PATTERN
         
     | 
| 
      
 87 
     | 
    
         
            +
             
     | 
| 
      
 88 
     | 
    
         
            +
                      def legacy_chefspec_platform(platform, version)
         
     | 
| 
      
 89 
     | 
    
         
            +
                        return false unless DEPRECATED_MAPPING.key?(platform)
         
     | 
| 
      
 90 
     | 
    
         
            +
             
     | 
| 
      
 91 
     | 
    
         
            +
                        DEPRECATED_MAPPING[platform].each_pair do |match_string, replacement|
         
     | 
| 
      
 92 
     | 
    
         
            +
                          return true if Gem::Dependency.new('', match_string.split(',')).match?('', version) &&
         
     | 
| 
      
 93 
     | 
    
         
            +
                                         replacement != version # we want to catch '7.0' and suggest '7', but not alert on '7'
         
     | 
| 
      
 94 
     | 
    
         
            +
                        end
         
     | 
| 
      
 95 
     | 
    
         
            +
             
     | 
| 
      
 96 
     | 
    
         
            +
                        false
         
     | 
| 
      
 97 
     | 
    
         
            +
                      end
         
     | 
| 
      
 98 
     | 
    
         
            +
             
     | 
| 
      
 99 
     | 
    
         
            +
                      def replacement_string(platform, version)
         
     | 
| 
      
 100 
     | 
    
         
            +
                        DEPRECATED_MAPPING[platform].each_pair do |match_string, replacement|
         
     | 
| 
      
 101 
     | 
    
         
            +
                          return replacement if Gem::Dependency.new('', match_string.split(',')).match?('', version) &&
         
     | 
| 
      
 102 
     | 
    
         
            +
                                                replacement != version && # we want to catch '7.0' and suggest '7', but not alert on '7'
         
     | 
| 
      
 103 
     | 
    
         
            +
                                                replacement != true # true means it's busted, but requires human intervention to fix
         
     | 
| 
      
 104 
     | 
    
         
            +
                        end
         
     | 
| 
      
 105 
     | 
    
         
            +
             
     | 
| 
      
 106 
     | 
    
         
            +
                        nil # we don't have a replacement os return nil
         
     | 
| 
      
 107 
     | 
    
         
            +
                      end
         
     | 
| 
      
 108 
     | 
    
         
            +
             
     | 
| 
      
 109 
     | 
    
         
            +
                      def on_send(node)
         
     | 
| 
      
 110 
     | 
    
         
            +
                        chefspec_definition?(node) do |plat, ver|
         
     | 
| 
      
 111 
     | 
    
         
            +
                          add_offense(node, location: :expression, message: MSG, severity: :refactor) if legacy_chefspec_platform(plat.value, ver.value)
         
     | 
| 
      
 112 
     | 
    
         
            +
                        end
         
     | 
| 
      
 113 
     | 
    
         
            +
                      end
         
     | 
| 
      
 114 
     | 
    
         
            +
             
     | 
| 
      
 115 
     | 
    
         
            +
                      def autocorrect(node)
         
     | 
| 
      
 116 
     | 
    
         
            +
                        chefspec_definition?(node) do |plat, ver|
         
     | 
| 
      
 117 
     | 
    
         
            +
                          if replacement = replacement_string(plat.value, ver.value) # rubocop: disable Lint/AssignmentInCondition
         
     | 
| 
      
 118 
     | 
    
         
            +
                            lambda do |corrector|
         
     | 
| 
      
 119 
     | 
    
         
            +
                              corrector.replace(ver.loc.expression, "'#{replacement}'")
         
     | 
| 
      
 120 
     | 
    
         
            +
                            end
         
     | 
| 
      
 121 
     | 
    
         
            +
                          end
         
     | 
| 
      
 122 
     | 
    
         
            +
                        end
         
     | 
| 
      
 123 
     | 
    
         
            +
                      end
         
     | 
| 
      
 124 
     | 
    
         
            +
                    end
         
     | 
| 
      
 125 
     | 
    
         
            +
                  end
         
     | 
| 
      
 126 
     | 
    
         
            +
                end
         
     | 
| 
      
 127 
     | 
    
         
            +
              end
         
     | 
| 
      
 128 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -22,10 +22,7 @@ module RuboCop 
     | 
|
| 
       22 
22 
     | 
    
         
             
                    #
         
     | 
| 
       23 
23 
     | 
    
         
             
                    #   # bad
         
     | 
| 
       24 
24 
     | 
    
         
             
                    #   file '/etc/sysctl.d/ipv4.conf' do
         
     | 
| 
       25 
     | 
    
         
            -
                    #      
     | 
| 
       26 
     | 
    
         
            -
                    #     group 'root'
         
     | 
| 
       27 
     | 
    
         
            -
                    #     mode '0755'
         
     | 
| 
       28 
     | 
    
         
            -
                    #     notifies :run, 'service[sysctl -p /etc/sysctl.d/ipv4.conf]', :immediately
         
     | 
| 
      
 25 
     | 
    
         
            +
                    #     notifies :run, 'execute[sysctl -p /etc/sysctl.d/ipv4.conf]', :immediately
         
     | 
| 
       29 
26 
     | 
    
         
             
                    #     content '9000 65500'
         
     | 
| 
       30 
27 
     | 
    
         
             
                    #   end
         
     | 
| 
       31 
28 
     | 
    
         
             
                    #
         
     | 
| 
         @@ -61,7 +58,7 @@ module RuboCop 
     | 
|
| 
       61 
58 
     | 
    
         
             
                      def on_block(node)
         
     | 
| 
       62 
59 
     | 
    
         
             
                        match_property_in_resource?(:execute, 'command', node) do |code_property|
         
     | 
| 
       63 
60 
     | 
    
         
             
                          property_data = method_arg_ast_to_string(code_property)
         
     | 
| 
       64 
     | 
    
         
            -
                          if property_data && property_data.match?( 
     | 
| 
      
 61 
     | 
    
         
            +
                          if property_data && property_data.match?(%r{^(/sbin/)?sysctl -p}i)
         
     | 
| 
       65 
62 
     | 
    
         
             
                            add_offense(node, location: :expression, message: MSG, severity: :refactor)
         
     | 
| 
       66 
63 
     | 
    
         
             
                          end
         
     | 
| 
       67 
64 
     | 
    
         
             
                        end
         
     | 
| 
         @@ -1,5 +1,5 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            #
         
     | 
| 
       2 
     | 
    
         
            -
            # Copyright:: 2019, Chef Software Inc.
         
     | 
| 
      
 2 
     | 
    
         
            +
            # Copyright:: 2019-2020, Chef Software Inc.
         
     | 
| 
       3 
3 
     | 
    
         
             
            # Author:: Tim Smith (<tsmith@chef.io>)
         
     | 
| 
       4 
4 
     | 
    
         
             
            #
         
     | 
| 
       5 
5 
     | 
    
         
             
            # Licensed under the Apache License, Version 2.0 (the "License");
         
     | 
| 
         @@ -19,12 +19,17 @@ module RuboCop 
     | 
|
| 
       19 
19 
     | 
    
         
             
              module Cop
         
     | 
| 
       20 
20 
     | 
    
         
             
                module Chef
         
     | 
| 
       21 
21 
     | 
    
         
             
                  module ChefModernize
         
     | 
| 
       22 
     | 
    
         
            -
                    # Use the archive_file resource built into Chef Infra Client 15+ instead of the libarchive_file resource
         
     | 
| 
      
 22 
     | 
    
         
            +
                    # Use the archive_file resource built into Chef Infra Client 15+ instead of the libarchive_file resource.
         
     | 
| 
       23 
23 
     | 
    
         
             
                    #
         
     | 
| 
       24 
24 
     | 
    
         
             
                    # @example
         
     | 
| 
       25 
25 
     | 
    
         
             
                    #
         
     | 
| 
       26 
26 
     | 
    
         
             
                    #   # bad
         
     | 
| 
       27 
     | 
    
         
            -
                    #    
     | 
| 
      
 27 
     | 
    
         
            +
                    #   libarchive_file "C:\file.zip" do
         
     | 
| 
      
 28 
     | 
    
         
            +
                    #     path 'C:\expand_here'
         
     | 
| 
      
 29 
     | 
    
         
            +
                    #   end
         
     | 
| 
      
 30 
     | 
    
         
            +
                    #
         
     | 
| 
      
 31 
     | 
    
         
            +
                    #   # good
         
     | 
| 
      
 32 
     | 
    
         
            +
                    #   archive_file "C:\file.zip" do
         
     | 
| 
       28 
33 
     | 
    
         
             
                    #     path 'C:\expand_here'
         
     | 
| 
       29 
34 
     | 
    
         
             
                    #   end
         
     | 
| 
       30 
35 
     | 
    
         
             
                    #
         
     | 
| 
         @@ -35,8 +40,22 @@ module RuboCop 
     | 
|
| 
       35 
40 
     | 
    
         | 
| 
       36 
41 
     | 
    
         
             
                      MSG = 'Use the archive_file resource built into Chef Infra Client 15+ instead of the libarchive file resource'.freeze
         
     | 
| 
       37 
42 
     | 
    
         | 
| 
      
 43 
     | 
    
         
            +
                      def_node_matcher :notification_property?, <<-PATTERN
         
     | 
| 
      
 44 
     | 
    
         
            +
                        (send nil? {:notifies :subscribes} (sym _) $(...) (sym _))
         
     | 
| 
      
 45 
     | 
    
         
            +
                      PATTERN
         
     | 
| 
      
 46 
     | 
    
         
            +
             
     | 
| 
       38 
47 
     | 
    
         
             
                      def on_send(node)
         
     | 
| 
       39 
48 
     | 
    
         
             
                        add_offense(node, location: :expression, message: MSG, severity: :refactor) if node.method_name == :libarchive_file
         
     | 
| 
      
 49 
     | 
    
         
            +
             
     | 
| 
      
 50 
     | 
    
         
            +
                        notification_property?(node) do |val|
         
     | 
| 
      
 51 
     | 
    
         
            +
                          add_offense(val, location: :expression, message: MSG, severity: :refactor) if val.str_content&.start_with?('libarchive_file')
         
     | 
| 
      
 52 
     | 
    
         
            +
                        end
         
     | 
| 
      
 53 
     | 
    
         
            +
                      end
         
     | 
| 
      
 54 
     | 
    
         
            +
             
     | 
| 
      
 55 
     | 
    
         
            +
                      def autocorrect(node)
         
     | 
| 
      
 56 
     | 
    
         
            +
                        lambda do |corrector|
         
     | 
| 
      
 57 
     | 
    
         
            +
                          corrector.replace(node.loc.expression, node.source.gsub('libarchive_file', 'archive_file'))
         
     | 
| 
      
 58 
     | 
    
         
            +
                        end
         
     | 
| 
       40 
59 
     | 
    
         
             
                      end
         
     | 
| 
       41 
60 
     | 
    
         
             
                    end
         
     | 
| 
       42 
61 
     | 
    
         
             
                  end
         
     | 
| 
         @@ -0,0 +1,50 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            #
         
     | 
| 
      
 2 
     | 
    
         
            +
            # Copyright:: 2020, Chef Software, Inc.
         
     | 
| 
      
 3 
     | 
    
         
            +
            # Author:: Tim Smith (<tsmith@chef.io>)
         
     | 
| 
      
 4 
     | 
    
         
            +
            #
         
     | 
| 
      
 5 
     | 
    
         
            +
            # Licensed under the Apache License, Version 2.0 (the "License");
         
     | 
| 
      
 6 
     | 
    
         
            +
            # you may not use this file except in compliance with the License.
         
     | 
| 
      
 7 
     | 
    
         
            +
            # You may obtain a copy of the License at
         
     | 
| 
      
 8 
     | 
    
         
            +
            #
         
     | 
| 
      
 9 
     | 
    
         
            +
            # http://www.apache.org/licenses/LICENSE-2.0
         
     | 
| 
      
 10 
     | 
    
         
            +
            #
         
     | 
| 
      
 11 
     | 
    
         
            +
            # Unless required by applicable law or agreed to in writing, software
         
     | 
| 
      
 12 
     | 
    
         
            +
            # distributed under the License is distributed on an "AS IS" BASIS,
         
     | 
| 
      
 13 
     | 
    
         
            +
            # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
         
     | 
| 
      
 14 
     | 
    
         
            +
            # See the License for the specific language governing permissions and
         
     | 
| 
      
 15 
     | 
    
         
            +
            # limitations under the License.
         
     | 
| 
      
 16 
     | 
    
         
            +
            #
         
     | 
| 
      
 17 
     | 
    
         
            +
             
     | 
| 
      
 18 
     | 
    
         
            +
            module RuboCop
         
     | 
| 
      
 19 
     | 
    
         
            +
              module Cop
         
     | 
| 
      
 20 
     | 
    
         
            +
                module Chef
         
     | 
| 
      
 21 
     | 
    
         
            +
                  module ChefStyle
         
     | 
| 
      
 22 
     | 
    
         
            +
                    # Checks for comments that mention "Chef" without context. Do you mean Chef Infra or Chef Software?
         
     | 
| 
      
 23 
     | 
    
         
            +
                    #
         
     | 
| 
      
 24 
     | 
    
         
            +
                    # @example
         
     | 
| 
      
 25 
     | 
    
         
            +
                    #
         
     | 
| 
      
 26 
     | 
    
         
            +
                    #   # bad
         
     | 
| 
      
 27 
     | 
    
         
            +
                    #   Chef makes software
         
     | 
| 
      
 28 
     | 
    
         
            +
                    #   Chef configures your systems
         
     | 
| 
      
 29 
     | 
    
         
            +
                    #
         
     | 
| 
      
 30 
     | 
    
         
            +
                    #   # good
         
     | 
| 
      
 31 
     | 
    
         
            +
                    #   Chef Software makes software
         
     | 
| 
      
 32 
     | 
    
         
            +
                    #   Chef Infra configures your systems
         
     | 
| 
      
 33 
     | 
    
         
            +
                    #
         
     | 
| 
      
 34 
     | 
    
         
            +
                    class ChefWhaaat < Cop
         
     | 
| 
      
 35 
     | 
    
         
            +
                      MSG = 'Do you mean Chef (the company) or a Chef product (e.g. Chef Infra, Chef InSpec, etc)?'.freeze
         
     | 
| 
      
 36 
     | 
    
         
            +
             
     | 
| 
      
 37 
     | 
    
         
            +
                      def investigate(processed_source)
         
     | 
| 
      
 38 
     | 
    
         
            +
                        return unless processed_source.ast
         
     | 
| 
      
 39 
     | 
    
         
            +
             
     | 
| 
      
 40 
     | 
    
         
            +
                        processed_source.comments.each do |comment|
         
     | 
| 
      
 41 
     | 
    
         
            +
                          if comment.text.match?(/Chef [a-z]/) # https://rubular.com/r/0YzfDAbwJrDHix
         
     | 
| 
      
 42 
     | 
    
         
            +
                            add_offense(comment, location: comment.loc.expression, message: MSG, severity: :refactor)
         
     | 
| 
      
 43 
     | 
    
         
            +
                          end
         
     | 
| 
      
 44 
     | 
    
         
            +
                        end
         
     | 
| 
      
 45 
     | 
    
         
            +
                      end
         
     | 
| 
      
 46 
     | 
    
         
            +
                    end
         
     | 
| 
      
 47 
     | 
    
         
            +
                  end
         
     | 
| 
      
 48 
     | 
    
         
            +
                end
         
     | 
| 
      
 49 
     | 
    
         
            +
              end
         
     | 
| 
      
 50 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -60,29 +60,42 @@ module RuboCop 
     | 
|
| 
       60 
60 
     | 
    
         
             
                      def autocorrect(node)
         
     | 
| 
       61 
61 
     | 
    
         
             
                        lambda do |corrector|
         
     | 
| 
       62 
62 
     | 
    
         
             
                          platform_case?(node) do |node_, type|
         
     | 
| 
       63 
     | 
    
         
            -
                             
     | 
| 
      
 63 
     | 
    
         
            +
                            # we have at least one supermarket cookbook with an entirely empty platform case statement
         
     | 
| 
      
 64 
     | 
    
         
            +
                            # we can't actually fix that so let's do nothing here.
         
     | 
| 
      
 65 
     | 
    
         
            +
                            unless empty_case_statement?(node)
         
     | 
| 
      
 66 
     | 
    
         
            +
                              condition_string = node.when_branches.first.conditions.map(&:source).join(', ')
         
     | 
| 
       64 
67 
     | 
    
         | 
| 
       65 
     | 
    
         
            -
             
     | 
| 
       66 
     | 
    
         
            -
             
     | 
| 
       67 
     | 
    
         
            -
             
     | 
| 
       68 
     | 
    
         
            -
             
     | 
| 
       69 
     | 
    
         
            -
             
     | 
| 
       70 
     | 
    
         
            -
             
     | 
| 
       71 
     | 
    
         
            -
             
     | 
| 
       72 
     | 
    
         
            -
             
     | 
| 
      
 68 
     | 
    
         
            +
                              # single line bodies without an else statement should be transformed into `X if platform?('ubuntu')` style statements
         
     | 
| 
      
 69 
     | 
    
         
            +
                              # while multiline statements should just have the case and when bits replace with `if platform?('ubuntu')`
         
     | 
| 
      
 70 
     | 
    
         
            +
                              if !node.else? && !node.when_branches.first.body.multiline?
         
     | 
| 
      
 71 
     | 
    
         
            +
                                new_source = "#{node.when_branches.first.body.source} if #{type.value}?(#{condition_string})"
         
     | 
| 
      
 72 
     | 
    
         
            +
                                corrector.replace(node.loc.expression, new_source)
         
     | 
| 
      
 73 
     | 
    
         
            +
                              else
         
     | 
| 
      
 74 
     | 
    
         
            +
                                # find the range from the beginning of the case to the end of the node['platform'] or node['platform_family']
         
     | 
| 
      
 75 
     | 
    
         
            +
                                case_range = node.loc.keyword.join(node_.loc.expression.end)
         
     | 
| 
       73 
76 
     | 
    
         | 
| 
       74 
     | 
    
         
            -
             
     | 
| 
       75 
     | 
    
         
            -
             
     | 
| 
      
 77 
     | 
    
         
            +
                                # replace the complete conditional range with a new if statement
         
     | 
| 
      
 78 
     | 
    
         
            +
                                corrector.replace(case_range, "if #{type.value}?(#{condition_string})")
         
     | 
| 
       76 
79 
     | 
    
         | 
| 
       77 
     | 
    
         
            -
             
     | 
| 
       78 
     | 
    
         
            -
             
     | 
| 
      
 80 
     | 
    
         
            +
                                # find the range from the start of the when to the end of the last argument
         
     | 
| 
      
 81 
     | 
    
         
            +
                                conditional_range = node.when_branches.first.conditions[-1].source_range.join(node.when_branches.first.loc.keyword.begin)
         
     | 
| 
       79 
82 
     | 
    
         | 
| 
       80 
     | 
    
         
            -
             
     | 
| 
       81 
     | 
    
         
            -
             
     | 
| 
      
 83 
     | 
    
         
            +
                                # remove the when XYZ condition along with any leading spaces so that we remove the whole empty line
         
     | 
| 
      
 84 
     | 
    
         
            +
                                corrector.remove(range_with_surrounding_space(range: conditional_range, side: :left))
         
     | 
| 
      
 85 
     | 
    
         
            +
                              end
         
     | 
| 
       82 
86 
     | 
    
         
             
                            end
         
     | 
| 
       83 
87 
     | 
    
         
             
                          end
         
     | 
| 
       84 
88 
     | 
    
         
             
                        end
         
     | 
| 
       85 
89 
     | 
    
         
             
                      end
         
     | 
| 
      
 90 
     | 
    
         
            +
             
     | 
| 
      
 91 
     | 
    
         
            +
                      private
         
     | 
| 
      
 92 
     | 
    
         
            +
             
     | 
| 
      
 93 
     | 
    
         
            +
                      # determine if each branch of the case statement contains no code
         
     | 
| 
      
 94 
     | 
    
         
            +
                      #
         
     | 
| 
      
 95 
     | 
    
         
            +
                      # @private
         
     | 
| 
      
 96 
     | 
    
         
            +
                      def empty_case_statement?(node)
         
     | 
| 
      
 97 
     | 
    
         
            +
                        node.else_branch.nil? && node.when_branches.all? { |x| x.body.nil? }
         
     | 
| 
      
 98 
     | 
    
         
            +
                      end
         
     | 
| 
       86 
99 
     | 
    
         
             
                    end
         
     | 
| 
       87 
100 
     | 
    
         
             
                  end
         
     | 
| 
       88 
101 
     | 
    
         
             
                end
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: cookstyle
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 5. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 5.20.0
         
     | 
| 
       5 
5 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       6 
6 
     | 
    
         
             
            authors:
         
     | 
| 
       7 
7 
     | 
    
         
             
            - Thom May
         
     | 
| 
         @@ -9,7 +9,7 @@ authors: 
     | 
|
| 
       9 
9 
     | 
    
         
             
            autorequire: 
         
     | 
| 
       10 
10 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       11 
11 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       12 
     | 
    
         
            -
            date: 2020-01- 
     | 
| 
      
 12 
     | 
    
         
            +
            date: 2020-01-23 00:00:00.000000000 Z
         
     | 
| 
       13 
13 
     | 
    
         
             
            dependencies:
         
     | 
| 
       14 
14 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       15 
15 
     | 
    
         
             
              name: rubocop
         
     | 
| 
         @@ -81,6 +81,7 @@ files: 
     | 
|
| 
       81 
81 
     | 
    
         
             
            - lib/rubocop/cop/chef/deprecation/depends_compat_resource.rb
         
     | 
| 
       82 
82 
     | 
    
         
             
            - lib/rubocop/cop/chef/deprecation/depends_partial_search.rb
         
     | 
| 
       83 
83 
     | 
    
         
             
            - lib/rubocop/cop/chef/deprecation/depends_poise.rb
         
     | 
| 
      
 84 
     | 
    
         
            +
            - lib/rubocop/cop/chef/deprecation/deprecated_chefspec_platform.rb
         
     | 
| 
       84 
85 
     | 
    
         
             
            - lib/rubocop/cop/chef/deprecation/deprecated_mixins.rb
         
     | 
| 
       85 
86 
     | 
    
         
             
            - lib/rubocop/cop/chef/deprecation/deprecated_platform_methods.rb
         
     | 
| 
       86 
87 
     | 
    
         
             
            - lib/rubocop/cop/chef/deprecation/deprecated_yum_repository_properties.rb
         
     | 
| 
         @@ -196,6 +197,7 @@ files: 
     | 
|
| 
       196 
197 
     | 
    
         
             
            - lib/rubocop/cop/chef/sharing/insecure_cookbook_url.rb
         
     | 
| 
       197 
198 
     | 
    
         
             
            - lib/rubocop/cop/chef/sharing/invalid_license_string.rb
         
     | 
| 
       198 
199 
     | 
    
         
             
            - lib/rubocop/cop/chef/style/attribute_keys.rb
         
     | 
| 
      
 200 
     | 
    
         
            +
            - lib/rubocop/cop/chef/style/chef_whaaat.rb
         
     | 
| 
       199 
201 
     | 
    
         
             
            - lib/rubocop/cop/chef/style/comment_sentence_spacing.rb
         
     | 
| 
       200 
202 
     | 
    
         
             
            - lib/rubocop/cop/chef/style/comments_copyright_format.rb
         
     | 
| 
       201 
203 
     | 
    
         
             
            - lib/rubocop/cop/chef/style/comments_default_copyright.rb
         
     |