services: fix array being flattened

This commit is contained in:
Sean Molenaar 2021-04-29 09:43:39 +02:00
parent c11067c5d6
commit d4197835bb
No known key found for this signature in database
GPG Key ID: 6BF5D8DF0D34FAAE
2 changed files with 79 additions and 54 deletions

View File

@ -121,20 +121,17 @@ module Homebrew
sig { returns(T::Array[String]) } sig { returns(T::Array[String]) }
def command def command
instance_eval(&@service_block) instance_eval(&@service_block)
@run.select { |i| i.is_a?(Pathname) } @run.map(&:to_s)
.map(&:to_s)
end end
# Returns a `String` plist. # Returns a `String` plist.
# @return [String] # @return [String]
sig { returns(String) } sig { returns(String) }
def to_plist def to_plist
instance_eval(&@service_block)
base = { base = {
Label: @formula.plist_name, Label: @formula.plist_name,
RunAtLoad: @run_type == RUN_TYPE_IMMEDIATE, RunAtLoad: @run_type == RUN_TYPE_IMMEDIATE,
ProgramArguments: command.join, ProgramArguments: command,
} }
base[:KeepAlive] = @keep_alive if @keep_alive == true base[:KeepAlive] = @keep_alive if @keep_alive == true
@ -150,15 +147,13 @@ module Homebrew
# @return [String] # @return [String]
sig { returns(String) } sig { returns(String) }
def to_systemd_unit def to_systemd_unit
instance_eval(&@service_block)
unit = <<~EOS unit = <<~EOS
[Unit] [Unit]
Description=Homebrew generated unit for #{@formula.name} Description=Homebrew generated unit for #{@formula.name}
[Service] [Service]
Type=simple Type=simple
ExecStart=#{command.join} ExecStart=#{command.join(" ")}
EOS EOS
options = [] options = []

View File

@ -35,7 +35,7 @@ describe Homebrew::Service do
describe "#to_plist" do describe "#to_plist" do
it "returns valid plist" do it "returns valid plist" do
f.class.service do f.class.service do
run opt_bin/"beanstalkd" run [opt_bin/"beanstalkd", "test"]
run_type :immediate run_type :immediate
environment_variables PATH: std_service_path_env environment_variables PATH: std_service_path_env
error_log_path var/"log/beanstalkd.error.log" error_log_path var/"log/beanstalkd.error.log"
@ -45,46 +45,70 @@ describe Homebrew::Service do
end end
plist = f.service.to_plist plist = f.service.to_plist
expect(plist).to include("<key>Label</key>") plist_expect = <<~EOS
expect(plist).to include("<string>homebrew.mxcl.#{name}</string>") <?xml version="1.0" encoding="UTF-8"?>
expect(plist).to include("<key>KeepAlive</key>") <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
expect(plist).to include("<key>RunAtLoad</key>") <plist version="1.0">
expect(plist).to include("<key>ProgramArguments</key>") <dict>
expect(plist).to include("<string>#{HOMEBREW_PREFIX}/opt/#{name}/bin/beanstalkd</string>") \t<key>EnvironmentVariables</key>
expect(plist).to include("<key>WorkingDirectory</key>") \t<dict>
expect(plist).to include("<string>#{HOMEBREW_PREFIX}/var</string>") \t\t<key>PATH</key>
expect(plist).to include("<key>StandardOutPath</key>") \t\t<string>#{HOMEBREW_PREFIX}/bin:#{HOMEBREW_PREFIX}/sbin:/usr/bin:/bin:/usr/sbin:/sbin</string>
expect(plist).to include("<string>#{HOMEBREW_PREFIX}/var/log/beanstalkd.log</string>") \t</dict>
expect(plist).to include("<key>StandardErrorPath</key>") \t<key>KeepAlive</key>
expect(plist).to include("<string>#{HOMEBREW_PREFIX}/var/log/beanstalkd.error.log</string>") \t<true/>
expect(plist).to include("<key>EnvironmentVariables</key>") \t<key>Label</key>
expect(plist).to include("<key>PATH</key>") \t<string>homebrew.mxcl.formula_name</string>
expect(plist).to include("<string>#{HOMEBREW_PREFIX}/bin:#{HOMEBREW_PREFIX}/sbin:/usr/bin:/bin:") \t<key>ProgramArguments</key>
\t<array>
\t\t<string>#{HOMEBREW_PREFIX}/opt/formula_name/bin/beanstalkd</string>
\t\t<string>test</string>
\t</array>
\t<key>RunAtLoad</key>
\t<true/>
\t<key>StandardErrorPath</key>
\t<string>#{HOMEBREW_PREFIX}/var/log/beanstalkd.error.log</string>
\t<key>StandardOutPath</key>
\t<string>#{HOMEBREW_PREFIX}/var/log/beanstalkd.log</string>
\t<key>WorkingDirectory</key>
\t<string>#{HOMEBREW_PREFIX}/var</string>
</dict>
</plist>
EOS
expect(plist).to eq(plist_expect)
end end
it "returns valid partial plist" do it "returns valid partial plist" do
f.class.service do f.class.service do
run bin/"beanstalkd" run opt_bin/"beanstalkd"
run_type :immediate run_type :immediate
end end
plist = f.service.to_plist plist = f.service.to_plist
expect(plist).to include("<string>homebrew.mxcl.#{name}</string>") plist_expect = <<~EOS
expect(plist).to include("<key>Label</key>") <?xml version="1.0" encoding="UTF-8"?>
expect(plist).not_to include("<key>KeepAlive</key>") <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
expect(plist).to include("<key>RunAtLoad</key>") <plist version="1.0">
expect(plist).to include("<key>ProgramArguments</key>") <dict>
expect(plist).not_to include("<key>WorkingDirectory</key>") \t<key>Label</key>
expect(plist).not_to include("<key>StandardOutPath</key>") \t<string>homebrew.mxcl.formula_name</string>
expect(plist).not_to include("<key>StandardErrorPath</key>") \t<key>ProgramArguments</key>
expect(plist).not_to include("<key>EnvironmentVariables</key>") \t<array>
\t\t<string>#{HOMEBREW_PREFIX}/opt/formula_name/bin/beanstalkd</string>
\t</array>
\t<key>RunAtLoad</key>
\t<true/>
</dict>
</plist>
EOS
expect(plist).to eq(plist_expect)
end end
end end
describe "#to_systemd_unit" do describe "#to_systemd_unit" do
it "returns valid unit" do it "returns valid unit" do
f.class.service do f.class.service do
run opt_bin/"beanstalkd" run [opt_bin/"beanstalkd", "test"]
run_type :immediate run_type :immediate
environment_variables PATH: std_service_path_env environment_variables PATH: std_service_path_env
error_log_path var/"log/beanstalkd.error.log" error_log_path var/"log/beanstalkd.error.log"
@ -94,15 +118,21 @@ describe Homebrew::Service do
end end
unit = f.service.to_systemd_unit unit = f.service.to_systemd_unit
expect(unit).to include("Description=Homebrew generated unit for formula_name")
expect(unit).to include("Type=simple")
expect(unit).to include("ExecStart=#{HOMEBREW_PREFIX}/opt/#{name}/bin/beanstalkd")
expect(unit).to include("Restart=always")
expect(unit).to include("WorkingDirectory=#{HOMEBREW_PREFIX}/var")
expect(unit).to include("StandardOutput=append:#{HOMEBREW_PREFIX}/var/log/beanstalkd.log")
expect(unit).to include("StandardError=append:#{HOMEBREW_PREFIX}/var/log/beanstalkd.error.log")
std_path = "#{HOMEBREW_PREFIX}/bin:#{HOMEBREW_PREFIX}/sbin:/usr/bin:/bin:/usr/sbin:/sbin" std_path = "#{HOMEBREW_PREFIX}/bin:#{HOMEBREW_PREFIX}/sbin:/usr/bin:/bin:/usr/sbin:/sbin"
expect(unit).to include("Environment=\"PATH=#{std_path}\"") unit_expect = <<~EOS
[Unit]
Description=Homebrew generated unit for formula_name
[Service]
Type=simple
ExecStart=#{HOMEBREW_PREFIX}/opt/#{name}/bin/beanstalkd test
Restart=always
WorkingDirectory=#{HOMEBREW_PREFIX}/var
StandardOutput=append:#{HOMEBREW_PREFIX}/var/log/beanstalkd.log
StandardError=append:#{HOMEBREW_PREFIX}/var/log/beanstalkd.error.log
Environment=\"PATH=#{std_path}\"
EOS
expect(unit).to eq(unit_expect.strip)
end end
it "returns valid partial unit" do it "returns valid partial unit" do
@ -112,27 +142,27 @@ describe Homebrew::Service do
end end
unit = f.service.to_systemd_unit unit = f.service.to_systemd_unit
expect(unit).to include("Description=Homebrew generated unit for formula_name") unit_expect = <<~EOS
expect(unit).to include("Type=simple") [Unit]
expect(unit).to include("ExecStart=#{HOMEBREW_PREFIX}/opt/#{name}/bin/beanstalkd") Description=Homebrew generated unit for formula_name
expect(unit).not_to include("Restart=always")
expect(unit).not_to include("WorkingDirectory=#{HOMEBREW_PREFIX}/var") [Service]
expect(unit).not_to include("StandardOutput=append:#{HOMEBREW_PREFIX}/var/log/beanstalkd.log") Type=simple
expect(unit).not_to include("StandardError=append:#{HOMEBREW_PREFIX}/var/log/beanstalkd.error.log") ExecStart=#{HOMEBREW_PREFIX}/opt/#{name}/bin/beanstalkd
std_path = "#{HOMEBREW_PREFIX}/bin:#{HOMEBREW_PREFIX}/sbin:/usr/bin:/bin:/usr/sbin:/sbin" EOS
expect(unit).not_to include("Environment=\"PATH=#{std_path}\"") expect(unit).to eq(unit_expect)
end end
end end
describe "#command" do describe "#command" do
it "returns @run data" do it "returns @run data" do
f.class.service do f.class.service do
run opt_bin/"beanstalkd" run [opt_bin/"beanstalkd", "test"]
run_type :immediate run_type :immediate
end end
command = f.service.command command = f.service.command
expect(command).to eq(["#{HOMEBREW_PREFIX}/opt/#{name}/bin/beanstalkd"]) expect(command).to eq(["#{HOMEBREW_PREFIX}/opt/#{name}/bin/beanstalkd", "test"])
end end
end end
end end