From b7b5f53979b8bec2be45fe06a03b3ab4216f66e2 Mon Sep 17 00:00:00 2001 From: Bo Anderson Date: Fri, 15 Dec 2023 16:49:03 +0000 Subject: [PATCH] Fix various style issues under newer RuboCop --- lib/service/formula_wrapper.rb | 6 +- spec/homebrew/formula_wrapper_spec.rb | 93 ++++++++++----------------- spec/homebrew/services_cli_spec.rb | 3 +- spec/homebrew/system_spec.rb | 31 +++------ 4 files changed, 48 insertions(+), 85 deletions(-) diff --git a/lib/service/formula_wrapper.rb b/lib/service/formula_wrapper.rb index e3afaa39f..79dc99c4f 100644 --- a/lib/service/formula_wrapper.rb +++ b/lib/service/formula_wrapper.rb @@ -93,6 +93,8 @@ def plist? return true unless formula.plist.nil? return false unless formula.opt_prefix.exist? return true if Keg.for(formula.opt_prefix).plist_installed? + + false rescue NotAKegError false end @@ -155,13 +157,13 @@ def unknown_status? # Get current PID of daemon process from status output. def pid status_output, _, status_type = status_output_success_type - return Regexp.last_match(1).to_i if status_output =~ pid_regex(status_type) + Regexp.last_match(1).to_i if status_output =~ pid_regex(status_type) end # Get current exit code of daemon process from status output. def exit_code status_output, _, status_type = status_output_success_type - return Regexp.last_match(1).to_i if status_output =~ exit_code_regex(status_type) + Regexp.last_match(1).to_i if status_output =~ exit_code_regex(status_type) end def to_hash diff --git a/spec/homebrew/formula_wrapper_spec.rb b/spec/homebrew/formula_wrapper_spec.rb index 67c392746..6c0bf26f2 100644 --- a/spec/homebrew/formula_wrapper_spec.rb +++ b/spec/homebrew/formula_wrapper_spec.rb @@ -24,14 +24,12 @@ end it "systemD - outputs the full service file path" do - allow(Service::System).to receive(:launchctl?).and_return(false) - allow(Service::System).to receive(:systemctl?).and_return(true) + allow(Service::System).to receive_messages(launchctl?: false, systemctl?: true) expect(service.service_file.to_s).to eq("/usr/local/opt/mysql/homebrew.mysql.service") end it "Other - outputs no service file" do - allow(Service::System).to receive(:launchctl?).and_return(false) - allow(Service::System).to receive(:systemctl?).and_return(false) + allow(Service::System).to receive_messages(launchctl?: false, systemctl?: false) expect(service.service_file).to be_nil end end @@ -49,14 +47,12 @@ end it "systemD - outputs the service name" do - allow(Service::System).to receive(:launchctl?).and_return(false) - allow(Service::System).to receive(:systemctl?).and_return(true) + allow(Service::System).to receive_messages(launchctl?: false, systemctl?: true) expect(service.service_name).to eq("plist-mysql-test") end it "Other - outputs no service name" do - allow(Service::System).to receive(:launchctl?).and_return(false) - allow(Service::System).to receive(:systemctl?).and_return(false) + allow(Service::System).to receive_messages(launchctl?: false, systemctl?: false) expect(service.service_name).to be_nil end end @@ -64,29 +60,23 @@ describe "#dest_dir" do it "macOS - user - outputs the destination directory for the service file" do ENV["HOME"] = "/tmp_home" - allow(Service::System).to receive(:root?).and_return(false) - allow(Service::System).to receive(:launchctl?).and_return(true) + allow(Service::System).to receive_messages(root?: false, launchctl?: true) expect(service.dest_dir.to_s).to eq("/tmp_home/Library/LaunchAgents") end it "macOS - root - outputs the destination directory for the service file" do - allow(Service::System).to receive(:launchctl?).and_return(true) - allow(Service::System).to receive(:root?).and_return(true) + allow(Service::System).to receive_messages(launchctl?: true, root?: true) expect(service.dest_dir.to_s).to eq("/Library/LaunchDaemons") end it "systemD - user - outputs the destination directory for the service file" do ENV["HOME"] = "/tmp_home" - allow(Service::System).to receive(:root?).and_return(false) - allow(Service::System).to receive(:launchctl?).and_return(false) - allow(Service::System).to receive(:systemctl?).and_return(true) + allow(Service::System).to receive_messages(root?: false, launchctl?: false, systemctl?: true) expect(service.dest_dir.to_s).to eq("/tmp_home/.config/systemd/user") end it "systemD - root - outputs the destination directory for the service file" do - allow(Service::System).to receive(:root?).and_return(true) - allow(Service::System).to receive(:launchctl?).and_return(false) - allow(Service::System).to receive(:systemctl?).and_return(true) + allow(Service::System).to receive_messages(root?: true, launchctl?: false, systemctl?: true) expect(service.dest_dir.to_s).to eq("/usr/lib/systemd/system") end end @@ -95,16 +85,14 @@ it "macOS - outputs the destination for the service file" do ENV["HOME"] = "/tmp_home" - allow(Service::System).to receive(:launchctl?).and_return(true) - allow(Service::System).to receive(:systemctl?).and_return(false) + allow(Service::System).to receive_messages(launchctl?: true, systemctl?: false) expect(service.dest.to_s).to eq("/tmp_home/Library/LaunchAgents/homebrew.mysql.plist") end it "systemD - outputs the destination for the service file" do ENV["HOME"] = "/tmp_home" - allow(Service::System).to receive(:launchctl?).and_return(false) - allow(Service::System).to receive(:systemctl?).and_return(true) + allow(Service::System).to receive_messages(launchctl?: false, systemctl?: true) expect(service.dest.to_s).to eq("/tmp_home/.config/systemd/user/homebrew.mysql.service") end end @@ -117,22 +105,19 @@ describe "#loaded?" do it "macOS - outputs if the service is loaded" do - allow(Service::System).to receive(:launchctl?).and_return(true) - allow(Service::System).to receive(:systemctl?).and_return(false) + allow(Service::System).to receive_messages(launchctl?: true, systemctl?: false) allow(Utils).to receive(:safe_popen_read) expect(service.loaded?).to be(false) end it "systemD - outputs if the service is loaded" do - allow(Service::System).to receive(:launchctl?).and_return(false) - allow(Service::System).to receive(:systemctl?).and_return(true) + allow(Service::System).to receive_messages(launchctl?: false, systemctl?: true) allow(Utils).to receive(:safe_popen_read) expect(service.loaded?).to be(false) end it "Other - outputs no status" do - allow(Service::System).to receive(:launchctl?).and_return(false) - allow(Service::System).to receive(:systemctl?).and_return(false) + allow(Service::System).to receive_messages(launchctl?: false, systemctl?: false) expect(service.loaded?).to be_nil end end @@ -145,24 +130,23 @@ it "true if installed and file" do tempfile = File.new("/tmp/foo", File::CREAT) - allow(service).to receive(:installed?).and_return(true) - allow(service).to receive(:service_file).and_return(Pathname.new(tempfile)) + allow(service).to receive_messages(installed?: true, service_file: Pathname.new(tempfile)) expect(service.plist?).to be(true) File.delete(tempfile) end it "true if plist" do - allow(service).to receive(:installed?).and_return(true) - allow(service).to receive(:service_file).and_return(Pathname.new("/dev/null")) - allow(service).to receive(:formula).and_return(OpenStruct.new(plist: "a")) + allow(service).to receive_messages(installed?: true, + service_file: Pathname.new("/dev/null"), + formula: OpenStruct.new(plist: "a")) expect(service.plist?).to be(true) end it "false if opt_prefix missing" do - allow(service).to receive(:installed?).and_return(true) - allow(service).to receive(:service_file).and_return(Pathname.new("/dev/null")) - allow(service).to receive(:formula).and_return(OpenStruct.new(plist: nil, - opt_prefix: Pathname.new("/dfslkfhjdsolshlk"))) + allow(service).to receive_messages(installed?: true, + service_file: Pathname.new("/dev/null"), + formula: OpenStruct.new(plist: nil, + opt_prefix: Pathname.new("/dfslkfhjdsolshlk"))) expect(service.plist?).to be(false) end end @@ -174,43 +158,39 @@ end it "user if file present" do - allow(service).to receive(:boot_path_service_file_present?).and_return(false) - allow(service).to receive(:user_path_service_file_present?).and_return(true) + allow(service).to receive_messages(boot_path_service_file_present?: false, + user_path_service_file_present?: true) allow(Service::System).to receive(:user).and_return("user") expect(service.owner).to eq("user") end it "nil if no file present" do - allow(service).to receive(:boot_path_service_file_present?).and_return(false) - allow(service).to receive(:user_path_service_file_present?).and_return(false) + allow(service).to receive_messages(boot_path_service_file_present?: false, + user_path_service_file_present?: false) expect(service.owner).to be_nil end end describe "#service_file_present?" do it "macOS - outputs if the service file is present" do - allow(Service::System).to receive(:launchctl?).and_return(true) - allow(Service::System).to receive(:systemctl?).and_return(false) + allow(Service::System).to receive_messages(launchctl?: true, systemctl?: false) expect(service.service_file_present?).to be(false) end it "macOS - outputs if the service file is present for root" do - allow(Service::System).to receive(:launchctl?).and_return(true) - allow(Service::System).to receive(:systemctl?).and_return(false) + allow(Service::System).to receive_messages(launchctl?: true, systemctl?: false) expect(service.service_file_present?(for: :root)).to be(false) end it "macOS - outputs if the service file is present for user" do - allow(Service::ServicesCli).to receive(:launchctl?).and_return(true) - allow(Service::ServicesCli).to receive(:systemctl?).and_return(false) + allow(Service::ServicesCli).to receive_messages(launchctl?: true, systemctl?: false) expect(service.service_file_present?(for: :user)).to be(false) end end describe "#owner?" do it "macOS - outputs the service file owner" do - allow(Service::System).to receive(:launchctl?).and_return(true) - allow(Service::System).to receive(:systemctl?).and_return(false) + allow(Service::System).to receive_messages(launchctl?: true, systemctl?: false) expect(service.owner).to be_nil end end @@ -235,8 +215,7 @@ end it "outputs false because there is a PID but no exit" do - allow(service).to receive(:pid).and_return(12) - allow(service).to receive(:exit_code).and_return(nil) + allow(service).to receive_messages(pid: 12, exit_code: nil) expect(service.error?).to be(false) end end @@ -333,10 +312,8 @@ describe "#to_hash" do it "represents non-service values" do - allow(Service::System).to receive(:launchctl?).and_return(true) - allow(Service::System).to receive(:systemctl?).and_return(false) - allow(described_class).to receive(:service?).and_return(false) - allow(described_class).to receive(:service_file_present?).and_return(false) + allow(Service::System).to receive_messages(launchctl?: true, systemctl?: false) + allow(described_class).to receive_messages(service?: false, service_file_present?: false) expected = { exit_code: nil, file: Pathname.new("/usr/local/opt/mysql/homebrew.mysql.plist"), @@ -354,8 +331,7 @@ it "represents running non-service values" do ENV["HOME"] = "/tmp_home" - allow(Service::System).to receive(:launchctl?).and_return(true) - allow(Service::System).to receive(:systemctl?).and_return(false) + allow(Service::System).to receive_messages(launchctl?: true, systemctl?: false) expect(service).to receive(:service?).twice.and_return(false) expect(service).to receive(:service_file_present?).and_return(true) expected = { @@ -376,8 +352,7 @@ it "represents service values" do ENV["HOME"] = "/tmp_home" service_stub = OpenStruct.new(command: "/bin/cmd", manual_command: "/bin/cmd") - allow(Service::System).to receive(:launchctl?).and_return(true) - allow(Service::System).to receive(:systemctl?).and_return(false) + allow(Service::System).to receive_messages(launchctl?: true, systemctl?: false) expect(service).to receive(:service?).twice.and_return(true) expect(service).to receive(:service_file_present?).and_return(true) expect(service).to receive(:load_service).twice.and_return(service_stub) diff --git a/spec/homebrew/services_cli_spec.rb b/spec/homebrew/services_cli_spec.rb index 92d398de1..48bffc4c1 100644 --- a/spec/homebrew/services_cli_spec.rb +++ b/spec/homebrew/services_cli_spec.rb @@ -15,8 +15,7 @@ describe "#running" do it "macOS - returns the currently running services" do - allow(Service::System).to receive(:launchctl?).and_return(true) - allow(Service::System).to receive(:systemctl?).and_return(false) + allow(Service::System).to receive_messages(launchctl?: true, systemctl?: false) allow(Utils).to receive(:popen_read).and_return <<~EOS 77513 50 homebrew.mxcl.php 495 0 homebrew.mxcl.node_exporter diff --git a/spec/homebrew/system_spec.rb b/spec/homebrew/system_spec.rb index 19d4f853b..071e941ed 100644 --- a/spec/homebrew/system_spec.rb +++ b/spec/homebrew/system_spec.rb @@ -91,14 +91,12 @@ end it "SystemD - returns the boot path" do - allow(described_class).to receive(:launchctl?).and_return(false) - allow(described_class).to receive(:systemctl?).and_return(true) + 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(:launchctl?).and_return(false) - allow(described_class).to receive(:systemctl?).and_return(false) + allow(described_class).to receive_messages(launchctl?: false, systemctl?: false) expect(described_class.boot_path.to_s).to eq("") end end @@ -106,22 +104,19 @@ describe "#user_path" do it "macOS - returns the user path" do ENV["HOME"] = "/tmp_home" - allow(described_class).to receive(:launchctl?).and_return(true) - allow(described_class).to receive(:systemctl?).and_return(false) + 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(:launchctl?).and_return(false) - allow(described_class).to receive(:systemctl?).and_return(true) + 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(:launchctl?).and_return(false) - allow(described_class).to receive(:systemctl?).and_return(false) + allow(described_class).to receive_messages(launchctl?: false, systemctl?: false) expect(described_class.user_path.to_s).to eq("") end end @@ -129,33 +124,25 @@ describe "#path" do it "macOS - user - returns the current relevant path" do ENV["HOME"] = "/tmp_home" - allow(described_class).to receive(:root?).and_return(false) - allow(described_class).to receive(:launchctl?).and_return(true) - allow(described_class).to receive(:systemctl?).and_return(false) + 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(:root?).and_return(true) - allow(described_class).to receive(:launchctl?).and_return(true) - allow(described_class).to receive(:systemctl?).and_return(false) + 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(:root?).and_return(false) - allow(described_class).to receive(:launchctl?).and_return(false) - allow(described_class).to receive(:systemctl?).and_return(true) + 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(:root?).and_return(true) - allow(described_class).to receive(:launchctl?).and_return(false) - allow(described_class).to receive(:systemctl?).and_return(true) + 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