Skip to content

Commit

Permalink
Merge pull request #689 from chef/floats_in_metadata
Browse files Browse the repository at this point in the history
Add ChefCorrectness/SupportsMustBeFloat
  • Loading branch information
tas50 authored Jul 17, 2020
2 parents f52ba24 + cc770a2 commit 06cd762
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config/cookstyle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,14 @@ ChefCorrectness/InvalidDefaultAction:
- '**/libraries/*.rb'
- '**/resources/*.rb'

ChefCorrectness/SupportsMustBeFloat:
Description: Versions used in metadata.rb supports calls should be floats not integers.
StyleGuide: '#chefcorrectnesssupportsmustbefloat'
Enabled: true
VersionAdded: '6.13.0'
Include:
- '**/metadata.rb'

###############################
# ChefSharing: Issues that prevent sharing code with other teams or with the Chef community in general
###############################
Expand Down
52 changes: 52 additions & 0 deletions lib/rubocop/cop/chef/correctness/supports_must_be_float.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# frozen_string_literal: true
#
# Copyright:: 2020, 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 ChefCorrectness
# Versions used in metadata.rb supports calls should be floats not integers.
#
# @example
#
# # bad
# supports 'redhat', '> 8'
#
# # good
# supports 'redhat', '> 8.0'
#
class SupportsMustBeFloat < Base
extend RuboCop::Cop::AutoCorrector

MSG = 'Versions used in metadata.rb supports calls should be floats not integers.'

def_node_matcher :supports_with_constraint?, '(send nil? :supports str $str)'

def on_send(node)
supports_with_constraint?(node) do |ver|
return if ver.source.include?('.')
add_offense(ver.loc.expression, message: MSG, severity: :refactor) do |corrector|
corrector.replace(ver.loc.expression, ver.source.gsub(ver.value, (ver.value + '.0')))
end
end
end
end
end
end
end
end
42 changes: 42 additions & 0 deletions spec/rubocop/cop/chef/correctness/supports_must_be_float_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true
#
# Copyright:: Copyright 2020, 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.
#

require 'spec_helper'

describe RuboCop::Cop::Chef::ChefCorrectness::SupportsMustBeFloat do
subject(:cop) { described_class.new }

it 'registers an offense when a version constraint uses an integer' do
expect_offense(<<~RUBY)
supports 'redhat', '> 8'
^^^^^ Versions used in metadata.rb supports calls should be floats not integers.
RUBY

expect_correction(<<~RUBY)
supports 'redhat', '> 8.0'
RUBY
end

it 'does not register an offense when supports has no constraint' do
expect_no_offenses("supports 'redhat'")
end

it 'does not register an offense when the supports constraint uses a float' do
expect_no_offenses("supports 'redhat', '> 8.0'")
end
end

0 comments on commit 06cd762

Please sign in to comment.