mirror of
https://github.com/Homebrew/brew.git
synced 2025-07-14 16:09:03 +08:00
formula_creator: improve code/test style.
This commit is contained in:
parent
3964186bec
commit
2cd7a32660
@ -23,20 +23,23 @@ module Homebrew
|
||||
}
|
||||
def initialize(url:, name: nil, version: nil, tap: nil, mode: nil, license: nil, fetch: false, head: false)
|
||||
@url = url
|
||||
@mode = mode
|
||||
@license = license
|
||||
@fetch = fetch
|
||||
|
||||
version = if version.present?
|
||||
odebug "version from user: #{version}"
|
||||
Version.new(version)
|
||||
tap = if tap.blank?
|
||||
CoreTap.instance
|
||||
else
|
||||
odebug "version from url: #{version}"
|
||||
Version.detect(url)
|
||||
Tap.fetch(tap)
|
||||
end
|
||||
@tap = T.let(tap, Tap)
|
||||
|
||||
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])
|
||||
if repository.end_with?(".git")
|
||||
repository = repository.delete_suffix(".git")
|
||||
# e.g. https://github.com/Homebrew/brew.git
|
||||
repository.delete_suffix!(".git")
|
||||
head = true
|
||||
end
|
||||
odebug "github: #{user} #{repository} head:#{head}"
|
||||
@ -44,22 +47,6 @@ module Homebrew
|
||||
name = repository
|
||||
odebug "name from github: #{name}"
|
||||
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?
|
||||
stem = Pathname.new(url).stem
|
||||
name = if stem.start_with?("index.cgi") && stem.include?("=")
|
||||
@ -74,22 +61,33 @@ module Homebrew
|
||||
odebug "name from url: #{name}"
|
||||
end
|
||||
@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)
|
||||
@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))
|
||||
@desc = T.let(nil, T.nilable(String))
|
||||
@homepage = T.let(nil, T.nilable(String))
|
||||
|
@ -5,72 +5,66 @@ require "formula_creator"
|
||||
RSpec.describe Homebrew::FormulaCreator do
|
||||
describe ".new" do
|
||||
tests = {
|
||||
"generic tarball URL": {
|
||||
url: "http://digit-labs.org/files/tools/synscan/releases/synscan-5.02.tar.gz",
|
||||
name: "synscan",
|
||||
version: "5.02",
|
||||
"generic tarball URL": {
|
||||
url: "http://digit-labs.org/files/tools/synscan/releases/synscan-5.02.tar.gz",
|
||||
expected_name: "synscan",
|
||||
expected_version: "5.02",
|
||||
},
|
||||
"gitweb URL": {
|
||||
url: "http://www.codesrc.com/gitweb/index.cgi?p=libzipper.git;a=summary",
|
||||
name: "libzipper",
|
||||
"gitweb URL": {
|
||||
url: "http://www.codesrc.com/gitweb/index.cgi?p=libzipper.git;a=summary",
|
||||
expected_name: "libzipper",
|
||||
},
|
||||
"GitHub repo URL with .git": {
|
||||
url: "https://github.com/Homebrew/brew.git",
|
||||
name: "brew",
|
||||
head: true,
|
||||
fetch: true,
|
||||
github_user: "Homebrew",
|
||||
github_repo: "brew",
|
||||
"GitHub repository URL with .git": {
|
||||
url: "https://github.com/Homebrew/brew.git",
|
||||
fetch: true,
|
||||
github_user_repository: ["Homebrew", "brew"],
|
||||
expected_name: "brew",
|
||||
expected_head: true,
|
||||
},
|
||||
"GitHub archive URL": {
|
||||
url: "https://github.com/Homebrew/brew/archive/4.5.7.tar.gz",
|
||||
name: "brew",
|
||||
version: "4.5.7",
|
||||
fetch: true,
|
||||
github_user: "Homebrew",
|
||||
github_repo: "brew",
|
||||
"GitHub archive URL": {
|
||||
url: "https://github.com/Homebrew/brew/archive/4.5.7.tar.gz",
|
||||
fetch: true,
|
||||
github_user_repository: ["Homebrew", "brew"],
|
||||
expected_name: "brew",
|
||||
expected_version: "4.5.7",
|
||||
},
|
||||
"GitHub releases URL": {
|
||||
url: "https://github.com/stella-emu/stella/releases/download/6.7/stella-6.7-src.tar.xz",
|
||||
name: "stella",
|
||||
version: "6.7",
|
||||
fetch: true,
|
||||
github_user: "stella-emu",
|
||||
github_repo: "stella",
|
||||
"GitHub releases URL": {
|
||||
url: "https://github.com/stella-emu/stella/releases/download/6.7/stella-6.7-src.tar.xz",
|
||||
fetch: true,
|
||||
github_user_repository: ["stella-emu", "stella"],
|
||||
expected_name: "stella",
|
||||
expected_version: "6.7",
|
||||
},
|
||||
"GitHub latest release": {
|
||||
url: "https://github.com/buildpacks/pack",
|
||||
name: "pack",
|
||||
version: "v0.37.0",
|
||||
fetch: true,
|
||||
github_user: "buildpacks",
|
||||
github_repo: "pack",
|
||||
latest_release: { "tag_name" => "v0.37.0" },
|
||||
"GitHub latest release": {
|
||||
url: "https://github.com/buildpacks/pack",
|
||||
fetch: true,
|
||||
github_user_repository: ["buildpacks", "pack"],
|
||||
latest_release: { "tag_name" => "v0.37.0" },
|
||||
expected_name: "pack",
|
||||
expected_version: "v0.37.0",
|
||||
},
|
||||
}
|
||||
|
||||
tests.each do |description, test|
|
||||
it "parses #{description}" do
|
||||
fetch = test.fetch(:fetch, false)
|
||||
allow(GitHub).to receive(:repository).with(test.fetch(:github_user), test.fetch(:github_repo)).once if fetch
|
||||
|
||||
latest_release = test.fetch(:latest_release, nil) if fetch
|
||||
if latest_release
|
||||
expect(GitHub).to receive(:get_latest_release)
|
||||
.with(test.fetch(:github_user), test.fetch(:github_repo))
|
||||
.and_return(test.fetch(:latest_release))
|
||||
.once
|
||||
if fetch
|
||||
github_user_repository = test.fetch(:github_user_repository)
|
||||
allow(GitHub).to receive(:repository).with(*github_user_repository)
|
||||
if (latest_release = test[:latest_release])
|
||||
expect(GitHub).to receive(:get_latest_release).with(*github_user_repository).and_return(latest_release)
|
||||
end
|
||||
end
|
||||
|
||||
formula_creator = described_class.new(url: test.fetch(:url), fetch:)
|
||||
|
||||
expect(formula_creator.name).to eq(test.fetch(:name))
|
||||
if (version = test[:version])
|
||||
expect(formula_creator.version).to eq(version)
|
||||
expect(formula_creator.name).to eq(test.fetch(:expected_name))
|
||||
if (expected_version = test[:expected_version])
|
||||
expect(formula_creator.version).to eq(expected_version)
|
||||
else
|
||||
expect(formula_creator.version).to be_null
|
||||
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user