Deprecate and warn when comparing against OS name with capitals/spaces (#2397)

* Testing train downcase platform names.
* Added NameCleaned tests and fixed some formatting.
* Clean up tests with helper method.
* Update to new gemfile and platform resource.

Signed-off-by: Jared Quick <jquick@chef.io>
This commit is contained in:
Jared Quick 2018-01-04 14:15:02 -05:00 committed by Adam Leff
parent 330d522577
commit 839ab3eef4
4 changed files with 45 additions and 6 deletions

View file

@ -26,7 +26,7 @@ Gem::Specification.new do |spec|
spec.required_ruby_version = '>= 2.3'
spec.add_dependency 'train', '~> 0.31', '>= 0.31.1'
spec.add_dependency 'train', '~> 0.32'
spec.add_dependency 'thor', '~> 0.19'
spec.add_dependency 'json', '>= 1.8', '< 3.0'
spec.add_dependency 'method_source', '~> 0.8'

View file

@ -32,7 +32,7 @@ module Inspec::Resources
# helper to collect a hash object easily
def params
{
name: @platform[:name],
name: name,
family: @platform[:family],
release: @platform[:release],
arch: @platform[:arch],

View file

@ -19,16 +19,36 @@ module Inspec::Resources
end
# add helper methods for easy access of properties
%w{name family release arch}.each do |property|
%w{family release arch}.each do |property|
define_method(property.to_sym) do
@platform.send(property)
end
end
def [](name)
# This is a string override for platform.name.
# TODO: removed in inspec 2.0
class NameCleaned < String
def ==(other)
if other =~ /[A-Z ]/
cleaned = other.downcase.tr(' ', '_')
Inspec::Log.warn "[DEPRECATED] Platform names will become lowercase in InSpec 2.0. Please match on '#{cleaned}' instead of '#{other}'"
super(cleaned)
else
super(other)
end
end
end
def name
NameCleaned.new(@platform.name)
end
def [](key)
# convert string to symbol
name = name.to_sym if name.is_a? String
@platform[name]
key = key.to_sym if key.is_a? String
return name if key == :name
@platform[key]
end
def platform?(name)

View file

@ -6,6 +6,25 @@ require 'inspec/resource'
describe 'Inspec::Resources::Platform' do
let(:resource) { resource = MockLoader.new(:ubuntu1504).load_resource('platform') }
describe 'NamedCleaned' do
let(:cleaner) { Inspec::Resources::PlatformResource::NameCleaned }
it 'verify name cleaned with uppercase' do
name = cleaner.new('upper_case_with_spaces')
_(name == 'Upper Case with Spaces').must_equal true
end
it 'verify name cleaned with uppercase fail' do
name = cleaner.new('upper_case_with_spaces')
_(name == 'Upper Case withFAIL Spaces').must_equal false
end
it 'verify name cleaned with lowercase' do
name = cleaner.new('lower_case_with_underscore')
_(name == 'lower_case_with_underscore').must_equal true
end
end
it 'verify platform parsing on Ubuntu' do
_(resource.name).must_equal 'ubuntu'
_(resource.family).must_equal 'debian'