From cc770a2e7225d6827326b57cdce3275bd69de083 Mon Sep 17 00:00:00 2001 From: Tim Smith Date: Fri, 17 Jul 2020 14:42:38 -0700 Subject: [PATCH] Add ChefCorrectness/SupportsMustBeFloat Signed-off-by: Tim Smith --- config/cookstyle.yml | 8 +++ .../correctness/supports_must_be_float.rb | 52 +++++++++++++++++++ .../supports_must_be_float_spec.rb | 42 +++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 lib/rubocop/cop/chef/correctness/supports_must_be_float.rb create mode 100644 spec/rubocop/cop/chef/correctness/supports_must_be_float_spec.rb diff --git a/config/cookstyle.yml b/config/cookstyle.yml index 7ef503113..b61c409af 100644 --- a/config/cookstyle.yml +++ b/config/cookstyle.yml @@ -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 ############################### diff --git a/lib/rubocop/cop/chef/correctness/supports_must_be_float.rb b/lib/rubocop/cop/chef/correctness/supports_must_be_float.rb new file mode 100644 index 000000000..5ec687485 --- /dev/null +++ b/lib/rubocop/cop/chef/correctness/supports_must_be_float.rb @@ -0,0 +1,52 @@ +# frozen_string_literal: true +# +# Copyright:: 2020, Chef Software Inc. +# Author:: Tim Smith () +# +# 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 diff --git a/spec/rubocop/cop/chef/correctness/supports_must_be_float_spec.rb b/spec/rubocop/cop/chef/correctness/supports_must_be_float_spec.rb new file mode 100644 index 000000000..0084a50c8 --- /dev/null +++ b/spec/rubocop/cop/chef/correctness/supports_must_be_float_spec.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true +# +# Copyright:: Copyright 2020, Chef Software Inc. +# Author:: Tim Smith () +# +# 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 \ No newline at end of file