Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

service: handle string run cmd #15120

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 19 additions & 9 deletions Library/Homebrew/service.rb
Expand Up @@ -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)
Expand Down Expand Up @@ -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, "Unexpected run command: #{api_hash["run"]}"
end

hash[:keep_alive] = api_hash["keep_alive"].transform_keys(&:to_sym) if api_hash.key?("keep_alive")
Expand Down
32 changes: 32 additions & 0 deletions Library/Homebrew/test/service_spec.rb
Expand Up @@ -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