Skip to content

Commit

Permalink
Normalize service serialization method names
Browse files Browse the repository at this point in the history
  • Loading branch information
apainintheneck committed Jan 31, 2024
1 parent 3a70b6a commit df8ed66
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions Library/Homebrew/formula.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2255,7 +2255,7 @@ def to_hash
"disable_date" => disable_date,
"disable_reason" => disable_reason,
"post_install_defined" => post_install_defined?,
"service" => (service.serialize if service?),
"service" => (service.to_hash if service?),
"tap_git_head" => tap_git_head,
"ruby_source_path" => ruby_source_path,
"ruby_source_checksum" => {},
Expand Down Expand Up @@ -2318,7 +2318,7 @@ def to_api_hash
api_hash["pour_bottle_only_if"] = self.class.pour_bottle_only_if.to_s if self.class.pour_bottle_only_if
api_hash["link_overwrite"] = self.class.link_overwrite_paths.to_a if self.class.link_overwrite_paths.present?
api_hash["caveats"] = caveats_with_placeholders if caveats
api_hash["service"] = service.serialize if service?
api_hash["service"] = service.to_hash if service?

if stable
api_hash["version"] = stable&.version&.to_s
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/formulary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ def post_install_defined?
end

if (service_hash = json_formula["service"].presence)
service_hash = Homebrew::Service.deserialize(service_hash)
service_hash = Homebrew::Service.from_hash(service_hash)
service do
T.bind(self, Homebrew::Service)

Expand Down
4 changes: 2 additions & 2 deletions Library/Homebrew/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ def to_systemd_timer

# Prepare the service hash for inclusion in the formula API JSON.
sig { returns(Hash) }
def serialize
def to_hash
name_params = {
macos: (plist_name if plist_name != default_plist_name),
linux: (service_name if service_name != default_service_name),
Expand Down Expand Up @@ -568,7 +568,7 @@ def serialize

# Turn the service API hash values back into what is expected by the formula DSL.
sig { params(api_hash: Hash).returns(Hash) }
def self.deserialize(api_hash)
def self.from_hash(api_hash)
hash = {}
hash[:name] = api_hash["name"].transform_keys(&:to_sym) if api_hash.key?("name")

Expand Down
8 changes: 4 additions & 4 deletions Library/Homebrew/test/formula_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ def post_install
url "https://brew.sh/test-1.0.tbz"
end

expect(f.service.serialize).to eq({})
expect(f.service.to_hash).to eq({})
end

specify "service complicated" do
Expand All @@ -754,7 +754,7 @@ def post_install
keep_alive true
end
end
expect(f.service.serialize.keys)
expect(f.service.to_hash.keys)
.to contain_exactly(:run, :run_type, :error_log_path, :log_path, :working_dir, :keep_alive)
end

Expand All @@ -766,7 +766,7 @@ def post_install
end
end

expect(f.service.serialize.keys).to contain_exactly(:run, :run_type)
expect(f.service.to_hash.keys).to contain_exactly(:run, :run_type)
end

specify "service with only custom names" do
Expand All @@ -779,7 +779,7 @@ def post_install

expect(f.plist_name).to eq("custom.macos.beanstalkd")
expect(f.service_name).to eq("custom.linux.beanstalkd")
expect(f.service.serialize.keys).to contain_exactly(:name)
expect(f.service.to_hash.keys).to contain_exactly(:name)
end

specify "service helpers return data" do
Expand Down
14 changes: 7 additions & 7 deletions Library/Homebrew/test/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,7 @@ def stub_formula_with_service_sockets(sockets_var)
end
end

describe "#serialize" do
describe "#to_hash" do
let(:serialized_hash) do
{
environment_variables: {
Expand Down Expand Up @@ -1072,12 +1072,12 @@ def stub_formula_with_service_sockets(sockets_var)
end

Formula.generating_hash!
expect(f.service.serialize).to eq(serialized_hash)
expect(f.service.to_hash).to eq(serialized_hash)
Formula.generated_hash!
end
end

describe ".deserialize" do
describe ".from_hash" do
let(:serialized_hash) do
{
"name" => {
Expand Down Expand Up @@ -1111,28 +1111,28 @@ def stub_formula_with_service_sockets(sockets_var)
end

it "replaces placeholders with local paths" do
expect(described_class.deserialize(serialized_hash)).to eq(deserialized_hash)
expect(described_class.from_hash(serialized_hash)).to eq(deserialized_hash)
end

describe "run command" do
it "handles String argument correctly" do
expect(described_class.deserialize({
expect(described_class.from_hash({
"run" => "$HOMEBREW_PREFIX/opt/formula_name/bin/beanstalkd",
})).to eq({
run: "#{HOMEBREW_PREFIX}/opt/formula_name/bin/beanstalkd",
})
end

it "handles Array argument correctly" do
expect(described_class.deserialize({
expect(described_class.from_hash({
"run" => ["$HOMEBREW_PREFIX/opt/formula_name/bin/beanstalkd", "--option"],
})).to eq({
run: ["#{HOMEBREW_PREFIX}/opt/formula_name/bin/beanstalkd", "--option"],
})
end

it "handles Hash argument correctly" do
expect(described_class.deserialize({
expect(described_class.from_hash({
"run" => {
"linux" => "$HOMEBREW_PREFIX/opt/formula_name/bin/beanstalkd",
"macos" => ["$HOMEBREW_PREFIX/opt/formula_name/bin/beanstalkd", "--option"],
Expand Down

0 comments on commit df8ed66

Please sign in to comment.