From 21cd8e92c3283f9ba7c2cf45ef006ee4c267d115 Mon Sep 17 00:00:00 2001 From: Ruoyu Zhong Date: Fri, 22 Nov 2024 18:58:20 +0800 Subject: [PATCH 1/2] service: end systemd configs with a new line systemd configs, like all UNIX text files, should end with a new line. --- Library/Homebrew/service.rb | 44 ++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/Library/Homebrew/service.rb b/Library/Homebrew/service.rb index e4f27fd73c..2ef8c421f4 100644 --- a/Library/Homebrew/service.rb +++ b/Library/Homebrew/service.rb @@ -454,16 +454,6 @@ module Homebrew # @return [String] sig { returns(String) } def to_systemd_unit - unit = <<~EOS - [Unit] - Description=Homebrew generated unit for #{@formula.name} - - [Install] - WantedBy=default.target - - [Service] - EOS - # command needs to be first because it initializes all other values cmd = command&.map { |arg| Utils::Shell.sh_quote(arg) } &.join(" ") @@ -481,24 +471,22 @@ module Homebrew options << "StandardError=append:#{File.expand_path(@error_log_path)}" if @error_log_path.present? options += @environment_variables.map { |k, v| "Environment=\"#{k}=#{v}\"" } if @environment_variables.present? - unit + options.join("\n") + <<~SYSTEMD + [Unit] + Description=Homebrew generated unit for #{@formula.name} + + [Install] + WantedBy=default.target + + [Service] + #{options.join("\n")} + SYSTEMD end # Returns a `String` systemd unit timer. # @return [String] sig { returns(String) } def to_systemd_timer - timer = <<~EOS - [Unit] - Description=Homebrew generated timer for #{@formula.name} - - [Install] - WantedBy=timers.target - - [Timer] - Unit=#{service_name} - EOS - options = [] options << "Persistent=true" if @run_type == RUN_TYPE_CRON options << "OnUnitActiveSec=#{@interval}" if @run_type == RUN_TYPE_INTERVAL @@ -509,7 +497,17 @@ module Homebrew options << "OnCalendar=#{@cron[:Weekday]}-*-#{@cron[:Month]}-#{@cron[:Day]} #{hours}:#{minutes}:00" end - timer + options.join("\n") + <<~SYSTEMD + [Unit] + Description=Homebrew generated timer for #{@formula.name} + + [Install] + WantedBy=timers.target + + [Timer] + Unit=#{service_name} + #{options.join("\n")} + SYSTEMD end # Prepare the service hash for inclusion in the formula API JSON. From 46fab4458a0fbb8a014bbeccd6da53524879f18c Mon Sep 17 00:00:00 2001 From: Ruoyu Zhong Date: Wed, 4 Dec 2024 03:02:24 +0800 Subject: [PATCH 2/2] test/service_spec: fix tests --- Library/Homebrew/test/service_spec.rb | 35 ++++++++++++++------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/Library/Homebrew/test/service_spec.rb b/Library/Homebrew/test/service_spec.rb index a4b7a913a9..f280b1981d 100644 --- a/Library/Homebrew/test/service_spec.rb +++ b/Library/Homebrew/test/service_spec.rb @@ -718,7 +718,7 @@ RSpec.describe Homebrew::Service do unit = f.service.to_systemd_unit std_path = "#{HOMEBREW_PREFIX}/bin:#{HOMEBREW_PREFIX}/sbin:/usr/bin:/bin:/usr/sbin:/sbin" - unit_expect = <<~EOS + unit_expect = <<~SYSTEMD [Unit] Description=Homebrew generated unit for formula_name @@ -737,8 +737,8 @@ RSpec.describe Homebrew::Service do StandardError=append:#{HOMEBREW_PREFIX}/var/log/beanstalkd.error.log Environment="PATH=#{std_path}" Environment="FOO=BAR" - EOS - expect(unit).to eq(unit_expect.strip) + SYSTEMD + expect(unit).to eq(unit_expect) end it "returns valid partial oneshot unit" do @@ -751,7 +751,7 @@ RSpec.describe Homebrew::Service do end unit = f.service.to_systemd_unit - unit_expect = <<~EOS + unit_expect = <<~SYSTEMD [Unit] Description=Homebrew generated unit for formula_name @@ -761,8 +761,8 @@ RSpec.describe Homebrew::Service do [Service] Type=oneshot ExecStart=#{HOMEBREW_PREFIX}/opt/#{name}/bin/beanstalkd - EOS - expect(unit).to eq(unit_expect.strip) + SYSTEMD + expect(unit).to eq(unit_expect) end it "expands paths" do @@ -774,7 +774,7 @@ RSpec.describe Homebrew::Service do end unit = f.service.to_systemd_unit - unit_expect = <<~EOS + unit_expect = <<~SYSTEMD [Unit] Description=Homebrew generated unit for formula_name @@ -785,8 +785,8 @@ RSpec.describe Homebrew::Service do Type=simple ExecStart=#{HOMEBREW_PREFIX}/opt/#{name}/bin/beanstalkd WorkingDirectory=#{Dir.home} - EOS - expect(unit).to eq(unit_expect.strip) + SYSTEMD + expect(unit).to eq(unit_expect) end end @@ -801,7 +801,7 @@ RSpec.describe Homebrew::Service do end unit = f.service.to_systemd_timer - unit_expect = <<~EOS + unit_expect = <<~SYSTEMD [Unit] Description=Homebrew generated timer for formula_name @@ -811,8 +811,8 @@ RSpec.describe Homebrew::Service do [Timer] Unit=homebrew.formula_name OnUnitActiveSec=5 - EOS - expect(unit).to eq(unit_expect.strip) + SYSTEMD + expect(unit).to eq(unit_expect) end it "returns valid partial timer" do @@ -824,7 +824,7 @@ RSpec.describe Homebrew::Service do end unit = f.service.to_systemd_timer - unit_expect = <<~EOS + unit_expect = <<~SYSTEMD [Unit] Description=Homebrew generated timer for formula_name @@ -833,7 +833,8 @@ RSpec.describe Homebrew::Service do [Timer] Unit=homebrew.formula_name - EOS + + SYSTEMD expect(unit).to eq(unit_expect) end @@ -872,7 +873,7 @@ RSpec.describe Homebrew::Service do end unit = f.service.to_systemd_timer - unit_expect = <<~EOS + unit_expect = <<~SYSTEMD [Unit] Description=Homebrew generated timer for formula_name @@ -883,8 +884,8 @@ RSpec.describe Homebrew::Service do Unit=homebrew.formula_name Persistent=true OnCalendar=#{calendar} - EOS - expect(unit).to eq(unit_expect.chomp) + SYSTEMD + expect(unit).to eq(unit_expect) end end end