Skip to content

Commit

Permalink
Handle additional scenarios of platform checks
Browse files Browse the repository at this point in the history
['rhel', some_variable].include?(node['platform_family'] is valid and we
should handle it all correctly without added or double quotes

Signed-off-by: Tim Smith <tsmith@chef.io>
  • Loading branch information
tas50 committed Dec 16, 2019
1 parent 138c270 commit 9f06565
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/rubocop/cop/chef/style/use_platform_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ def autocorrect(node)
end

platform_include?(node) do |plats, type|
corrected_string = "#{type.value}?('#{plats.values.map(&:source).join("', '")}')"
platforms = plats.values.map { |x| x.str_type? ? "'#{x.value}'" : x.source }
corrected_string = "#{type.value}?(#{platforms.join(', ')})"
corrector.replace(node.loc.expression, corrected_string)
end
end
Expand Down
13 changes: 13 additions & 0 deletions spec/rubocop/cop/chef/style/use_platform_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,17 @@
end
RUBY
end

it 'registers an offense when checking platform family with include? and a quoted array of values' do
expect_offense(<<~RUBY)
if ['rhel', some_variable].include?(node['platform_family'])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use platform? and platform_family? helpers to check a node's platform
end
RUBY

expect_correction(<<~RUBY)
if platform_family?('rhel', some_variable)
end
RUBY
end
end

0 comments on commit 9f06565

Please sign in to comment.