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(services/list): correctly handle services with an error code #19502

Merged
merged 1 commit into from
Mar 15, 2025
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
4 changes: 2 additions & 2 deletions Library/Homebrew/services/formula_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,14 @@ def owner

sig { returns(T::Boolean) }
def pid?
pid.present? && !pid.zero?
pid.present? && pid.positive?
end

sig { returns(T::Boolean) }
def error?
return false if pid?

exit_code.present? && exit_code.nonzero?
exit_code.present? && !exit_code.zero?
end

sig { returns(T::Boolean) }
Expand Down
50 changes: 46 additions & 4 deletions Library/Homebrew/test/services/formula_wrapper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,26 @@
end

describe "#pid?" do
it "outputs false because there is not pid" do
it "outputs false because there is not PID" do
allow(service).to receive(:pid).and_return(nil)
expect(service.pid?).to be(false)
end

it "outputs false because there is a PID and it is zero" do
allow(service).to receive(:pid).and_return(0)
expect(service.pid?).to be(false)
end

it "outputs true because there is a PID and it is positive" do
allow(service).to receive(:pid).and_return(12)
expect(service.pid?).to be(true)
end

# This should never happen in practice, as PIDs cannot be negative.
it "outputs false because there is a PID and it is negative" do
allow(service).to receive(:pid).and_return(-1)
expect(service.pid?).to be(false)
end
end

describe "#pid", :needs_systemd do
Expand All @@ -230,16 +246,42 @@
end
end

describe "#error?", :needs_systemd do
it "outputs false because there a no PID" do
allow(service).to receive(:pid).and_return(nil)
describe "#error?" do
it "outputs false because there is no PID or exit code" do
allow(service).to receive_messages(pid: nil, exit_code: nil)
expect(service.error?).to be(false)
end

it "outputs false because there is a PID but no exit" do
allow(service).to receive_messages(pid: 12, exit_code: nil)
expect(service.error?).to be(false)
end

it "outputs false because there is a PID and a zero exit code" do
allow(service).to receive_messages(pid: 12, exit_code: 0)
expect(service.error?).to be(false)
end

it "outputs false because there is a PID and a positive exit code" do
allow(service).to receive_messages(pid: 12, exit_code: 1)
expect(service.error?).to be(false)
end

it "outputs false because there is no PID and a zero exit code" do
allow(service).to receive_messages(pid: nil, exit_code: 0)
expect(service.error?).to be(false)
end

it "outputs true because there is no PID and a positive exit code" do
allow(service).to receive_messages(pid: nil, exit_code: 1)
expect(service.error?).to be(true)
end

# This should never happen in practice, as exit codes cannot be negative.
it "outputs true because there is no PID and a negative exit code" do
allow(service).to receive_messages(pid: nil, exit_code: -1)
expect(service.error?).to be(true)
end
end

describe "#exit_code", :needs_systemd do
Expand Down
Loading