From 6927637126b8d4ce1fe08687af9406af218c4e0f Mon Sep 17 00:00:00 2001 From: Eric Hanko Date: Thu, 26 Oct 2017 00:30:58 -0700 Subject: [PATCH 01/24] first commit plistbuddy --- libraries/plistbuddy.rb | 47 +++++++++++++++++++++++ resources/plistbuddy.rb | 30 +++++++++++++++ spec/unit/libraries/plistbuddy_helpers.rb | 0 3 files changed, 77 insertions(+) create mode 100644 libraries/plistbuddy.rb create mode 100644 resources/plistbuddy.rb create mode 100644 spec/unit/libraries/plistbuddy_helpers.rb diff --git a/libraries/plistbuddy.rb b/libraries/plistbuddy.rb new file mode 100644 index 00000000..a4de3654 --- /dev/null +++ b/libraries/plistbuddy.rb @@ -0,0 +1,47 @@ +module OfficeMacDevEnv + module PlistbuddyHelpers + class PlistBuddy < Dir + include Chef::Mixin::ShellOut + + def exist?(entry, value) + shell_out!(plistbuddy_command('Print', entry, value)) + end + + private + + def convert_to_data_type(entry) + data_type_cases = { Array.to_s => "array #{value}", + Integer.to_s => "int #{value}", + TrueClass.to_s => 'bool TRUE', + FalseClass.to_s => 'bool FALSE', + Hash.to_s => "dict #{value}", + String.to_s => "string #{value}", + Float.to_s => "float #{value}" } + data_type_cases[entry] + end + + def execute_plistbuddy_command(command_action, entry, value) + entry = convert_to_data_type new_resource.entry + "/usr/libexec/Plistbuddy -c \':#{command_action.capitalize.to_s} #{entry} #{value}\'" + end + + end +end + +# +# The module you have defined may be extended within the recipe to grant the +# recipe the helper methods you define. +# +# Within your recipe you would write: +# +# extend OfficeMacDevEnv::PlistbuddyHelpers +# +# my_helper_method +# +# You may also add this to a single resource within a recipe: +# +# template '/etc/app.conf' do +# extend OfficeMacDevEnv::PlistbuddyHelpers +# variables specific_key: my_helper_method +# end +# diff --git a/resources/plistbuddy.rb b/resources/plistbuddy.rb new file mode 100644 index 00000000..ba5b57ff --- /dev/null +++ b/resources/plistbuddy.rb @@ -0,0 +1,30 @@ +resource_name :plistbuddy + +property :action, Symbol, name_property: true + +property :entry +property :value +property :file +property :clear +property :data_type +property :sub_command + +action_class do + def plistbuddy_command(command_action, entry, value) + entry = convert_to_data_type new_resource.entry + "/usr/libexec/Plistbuddy -c ':#{command_action.capitalize.to_s} #{entry} #{value}" + end + + private + + def convert_to_data_type(entry) + data_type_cases = { Array.to_s => "array #{value}", + Integer.to_s => "int #{value}", + TrueClass.to_s => 'bool TRUE', + FalseClass.to_s => 'bool FALSE', + Hash.to_s => "dict #{value}", + String.to_s => "string #{value}", + Float.to_s => "float #{value}" } + data_type_cases[entry] + end +end diff --git a/spec/unit/libraries/plistbuddy_helpers.rb b/spec/unit/libraries/plistbuddy_helpers.rb new file mode 100644 index 00000000..e69de29b From b6cc4f0c5aecf69f6aa28c5c0b1ee048ec094a6a Mon Sep 17 00:00:00 2001 From: Eric Hanko Date: Thu, 26 Oct 2017 01:30:48 -0700 Subject: [PATCH 02/24] all recipe/plist in macos --- libraries/plistbuddy.rb | 64 +++++++++-------------- resources/plistbuddy.rb | 25 ++++----- spec/unit/libraries/plistbuddy_helpers.rb | 0 spec/unit/libraries/plistbuddy_spec.rb | 9 ++++ 4 files changed, 44 insertions(+), 54 deletions(-) delete mode 100644 spec/unit/libraries/plistbuddy_helpers.rb create mode 100644 spec/unit/libraries/plistbuddy_spec.rb diff --git a/libraries/plistbuddy.rb b/libraries/plistbuddy.rb index a4de3654..ebc7c6cd 100644 --- a/libraries/plistbuddy.rb +++ b/libraries/plistbuddy.rb @@ -1,47 +1,31 @@ module OfficeMacDevEnv module PlistbuddyHelpers - class PlistBuddy < Dir - include Chef::Mixin::ShellOut + module PlistBuddy + extend Dir + include Chef::Mixin::ShellOut - def exist?(entry, value) - shell_out!(plistbuddy_command('Print', entry, value)) - end - - private - - def convert_to_data_type(entry) - data_type_cases = { Array.to_s => "array #{value}", - Integer.to_s => "int #{value}", - TrueClass.to_s => 'bool TRUE', - FalseClass.to_s => 'bool FALSE', - Hash.to_s => "dict #{value}", - String.to_s => "string #{value}", - Float.to_s => "float #{value}" } - data_type_cases[entry] - end + def self.exist? + command = format_plistbuddy_command('print', entry, value) + shell_out(command).error? + end - def execute_plistbuddy_command(command_action, entry, value) - entry = convert_to_data_type new_resource.entry - "/usr/libexec/Plistbuddy -c \':#{command_action.capitalize.to_s} #{entry} #{value}\'" - end + private + def convert_to_string_from_data_type(entry) + data_type_cases = { Array.to_s => "array #{value}", + Integer.to_s => "int #{value}", + TrueClass.to_s => 'bool TRUE', + FalseClass.to_s => 'bool FALSE', + Hash.to_s => "dict #{value}", + String.to_s => "string #{value}", + Float.to_s => "float #{value}" } + data_type_cases[entry] + end + + def format_plistbuddy_command(action_property, entry, value) + entry = convert_to_data_type enty + "/usr/libexec/Plistbuddy -c \':#{action_property.to_s.capitalize} #{entry} #{value}\'" + end + end end end - -# -# The module you have defined may be extended within the recipe to grant the -# recipe the helper methods you define. -# -# Within your recipe you would write: -# -# extend OfficeMacDevEnv::PlistbuddyHelpers -# -# my_helper_method -# -# You may also add this to a single resource within a recipe: -# -# template '/etc/app.conf' do -# extend OfficeMacDevEnv::PlistbuddyHelpers -# variables specific_key: my_helper_method -# end -# diff --git a/resources/plistbuddy.rb b/resources/plistbuddy.rb index ba5b57ff..5d86d60f 100644 --- a/resources/plistbuddy.rb +++ b/resources/plistbuddy.rb @@ -2,29 +2,26 @@ property :action, Symbol, name_property: true -property :entry -property :value -property :file -property :clear -property :data_type -property :sub_command +property :name, String +property :entry, String +property :value, [Hash, String, Array, TrueClass, FalseClass, Int, Float] action_class do def plistbuddy_command(command_action, entry, value) entry = convert_to_data_type new_resource.entry - "/usr/libexec/Plistbuddy -c ':#{command_action.capitalize.to_s} #{entry} #{value}" + "/usr/libexec/Plistbuddy -c ':#{command_action.capitalize} #{entry} #{value}" end private def convert_to_data_type(entry) - data_type_cases = { Array.to_s => "array #{value}", - Integer.to_s => "int #{value}", - TrueClass.to_s => 'bool TRUE', - FalseClass.to_s => 'bool FALSE', - Hash.to_s => "dict #{value}", - String.to_s => "string #{value}", - Float.to_s => "float #{value}" } + data_type_cases = { Array.to_s => "array #{new_resource.value}", + Integer.to_s => "int #{new_resource.value}", + TrueClass.to_s => 'bool TRUE', + FalseClass.to_s => 'bool FALSE', + Hash.to_s => "dict #{new_resource.value}", + String.to_s => "string #{new_resource.value}", + Float.to_s => "float #{new_resource.value}" } data_type_cases[entry] end end diff --git a/spec/unit/libraries/plistbuddy_helpers.rb b/spec/unit/libraries/plistbuddy_helpers.rb deleted file mode 100644 index e69de29b..00000000 diff --git a/spec/unit/libraries/plistbuddy_spec.rb b/spec/unit/libraries/plistbuddy_spec.rb new file mode 100644 index 00000000..8b91b9ed --- /dev/null +++ b/spec/unit/libraries/plistbuddy_spec.rb @@ -0,0 +1,9 @@ +require 'spec_helper' + +include OfficeMacDevEnv::PlistbuddyHelpers::PlistBuddy + +context 'When given some commands' do + it 'the command is formatted properly' do + expect(format_plistbuddy_command(:add, 'DidSeeSiriSetup', 0)).to eq "/usr/libexec/Plistbuddy -c ':Add DidSiriSetup 0'" + end +end From ed74b85aa6c7b557b7fcb51bc13db089a37950dc Mon Sep 17 00:00:00 2001 From: Eric Hanko Date: Thu, 26 Oct 2017 10:44:30 -0700 Subject: [PATCH 03/24] extract plistbuddy to resource --- libraries/plistbuddy.rb | 48 +++++++++++++------------- spec/spec_helper.rb | 7 ++-- spec/unit/libraries/plistbuddy_spec.rb | 5 ++- 3 files changed, 31 insertions(+), 29 deletions(-) diff --git a/libraries/plistbuddy.rb b/libraries/plistbuddy.rb index ebc7c6cd..a51099b7 100644 --- a/libraries/plistbuddy.rb +++ b/libraries/plistbuddy.rb @@ -1,31 +1,31 @@ -module OfficeMacDevEnv - module PlistbuddyHelpers - module PlistBuddy - extend Dir - include Chef::Mixin::ShellOut +module PlistBuddy + module Helper + include Chef::Mixin::ShellOut - def self.exist? - command = format_plistbuddy_command('print', entry, value) - shell_out(command).error? - end + def self.exist? + command = format_plistbuddy_command('print', entry, value) + shell_out(command).error? + end - private + private - def convert_to_string_from_data_type(entry) - data_type_cases = { Array.to_s => "array #{value}", - Integer.to_s => "int #{value}", - TrueClass.to_s => 'bool TRUE', - FalseClass.to_s => 'bool FALSE', - Hash.to_s => "dict #{value}", - String.to_s => "string #{value}", - Float.to_s => "float #{value}" } - data_type_cases[entry] - end + def convert_to_string_from_data_type(entry) + data_type_cases = { Array.to_s => "array #{value}", + Integer.to_s => "int #{value}", + TrueClass.to_s => 'bool TRUE', + FalseClass.to_s => 'bool FALSE', + Hash.to_s => "dict #{value}", + String.to_s => "string #{value}", + Float.to_s => "float #{value}" } + data_type_cases[entry] + end - def format_plistbuddy_command(action_property, entry, value) - entry = convert_to_data_type enty - "/usr/libexec/Plistbuddy -c \':#{action_property.to_s.capitalize} #{entry} #{value}\'" - end + def format_plistbuddy_command(action_property, entry, value) + entry = convert_to_data_type enty + "/usr/libexec/Plistbuddy -c \':#{action_property.to_s.capitalize} #{entry} #{value}\'" end end end + +Chef::Recipe.include(PlistBuddy::Helper) +Chef::Resource.include(PlistBuddy::Helper) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 1dd5126b..b16fba26 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,2 +1,5 @@ -require 'chefspec' -require 'chefspec/berkshelf' +# require 'chefspec' +# require 'chefspec/berkshelf' + +require_relative '../libraries/plistbuddy' +require_relative '../libraries/xcode' \ No newline at end of file diff --git a/spec/unit/libraries/plistbuddy_spec.rb b/spec/unit/libraries/plistbuddy_spec.rb index 8b91b9ed..74528bff 100644 --- a/spec/unit/libraries/plistbuddy_spec.rb +++ b/spec/unit/libraries/plistbuddy_spec.rb @@ -1,9 +1,8 @@ require 'spec_helper' - -include OfficeMacDevEnv::PlistbuddyHelpers::PlistBuddy +include PlistBuddy::Helper context 'When given some commands' do it 'the command is formatted properly' do - expect(format_plistbuddy_command(:add, 'DidSeeSiriSetup', 0)).to eq "/usr/libexec/Plistbuddy -c ':Add DidSiriSetup 0'" + expect(Plist.format_plistbuddy_command(:add, 'DidSeeSiriSetup', 0)).to eq "/usr/libexec/Plistbuddy -c ':Add DidSiriSetup 0'" end end From 9beca33ee6128e198c3766faf8b3f948480de2e1 Mon Sep 17 00:00:00 2001 From: Eric Hanko Date: Thu, 26 Oct 2017 11:27:33 -0700 Subject: [PATCH 04/24] Straighten out unit tests - Correctly include modules - Correctly setup spec_helper - Correct unit test / respec setup --- libraries/plistbuddy.rb | 10 +++++----- spec/spec_helper.rb | 6 +++--- spec/unit/libraries/plistbuddy_spec.rb | 11 +++++++---- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/libraries/plistbuddy.rb b/libraries/plistbuddy.rb index a51099b7..9c456554 100644 --- a/libraries/plistbuddy.rb +++ b/libraries/plistbuddy.rb @@ -1,7 +1,7 @@ -module PlistBuddy - module Helper - include Chef::Mixin::ShellOut +include Chef::Mixin::ShellOut +module MacOS + module PlistBuddyHelpers def self.exist? command = format_plistbuddy_command('print', entry, value) shell_out(command).error? @@ -27,5 +27,5 @@ def format_plistbuddy_command(action_property, entry, value) end end -Chef::Recipe.include(PlistBuddy::Helper) -Chef::Resource.include(PlistBuddy::Helper) +Chef::Recipe.include(MacOS::PlistBuddyHelpers) +Chef::Resource.include(MacOS::PlistBuddyHelpers) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b16fba26..08509c91 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,5 +1,5 @@ -# require 'chefspec' -# require 'chefspec/berkshelf' +require 'chefspec' +require 'chefspec/berkshelf' require_relative '../libraries/plistbuddy' -require_relative '../libraries/xcode' \ No newline at end of file +require_relative '../libraries/xcode' diff --git a/spec/unit/libraries/plistbuddy_spec.rb b/spec/unit/libraries/plistbuddy_spec.rb index 74528bff..b948fa96 100644 --- a/spec/unit/libraries/plistbuddy_spec.rb +++ b/spec/unit/libraries/plistbuddy_spec.rb @@ -1,8 +1,11 @@ require 'spec_helper' -include PlistBuddy::Helper -context 'When given some commands' do - it 'the command is formatted properly' do - expect(Plist.format_plistbuddy_command(:add, 'DidSeeSiriSetup', 0)).to eq "/usr/libexec/Plistbuddy -c ':Add DidSiriSetup 0'" +include MacOS::PlistBuddyHelpers + +describe MacOS::PlistBuddyHelpers, '#format_plistbuddy_command' do + context 'When given some commands' do + it 'the command is formatted properly' do + expect(format_plistbuddy_command(:add, 'DidSeeSiriSetup', 0)).to eq "/usr/libexec/Plistbuddy -c ':Add DidSiriSetup 0'" + end end end From 2bca4d53a88914997c1279c18f93bfa6cc2099b7 Mon Sep 17 00:00:00 2001 From: Eric Hanko Date: Thu, 26 Oct 2017 12:07:23 -0700 Subject: [PATCH 05/24] first passing test --- libraries/plistbuddy.rb | 31 ++++++++++++-------------- resources/plistbuddy.rb | 30 ++++++++++++------------- spec/unit/libraries/plistbuddy_spec.rb | 2 +- spec/unit/recipes/configurator.rb | 2 +- spec/unit/recipes/default_spec.rb | 2 +- spec/unit/recipes/keep_awake_spec.rb | 2 +- spec/unit/recipes/xcode_spec.rb | 2 +- 7 files changed, 34 insertions(+), 37 deletions(-) diff --git a/libraries/plistbuddy.rb b/libraries/plistbuddy.rb index 9c456554..6f514eb2 100644 --- a/libraries/plistbuddy.rb +++ b/libraries/plistbuddy.rb @@ -2,27 +2,24 @@ module MacOS module PlistBuddyHelpers - def self.exist? - command = format_plistbuddy_command('print', entry, value) - shell_out(command).error? - end - - private - - def convert_to_string_from_data_type(entry) - data_type_cases = { Array.to_s => "array #{value}", - Integer.to_s => "int #{value}", + # def self.exist? + # command = format_plistbuddy_command('print', entry, value) + # shell_out(command).error? + # end + def convert_to_string_from_data_type(plist_entry) + data_type_cases = { Array.to_s => "array #{plist_entry}", + Integer.to_s => "int #{plist_entry}", TrueClass.to_s => 'bool TRUE', FalseClass.to_s => 'bool FALSE', - Hash.to_s => "dict #{value}", - String.to_s => "string #{value}", - Float.to_s => "float #{value}" } - data_type_cases[entry] + Hash.to_s => "dict #{plist_entry}", + String.to_s => "string #{plist_entry}", + Float.to_s => "float #{plist_entry}" } + data_type_cases[plist_entry.class] end - def format_plistbuddy_command(action_property, entry, value) - entry = convert_to_data_type enty - "/usr/libexec/Plistbuddy -c \':#{action_property.to_s.capitalize} #{entry} #{value}\'" + def format_plistbuddy_command(action_property, plist_entry, plist_value = nil) + entry_wdt = convert_to_string_from_data_type plist_entry + "/usr/libexec/Plistbuddy -c \':#{action_property.to_s.capitalize} #{plist_entry} #{plist_value}#{entry_wdt}\'" end end end diff --git a/resources/plistbuddy.rb b/resources/plistbuddy.rb index 5d86d60f..12b55688 100644 --- a/resources/plistbuddy.rb +++ b/resources/plistbuddy.rb @@ -7,21 +7,21 @@ property :value, [Hash, String, Array, TrueClass, FalseClass, Int, Float] action_class do - def plistbuddy_command(command_action, entry, value) - entry = convert_to_data_type new_resource.entry - "/usr/libexec/Plistbuddy -c ':#{command_action.capitalize} #{entry} #{value}" - end + # def plistbuddy_command(command_action, entry, value) + # entry = convert_to_data_type new_resource.entry + # "/usr/libexec/Plistbuddy -c ':#{command_action.capitalize} #{entry} #{value}" + # end - private + # private - def convert_to_data_type(entry) - data_type_cases = { Array.to_s => "array #{new_resource.value}", - Integer.to_s => "int #{new_resource.value}", - TrueClass.to_s => 'bool TRUE', - FalseClass.to_s => 'bool FALSE', - Hash.to_s => "dict #{new_resource.value}", - String.to_s => "string #{new_resource.value}", - Float.to_s => "float #{new_resource.value}" } - data_type_cases[entry] - end + # def convert_to_data_type(entry) + # data_type_cases = { Array.to_s => "array #{new_resource.value}", + # Integer.to_s => "int #{new_resource.value}", + # TrueClass.to_s => 'bool TRUE', + # FalseClass.to_s => 'bool FALSE', + # Hash.to_s => "dict #{new_resource.value}", + # String.to_s => "string #{new_resource.value}", + # Float.to_s => "float #{new_resource.value}" } + # data_type_cases[entry] + # end end diff --git a/spec/unit/libraries/plistbuddy_spec.rb b/spec/unit/libraries/plistbuddy_spec.rb index b948fa96..4f950ecd 100644 --- a/spec/unit/libraries/plistbuddy_spec.rb +++ b/spec/unit/libraries/plistbuddy_spec.rb @@ -5,7 +5,7 @@ describe MacOS::PlistBuddyHelpers, '#format_plistbuddy_command' do context 'When given some commands' do it 'the command is formatted properly' do - expect(format_plistbuddy_command(:add, 'DidSeeSiriSetup', 0)).to eq "/usr/libexec/Plistbuddy -c ':Add DidSiriSetup 0'" + expect(format_plistbuddy_command(:add, 'DidSeeSiriSetup', 0)).to eq "/usr/libexec/Plistbuddy -c ':Add DidSeeSiriSetup 0'" end end end diff --git a/spec/unit/recipes/configurator.rb b/spec/unit/recipes/configurator.rb index 96969ac4..21c21940 100644 --- a/spec/unit/recipes/configurator.rb +++ b/spec/unit/recipes/configurator.rb @@ -13,7 +13,7 @@ runner.converge(described_recipe) end - it 'converges successfully' do + xit 'converges successfully' do expect { chef_run }.to_not raise_error end end diff --git a/spec/unit/recipes/default_spec.rb b/spec/unit/recipes/default_spec.rb index df3ad1c3..e677b686 100644 --- a/spec/unit/recipes/default_spec.rb +++ b/spec/unit/recipes/default_spec.rb @@ -7,7 +7,7 @@ runner.converge(described_recipe) end - it 'converges successfully' do + xit 'converges successfully' do expect { chef_run }.to_not raise_error end end diff --git a/spec/unit/recipes/keep_awake_spec.rb b/spec/unit/recipes/keep_awake_spec.rb index 4e731e2e..5f44447e 100644 --- a/spec/unit/recipes/keep_awake_spec.rb +++ b/spec/unit/recipes/keep_awake_spec.rb @@ -7,7 +7,7 @@ runner.converge(described_recipe) end - it 'converges successfully' do + xit 'converges successfully' do expect { chef_run }.to_not raise_error end end diff --git a/spec/unit/recipes/xcode_spec.rb b/spec/unit/recipes/xcode_spec.rb index 7f53222f..b8d8a17c 100644 --- a/spec/unit/recipes/xcode_spec.rb +++ b/spec/unit/recipes/xcode_spec.rb @@ -13,7 +13,7 @@ runner.converge(described_recipe) end - it 'converges successfully' do + xit 'converges successfully' do expect { chef_run }.to_not raise_error end end From b44a02320b44d2f1ed5a5ed1c231f807d7477c59 Mon Sep 17 00:00:00 2001 From: Eric Hanko Date: Thu, 26 Oct 2017 12:24:18 -0700 Subject: [PATCH 06/24] add more data type tests --- libraries/plistbuddy.rb | 9 +++++---- spec/unit/libraries/plistbuddy_spec.rb | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/libraries/plistbuddy.rb b/libraries/plistbuddy.rb index 6f514eb2..4642f43e 100644 --- a/libraries/plistbuddy.rb +++ b/libraries/plistbuddy.rb @@ -2,10 +2,11 @@ module MacOS module PlistBuddyHelpers - # def self.exist? - # command = format_plistbuddy_command('print', entry, value) - # shell_out(command).error? - # end + def self.exist? + command = format_plistbuddy_command('print', entry, value) + shell_out(command).error? + end + def convert_to_string_from_data_type(plist_entry) data_type_cases = { Array.to_s => "array #{plist_entry}", Integer.to_s => "int #{plist_entry}", diff --git a/spec/unit/libraries/plistbuddy_spec.rb b/spec/unit/libraries/plistbuddy_spec.rb index 4f950ecd..0c48d02a 100644 --- a/spec/unit/libraries/plistbuddy_spec.rb +++ b/spec/unit/libraries/plistbuddy_spec.rb @@ -9,3 +9,27 @@ end end end + +describe MacOS::PlistBuddyHelpers, '#convert_to_string_from_data_type' do + context 'When given a certain data type' do + it 'returns the required PlistBuddy boolean entry' do + expect(convert_to_string_from_data_type(true)).to eq 'bool TRUE' + end + + it 'returns the required PlistBuddy boolean entry' do + expect(convert_to_string_from_data_type(%w(foo bar))).to eq "array ['foo', 'bar']" + end + + it 'returns the required PlistBuddy boolean entry' do + expect(convert_to_string_from_data_type({'baz' => 'qux'})).to eq "dict {'baz' => 'qux'}" + end + + it 'returns the required PlistBuddy boolean entry' do + expect(convert_to_string_from_data_type('quux')).to eq 'string quux' + end + + it 'returns the required PlistBuddy boolean entry' do + expect(convert_to_string_from_data_type(1)).to eq 'int 1' + end + end +end \ No newline at end of file From 21dddd9c1b4290cad9b0c7875f11ff5f2e0e686c Mon Sep 17 00:00:00 2001 From: Eric Hanko Date: Thu, 26 Oct 2017 13:09:08 -0700 Subject: [PATCH 07/24] Most data types passing - Boolean values are working - Array and data type values are not - PlistBuddy "commands" (add, print, etc.) are passing --- libraries/plistbuddy.rb | 18 +++++++++--------- spec/unit/libraries/plistbuddy_spec.rb | 6 +++--- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/libraries/plistbuddy.rb b/libraries/plistbuddy.rb index 4642f43e..4c3cc431 100644 --- a/libraries/plistbuddy.rb +++ b/libraries/plistbuddy.rb @@ -8,19 +8,19 @@ def self.exist? end def convert_to_string_from_data_type(plist_entry) - data_type_cases = { Array.to_s => "array #{plist_entry}", - Integer.to_s => "int #{plist_entry}", - TrueClass.to_s => 'bool TRUE', - FalseClass.to_s => 'bool FALSE', - Hash.to_s => "dict #{plist_entry}", - String.to_s => "string #{plist_entry}", - Float.to_s => "float #{plist_entry}" } + data_type_cases = { Array => "array #{plist_entry}", + Integer => "int #{plist_entry}", + TrueClass => 'bool TRUE', + FalseClass => 'bool FALSE', + Hash => "dict #{plist_entry}", + String => "string #{plist_entry}", + Float => "float #{plist_entry}" } data_type_cases[plist_entry.class] end def format_plistbuddy_command(action_property, plist_entry, plist_value = nil) - entry_wdt = convert_to_string_from_data_type plist_entry - "/usr/libexec/Plistbuddy -c \':#{action_property.to_s.capitalize} #{plist_entry} #{plist_value}#{entry_wdt}\'" + value_with_data_type = convert_to_string_from_data_type plist_value + "/usr/libexec/Plistbuddy -c \'#{action_property.to_s.capitalize} :#{plist_entry} #{value_with_data_type}\'" end end end diff --git a/spec/unit/libraries/plistbuddy_spec.rb b/spec/unit/libraries/plistbuddy_spec.rb index 0c48d02a..df66bab6 100644 --- a/spec/unit/libraries/plistbuddy_spec.rb +++ b/spec/unit/libraries/plistbuddy_spec.rb @@ -5,7 +5,7 @@ describe MacOS::PlistBuddyHelpers, '#format_plistbuddy_command' do context 'When given some commands' do it 'the command is formatted properly' do - expect(format_plistbuddy_command(:add, 'DidSeeSiriSetup', 0)).to eq "/usr/libexec/Plistbuddy -c ':Add DidSeeSiriSetup 0'" + expect(format_plistbuddy_command(:add, 'DidSeeSiriSetup', true)).to eq "/usr/libexec/Plistbuddy -c 'Add :DidSeeSiriSetup bool TRUE'" end end end @@ -21,7 +21,7 @@ end it 'returns the required PlistBuddy boolean entry' do - expect(convert_to_string_from_data_type({'baz' => 'qux'})).to eq "dict {'baz' => 'qux'}" + expect(convert_to_string_from_data_type('baz' => 'qux')).to eq "dict {'baz' => 'qux'}" end it 'returns the required PlistBuddy boolean entry' do @@ -32,4 +32,4 @@ expect(convert_to_string_from_data_type(1)).to eq 'int 1' end end -end \ No newline at end of file +end From 70f9d09e2ec4a8a3ef346daf0a9df9e1c51201bb Mon Sep 17 00:00:00 2001 From: Eric Hanko Date: Thu, 26 Oct 2017 13:39:55 -0700 Subject: [PATCH 08/24] More testing - Skip Array and Dict types as their implementation will be more complicated - Add test for float type - Put the correct type in the test description - Cookstyle changes --- spec/unit/libraries/plistbuddy_spec.rb | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/spec/unit/libraries/plistbuddy_spec.rb b/spec/unit/libraries/plistbuddy_spec.rb index df66bab6..03c18cb3 100644 --- a/spec/unit/libraries/plistbuddy_spec.rb +++ b/spec/unit/libraries/plistbuddy_spec.rb @@ -16,20 +16,24 @@ expect(convert_to_string_from_data_type(true)).to eq 'bool TRUE' end - it 'returns the required PlistBuddy boolean entry' do - expect(convert_to_string_from_data_type(%w(foo bar))).to eq "array ['foo', 'bar']" + xit 'returns the required PlistBuddy array entry' do # TODO: Implement proper plist array syntax + expect(convert_to_string_from_data_type(%w(foo bar))).to eq 'array foo bar' end - it 'returns the required PlistBuddy boolean entry' do - expect(convert_to_string_from_data_type('baz' => 'qux')).to eq "dict {'baz' => 'qux'}" + xit 'returns the required PlistBuddy dictionary entry' do # TODO: Implement proper plist array syntax + expect(convert_to_string_from_data_type('baz' => 'qux')).to eq 'dict key value' end - it 'returns the required PlistBuddy boolean entry' do + it 'returns the required PlistBuddy string entry' do expect(convert_to_string_from_data_type('quux')).to eq 'string quux' end - it 'returns the required PlistBuddy boolean entry' do + it 'returns the required PlistBuddy int entry' do expect(convert_to_string_from_data_type(1)).to eq 'int 1' end + + it 'returns the required PlistBuddy float entry' do + expect(convert_to_string_from_data_type(1.0)).to eq 'float 1.0' + end end end From 4a942c3dc48dafb2af19ef2dae19044dd19881d6 Mon Sep 17 00:00:00 2001 From: Eric Hanko Date: Thu, 26 Oct 2017 15:38:14 -0700 Subject: [PATCH 09/24] PlistBuddy and testing update - Add separate test cookbook for PlistBuddy resource - Add kitchen.yml for the above purpose - Modify and update plistbuddy resource/helpers --- .kitchen.yml | 32 +++++++++++++++++++ libraries/plistbuddy.rb | 5 --- resources/plistbuddy.rb | 28 +++++++--------- spec/unit/libraries/plistbuddy_spec.rb | 20 +++++++++--- spec/unit/recipes/plistbuddy_spec.rb | 14 ++++++++ .../plistbuddy/recipes/customize_dock.rb | 5 +++ test/cookbooks/plistbuddy/recipes/default.rb | 2 ++ .../plistbuddy/recipes/show_hidden_files.rb | 5 +++ .../test/smoke/default/default_test.rb | 8 +++++ 9 files changed, 93 insertions(+), 26 deletions(-) create mode 100644 .kitchen.yml create mode 100644 spec/unit/recipes/plistbuddy_spec.rb create mode 100644 test/cookbooks/plistbuddy/recipes/customize_dock.rb create mode 100644 test/cookbooks/plistbuddy/recipes/default.rb create mode 100644 test/cookbooks/plistbuddy/recipes/show_hidden_files.rb create mode 100644 test/cookbooks/plistbuddy/test/smoke/default/default_test.rb diff --git a/.kitchen.yml b/.kitchen.yml new file mode 100644 index 00000000..a7be1b91 --- /dev/null +++ b/.kitchen.yml @@ -0,0 +1,32 @@ +--- +driver: + name: vagrant + provider: parallels + customize: + memory: 8192 + cpus: 2 + +provisioner: + name: chef_zero + +verifier: + name: inspec + +transport: + name: ssh + elevated: true + +platforms: + - name: apex/macos-10.12.6 + driver: + box: apex/macos-10.12.6 + box_url: http://imagr.corp.microsoft.com:8099/apex/macos-10.12.6 + box_check_update: true + +suites: + - name: plistbuddy + run_list: + - recipe[plistbuddy::default] + verifier: + inspec_tests: + - test/smoke/default diff --git a/libraries/plistbuddy.rb b/libraries/plistbuddy.rb index 4c3cc431..041e03d9 100644 --- a/libraries/plistbuddy.rb +++ b/libraries/plistbuddy.rb @@ -2,11 +2,6 @@ module MacOS module PlistBuddyHelpers - def self.exist? - command = format_plistbuddy_command('print', entry, value) - shell_out(command).error? - end - def convert_to_string_from_data_type(plist_entry) data_type_cases = { Array => "array #{plist_entry}", Integer => "int #{plist_entry}", diff --git a/resources/plistbuddy.rb b/resources/plistbuddy.rb index 12b55688..405611a7 100644 --- a/resources/plistbuddy.rb +++ b/resources/plistbuddy.rb @@ -3,25 +3,19 @@ property :action, Symbol, name_property: true property :name, String -property :entry, String +property :entry, String, required: true property :value, [Hash, String, Array, TrueClass, FalseClass, Int, Float] action_class do - # def plistbuddy_command(command_action, entry, value) - # entry = convert_to_data_type new_resource.entry - # "/usr/libexec/Plistbuddy -c ':#{command_action.capitalize} #{entry} #{value}" - # end - - # private + def entry_exists? + command = format_plistbuddy_command(:print, new_resource.entry, new_resource.value) + shell_out(command).error? + end +end - # def convert_to_data_type(entry) - # data_type_cases = { Array.to_s => "array #{new_resource.value}", - # Integer.to_s => "int #{new_resource.value}", - # TrueClass.to_s => 'bool TRUE', - # FalseClass.to_s => 'bool FALSE', - # Hash.to_s => "dict #{new_resource.value}", - # String.to_s => "string #{new_resource.value}", - # Float.to_s => "float #{new_resource.value}" } - # data_type_cases[entry] - # end +action :execute do + if new_resource.action == :set && !entry_exist? + format_plistbuddy_command(:add, new_resource.entry) + end + format_plistbuddy_command(new_resource.action, new_resource.entry, new_resource.value) end diff --git a/spec/unit/libraries/plistbuddy_spec.rb b/spec/unit/libraries/plistbuddy_spec.rb index 03c18cb3..75d7434a 100644 --- a/spec/unit/libraries/plistbuddy_spec.rb +++ b/spec/unit/libraries/plistbuddy_spec.rb @@ -4,8 +4,20 @@ describe MacOS::PlistBuddyHelpers, '#format_plistbuddy_command' do context 'When given some commands' do - it 'the command is formatted properly' do - expect(format_plistbuddy_command(:add, 'DidSeeSiriSetup', true)).to eq "/usr/libexec/Plistbuddy -c 'Add :DidSeeSiriSetup bool TRUE'" + it 'the add command is formatted properly' do + expect(format_plistbuddy_command(:add, 'FooEntry', true)).to eq "/usr/libexec/Plistbuddy -c 'Add :FooEntry bool TRUE'" + end + + it 'the delete command is formatted properly' do + expect(format_plistbuddy_command(:delete, 'BarEntry')).to eq "/usr/libexec/Plistbuddy -c 'Delete :BarEntry '" + end + + it 'the set command is formatted properly' do + expect(format_plistbuddy_command(:set, 'BazEntry', false)).to eq "/usr/libexec/Plistbuddy -c 'Set :BazEntry bool FALSE'" + end + + it 'the print command is formatted properly' do + expect(format_plistbuddy_command(:print, 'QuxEntry')).to eq "/usr/libexec/Plistbuddy -c 'Print :QuxEntry'" end end end @@ -16,11 +28,11 @@ expect(convert_to_string_from_data_type(true)).to eq 'bool TRUE' end - xit 'returns the required PlistBuddy array entry' do # TODO: Implement proper plist array syntax + xit 'returns the required PlistBuddy array entry' do # TODO: Implement proper plist array syntax (i.e. containers) expect(convert_to_string_from_data_type(%w(foo bar))).to eq 'array foo bar' end - xit 'returns the required PlistBuddy dictionary entry' do # TODO: Implement proper plist array syntax + xit 'returns the required PlistBuddy dictionary entry' do # TODO: Implement proper plist dict syntax (i.e. containers) expect(convert_to_string_from_data_type('baz' => 'qux')).to eq 'dict key value' end diff --git a/spec/unit/recipes/plistbuddy_spec.rb b/spec/unit/recipes/plistbuddy_spec.rb new file mode 100644 index 00000000..e677b686 --- /dev/null +++ b/spec/unit/recipes/plistbuddy_spec.rb @@ -0,0 +1,14 @@ +require 'spec_helper' + +describe 'macos::default' do + context 'When all attributes are default, on macOS 10.12' do + let(:chef_run) do + runner = ChefSpec::ServerRunner.new(platform: 'mac_os_x', version: '10.12') + runner.converge(described_recipe) + end + + xit 'converges successfully' do + expect { chef_run }.to_not raise_error + end + end +end diff --git a/test/cookbooks/plistbuddy/recipes/customize_dock.rb b/test/cookbooks/plistbuddy/recipes/customize_dock.rb new file mode 100644 index 00000000..b81f8452 --- /dev/null +++ b/test/cookbooks/plistbuddy/recipes/customize_dock.rb @@ -0,0 +1,5 @@ +plistbuddy :set do + plist_path '/Users/vagrant/Library/Preferences/com.apple.dock.plist' + entry 'showMissionControlGestureEnabled' + value false +end diff --git a/test/cookbooks/plistbuddy/recipes/default.rb b/test/cookbooks/plistbuddy/recipes/default.rb new file mode 100644 index 00000000..0d12f25f --- /dev/null +++ b/test/cookbooks/plistbuddy/recipes/default.rb @@ -0,0 +1,2 @@ +include_recipe 'plistbuddy::customize_dock' +include_recipe 'plistbuddy::show_hidden_files' diff --git a/test/cookbooks/plistbuddy/recipes/show_hidden_files.rb b/test/cookbooks/plistbuddy/recipes/show_hidden_files.rb new file mode 100644 index 00000000..4926da21 --- /dev/null +++ b/test/cookbooks/plistbuddy/recipes/show_hidden_files.rb @@ -0,0 +1,5 @@ +plistbuddy :set do + path '~/Library/Preferences/com.apple.finder.plist' + entry 'AppleShowAllFiles' + value true +end diff --git a/test/cookbooks/plistbuddy/test/smoke/default/default_test.rb b/test/cookbooks/plistbuddy/test/smoke/default/default_test.rb new file mode 100644 index 00000000..13647208 --- /dev/null +++ b/test/cookbooks/plistbuddy/test/smoke/default/default_test.rb @@ -0,0 +1,8 @@ +describe command("/usr/libexec/PlistBuddy -c 'Print :showMissionControlGestureEnabled' /Users/vagrant/Library/Preferences/com.apple.dock.plist") do + its('stdout') { should be 'true' } +end + +describe command("/usr/libexec/PlistBuddy -c 'Print :AppleShowAllFiles' /Users/vagrant/Library/Preferences/com.apple.finder.plist") do + its('stdout') { should be 'true' } +end + From f481503589166af2384f43de95200599b4f38ed9 Mon Sep 17 00:00:00 2001 From: Eric Hanko Date: Thu, 26 Oct 2017 15:39:33 -0700 Subject: [PATCH 10/24] remove extra newline --- test/cookbooks/plistbuddy/test/smoke/default/default_test.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/test/cookbooks/plistbuddy/test/smoke/default/default_test.rb b/test/cookbooks/plistbuddy/test/smoke/default/default_test.rb index 13647208..98a29aa9 100644 --- a/test/cookbooks/plistbuddy/test/smoke/default/default_test.rb +++ b/test/cookbooks/plistbuddy/test/smoke/default/default_test.rb @@ -5,4 +5,3 @@ describe command("/usr/libexec/PlistBuddy -c 'Print :AppleShowAllFiles' /Users/vagrant/Library/Preferences/com.apple.finder.plist") do its('stdout') { should be 'true' } end - From 9d43d463f3cd272578a0271dfb4fea049d7f9461 Mon Sep 17 00:00:00 2001 From: Eric Hanko Date: Thu, 26 Oct 2017 15:56:55 -0700 Subject: [PATCH 11/24] add metadata file --- test/cookbooks/plistbuddy/metadata.rb | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 test/cookbooks/plistbuddy/metadata.rb diff --git a/test/cookbooks/plistbuddy/metadata.rb b/test/cookbooks/plistbuddy/metadata.rb new file mode 100644 index 00000000..b25a1b96 --- /dev/null +++ b/test/cookbooks/plistbuddy/metadata.rb @@ -0,0 +1,4 @@ +name 'osx-basic' +version '1.0.0' + +depends 'macos' From 9311a37bccf59473fbc665f3b3e3116827c168ef Mon Sep 17 00:00:00 2001 From: Eric Hanko Date: Thu, 26 Oct 2017 16:04:02 -0700 Subject: [PATCH 12/24] edit metadata; add Berksfile --- test/cookbooks/Berksfile | 1 + test/cookbooks/plistbuddy/metadata.rb | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) create mode 100644 test/cookbooks/Berksfile diff --git a/test/cookbooks/Berksfile b/test/cookbooks/Berksfile new file mode 100644 index 00000000..cde259ef --- /dev/null +++ b/test/cookbooks/Berksfile @@ -0,0 +1 @@ +cookbook 'macos', path: '../../../../macos/' \ No newline at end of file diff --git a/test/cookbooks/plistbuddy/metadata.rb b/test/cookbooks/plistbuddy/metadata.rb index b25a1b96..21d229d5 100644 --- a/test/cookbooks/plistbuddy/metadata.rb +++ b/test/cookbooks/plistbuddy/metadata.rb @@ -1,4 +1,4 @@ -name 'osx-basic' +name 'plistbuddy' version '1.0.0' -depends 'macos' +depends 'macos', '0.8.4' From f16ecae33604ff43d86412d761995b72db7965df Mon Sep 17 00:00:00 2001 From: Eric Hanko Date: Thu, 26 Oct 2017 16:05:16 -0700 Subject: [PATCH 13/24] move Berksfile to correct location --- test/cookbooks/{ => plistbuddy}/Berksfile | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename test/cookbooks/{ => plistbuddy}/Berksfile (100%) diff --git a/test/cookbooks/Berksfile b/test/cookbooks/plistbuddy/Berksfile similarity index 100% rename from test/cookbooks/Berksfile rename to test/cookbooks/plistbuddy/Berksfile From 19645bc6d046344e916ea8eb96e7e7d2a4aa0897 Mon Sep 17 00:00:00 2001 From: Eric Hanko Date: Thu, 26 Oct 2017 16:11:07 -0700 Subject: [PATCH 14/24] add testing requirements for berks vendor --- metadata.rb | 2 +- test/cookbooks/plistbuddy/Berksfile | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/metadata.rb b/metadata.rb index a9ad5907..1990d7f4 100644 --- a/metadata.rb +++ b/metadata.rb @@ -5,7 +5,7 @@ description 'Resources for configuring and provisioning macOS' long_description 'Resources for configuring and provisioning macOS' chef_version '~> 13.0' if respond_to?(:chef_version) -version '0.8.2' +version '0.8.4' source_url 'https://github.com/Microsoft/macos-cookbook' issues_url 'https://github.com/Microsoft/macos-cookbook/issues' diff --git a/test/cookbooks/plistbuddy/Berksfile b/test/cookbooks/plistbuddy/Berksfile index cde259ef..0fac3b01 100644 --- a/test/cookbooks/plistbuddy/Berksfile +++ b/test/cookbooks/plistbuddy/Berksfile @@ -1 +1,5 @@ -cookbook 'macos', path: '../../../../macos/' \ No newline at end of file +source 'https://supermarket.chef.io' + +metadata + +cookbook 'macos', path: '../../../../macos/' From ec4b4897000502ef963995dfdfada1ce0eb569d8 Mon Sep 17 00:00:00 2001 From: Eric Hanko Date: Thu, 26 Oct 2017 16:13:11 -0700 Subject: [PATCH 15/24] move kitchen file --- .kitchen.yml => test/cookbooks/plistbuddy/.kitchen.yml | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .kitchen.yml => test/cookbooks/plistbuddy/.kitchen.yml (100%) diff --git a/.kitchen.yml b/test/cookbooks/plistbuddy/.kitchen.yml similarity index 100% rename from .kitchen.yml rename to test/cookbooks/plistbuddy/.kitchen.yml From 532899501cbff02260dc4c01f9f5244d6eaecc45 Mon Sep 17 00:00:00 2001 From: Eric Hanko Date: Thu, 26 Oct 2017 16:26:35 -0700 Subject: [PATCH 16/24] Converge passing in test cookbook - Opportunistic cleanup of xcode test - some temp debugging info in resource definition - Rename name_property to command so as to not override action --- resources/plistbuddy.rb | 14 +++++++------- .../cookbooks/plistbuddy/recipes/customize_dock.rb | 2 +- test/smoke/default/xcode_test.rb | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/resources/plistbuddy.rb b/resources/plistbuddy.rb index 405611a7..da7a2cde 100644 --- a/resources/plistbuddy.rb +++ b/resources/plistbuddy.rb @@ -1,21 +1,21 @@ resource_name :plistbuddy -property :action, Symbol, name_property: true +property :command, String, name_property: true -property :name, String +property :path, String property :entry, String, required: true -property :value, [Hash, String, Array, TrueClass, FalseClass, Int, Float] +property :value, [Hash, String, Array, TrueClass, FalseClass, Integer, Float] action_class do - def entry_exists? + def entry_exist? command = format_plistbuddy_command(:print, new_resource.entry, new_resource.value) shell_out(command).error? end end action :execute do - if new_resource.action == :set && !entry_exist? - format_plistbuddy_command(:add, new_resource.entry) + if new_resource.command.to_s == 'set' && !entry_exist? + p format_plistbuddy_command(:add, new_resource.entry) end - format_plistbuddy_command(new_resource.action, new_resource.entry, new_resource.value) + p format_plistbuddy_command(new_resource.command, new_resource.entry, new_resource.value) end diff --git a/test/cookbooks/plistbuddy/recipes/customize_dock.rb b/test/cookbooks/plistbuddy/recipes/customize_dock.rb index b81f8452..8b224241 100644 --- a/test/cookbooks/plistbuddy/recipes/customize_dock.rb +++ b/test/cookbooks/plistbuddy/recipes/customize_dock.rb @@ -1,5 +1,5 @@ plistbuddy :set do - plist_path '/Users/vagrant/Library/Preferences/com.apple.dock.plist' + path '/Users/vagrant/Library/Preferences/com.apple.dock.plist' entry 'showMissionControlGestureEnabled' value false end diff --git a/test/smoke/default/xcode_test.rb b/test/smoke/default/xcode_test.rb index b34ada6a..e3b7cd61 100644 --- a/test/smoke/default/xcode_test.rb +++ b/test/smoke/default/xcode_test.rb @@ -11,6 +11,6 @@ end describe command('/usr/local/bin/xcversion simulators') do - its('stdout') { should match /iOS 10\.3\.1 Simulator \(installed\)/ } + its('stdout') { should match(/iOS 10\.3\.1 Simulator \(installed\)/) } end end From 55bed5dcc06993c74269b0c537e6c83595f0fcb5 Mon Sep 17 00:00:00 2001 From: Eric Hanko Date: Thu, 26 Oct 2017 18:45:51 -0700 Subject: [PATCH 17/24] Itempotence - Removed 'bool' prefix from boolean types, as that was somehow causing issues - Changed name_property to the path and removed that property - Use actual actions in the resource for the PlistBuddy commands --- libraries/plistbuddy.rb | 6 ++-- resources/plistbuddy.rb | 36 ++++++++++++++----- spec/unit/libraries/plistbuddy_spec.rb | 8 ++--- .../plistbuddy/recipes/customize_dock.rb | 3 +- .../plistbuddy/recipes/show_hidden_files.rb | 3 +- 5 files changed, 36 insertions(+), 20 deletions(-) diff --git a/libraries/plistbuddy.rb b/libraries/plistbuddy.rb index 041e03d9..cd1a7b1d 100644 --- a/libraries/plistbuddy.rb +++ b/libraries/plistbuddy.rb @@ -5,8 +5,8 @@ module PlistBuddyHelpers def convert_to_string_from_data_type(plist_entry) data_type_cases = { Array => "array #{plist_entry}", Integer => "int #{plist_entry}", - TrueClass => 'bool TRUE', - FalseClass => 'bool FALSE', + TrueClass => 'true', + FalseClass => 'false', Hash => "dict #{plist_entry}", String => "string #{plist_entry}", Float => "float #{plist_entry}" } @@ -15,7 +15,7 @@ def convert_to_string_from_data_type(plist_entry) def format_plistbuddy_command(action_property, plist_entry, plist_value = nil) value_with_data_type = convert_to_string_from_data_type plist_value - "/usr/libexec/Plistbuddy -c \'#{action_property.to_s.capitalize} :#{plist_entry} #{value_with_data_type}\'" + "/usr/libexec/Plistbuddy -c \'#{action_property.to_s.capitalize} :#{plist_entry} #{value_with_data_type}\'" # Add a space here for the plist path end end end diff --git a/resources/plistbuddy.rb b/resources/plistbuddy.rb index da7a2cde..b754e088 100644 --- a/resources/plistbuddy.rb +++ b/resources/plistbuddy.rb @@ -1,21 +1,39 @@ resource_name :plistbuddy -property :command, String, name_property: true - -property :path, String +property :path, String, name_property: true property :entry, String, required: true property :value, [Hash, String, Array, TrueClass, FalseClass, Integer, Float] +default_action :set + action_class do - def entry_exist? + def entry_missing? command = format_plistbuddy_command(:print, new_resource.entry, new_resource.value) - shell_out(command).error? + full_command = command + ' ' + new_resource.path + shell_out(full_command).error? + end + + def current_entry_value + command = format_plistbuddy_command(:print, new_resource.entry) + full_command = command + ' ' + new_resource.path + shell_out(full_command).stdout.chomp end end -action :execute do - if new_resource.command.to_s == 'set' && !entry_exist? - p format_plistbuddy_command(:add, new_resource.entry) +action :set do + puts "\n\n" + p "current_entry_value: #{current_entry_value}" + p "value: #{new_resource.value}" + puts "\n\n" + + if entry_missing? + execute format_plistbuddy_command(:add, new_resource.entry, new_resource.value) + ' ' + new_resource.path + execute format_plistbuddy_command(:set, new_resource.entry, new_resource.value) + ' ' + new_resource.path + elsif current_entry_value != new_resource.value.to_s + execute format_plistbuddy_command(:set, new_resource.entry, new_resource.value) + ' ' + new_resource.path end - p format_plistbuddy_command(new_resource.command, new_resource.entry, new_resource.value) +end + +action :delete do + execute format_plistbuddy_command(:delete, new_resource.entry, new_resource.value) + ' ' + new_resource.path end diff --git a/spec/unit/libraries/plistbuddy_spec.rb b/spec/unit/libraries/plistbuddy_spec.rb index 75d7434a..6fc6de47 100644 --- a/spec/unit/libraries/plistbuddy_spec.rb +++ b/spec/unit/libraries/plistbuddy_spec.rb @@ -5,7 +5,7 @@ describe MacOS::PlistBuddyHelpers, '#format_plistbuddy_command' do context 'When given some commands' do it 'the add command is formatted properly' do - expect(format_plistbuddy_command(:add, 'FooEntry', true)).to eq "/usr/libexec/Plistbuddy -c 'Add :FooEntry bool TRUE'" + expect(format_plistbuddy_command(:add, 'FooEntry', true)).to eq "/usr/libexec/Plistbuddy -c 'Add :FooEntry true'" end it 'the delete command is formatted properly' do @@ -13,11 +13,11 @@ end it 'the set command is formatted properly' do - expect(format_plistbuddy_command(:set, 'BazEntry', false)).to eq "/usr/libexec/Plistbuddy -c 'Set :BazEntry bool FALSE'" + expect(format_plistbuddy_command(:set, 'BazEntry', false)).to eq "/usr/libexec/Plistbuddy -c 'Set :BazEntry true'" end it 'the print command is formatted properly' do - expect(format_plistbuddy_command(:print, 'QuxEntry')).to eq "/usr/libexec/Plistbuddy -c 'Print :QuxEntry'" + expect(format_plistbuddy_command(:print, 'QuxEntry')).to eq "/usr/libexec/Plistbuddy -c 'Print :QuxEntry '" end end end @@ -25,7 +25,7 @@ describe MacOS::PlistBuddyHelpers, '#convert_to_string_from_data_type' do context 'When given a certain data type' do it 'returns the required PlistBuddy boolean entry' do - expect(convert_to_string_from_data_type(true)).to eq 'bool TRUE' + expect(convert_to_string_from_data_type(true)).to eq 'true' end xit 'returns the required PlistBuddy array entry' do # TODO: Implement proper plist array syntax (i.e. containers) diff --git a/test/cookbooks/plistbuddy/recipes/customize_dock.rb b/test/cookbooks/plistbuddy/recipes/customize_dock.rb index 8b224241..73f05800 100644 --- a/test/cookbooks/plistbuddy/recipes/customize_dock.rb +++ b/test/cookbooks/plistbuddy/recipes/customize_dock.rb @@ -1,5 +1,4 @@ -plistbuddy :set do - path '/Users/vagrant/Library/Preferences/com.apple.dock.plist' +plistbuddy '/Users/vagrant/Library/Preferences/com.apple.dock.plist' do entry 'showMissionControlGestureEnabled' value false end diff --git a/test/cookbooks/plistbuddy/recipes/show_hidden_files.rb b/test/cookbooks/plistbuddy/recipes/show_hidden_files.rb index 4926da21..5dcd3662 100644 --- a/test/cookbooks/plistbuddy/recipes/show_hidden_files.rb +++ b/test/cookbooks/plistbuddy/recipes/show_hidden_files.rb @@ -1,5 +1,4 @@ -plistbuddy :set do - path '~/Library/Preferences/com.apple.finder.plist' +plistbuddy '/Users/vagrant/Library/Preferences/com.apple.finder.plist' do entry 'AppleShowAllFiles' value true end From a493029a1c84826834b604ff35ca2feedb849788 Mon Sep 17 00:00:00 2001 From: Eric Hanko Date: Thu, 26 Oct 2017 18:47:49 -0700 Subject: [PATCH 18/24] Fix false positive? - Not sure how the previous test was passing previously, but I adjusted the boolean value for the BazEntry test so that the signature input matches the output --- spec/unit/libraries/plistbuddy_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/unit/libraries/plistbuddy_spec.rb b/spec/unit/libraries/plistbuddy_spec.rb index 6fc6de47..30bc59cf 100644 --- a/spec/unit/libraries/plistbuddy_spec.rb +++ b/spec/unit/libraries/plistbuddy_spec.rb @@ -13,7 +13,7 @@ end it 'the set command is formatted properly' do - expect(format_plistbuddy_command(:set, 'BazEntry', false)).to eq "/usr/libexec/Plistbuddy -c 'Set :BazEntry true'" + expect(format_plistbuddy_command(:set, 'BazEntry', false)).to eq "/usr/libexec/Plistbuddy -c 'Set :BazEntry false'" end it 'the print command is formatted properly' do From 92a5e41c92d3449b6c9d20c11d86fad7b5e91203 Mon Sep 17 00:00:00 2001 From: Eric Hanko Date: Thu, 26 Oct 2017 19:36:59 -0700 Subject: [PATCH 19/24] Add logic for adding a value - The data type should only be used when adding a value to a plist. Otherwise, we don't need to use the data type --- libraries/plistbuddy.rb | 30 ++++++++++++++++---------- spec/unit/libraries/plistbuddy_spec.rb | 12 +++++++---- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/libraries/plistbuddy.rb b/libraries/plistbuddy.rb index cd1a7b1d..88f40ff6 100644 --- a/libraries/plistbuddy.rb +++ b/libraries/plistbuddy.rb @@ -2,20 +2,28 @@ module MacOS module PlistBuddyHelpers - def convert_to_string_from_data_type(plist_entry) - data_type_cases = { Array => "array #{plist_entry}", - Integer => "int #{plist_entry}", - TrueClass => 'true', - FalseClass => 'false', - Hash => "dict #{plist_entry}", - String => "string #{plist_entry}", - Float => "float #{plist_entry}" } - data_type_cases[plist_entry.class] + def convert_to_string_from_data_type(value) + data_type_cases = { Array => "array #{value}", + Integer => "int #{value}", + TrueClass => "bool #{value}", + FalseClass => "bool #{value}", + Hash => "dict #{value}", + String => "string #{value}", + Float => "float #{value}" } + data_type_cases[value.class] end def format_plistbuddy_command(action_property, plist_entry, plist_value = nil) - value_with_data_type = convert_to_string_from_data_type plist_value - "/usr/libexec/Plistbuddy -c \'#{action_property.to_s.capitalize} :#{plist_entry} #{value_with_data_type}\'" # Add a space here for the plist path + plist_value = args_formatter(action_property, plist_value) + "/usr/libexec/Plistbuddy -c \'#{action_property.to_s.capitalize} :#{plist_entry} #{plist_value}\'" # Add a space here for the plist path + end + + def args_formatter(action_property, plist_value) + if action_property == :add + convert_to_string_from_data_type plist_value + else + plist_value + end end end end diff --git a/spec/unit/libraries/plistbuddy_spec.rb b/spec/unit/libraries/plistbuddy_spec.rb index 30bc59cf..ac11c30c 100644 --- a/spec/unit/libraries/plistbuddy_spec.rb +++ b/spec/unit/libraries/plistbuddy_spec.rb @@ -3,9 +3,13 @@ include MacOS::PlistBuddyHelpers describe MacOS::PlistBuddyHelpers, '#format_plistbuddy_command' do - context 'When given some commands' do - it 'the add command is formatted properly' do - expect(format_plistbuddy_command(:add, 'FooEntry', true)).to eq "/usr/libexec/Plistbuddy -c 'Add :FooEntry true'" + context 'Adding a value to a plist' do + it 'the bool arguments contain the data type' do + expect(format_plistbuddy_command(:add, 'FooEntry', true)).to eq "/usr/libexec/Plistbuddy -c 'Add :FooEntry bool true'" + end + + it 'the int arguments contain the data type' do + expect(format_plistbuddy_command(:add, 'QuuxEntry', 50)).to eq "/usr/libexec/Plistbuddy -c 'Add :QuuxEntry int 50'" end it 'the delete command is formatted properly' do @@ -25,7 +29,7 @@ describe MacOS::PlistBuddyHelpers, '#convert_to_string_from_data_type' do context 'When given a certain data type' do it 'returns the required PlistBuddy boolean entry' do - expect(convert_to_string_from_data_type(true)).to eq 'true' + expect(convert_to_string_from_data_type(true)).to eq 'bool true' end xit 'returns the required PlistBuddy array entry' do # TODO: Implement proper plist array syntax (i.e. containers) From a734d4af369b3c25770358fa0725d1319da4e5bb Mon Sep 17 00:00:00 2001 From: Eric Hanko Date: Thu, 26 Oct 2017 20:01:44 -0700 Subject: [PATCH 20/24] remove debugging; fix smoke tests --- libraries/plistbuddy.rb | 2 +- resources/plistbuddy.rb | 5 ----- test/cookbooks/plistbuddy/test/smoke/default/default_test.rb | 4 ++-- 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/libraries/plistbuddy.rb b/libraries/plistbuddy.rb index 88f40ff6..e871c423 100644 --- a/libraries/plistbuddy.rb +++ b/libraries/plistbuddy.rb @@ -15,7 +15,7 @@ def convert_to_string_from_data_type(value) def format_plistbuddy_command(action_property, plist_entry, plist_value = nil) plist_value = args_formatter(action_property, plist_value) - "/usr/libexec/Plistbuddy -c \'#{action_property.to_s.capitalize} :#{plist_entry} #{plist_value}\'" # Add a space here for the plist path + "/usr/libexec/Plistbuddy -c \'#{action_property.to_s.capitalize} :#{plist_entry} #{plist_value}\'" end def args_formatter(action_property, plist_value) diff --git a/resources/plistbuddy.rb b/resources/plistbuddy.rb index b754e088..fa052905 100644 --- a/resources/plistbuddy.rb +++ b/resources/plistbuddy.rb @@ -21,11 +21,6 @@ def current_entry_value end action :set do - puts "\n\n" - p "current_entry_value: #{current_entry_value}" - p "value: #{new_resource.value}" - puts "\n\n" - if entry_missing? execute format_plistbuddy_command(:add, new_resource.entry, new_resource.value) + ' ' + new_resource.path execute format_plistbuddy_command(:set, new_resource.entry, new_resource.value) + ' ' + new_resource.path diff --git a/test/cookbooks/plistbuddy/test/smoke/default/default_test.rb b/test/cookbooks/plistbuddy/test/smoke/default/default_test.rb index 98a29aa9..c86f1025 100644 --- a/test/cookbooks/plistbuddy/test/smoke/default/default_test.rb +++ b/test/cookbooks/plistbuddy/test/smoke/default/default_test.rb @@ -1,7 +1,7 @@ describe command("/usr/libexec/PlistBuddy -c 'Print :showMissionControlGestureEnabled' /Users/vagrant/Library/Preferences/com.apple.dock.plist") do - its('stdout') { should be 'true' } + its('stdout') { should match 'false' } end describe command("/usr/libexec/PlistBuddy -c 'Print :AppleShowAllFiles' /Users/vagrant/Library/Preferences/com.apple.finder.plist") do - its('stdout') { should be 'true' } + its('stdout') { should match 'true' } end From f907c498bb491335c56b33fbbfbbf676d6ea5ae4 Mon Sep 17 00:00:00 2001 From: Eric Hanko Date: Fri, 27 Oct 2017 00:10:24 -0700 Subject: [PATCH 21/24] remove .kitchen.yml --- test/cookbooks/plistbuddy/.gitignore | 103 +++++++++++++++++++++++++ test/cookbooks/plistbuddy/.kitchen.yml | 32 -------- 2 files changed, 103 insertions(+), 32 deletions(-) create mode 100644 test/cookbooks/plistbuddy/.gitignore delete mode 100644 test/cookbooks/plistbuddy/.kitchen.yml diff --git a/test/cookbooks/plistbuddy/.gitignore b/test/cookbooks/plistbuddy/.gitignore new file mode 100644 index 00000000..17de6c82 --- /dev/null +++ b/test/cookbooks/plistbuddy/.gitignore @@ -0,0 +1,103 @@ +*~ +*# +.#* +\#*# +.*.sw[a-z] +*.un~ + +# Bundler +Gemfile +Gemfile.lock +bin/* +.bundle/* + +# test kitchen +.kitchen/ +.kitchen.local.yml + +# Chef +Berksfile.lock +.zero-knife.rb +Policyfile.lock.json +.autotest +coverage +.DS_Store +pkg/* +tags +*/tags +.chef +results + +# You should check in your Gemfile.lock in applications, and not in gems +external_tests/*.lock +/Gemfile.local + +# ignore some common Bundler 'binstubs' directory names +# http://gembundler.com/man/bundle-exec.1.html +b/ +binstubs/ +.bundle +# RVM and RBENV ruby version files +.rbenv-version +.rvmrc +.ruby-version +.ruby-gemset + +# IDE files +.project + +# Documentation +_site/* +.yardoc/ +doc/ + +# Kitchen Tests Local Mode Data +kitchen-tests/nodes/* + +# Temporary files present during spec runs +spec/data/test-dir +spec/data/nodes +/config/ + +# acceptance binstubs +acceptance/bin/* + +vendor/ +acceptance/vendor +kitchen-tests/vendor + +# Visual Studio Code files +.vscode +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +.idea + +# CMake +cmake-build-debug/ + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Testing +*.box +berks-cookbooks +Vagrantfile +.rubocop.yml +.vagrant +data_bags diff --git a/test/cookbooks/plistbuddy/.kitchen.yml b/test/cookbooks/plistbuddy/.kitchen.yml deleted file mode 100644 index a7be1b91..00000000 --- a/test/cookbooks/plistbuddy/.kitchen.yml +++ /dev/null @@ -1,32 +0,0 @@ ---- -driver: - name: vagrant - provider: parallels - customize: - memory: 8192 - cpus: 2 - -provisioner: - name: chef_zero - -verifier: - name: inspec - -transport: - name: ssh - elevated: true - -platforms: - - name: apex/macos-10.12.6 - driver: - box: apex/macos-10.12.6 - box_url: http://imagr.corp.microsoft.com:8099/apex/macos-10.12.6 - box_check_update: true - -suites: - - name: plistbuddy - run_list: - - recipe[plistbuddy::default] - verifier: - inspec_tests: - - test/smoke/default From 43ea52b06d7942e0cf2ee19dde1d41ed0aaa0034 Mon Sep 17 00:00:00 2001 From: Eric Hanko Date: Fri, 27 Oct 2017 18:40:23 -0700 Subject: [PATCH 22/24] bump version to 0.8.5 --- metadata.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metadata.rb b/metadata.rb index 1990d7f4..733d1c68 100644 --- a/metadata.rb +++ b/metadata.rb @@ -5,7 +5,7 @@ description 'Resources for configuring and provisioning macOS' long_description 'Resources for configuring and provisioning macOS' chef_version '~> 13.0' if respond_to?(:chef_version) -version '0.8.4' +version '0.8.5' source_url 'https://github.com/Microsoft/macos-cookbook' issues_url 'https://github.com/Microsoft/macos-cookbook/issues' From 07fcc08ca719c363abaf666cc5420c5bd2cc317e Mon Sep 17 00:00:00 2001 From: Eric Hanko Date: Sun, 29 Oct 2017 04:07:08 -0700 Subject: [PATCH 23/24] unskip ChefSpec tests - they're passing --- spec/unit/recipes/configurator.rb | 2 +- spec/unit/recipes/default_spec.rb | 2 +- spec/unit/recipes/keep_awake_spec.rb | 2 +- spec/unit/recipes/plistbuddy_spec.rb | 2 +- spec/unit/recipes/xcode_spec.rb | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/spec/unit/recipes/configurator.rb b/spec/unit/recipes/configurator.rb index 21c21940..96969ac4 100644 --- a/spec/unit/recipes/configurator.rb +++ b/spec/unit/recipes/configurator.rb @@ -13,7 +13,7 @@ runner.converge(described_recipe) end - xit 'converges successfully' do + it 'converges successfully' do expect { chef_run }.to_not raise_error end end diff --git a/spec/unit/recipes/default_spec.rb b/spec/unit/recipes/default_spec.rb index e677b686..df3ad1c3 100644 --- a/spec/unit/recipes/default_spec.rb +++ b/spec/unit/recipes/default_spec.rb @@ -7,7 +7,7 @@ runner.converge(described_recipe) end - xit 'converges successfully' do + it 'converges successfully' do expect { chef_run }.to_not raise_error end end diff --git a/spec/unit/recipes/keep_awake_spec.rb b/spec/unit/recipes/keep_awake_spec.rb index 5f44447e..4e731e2e 100644 --- a/spec/unit/recipes/keep_awake_spec.rb +++ b/spec/unit/recipes/keep_awake_spec.rb @@ -7,7 +7,7 @@ runner.converge(described_recipe) end - xit 'converges successfully' do + it 'converges successfully' do expect { chef_run }.to_not raise_error end end diff --git a/spec/unit/recipes/plistbuddy_spec.rb b/spec/unit/recipes/plistbuddy_spec.rb index e677b686..df3ad1c3 100644 --- a/spec/unit/recipes/plistbuddy_spec.rb +++ b/spec/unit/recipes/plistbuddy_spec.rb @@ -7,7 +7,7 @@ runner.converge(described_recipe) end - xit 'converges successfully' do + it 'converges successfully' do expect { chef_run }.to_not raise_error end end diff --git a/spec/unit/recipes/xcode_spec.rb b/spec/unit/recipes/xcode_spec.rb index b8d8a17c..7f53222f 100644 --- a/spec/unit/recipes/xcode_spec.rb +++ b/spec/unit/recipes/xcode_spec.rb @@ -13,7 +13,7 @@ runner.converge(described_recipe) end - xit 'converges successfully' do + it 'converges successfully' do expect { chef_run }.to_not raise_error end end From 7297d6fee595a0809d078fe714cb2d6d9d20025f Mon Sep 17 00:00:00 2001 From: Eric Hanko Date: Sun, 29 Oct 2017 04:14:09 -0700 Subject: [PATCH 24/24] mark args_formatter as private method --- libraries/plistbuddy.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/plistbuddy.rb b/libraries/plistbuddy.rb index e871c423..e4fe683d 100644 --- a/libraries/plistbuddy.rb +++ b/libraries/plistbuddy.rb @@ -1,5 +1,3 @@ -include Chef::Mixin::ShellOut - module MacOS module PlistBuddyHelpers def convert_to_string_from_data_type(value) @@ -18,6 +16,8 @@ def format_plistbuddy_command(action_property, plist_entry, plist_value = nil) "/usr/libexec/Plistbuddy -c \'#{action_property.to_s.capitalize} :#{plist_entry} #{plist_value}\'" end + private + def args_formatter(action_property, plist_value) if action_property == :add convert_to_string_from_data_type plist_value