diff --git a/Library/Homebrew/service.rb b/Library/Homebrew/service.rb index 2e4aefe46b..588642960e 100644 --- a/Library/Homebrew/service.rb +++ b/Library/Homebrew/service.rb @@ -46,8 +46,11 @@ module Homebrew } 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 @@ module Homebrew 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, "Unexpected 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 f9089f012f..35fde30940 100644 --- a/Library/Homebrew/test/service_spec.rb +++ b/Library/Homebrew/test/service_spec.rb @@ -975,5 +975,37 @@ describe Homebrew::Service do 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