From 39092fa6293a208fb2eedc1d6b2773a7e641f006 Mon Sep 17 00:00:00 2001 From: apainintheneck Date: Fri, 31 Mar 2023 17:18:06 -0700 Subject: [PATCH 1/2] service: handle string run cmd This was not handled at all during deserialization. The string argument gets turned into an array internally but we skip that to preserve all args in the @run_params variable. That means that we have to handle strings when deserializing too. --- Library/Homebrew/service.rb | 28 +++++++++++++++-------- Library/Homebrew/test/service_spec.rb | 32 +++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/Library/Homebrew/service.rb b/Library/Homebrew/service.rb index 2e4aefe46b8b2..6a31ff35acfe7 100644 --- a/Library/Homebrew/service.rb +++ b/Library/Homebrew/service.rb @@ -46,8 +46,11 @@ def f } def run(command = nil, macos: nil, linux: nil) # Save parameters for serialization - @run_params ||= command - @run_params ||= { macos: macos, linux: linux }.compact + if command + @run_params = command + elsif macos || linux + @run_params = { macos: macos, linux: linux }.compact + end command ||= on_system_conditional(macos: macos, linux: linux) case T.unsafe(command) @@ -551,15 +554,22 @@ def self.deserialize(api_hash) hash = {} hash[:run] = case api_hash["run"] - when Hash - api_hash["run"].to_h do |key, array| - [ - key.to_sym, - array.map(&method(:replace_placeholders)), - ] - end + when String + replace_placeholders(api_hash["run"]) when Array api_hash["run"].map(&method(:replace_placeholders)) + when Hash + api_hash["run"].to_h do |key, elem| + run_cmd = if elem.is_a?(Array) + elem.map(&method(:replace_placeholders)) + else + replace_placeholders(elem) + end + + [key.to_sym, run_cmd] + end + else + raise ArgumentError, "Unexepected run command: #{api_hash["run"]}" end hash[:keep_alive] = api_hash["keep_alive"].transform_keys(&:to_sym) if api_hash.key?("keep_alive") diff --git a/Library/Homebrew/test/service_spec.rb b/Library/Homebrew/test/service_spec.rb index f9089f012f96d..35fde30940701 100644 --- a/Library/Homebrew/test/service_spec.rb +++ b/Library/Homebrew/test/service_spec.rb @@ -975,5 +975,37 @@ def stub_formula(&block) it "replaces placeholders with local paths" do expect(described_class.deserialize(serialized_hash)).to eq(deserialized_hash) end + + describe "run command" do + it "handles String argument correctly" do + expect(described_class.deserialize({ + "run" => "$HOMEBREW_PREFIX/opt/formula_name/bin/beanstalkd", + })).to eq({ + run: "#{HOMEBREW_PREFIX}/opt/formula_name/bin/beanstalkd", + }) + end + + it "handles Array argument correctly" do + expect(described_class.deserialize({ + "run" => ["$HOMEBREW_PREFIX/opt/formula_name/bin/beanstalkd", "--option"], + })).to eq({ + run: ["#{HOMEBREW_PREFIX}/opt/formula_name/bin/beanstalkd", "--option"], + }) + end + + it "handles Hash argument correctly" do + expect(described_class.deserialize({ + "run" => { + "linux" => "$HOMEBREW_PREFIX/opt/formula_name/bin/beanstalkd", + "macos" => ["$HOMEBREW_PREFIX/opt/formula_name/bin/beanstalkd", "--option"], + }, + })).to eq({ + run: { + linux: "#{HOMEBREW_PREFIX}/opt/formula_name/bin/beanstalkd", + macos: ["#{HOMEBREW_PREFIX}/opt/formula_name/bin/beanstalkd", "--option"], + }, + }) + end + end end end From 85a1b791543e48d82ec10bb221fbaed442741f48 Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Mon, 3 Apr 2023 08:41:21 +0100 Subject: [PATCH 2/2] service: fix typo. --- Library/Homebrew/service.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Homebrew/service.rb b/Library/Homebrew/service.rb index 6a31ff35acfe7..588642960e9a4 100644 --- a/Library/Homebrew/service.rb +++ b/Library/Homebrew/service.rb @@ -569,7 +569,7 @@ def self.deserialize(api_hash) [key.to_sym, run_cmd] end else - raise ArgumentError, "Unexepected run command: #{api_hash["run"]}" + raise ArgumentError, "Unexpected run command: #{api_hash["run"]}" end hash[:keep_alive] = api_hash["keep_alive"].transform_keys(&:to_sym) if api_hash.key?("keep_alive")