formula_creator: improve code/test style.

This commit is contained in:
Mike McQuaid 2025-06-19 09:02:27 +01:00
parent 3964186bec
commit 2cd7a32660
No known key found for this signature in database
2 changed files with 76 additions and 84 deletions

View File

@ -23,20 +23,23 @@ module Homebrew
} }
def initialize(url:, name: nil, version: nil, tap: nil, mode: nil, license: nil, fetch: false, head: false) def initialize(url:, name: nil, version: nil, tap: nil, mode: nil, license: nil, fetch: false, head: false)
@url = url @url = url
@mode = mode
@license = license
@fetch = fetch
version = if version.present? tap = if tap.blank?
odebug "version from user: #{version}" CoreTap.instance
Version.new(version)
else else
odebug "version from url: #{version}" Tap.fetch(tap)
Version.detect(url)
end end
@tap = T.let(tap, Tap)
if (match_github = url.match %r{github\.com/(?<user>[^/]+)/(?<repo>[^/]+).*}) if (match_github = url.match %r{github\.com/(?<user>[^/]+)/(?<repo>[^/]+).*})
user = match_github[:user] user = T.must(match_github[:user])
repository = T.must(match_github[:repo]) repository = T.must(match_github[:repo])
if repository.end_with?(".git") if repository.end_with?(".git")
repository = repository.delete_suffix(".git") # e.g. https://github.com/Homebrew/brew.git
repository.delete_suffix!(".git")
head = true head = true
end end
odebug "github: #{user} #{repository} head:#{head}" odebug "github: #{user} #{repository} head:#{head}"
@ -44,22 +47,6 @@ module Homebrew
name = repository name = repository
odebug "name from github: #{name}" odebug "name from github: #{name}"
end end
github = GitHub.repository(user, repository) if fetch
latest_release = if version.null? && fetch && !head
begin
GitHub.get_latest_release(user, repository)
rescue GitHub::API::HTTPNotFoundError
odebug "github: latest_release lookup failed: #{url}"
nil
end
end
if latest_release
version = Version.new(latest_release["tag_name"])
odebug "github: version from latest_release: #{@version}"
end
elsif name.blank? elsif name.blank?
stem = Pathname.new(url).stem stem = Pathname.new(url).stem
name = if stem.start_with?("index.cgi") && stem.include?("=") name = if stem.start_with?("index.cgi") && stem.include?("=")
@ -74,22 +61,33 @@ module Homebrew
odebug "name from url: #{name}" odebug "name from url: #{name}"
end end
@name = T.let(name, String) @name = T.let(name, String)
@head = head
version = if version.present?
odebug "version from user: #{version}"
Version.new(version)
else
odebug "version from url: #{version}"
Version.detect(url)
end
if fetch && user && repository
github = GitHub.repository(user, repository)
if version.null? && !head
begin
latest_release = GitHub.get_latest_release(user, repository)
version = Version.new(latest_release.fetch("tag_name"))
odebug "github: version from latest_release: #{@version}"
rescue GitHub::API::HTTPNotFoundError
odebug "github: latest_release lookup failed: #{url}"
nil
end
end
end
@github = T.let(github, T.untyped) @github = T.let(github, T.untyped)
@version = T.let(version, Version) @version = T.let(version, Version)
tap = if tap.blank?
CoreTap.instance
else
Tap.fetch(tap)
end
@tap = T.let(tap, Tap)
@mode = T.let(mode.presence, T.nilable(Symbol))
@license = T.let(license.presence, T.nilable(String))
@fetch = fetch
@head = head
@sha256 = T.let(nil, T.nilable(String)) @sha256 = T.let(nil, T.nilable(String))
@desc = T.let(nil, T.nilable(String)) @desc = T.let(nil, T.nilable(String))
@homepage = T.let(nil, T.nilable(String)) @homepage = T.let(nil, T.nilable(String))

View File

@ -5,72 +5,66 @@ require "formula_creator"
RSpec.describe Homebrew::FormulaCreator do RSpec.describe Homebrew::FormulaCreator do
describe ".new" do describe ".new" do
tests = { tests = {
"generic tarball URL": { "generic tarball URL": {
url: "http://digit-labs.org/files/tools/synscan/releases/synscan-5.02.tar.gz", url: "http://digit-labs.org/files/tools/synscan/releases/synscan-5.02.tar.gz",
name: "synscan", expected_name: "synscan",
version: "5.02", expected_version: "5.02",
}, },
"gitweb URL": { "gitweb URL": {
url: "http://www.codesrc.com/gitweb/index.cgi?p=libzipper.git;a=summary", url: "http://www.codesrc.com/gitweb/index.cgi?p=libzipper.git;a=summary",
name: "libzipper", expected_name: "libzipper",
}, },
"GitHub repo URL with .git": { "GitHub repository URL with .git": {
url: "https://github.com/Homebrew/brew.git", url: "https://github.com/Homebrew/brew.git",
name: "brew", fetch: true,
head: true, github_user_repository: ["Homebrew", "brew"],
fetch: true, expected_name: "brew",
github_user: "Homebrew", expected_head: true,
github_repo: "brew",
}, },
"GitHub archive URL": { "GitHub archive URL": {
url: "https://github.com/Homebrew/brew/archive/4.5.7.tar.gz", url: "https://github.com/Homebrew/brew/archive/4.5.7.tar.gz",
name: "brew", fetch: true,
version: "4.5.7", github_user_repository: ["Homebrew", "brew"],
fetch: true, expected_name: "brew",
github_user: "Homebrew", expected_version: "4.5.7",
github_repo: "brew",
}, },
"GitHub releases URL": { "GitHub releases URL": {
url: "https://github.com/stella-emu/stella/releases/download/6.7/stella-6.7-src.tar.xz", url: "https://github.com/stella-emu/stella/releases/download/6.7/stella-6.7-src.tar.xz",
name: "stella", fetch: true,
version: "6.7", github_user_repository: ["stella-emu", "stella"],
fetch: true, expected_name: "stella",
github_user: "stella-emu", expected_version: "6.7",
github_repo: "stella",
}, },
"GitHub latest release": { "GitHub latest release": {
url: "https://github.com/buildpacks/pack", url: "https://github.com/buildpacks/pack",
name: "pack", fetch: true,
version: "v0.37.0", github_user_repository: ["buildpacks", "pack"],
fetch: true, latest_release: { "tag_name" => "v0.37.0" },
github_user: "buildpacks", expected_name: "pack",
github_repo: "pack", expected_version: "v0.37.0",
latest_release: { "tag_name" => "v0.37.0" },
}, },
} }
tests.each do |description, test| tests.each do |description, test|
it "parses #{description}" do it "parses #{description}" do
fetch = test.fetch(:fetch, false) fetch = test.fetch(:fetch, false)
allow(GitHub).to receive(:repository).with(test.fetch(:github_user), test.fetch(:github_repo)).once if fetch if fetch
github_user_repository = test.fetch(:github_user_repository)
latest_release = test.fetch(:latest_release, nil) if fetch allow(GitHub).to receive(:repository).with(*github_user_repository)
if latest_release if (latest_release = test[:latest_release])
expect(GitHub).to receive(:get_latest_release) expect(GitHub).to receive(:get_latest_release).with(*github_user_repository).and_return(latest_release)
.with(test.fetch(:github_user), test.fetch(:github_repo)) end
.and_return(test.fetch(:latest_release))
.once
end end
formula_creator = described_class.new(url: test.fetch(:url), fetch:) formula_creator = described_class.new(url: test.fetch(:url), fetch:)
expect(formula_creator.name).to eq(test.fetch(:name)) expect(formula_creator.name).to eq(test.fetch(:expected_name))
if (version = test[:version]) if (expected_version = test[:expected_version])
expect(formula_creator.version).to eq(version) expect(formula_creator.version).to eq(expected_version)
else else
expect(formula_creator.version).to be_null expect(formula_creator.version).to be_null
end end
expect(formula_creator.head).to eq(test.fetch(:head, false)) expect(formula_creator.head).to eq(test.fetch(:expected_head, false))
end end
end end
end end