From fb2ee9e732688f0b9916d853e00bb9baa0ff3b09 Mon Sep 17 00:00:00 2001 From: Jared Quick Date: Thu, 14 Dec 2017 12:55:35 -0500 Subject: [PATCH 1/2] Clean platform name to be downcase and underscored. Signed-off-by: Jared Quick --- lib/train/platforms/platform.rb | 26 +++++++++++++++++++++----- test/unit/platforms/platform_test.rb | 12 +++++++++--- test/windows/local_test.rb | 2 +- test/windows/winrm_test.rb | 2 +- 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/lib/train/platforms/platform.rb b/lib/train/platforms/platform.rb index a86edf32..e3ff2c79 100644 --- a/lib/train/platforms/platform.rb +++ b/lib/train/platforms/platform.rb @@ -3,7 +3,7 @@ module Train::Platforms class Platform include Train::Platforms::Common - attr_accessor :backend, :condition, :families, :family_hierarchy, :platform + attr_accessor :backend, :condition, :families, :family_hierarchy, :name_updated, :platform def initialize(name, condition = {}) @name = name @@ -13,6 +13,7 @@ def initialize(name, condition = {}) @platform = {} @detect = nil @title = name.to_s.capitalize + clean_name # add itself to the platform list Train::Platforms.list[name] = self @@ -25,7 +26,18 @@ def direct_families def name # Override here incase a updated name was set # during the detect logic - @platform[:name] || @name + @clean_name + end + + def clean_name + name = (@platform[:name] || @name) + # This is a history of name change being used upstream in inspec + if name =~ /[A-Z ]/ + @name_updated = [name] + name = name.downcase.tr(' ', '_') + @name_updated << name + end + @clean_name = name end # This is for backwords compatability with @@ -53,6 +65,10 @@ def to_hash # This is done later to add any custom # families/properties that were created def add_platform_methods + # Clean name up and add in any detect overrides + clean_name + + # Add in family methods family_list = Train::Platforms.families family_list.each_value do |k| next if respond_to?(k.name + '?') @@ -70,9 +86,9 @@ def add_platform_methods end # Create method for name if its not already true - plat_name = name.downcase.tr(' ', '_') + '?' - return if respond_to?(plat_name) - define_singleton_method(plat_name) do + m = name + '?' + return if respond_to?(m) + define_singleton_method(m) do true end end diff --git a/test/unit/platforms/platform_test.rb b/test/unit/platforms/platform_test.rb index 8b74f6ba..3f5a5e7f 100644 --- a/test/unit/platforms/platform_test.rb +++ b/test/unit/platforms/platform_test.rb @@ -34,13 +34,19 @@ def mock_os_hierarchy(plat) plat.title.must_equal('The Best Mock') end + it 'clean init name' do + plat = mock_platform_family('Mo ck') + plat.name.must_equal('mo_ck') + end + it 'set name and name override' do plat = mock_platform_family('mock') plat.name.must_equal('mock') plat[:name].must_equal('mock') - plat.platform[:name] = 'mock2020' - plat.name.must_equal('mock2020') - plat[:name].must_equal('mock2020') + plat.platform[:name] = 'Mock 2020' + plat.add_platform_methods + plat.name.must_equal('mock_2020') + plat[:name].must_equal('mock_2020') end it 'check families' do diff --git a/test/windows/local_test.rb b/test/windows/local_test.rb index c80c5e6e..33ef50c8 100644 --- a/test/windows/local_test.rb +++ b/test/windows/local_test.rb @@ -25,7 +25,7 @@ it 'verify os' do os = conn.os - os[:name].must_equal 'Windows Server 2012 R2 Datacenter' + os[:name].must_equal 'windows_server_2012_r2_datacenter' os[:family].must_equal "windows" os[:release].must_equal '6.3.9600' os[:arch].must_equal 'x86_64' diff --git a/test/windows/winrm_test.rb b/test/windows/winrm_test.rb index cbfaa5a8..b92c5423 100644 --- a/test/windows/winrm_test.rb +++ b/test/windows/winrm_test.rb @@ -27,7 +27,7 @@ it 'verify os' do os = conn.os - os[:name].must_equal 'Windows Server 2012 R2 Datacenter' + os[:name].must_equal 'windows_server_2012_r2_datacenter' os[:family].must_equal 'windows' os[:release].must_equal '6.3.9600' os[:arch].must_equal 'x86_64' From eb7d1ec3a71bdfd672fa085545be48622f500644 Mon Sep 17 00:00:00 2001 From: Jared Quick Date: Tue, 19 Dec 2017 11:34:36 -0500 Subject: [PATCH 2/2] Remove name_updated and add in force clean. Signed-off-by: Jared Quick --- lib/train/platforms/platform.rb | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/lib/train/platforms/platform.rb b/lib/train/platforms/platform.rb index e3ff2c79..e0b25059 100644 --- a/lib/train/platforms/platform.rb +++ b/lib/train/platforms/platform.rb @@ -3,7 +3,7 @@ module Train::Platforms class Platform include Train::Platforms::Common - attr_accessor :backend, :condition, :families, :family_hierarchy, :name_updated, :platform + attr_accessor :backend, :condition, :families, :family_hierarchy, :platform def initialize(name, condition = {}) @name = name @@ -13,7 +13,6 @@ def initialize(name, condition = {}) @platform = {} @detect = nil @title = name.to_s.capitalize - clean_name # add itself to the platform list Train::Platforms.list[name] = self @@ -26,18 +25,16 @@ def direct_families def name # Override here incase a updated name was set # during the detect logic - @clean_name + clean_name end - def clean_name - name = (@platform[:name] || @name) - # This is a history of name change being used upstream in inspec - if name =~ /[A-Z ]/ - @name_updated = [name] - name = name.downcase.tr(' ', '_') - @name_updated << name - end - @clean_name = name + def clean_name(force: false) + @cleaned_name = nil if force + @cleaned_name ||= begin + name = (@platform[:name] || @name) + name.downcase!.tr!(' ', '_') if name =~ /[A-Z ]/ + name + end end # This is for backwords compatability with @@ -65,8 +62,8 @@ def to_hash # This is done later to add any custom # families/properties that were created def add_platform_methods - # Clean name up and add in any detect overrides - clean_name + # Redo clean name if there is a detect override + clean_name(force: true) unless @platform[:name].nil? # Add in family methods family_list = Train::Platforms.families