summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith84@gmail.com>2020-07-20 12:56:15 -0700
committerTim Smith <tsmith84@gmail.com>2020-07-20 12:56:15 -0700
commitcdd4db7607792640a709c38aebf16d750618b4df (patch)
tree0b298a557b88b8e18b4fa0ef27e8a5a3d3303133
parentc220880c0b81ee13bb02aa0a14ee09ed59076022 (diff)
downloadchef-cdd4db7607792640a709c38aebf16d750618b4df.tar.gz
Avoid failures if the host was named "current" and add some more tests
Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r--lib/chef/resource/macos_userdefaults.rb13
-rw-r--r--spec/unit/resource/macos_user_defaults_spec.rb34
2 files changed, 40 insertions, 7 deletions
diff --git a/lib/chef/resource/macos_userdefaults.rb b/lib/chef/resource/macos_userdefaults.rb
index e43a259135..ea9f5901ba 100644
--- a/lib/chef/resource/macos_userdefaults.rb
+++ b/lib/chef/resource/macos_userdefaults.rb
@@ -78,8 +78,8 @@ class Chef
required: true,
desired_state: false
- property :host, String,
- description: "Set either 'current' or a hostname to set the user default at the host level.",
+ property :host, [String, Symbol],
+ description: "Set either :current or a hostname to set the user default at the host level.",
desired_state: false,
introduced: "16.3"
@@ -169,12 +169,13 @@ class Chef
action_class do
def defaults_modify_cmd
- cmd = ["defaults"]
+ puts "The current action is #{action}"
+ cmd = ["/usr/bin/defaults"]
- if new_resource.host == "current"
- state_cmd.concat(["-currentHost"])
+ if new_resource.host == :current
+ cmd.concat(["-currentHost"])
elsif new_resource.host # they specified a non-nil value, which is a hostname
- state_cmd.concat(["-host", new_resource.host])
+ cmd.concat(["-host", new_resource.host])
end
cmd.concat([action.to_s, new_resource.domain, new_resource.key])
diff --git a/spec/unit/resource/macos_user_defaults_spec.rb b/spec/unit/resource/macos_user_defaults_spec.rb
index e839401d6f..6326313642 100644
--- a/spec/unit/resource/macos_user_defaults_spec.rb
+++ b/spec/unit/resource/macos_user_defaults_spec.rb
@@ -20,7 +20,7 @@ require "spec_helper"
describe Chef::Resource::MacosUserDefaults do
let(:resource) { Chef::Resource::MacosUserDefaults.new("foo") }
- let(:provider) { resource.provider_for_action(:create) }
+ let(:provider) { resource.provider_for_action(:write) }
it "has a resource name of :macos_userdefaults" do
expect(resource.resource_name).to eql(:macos_userdefaults)
@@ -66,4 +66,36 @@ describe Chef::Resource::MacosUserDefaults do
expect(provider.defaults_export_cmd(resource)).to eql(["/usr/bin/defaults", "-host", "tim-laptop", "export", "NSGlobalDomain", "-"])
end
end
+
+ describe "#defaults_modify_cmd" do
+ # avoid needing to set these required values over and over. We'll overwrite them where necessary
+ before do
+ resource.key = "foo"
+ resource.value = "bar"
+ 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"])
+ 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"])
+ 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"])
+ 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"])
+ 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"])
+ end
+ end
end