Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix various style issues under newer RuboCop #606

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/service/formula_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
93 changes: 34 additions & 59 deletions spec/homebrew/formula_wrapper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -49,44 +47,36 @@
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

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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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"),
Expand All @@ -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 = {
Expand All @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions spec/homebrew/services_cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 9 additions & 22 deletions spec/homebrew/system_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,71 +91,58 @@
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

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

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
Expand Down