diff options
author | Tim Smith <tsmith84@gmail.com> | 2020-07-20 16:24:08 -0700 |
---|---|---|
committer | Tim Smith <tsmith84@gmail.com> | 2020-07-20 16:24:08 -0700 |
commit | 0e7c7ed5afa78dfb428780f5c4e5fdbf9c40ea90 (patch) | |
tree | 80989396de76641a66b38c429c133f78bd6f1175 | |
parent | 8a646ae9e701af2adfe98f5a950cb01deb7023d3 (diff) | |
download | chef-0e7c7ed5afa78dfb428780f5c4e5fdbf9c40ea90.tar.gz |
Add specs and squash bugs that showed up
- Add back method to convert boolean-ish values to TRUE/FALSE
- Be explicit about string values as well as all the other types so we always pass a type
- Fix the type flag in the write command
Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r-- | lib/chef/resource/macos_userdefaults.rb | 40 | ||||
-rw-r--r-- | spec/unit/resource/macos_user_defaults_spec.rb | 39 |
2 files changed, 68 insertions, 11 deletions
diff --git a/lib/chef/resource/macos_userdefaults.rb b/lib/chef/resource/macos_userdefaults.rb index 0bf2421e94..1bf05194b8 100644 --- a/lib/chef/resource/macos_userdefaults.rb +++ b/lib/chef/resource/macos_userdefaults.rb @@ -89,7 +89,7 @@ class Chef property :type, String, description: "The value type of the preference key.", - equal_to: ["bool", "string", 'int', 'float', 'array', 'dict'] + equal_to: %w{bool string int float array dict}, desired_state: false property :user, String, @@ -169,8 +169,12 @@ class Chef end action_class do + # + # The command used to write or delete delete values from domains + # + # @return [Array] Array representation of defaults command to run + # def defaults_modify_cmd - puts "The current action is #{action}" cmd = ["/usr/bin/defaults"] if new_resource.host == :current @@ -194,12 +198,38 @@ class Chef type = new_resource.type || value_type(new_resource.value) # when dict this creates an array of values ["Key1", "Value1", "Key2", "Value2" ...] - cmd_values = [ type == "dict" ? new_resource.value.flatten : new_resource.value ] - cmd_values.prepend("-#{type}") if type + cmd_values = ["-#{type}"] + + case type + when "dict" + cmd_values.concat(new_resource.value.flatten) + when "array" + cmd_values.concat(new_resource.value) + when "bool" + cmd_values.concat(bool_to_defaults_bool(new_resource.value)) + else + cmd_values.concat([new_resource.value]) + end + cmd_values end # + # defaults booleans on the CLI must be 'TRUE' or 'FALSE' so convert various inputs to that + # + # @param [String, Integer, Boolean] input <description> + # + # @return [String] TRUE or FALSE + # + def bool_to_defaults_bool(input) + return ["TRUE"] if [true, "TRUE", "1", "true", "YES", "yes"].include?(input) + return ["FALSE"] if [false, "FALSE", "0", "false", "NO", "no"].include?(input) + + # make sure it's very clear bad input was given + raise ArgumentError, "#{input} cannot be converted to a boolean value for use with Apple's defaults command. Acceptable values are: 'TRUE', 'YES', 'true, 'yes', '0', true, 'FALSE', 'false', 'NO', 'no', 1, or false." + end + + # # convert ruby type to defaults type # # @param [Integer, Float, String, TrueClass, FalseClass, Hash, Array] value The value being set @@ -218,6 +248,8 @@ class Chef "dict" when Array "array" + when String + "string" end end end diff --git a/spec/unit/resource/macos_user_defaults_spec.rb b/spec/unit/resource/macos_user_defaults_spec.rb index 6326313642..1a222d0005 100644 --- a/spec/unit/resource/macos_user_defaults_spec.rb +++ b/spec/unit/resource/macos_user_defaults_spec.rb @@ -75,27 +75,52 @@ describe Chef::Resource::MacosUserDefaults do end it "writes to NSGlobalDomain if domain isn't specified" do - expect(provider.defaults_modify_cmd).to eql(["/usr/bin/defaults", "write", "NSGlobalDomain", "foo", "bar"]) + expect(provider.defaults_modify_cmd).to eql(["/usr/bin/defaults", "write", "NSGlobalDomain", "foo", "-string", "bar"]) end it "uses the domain property if set" do resource.domain = "MyCustomDomain" - expect(provider.defaults_modify_cmd).to eql(["/usr/bin/defaults", "write", "MyCustomDomain", "foo", "bar"]) + expect(provider.defaults_modify_cmd).to eql(["/usr/bin/defaults", "write", "MyCustomDomain", "foo", "-string", "bar"]) end it "sets host specific values using host property" do resource.host = "tims_laptop" - expect(provider.defaults_modify_cmd).to eql(["/usr/bin/defaults", "-host", "tims_laptop", "write", "NSGlobalDomain", "foo", "bar"]) + expect(provider.defaults_modify_cmd).to eql(["/usr/bin/defaults", "-host", "tims_laptop", "write", "NSGlobalDomain", "foo", "-string", "bar"]) end it "if host is set to :current it passes CurrentHost" do resource.host = :current - expect(provider.defaults_modify_cmd).to eql(["/usr/bin/defaults", "-currentHost", "write", "NSGlobalDomain", "foo", "bar"]) + expect(provider.defaults_modify_cmd).to eql(["/usr/bin/defaults", "-currentHost", "write", "NSGlobalDomain", "foo", "-string", "bar"]) end - it "if host is set to :current it passes CurrentHost" do - resource.host = :current - expect(provider.defaults_modify_cmd).to eql(["/usr/bin/defaults", "-currentHost", "write", "NSGlobalDomain", "foo", "bar"]) + it "raises ArgumentError if bool is specified, but the value can't be made into a bool" do + resource.type "bool" + expect { provider.defaults_modify_cmd }.to raise_error(ArgumentError) + end + + it "autodetects array type and passes individual values" do + resource.value = %w{one two three} + expect(provider.defaults_modify_cmd).to eql(["/usr/bin/defaults", "write", "NSGlobalDomain", "foo", "-array", "one", "two", "three"]) + end + + it "autodetects string type and passes a single value" do + resource.value = "one" + expect(provider.defaults_modify_cmd).to eql(["/usr/bin/defaults", "write", "NSGlobalDomain", "foo", "-string", "one"]) + end + + it "autodetects integer type and passes a single value" do + resource.value = 1 + expect(provider.defaults_modify_cmd).to eql(["/usr/bin/defaults", "write", "NSGlobalDomain", "foo", "-int", 1]) + end + + it "autodetects boolean type from TrueClass value and passes a 'TRUE' string" do + resource.value = true + expect(provider.defaults_modify_cmd).to eql(["/usr/bin/defaults", "write", "NSGlobalDomain", "foo", "-bool", "TRUE"]) + end + + it "autodetects boolean type from FalseClass value and passes a 'FALSE' string" do + resource.value = false + expect(provider.defaults_modify_cmd).to eql(["/usr/bin/defaults", "write", "NSGlobalDomain", "foo", "-bool", "FALSE"]) end end end |