| 
									
										
										
										
											2025-02-26 13:26:37 +01:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-13 14:50:03 +00:00
										 |  |  | require "services/system" | 
					
						
							| 
									
										
										
										
											2025-02-26 13:26:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-14 04:35:30 +00:00
										 |  |  | RSpec.describe Homebrew::Services::System do | 
					
						
							| 
									
										
										
										
											2025-02-26 13:26:37 +01:00
										 |  |  |   describe "#launchctl" do | 
					
						
							|  |  |  |     it "macOS - outputs launchctl command location", :needs_macos do | 
					
						
							|  |  |  |       expect(described_class.launchctl).to eq(Pathname.new("/bin/launchctl")) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "Other - outputs launchctl command location", :needs_linux do | 
					
						
							| 
									
										
										
										
											2025-03-13 17:36:46 +00:00
										 |  |  |       expect(described_class.launchctl).to be_nil | 
					
						
							| 
									
										
										
										
											2025-02-26 13:26:37 +01:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "#launchctl?" do | 
					
						
							|  |  |  |     it "macOS - outputs launchctl presence", :needs_macos do | 
					
						
							|  |  |  |       expect(described_class.launchctl?).to be(true) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "Other - outputs launchctl presence", :needs_linux do | 
					
						
							|  |  |  |       expect(described_class.launchctl?).to be(false) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "#systemctl?" do | 
					
						
							| 
									
										
										
										
											2025-03-13 20:13:13 +01:00
										 |  |  |     it "Linux with SystemD - outputs systemctl presence", :needs_systemd do | 
					
						
							| 
									
										
										
										
											2025-02-26 13:26:37 +01:00
										 |  |  |       expect(described_class.systemctl?).to be(true) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "Other - outputs systemctl presence", :needs_macos do | 
					
						
							|  |  |  |       expect(described_class.systemctl?).to be(false) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "#root?" do | 
					
						
							|  |  |  |     it "checks if the command is ran as root" do | 
					
						
							|  |  |  |       expect(described_class.root?).to be(false) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "#user" do | 
					
						
							|  |  |  |     it "returns the current username" do | 
					
						
							|  |  |  |       expect(described_class.user).to eq(ENV.fetch("USER")) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "#user_of_process" do | 
					
						
							|  |  |  |     it "returns the username for empty PID" do | 
					
						
							|  |  |  |       expect(described_class.user_of_process(nil)).to eq(ENV.fetch("USER")) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "returns the PID username" do | 
					
						
							|  |  |  |       allow(Utils).to receive(:safe_popen_read).and_return <<~EOS | 
					
						
							|  |  |  |         USER | 
					
						
							|  |  |  |         user | 
					
						
							|  |  |  |       EOS | 
					
						
							|  |  |  |       expect(described_class.user_of_process(50)).to eq("user") | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "returns nil if unavailable" do | 
					
						
							|  |  |  |       allow(Utils).to receive(:safe_popen_read).and_return <<~EOS | 
					
						
							|  |  |  |         USER | 
					
						
							|  |  |  |       EOS | 
					
						
							|  |  |  |       expect(described_class.user_of_process(50)).to be_nil | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "#domain_target" do | 
					
						
							|  |  |  |     it "returns the current domain target" do | 
					
						
							|  |  |  |       allow(described_class).to receive(:root?).and_return(false) | 
					
						
							|  |  |  |       expect(described_class.domain_target).to match(%r{gui/(\d+)}) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "returns the root domain target" do | 
					
						
							|  |  |  |       allow(described_class).to receive(:root?).and_return(true) | 
					
						
							|  |  |  |       expect(described_class.domain_target).to match("system") | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "#boot_path" do | 
					
						
							|  |  |  |     it "macOS - returns the boot path" do | 
					
						
							|  |  |  |       allow(described_class).to receive(:launchctl?).and_return(true) | 
					
						
							|  |  |  |       expect(described_class.boot_path.to_s).to eq("/Library/LaunchDaemons") | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "SystemD - returns the boot path" do | 
					
						
							|  |  |  |       allow(described_class).to receive_messages(launchctl?: false, systemctl?: true) | 
					
						
							|  |  |  |       expect(described_class.boot_path.to_s).to eq("/usr/lib/systemd/system") | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "Unknown - returns no boot path" do | 
					
						
							|  |  |  |       allow(described_class).to receive_messages(launchctl?: false, systemctl?: false) | 
					
						
							|  |  |  |       expect(described_class.boot_path.to_s).to eq("") | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "#user_path" do | 
					
						
							|  |  |  |     it "macOS - returns the user path" do | 
					
						
							|  |  |  |       ENV["HOME"] = "/tmp_home" | 
					
						
							|  |  |  |       allow(described_class).to receive_messages(launchctl?: true, systemctl?: false) | 
					
						
							|  |  |  |       expect(described_class.user_path.to_s).to eq("/tmp_home/Library/LaunchAgents") | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "systemD - returns the user path" do | 
					
						
							|  |  |  |       ENV["HOME"] = "/tmp_home" | 
					
						
							|  |  |  |       allow(described_class).to receive_messages(launchctl?: false, systemctl?: true) | 
					
						
							|  |  |  |       expect(described_class.user_path.to_s).to eq("/tmp_home/.config/systemd/user") | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "Unknown - returns no user path" do | 
					
						
							|  |  |  |       ENV["HOME"] = "/tmp_home" | 
					
						
							|  |  |  |       allow(described_class).to receive_messages(launchctl?: false, systemctl?: false) | 
					
						
							|  |  |  |       expect(described_class.user_path.to_s).to eq("") | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "#path" do | 
					
						
							|  |  |  |     it "macOS - user - returns the current relevant path" do | 
					
						
							|  |  |  |       ENV["HOME"] = "/tmp_home" | 
					
						
							|  |  |  |       allow(described_class).to receive_messages(root?: false, launchctl?: true, systemctl?: false) | 
					
						
							|  |  |  |       expect(described_class.path.to_s).to eq("/tmp_home/Library/LaunchAgents") | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "macOS - root- returns the current relevant path" do | 
					
						
							|  |  |  |       ENV["HOME"] = "/tmp_home" | 
					
						
							|  |  |  |       allow(described_class).to receive_messages(root?: true, launchctl?: true, systemctl?: false) | 
					
						
							|  |  |  |       expect(described_class.path.to_s).to eq("/Library/LaunchDaemons") | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "systemD - user - returns the current relevant path" do | 
					
						
							|  |  |  |       ENV["HOME"] = "/tmp_home" | 
					
						
							|  |  |  |       allow(described_class).to receive_messages(root?: false, launchctl?: false, systemctl?: true) | 
					
						
							|  |  |  |       expect(described_class.path.to_s).to eq("/tmp_home/.config/systemd/user") | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "systemD - root- returns the current relevant path" do | 
					
						
							|  |  |  |       ENV["HOME"] = "/tmp_home" | 
					
						
							|  |  |  |       allow(described_class).to receive_messages(root?: true, launchctl?: false, systemctl?: true) | 
					
						
							|  |  |  |       expect(described_class.path.to_s).to eq("/usr/lib/systemd/system") | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |