-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #689 from chef/floats_in_metadata
Add ChefCorrectness/SupportsMustBeFloat
- Loading branch information
Showing
3 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
lib/rubocop/cop/chef/correctness/supports_must_be_float.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
42
spec/rubocop/cop/chef/correctness/supports_must_be_float_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |