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.
This commit is contained in:
apainintheneck 2023-03-31 17:18:06 -07:00
parent 931327df1f
commit 39092fa629
2 changed files with 51 additions and 9 deletions

View File

@ -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, "Unexepected run command: #{api_hash["run"]}"
end
hash[:keep_alive] = api_hash["keep_alive"].transform_keys(&:to_sym) if api_hash.key?("keep_alive")

View File

@ -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