Skip to content

Commit

Permalink
services: specs for commands + fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
botantony committed Mar 7, 2025
1 parent 2c046fd commit 87f4582
Show file tree
Hide file tree
Showing 5 changed files with 332 additions and 295 deletions.
2 changes: 1 addition & 1 deletion Library/Homebrew/services/service/commands/restart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Restart

TRIGGERS = %w[restart relaunch reload r].freeze

sig { params(targets: T::Array[Service::FormulaWrapper], verbose: T.nilable(T::Boolean)).void }
sig { params(targets: T::Array[Service::FormulaWrapper], verbose: T.nilable(T::Boolean)).returns(NilClass) }
def self.run(targets, verbose:)
Service::ServicesCli.check(targets)

Expand Down
220 changes: 110 additions & 110 deletions Library/Homebrew/test/services/commands/info_spec.rb
Original file line number Diff line number Diff line change
@@ -1,112 +1,112 @@
# frozen_string_literal: true

# require "services/service"
#
# RSpec.describe Service::Commands::Info do
# before do
# allow_any_instance_of(IO).to receive(:tty?).and_return(true)
# end
#
# describe "#TRIGGERS" do
# it "contains all restart triggers" do
# expect(described_class::TRIGGERS).to eq(%w[info i])
# end
# end
#
# describe "#run" do
# it "fails with empty list" do
# expect do
# described_class.run([], verbose: false, json: false)
# end.to raise_error UsageError, "Formula(e) missing, please provide a formula name or use --all"
# end
#
# it "succeeds with items" do
# out = "<BOLD>service<RESET> ()\nRunning: true\nLoaded: true\nSchedulable: false\n"
# formula = {
# name: "service",
# user: "user",
# status: :started,
# file: "/dev/null",
# running: true,
# loaded: true,
# schedulable: false,
# }
# expect do
# described_class.run([formula], verbose: false, json: false)
# end.to output(out).to_stdout
# end
#
# it "succeeds with items - JSON" do
# formula = {
# name: "service",
# user: "user",
# status: :started,
# file: "/dev/null",
# running: true,
# loaded: true,
# schedulable: false,
# }
# out = "#{JSON.pretty_generate([formula])}\n"
# expect do
# described_class.run([formula], verbose: false, json: true)
# end.to output(out).to_stdout
# end
# end
#
# describe "#output" do
# it "returns minimal output" do
# out = "<BOLD>service<RESET> ()\nRunning: <BOLD>✔<RESET>\nLoaded: <BOLD>✔<RESET>\nSchedulable: <BOLD>✘<RESET>\n"
# formula = {
# name: "service",
# user: "user",
# status: :started,
# file: "/dev/null",
# running: true,
# loaded: true,
# schedulable: false,
# }
# expect(described_class.output(formula, verbose: false)).to eq(out)
# end
#
# it "returns normal output" do
# out = "<BOLD>service<RESET> ()\nRunning: <BOLD>✔<RESET>\nLoaded: <BOLD>✔<RESET>\nSchedulable: <BOLD>✘<RESET>\n"
# out += "User: user\nPID: 42\n"
# formula = {
# name: "service",
# user: "user",
# status: :started,
# file: "/dev/null",
# running: true,
# loaded: true,
# schedulable: false,
# pid: 42,
# }
# expect(described_class.output(formula, verbose: false)).to eq(out)
# end
#
# it "returns verbose output" do
# out = "<BOLD>service<RESET> ()\nRunning: <BOLD>✔<RESET>\nLoaded: <BOLD>✔<RESET>\nSchedulable: <BOLD>✘<RESET>\n"
# out += "User: user\nPID: 42\nFile: /dev/null <BOLD>✔<RESET>\nCommand: /bin/command\n"
# out += "Working directory: /working/dir\nRoot directory: /root/dir\nLog: /log/dir\nError log: /log/dir/error\n"
# out += "Interval: 3600s\nCron: 5 * * * *\n"
# formula = {
# name: "service",
# user: "user",
# status: :started,
# file: "/dev/null",
# running: true,
# loaded: true,
# schedulable: false,
# pid: 42,
# command: "/bin/command",
# working_dir: "/working/dir",
# root_dir: "/root/dir",
# log_path: "/log/dir",
# error_log_path: "/log/dir/error",
# interval: 3600,
# cron: "5 * * * *",
# }
# expect(described_class.output(formula, verbose: true)).to eq(out)
# end
# end
# end
require "services/service"

RSpec.describe Service::Commands::Info do
before do
allow_any_instance_of(IO).to receive(:tty?).and_return(true)
end

describe "#TRIGGERS" do
it "contains all restart triggers" do
expect(described_class::TRIGGERS).to eq(%w[info i])
end
end

describe "#run" do
it "fails with empty list" do
expect do
described_class.run([], verbose: false, json: false)
end.to raise_error UsageError, "Formula(e) missing, please provide a formula name or use --all"
end

it "succeeds with items" do
out = "<BOLD>service<RESET> ()\nRunning: true\nLoaded: true\nSchedulable: false\n"
formula = {
name: "service",
user: "user",
status: :started,
file: "/dev/null",
running: true,
loaded: true,
schedulable: false,
}
expect do
described_class.run([formula], verbose: false, json: false)
end.to output(out).to_stdout
end

it "succeeds with items - JSON" do
formula = {
name: "service",
user: "user",
status: :started,
file: "/dev/null",
running: true,
loaded: true,
schedulable: false,
}
out = "#{JSON.pretty_generate([formula])}\n"
expect do
described_class.run([formula], verbose: false, json: true)
end.to output(out).to_stdout
end
end

describe "#output" do
it "returns minimal output" do
out = "<BOLD>service<RESET> ()\nRunning: <BOLD>✔<RESET>\nLoaded: <BOLD>✔<RESET>\nSchedulable: <BOLD>✘<RESET>\n"
formula = {
name: "service",
user: "user",
status: :started,
file: "/dev/null",
running: true,
loaded: true,
schedulable: false,
}
expect(described_class.output(formula, verbose: false)).to eq(out)
end

it "returns normal output" do
out = "<BOLD>service<RESET> ()\nRunning: <BOLD>✔<RESET>\nLoaded: <BOLD>✔<RESET>\nSchedulable: <BOLD>✘<RESET>\n"
out += "User: user\nPID: 42\n"
formula = {
name: "service",
user: "user",
status: :started,
file: "/dev/null",
running: true,
loaded: true,
schedulable: false,
pid: 42,
}
expect(described_class.output(formula, verbose: false)).to eq(out)
end

it "returns verbose output" do
out = "<BOLD>service<RESET> ()\nRunning: <BOLD>✔<RESET>\nLoaded: <BOLD>✔<RESET>\nSchedulable: <BOLD>✘<RESET>\n"
out += "User: user\nPID: 42\nFile: /dev/null <BOLD>✔<RESET>\nCommand: /bin/command\n"
out += "Working directory: /working/dir\nRoot directory: /root/dir\nLog: /log/dir\nError log: /log/dir/error\n"
out += "Interval: 3600s\nCron: 5 * * * *\n"
formula = {
name: "service",
user: "user",
status: :started,
file: "/dev/null",
running: true,
loaded: true,
schedulable: false,
pid: 42,
command: "/bin/command",
working_dir: "/working/dir",
root_dir: "/root/dir",
log_path: "/log/dir",
error_log_path: "/log/dir/error",
interval: 3600,
cron: "5 * * * *",
}
expect(described_class.output(formula, verbose: true)).to eq(out)
end
end
end
Loading

0 comments on commit 87f4582

Please sign in to comment.