From a16cae84e555b814a4c690d518151ee63c289a0f Mon Sep 17 00:00:00 2001 From: Rishi Kumar Chawda Date: Fri, 26 Jun 2026 14:47:18 +0530 Subject: [PATCH] fix synchronize call after set value Signed-off-by: Rishi Kumar Chawda --- lib/corefoundation/preferences.rb | 5 ++++- spec/preferences_spec.rb | 29 +++++++++++------------------ 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/lib/corefoundation/preferences.rb b/lib/corefoundation/preferences.rb index 5f8f307..964e3b7 100644 --- a/lib/corefoundation/preferences.rb +++ b/lib/corefoundation/preferences.rb @@ -18,6 +18,7 @@ module CF attach_function 'CFPreferencesSetValue', [:key, :value, :application_id, :username, :hostname], :void attach_function 'CFPreferencesAppSynchronize', [:application_id], :bool + attach_function 'CFPreferencesSynchronize', [:application_id, :username, :hostname], :void # Interface to the preference utilities from Corefoundation.framework. # Documentation at https://developer.apple.com/documentation/corefoundation/preferences_utilities @@ -93,14 +94,16 @@ def set(key, value, application_id, username = nil, hostname = nil) arg_to_cf(username), arg_to_cf(hostname) ) + CF.CFPreferencesSynchronize(application_id.to_cf, arg_to_cf(username), arg_to_cf(hostname)) + true else CF.CFPreferencesSetAppValue( key.to_cf, arg_to_cf(value), application_id.to_cf ) + CF.CFPreferencesAppSynchronize(application_id.to_cf) end - CF.CFPreferencesAppSynchronize(application_id.to_cf) end # Calls the {#self.set} method and raise a `PreferenceSyncFailed` error if `false` is returned. diff --git a/spec/preferences_spec.rb b/spec/preferences_spec.rb index 4a9870b..27603e5 100644 --- a/spec/preferences_spec.rb +++ b/spec/preferences_spec.rb @@ -28,12 +28,19 @@ describe "self.set" do context "when called with valid domain/default pair" do context "(with username and hostname)" do - it "executes CF.CFPreferencesSetValue and returns true" do + it "executes CF.CFPreferencesSetValue and CFPreferencesSynchronize and returns true" do expect(CF).to receive(:CFPreferencesSetValue).with(@cf_key, @cf_value, @cf_domain, @cf_user, @cf_hostname) - expect(CF).to receive(:CFPreferencesAppSynchronize).with(@cf_domain).and_return(pref_sync_passed.result) + expect(CF).to receive(:CFPreferencesSynchronize).with(@cf_domain, @cf_user, @cf_hostname) expect(CF::Preferences.set(@key, @value, @domain, @user, @hostname)).to eql(true) end end + context "(with FFI::Pointer constants for username and hostname)" do + it "passes pointer values directly without conversion" do + expect(CF).to receive(:CFPreferencesSetValue).with(@cf_key, @cf_value, @cf_domain, CF::Preferences::CURRENT_USER, CF::Preferences::CURRENT_HOST) + expect(CF).to receive(:CFPreferencesSynchronize).with(@cf_domain, CF::Preferences::CURRENT_USER, CF::Preferences::CURRENT_HOST) + expect(CF::Preferences.set(@key, @value, @domain, CF::Preferences::CURRENT_USER, CF::Preferences::CURRENT_HOST)).to eql(true) + end + end context "(without username and hostname)" do it "executes CF.CFPreferencesSetAppValue and returns true" do expect(CF).to receive(:CFPreferencesSetAppValue).with(@cf_key, @cf_value, @cf_domain) @@ -43,13 +50,6 @@ end end context "when called with invalid domain/default pair" do - context "(with username and hostname)" do - it "executes CF.CFPreferencesSetValue and returns false" do - expect(CF).to receive(:CFPreferencesSetValue).with(@cf_key, @cf_value, @cf_domain, @cf_user, @cf_hostname) - expect(CF).to receive(:CFPreferencesAppSynchronize).with(@cf_domain).and_return(pref_sync_failed.result) - expect(CF::Preferences.set(@key, @value, @domain, @user, @hostname)).to eql(false) - end - end context "(without username and hostname)" do it "executes CF.CFPreferencesSetAppValue and returns false" do expect(CF).to receive(:CFPreferencesSetAppValue).with(@cf_key, @cf_value, @cf_domain) @@ -63,9 +63,9 @@ describe "self.set!" do context "when called with valid domain/default pair" do context "(with username and hostname)" do - it "executes CF.CFPreferencesSetValue and returns nil" do + it "executes CF.CFPreferencesSetValue and CFPreferencesSynchronize and returns nil" do expect(CF).to receive(:CFPreferencesSetValue).with(@cf_key, @cf_value, @cf_domain, @cf_user, @cf_hostname) - expect(CF).to receive(:CFPreferencesAppSynchronize).with(@cf_domain).and_return(pref_sync_passed.result) + expect(CF).to receive(:CFPreferencesSynchronize).with(@cf_domain, @cf_user, @cf_hostname) expect(CF::Preferences.set!(@key, @value, @domain, @user, @hostname)).to eql(nil) end end @@ -78,13 +78,6 @@ end end context "when called with invalid domain/default pair" do - context "(with username and hostname)" do - it "executes CF.CFPreferencesSetValue and raises error" do - expect(CF).to receive(:CFPreferencesSetValue).with(@cf_key, @cf_value, @cf_domain, @cf_user, @cf_hostname) - expect(CF).to receive(:CFPreferencesAppSynchronize).with(@cf_domain).and_return(pref_sync_failed.result) - expect { CF::Preferences.set!(@key, @value, @domain, @user, @hostname) }.to raise_error CF::Exceptions::PreferenceSyncFailed - end - end context "(without username and hostname)" do it "executes CF.CFPreferencesSetAppValue and raises error" do expect(CF).to receive(:CFPreferencesSetAppValue).with(@cf_key, @cf_value, @cf_domain)