From 0d3cbcad414aeb295c0c82122ae7f21428bfce97 Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Thu, 13 Mar 2025 17:36:46 +0000 Subject: [PATCH] test/services: fix some more tests. --- Library/Homebrew/services/system/systemctl.rb | 5 +++++ Library/Homebrew/test/services/cli_spec.rb | 10 ++++------ .../Homebrew/test/services/system/systemctl_spec.rb | 6 +++++- Library/Homebrew/test/services/system_spec.rb | 2 +- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/Library/Homebrew/services/system/systemctl.rb b/Library/Homebrew/services/system/systemctl.rb index aa2fc1150b..46244ad4b1 100644 --- a/Library/Homebrew/services/system/systemctl.rb +++ b/Library/Homebrew/services/system/systemctl.rb @@ -9,6 +9,11 @@ module Services @executable ||= T.let(which("systemctl"), T.nilable(Pathname)) end + sig { void } + def self.reset_executable! + @executable = nil + end + sig { returns(String) } def self.scope System.root? ? "--system" : "--user" diff --git a/Library/Homebrew/test/services/cli_spec.rb b/Library/Homebrew/test/services/cli_spec.rb index 8deb774b3f..ae3a83f95d 100644 --- a/Library/Homebrew/test/services/cli_spec.rb +++ b/Library/Homebrew/test/services/cli_spec.rb @@ -177,8 +177,7 @@ RSpec.describe Services::Cli do describe "#systemd_load", :needs_linux do it "checks non-enabling run" do - expect(Services::System::Systemctl).to receive(:executable).once.and_return(Pathname.new("/usr/bin/systemctl")) - expect(Services::System::Systemctl).to receive(:scope).once.and_return("--user") + expect(Services::System::Systemctl).to receive(:run).once.and_return(true) services_cli.systemd_load( instance_double(Services::FormulaWrapper, service_name: "name"), enable: false, @@ -186,8 +185,7 @@ RSpec.describe Services::Cli do end it "checks enabling run" do - expect(Services::System::Systemctl).to receive(:executable).once.and_return(Pathname.new("/usr/bin/systemctl")) - expect(Services::System::Systemctl).to receive(:scope).twice.and_return("--user") + expect(Services::System::Systemctl).to receive(:run).twice.and_return(true) services_cli.systemd_load( instance_double(Services::FormulaWrapper, service_name: "name"), enable: true, @@ -197,15 +195,15 @@ RSpec.describe Services::Cli do describe "#launchctl_load", :needs_macos do it "checks non-enabling run" do + allow(Services::System).to receive(:launchctl).and_return(Pathname.new("/bin/launchctl")) expect(Services::System).to receive(:domain_target).once.and_return("target") - expect(Services::System).to receive(:launchctl).once.and_return(Pathname.new("/bin/launchctl")) expect(described_class).to receive(:safe_system).once.and_return(true) services_cli.launchctl_load(instance_double(Services::FormulaWrapper), file: "a", enable: false) end it "checks enabling run" do + allow(Services::System).to receive(:launchctl).and_return(Pathname.new("/bin/launchctl")) expect(Services::System).to receive(:domain_target).twice.and_return("target") - expect(Services::System).to receive(:launchctl).once.and_return(Pathname.new("/bin/launchctl")) expect(described_class).to receive(:safe_system).twice.and_return(true) services_cli.launchctl_load(instance_double(Services::FormulaWrapper, service_name: "name"), file: "a", enable: true) diff --git a/Library/Homebrew/test/services/system/systemctl_spec.rb b/Library/Homebrew/test/services/system/systemctl_spec.rb index 6c2a7f6783..b66b3884c4 100644 --- a/Library/Homebrew/test/services/system/systemctl_spec.rb +++ b/Library/Homebrew/test/services/system/systemctl_spec.rb @@ -18,7 +18,11 @@ RSpec.describe Services::System::Systemctl do describe ".executable" do it "outputs systemctl command location", :needs_linux do - expect(described_class.executable).to eq(Pathname.new("/usr/bin/systemctl")) + systemctl = Pathname("/bin/systemctl") + expect(described_class).to receive(:which).and_return(systemctl) + described_class.reset_executable! + + expect(described_class.executable).to eq(systemctl) end end end diff --git a/Library/Homebrew/test/services/system_spec.rb b/Library/Homebrew/test/services/system_spec.rb index 9d6c1f3650..382b0c4f82 100644 --- a/Library/Homebrew/test/services/system_spec.rb +++ b/Library/Homebrew/test/services/system_spec.rb @@ -9,7 +9,7 @@ RSpec.describe Services::System do end it "Other - outputs launchctl command location", :needs_linux do - expect(described_class.launchctl).to eq_nil + expect(described_class.launchctl).to be_nil end end