From f54195408f026d34f10745f3bcc3db929fcc4931 Mon Sep 17 00:00:00 2001 From: Dominik Richter Date: Sat, 16 Apr 2016 18:09:28 -0400 Subject: [PATCH] simplify key symbolization in metadata --- lib/inspec/metadata.rb | 63 ++++++++++++++++++-------------------- test/unit/metadata_test.rb | 6 ++-- test/unit/profile_test.rb | 2 +- 3 files changed, 34 insertions(+), 37 deletions(-) diff --git a/lib/inspec/metadata.rb b/lib/inspec/metadata.rb index 22974ef70..5de314c98 100644 --- a/lib/inspec/metadata.rb +++ b/lib/inspec/metadata.rb @@ -43,7 +43,9 @@ module Inspec end def is_supported?(os, entry) - name, family, release = support_fields(entry) + name = entry[:'os-name'] || entry[:os] + family = entry[:'os-family'] + release = entry[:release] # return true if the backend matches the supported OS's # fields act as masks, i.e. any value configured for os-name, os-family, @@ -68,31 +70,8 @@ module Inspec name_ok && family_ok && release_ok end - def support_fields(entry) - if entry.is_a?(Hash) - try_support = self.class.symbolize_keys(entry) - name = try_support[:'os-name'] || try_support[:os] - family = try_support[:'os-family'] - release = try_support[:release] - elsif entry.is_a?(String) - @logger.warn( - "Do not use deprecated `supports: #{entry}` syntax. Instead use "\ - "`supports: {os-family: #{entry}}`.") - family = entry - end - - [name, family, release] - end - - def support_list - supp = params[:supports] - supp.is_a?(Hash) ? [supp] : Array(supp) - end - def inspec_requirement - supp = support_list - supp = supp.map { |x| self.class.symbolize_keys(x) } - inspec = supp.find { |x| !x[:inspec].nil? } || {} + inspec = params[:supports].find { |x| !x[:inspec].nil? } || {} Gem::Requirement.create(inspec[:inspec]) end @@ -105,10 +84,9 @@ module Inspec # with no supports specified, always return true, as there are no # constraints on the supported backend; it is equivalent to putting # all fields into accept-all mode - supp = support_list - return true if supp.empty? + return true if params[:supports].empty? - found = supp.find do |entry| + found = params[:supports].find do |entry| is_supported?(backend.os, entry) end @@ -148,32 +126,51 @@ module Inspec @missing_methods end - def self.symbolize_keys(hash) - hash.each_with_object({}) {|(k, v), h| + def self.symbolize_keys(obj) + return obj.map { |i| symbolize_keys(i) } if obj.is_a?(Array) + return obj unless obj.is_a?(Hash) + + obj.each_with_object({}) {|(k, v), h| v = symbolize_keys(v) if v.is_a?(Hash) + v = symbolize_keys(v) if v.is_a?(Array) h[k.to_sym] = v } end - def self.finalize(metadata, profile_id) + def self.finalize(metadata, profile_id, logger = nil) return nil if metadata.nil? param = metadata.params || {} param['name'] = profile_id.to_s unless profile_id.to_s.empty? param['version'] = param['version'].to_s unless param['version'].nil? metadata.params = symbolize_keys(param) + + # consolidate supports field with legacy mode + metadata.params[:supports] = + case x = metadata.params[:supports] + when Hash then [x] + when Array then x + when nil then [] + else + logger ||= Logger.new(nil) + logger.warn( + "Do not use deprecated `supports: #{x}` syntax. Instead use "\ + "`supports: {os-family: #{x}}`.") + [{ :'os-family' => x }] + end + metadata end def self.from_yaml(ref, contents, profile_id, logger = nil) res = Metadata.new(ref, logger) res.params = YAML.load(contents) - finalize(res, profile_id) + finalize(res, profile_id, logger) end def self.from_ruby(ref, contents, profile_id, logger = nil) res = Metadata.new(ref, logger) res.instance_eval(contents, ref, 1) - finalize(res, profile_id) + finalize(res, profile_id, logger) end def self.from_ref(ref, contents, profile_id, logger = nil) diff --git a/test/unit/metadata_test.rb b/test/unit/metadata_test.rb index 4cef921d8..c53374db7 100644 --- a/test/unit/metadata_test.rb +++ b/test/unit/metadata_test.rb @@ -12,7 +12,7 @@ describe 'metadata with supported operating systems' do res = Inspec::Metadata.from_yaml('mock', "---", nil, logger) # manually inject supported parameters res.params[:supports] = params - Inspec::Metadata.finalize(res, 'mock') + Inspec::Metadata.finalize(res, 'mock', logger) res end @@ -40,7 +40,7 @@ describe 'metadata with supported operating systems' do it 'loads the support field from metadata' do res = Inspec::Metadata.from_yaml('mock', "---\nsupports:\n - os: ubuntu", nil) - res.params[:supports].must_equal([{ 'os' => 'ubuntu' }]) + res.params[:supports].must_equal([{ os: 'ubuntu' }]) end it 'load a profile with empty supports clause' do @@ -50,10 +50,10 @@ describe 'metadata with supported operating systems' do it 'supports legacy simple support style, but warns' do # i.e. setting this to something that would fail: - m = supports_meta('linux') logger.expect :warn, nil, [ 'Do not use deprecated `supports: linux` '\ 'syntax. Instead use `supports: {os-family: linux}`.'] + m = supports_meta('linux') m.supports_transport?(backend).must_equal true logger.verify end diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index fb19deebd..74cb6c4a4 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -172,7 +172,7 @@ describe Inspec::Profile do end it 'doesnt have constraints on supported systems' do - profile.metadata.params.wont_include(:supports) + profile.metadata.params[:supports].must_equal([]) end end