refactor: Enable strict typing in download_strategy

This commit is contained in:
Douglas Eichelberger 2025-03-12 11:39:01 -07:00
parent 6ac3311f17
commit 1ca5299f40
No known key found for this signature in database
GPG Key ID: F90193CBD547EB81
12 changed files with 312 additions and 162 deletions

View File

@ -20,7 +20,7 @@ module Cask
sig { returns(T.nilable(T.any(Symbol, String))) }
attr_reader :user_agent
sig { returns(T.any(T::Class[T.anything], Symbol, NilClass)) }
sig { returns(T.any(T::Class[AbstractDownloadStrategy], Symbol, NilClass)) }
attr_reader :using
sig { returns(T.nilable(String)) }
@ -35,7 +35,7 @@ module Cask
# @api public
verified: T.nilable(String),
# @api public
using: T.any(Class, Symbol, NilClass),
using: T.any(T::Class[AbstractDownloadStrategy], Symbol, NilClass),
# @api public
tag: T.nilable(String),
# @api public
@ -186,7 +186,7 @@ module Cask
params(
uri: T.nilable(T.any(URI::Generic, String)),
verified: T.nilable(String),
using: T.any(Class, Symbol, NilClass),
using: T.any(T::Class[AbstractDownloadStrategy], Symbol, NilClass),
tag: T.nilable(String),
branch: T.nilable(String),
revisions: T.nilable(T::Array[String]),

View File

@ -115,7 +115,7 @@ module Homebrew
strategy = DownloadStrategyDetector.detect(url)
downloader = strategy.new(url, token, version.to_s, cache: Cask::Cache.path)
downloader.fetch
downloader.cached_location.sha256
downloader.cached_location!.sha256
end
[url.gsub(version.to_s, "\#{version}"), sha256]

File diff suppressed because it is too large Load Diff

View File

@ -56,7 +56,7 @@ module Downloadable
sig { overridable.returns(Pathname) }
def cached_download
downloader.cached_location
downloader.cached_location!
end
sig { overridable.void }

View File

@ -56,7 +56,7 @@ class Cask::URL
sig { returns(T.nilable(T.any(::String, ::Symbol))) }
def user_agent; end
sig { returns(T.nilable(T.any(::Symbol, T::Class[T.anything]))) }
sig { returns(T.nilable(T.any(::Symbol, T::Class[::AbstractDownloadStrategy]))) }
def using; end
sig { returns(T.nilable(::String)) }

View File

@ -3,7 +3,7 @@
require "download_strategy"
RSpec.describe AbstractDownloadStrategy do
subject(:strategy) { described_class.new(url, name, version, **specs) }
subject(:strategy) { Class.new(described_class).new(url, name, version, **specs) }
let(:specs) { {} }
let(:name) { "foo" }

View File

@ -6,7 +6,7 @@ RSpec.describe DownloadStrategyDetector do
describe "::detect" do
subject(:strategy_detector) { described_class.detect(url, strategy) }
let(:url) { Object.new }
let(:url) { "invalidurl" }
let(:strategy) { nil }
context "when given Git URL" do

View File

@ -17,6 +17,8 @@ RSpec.describe SubversionDownloadStrategy do
it "adds the appropriate svn args" do
expect(strategy).to receive(:system_command!)
.with("svn", hash_including(args: array_including("--trust-server-cert", "--non-interactive")))
.and_return(instance_double(SystemCommand::Result))
strategy.fetch
end
end
@ -27,6 +29,7 @@ RSpec.describe SubversionDownloadStrategy do
it "adds svn arguments for :revision" do
expect(strategy).to receive(:system_command!)
.with("svn", hash_including(args: array_including_cons("-r", "10")))
.and_return(instance_double(SystemCommand::Result))
strategy.fetch
end

View File

@ -9,7 +9,7 @@ RSpec.describe VCSDownloadStrategy do
describe "#cached_location" do
it "returns the path of the cached resource" do
allow_any_instance_of(described_class).to receive(:cache_tag).and_return("foo")
downloader = described_class.new(url, "baz", version)
downloader = Class.new(described_class).new(url, "baz", version)
expect(downloader.cached_location).to eq(HOMEBREW_CACHE/"baz--foo")
end
end

View File

@ -1,16 +1,20 @@
# typed: true # rubocop:todo Sorbet/StrictSigil
# typed: strict
# frozen_string_literal: true
require "version"
class URL
attr_reader :specs, :using
sig { returns(T::Hash[Symbol, T.untyped]) }
attr_reader :specs
sig { returns(T.any(NilClass, Symbol, T::Class[AbstractDownloadStrategy])) }
attr_reader :using
sig { params(url: String, specs: T::Hash[Symbol, T.untyped]).void }
def initialize(url, specs = {})
@url = url.freeze
@specs = specs.dup
@using = @specs.delete(:using)
@url = T.let(url.freeze, String)
@specs = T.let(specs.dup, T::Hash[Symbol, T.untyped])
@using = T.let(@specs.delete(:using), T.any(NilClass, Symbol, T::Class[AbstractDownloadStrategy]))
@specs.freeze
end
@ -19,13 +23,14 @@ class URL
@url
end
sig { returns(T.class_of(AbstractDownloadStrategy)) }
sig { returns(T::Class[AbstractDownloadStrategy]) }
def download_strategy
@download_strategy ||= DownloadStrategyDetector.detect(@url, @using)
@download_strategy ||=
T.let(DownloadStrategyDetector.detect(@url, @using), T.nilable(T::Class[AbstractDownloadStrategy]))
end
sig { returns(Version) }
def version
@version ||= Version.detect(@url, **@specs)
@version ||= T.let(Version.detect(@url, **@specs), T.nilable(Version))
end
end

View File

@ -211,7 +211,7 @@ module Utils
end
sig {
params(
overridable.params(
args: String,
print_stdout: T.any(T::Boolean, Symbol),
options: T.untyped,
@ -264,7 +264,7 @@ module Utils
curl(*args, **options)
end
sig { params(args: String, options: T.untyped).returns(SystemCommand::Result) }
sig { overridable.params(args: String, options: T.untyped).returns(SystemCommand::Result) }
def curl_output(*args, **options)
curl_with_workarounds(*args, print_stderr: false, show_output: true, **options)
end

View File

@ -31,14 +31,14 @@ class GitHubArtifactDownloadStrategy < AbstractFileDownloadStrategy
@token = T.let(token, String)
end
sig { params(timeout: T.nilable(Integer)).void }
sig { override.params(timeout: T.any(Float, Integer, NilClass)).void }
def fetch(timeout: nil)
ohai "Downloading #{url}"
if cached_location.exist?
puts "Already downloaded: #{cached_location}"
else
begin
Utils::Curl.curl("--location", "--create-dirs", "--output", temporary_path, url,
Utils::Curl.curl("--location", "--create-dirs", "--output", temporary_path.to_s, url,
"--header", "Authorization: token #{@token}",
secrets: [@token],
timeout:)
@ -46,7 +46,7 @@ class GitHubArtifactDownloadStrategy < AbstractFileDownloadStrategy
raise CurlDownloadStrategyError, url
end
cached_location.dirname.mkpath
temporary_path.rename(cached_location)
temporary_path.rename(cached_location.to_s)
end
symlink_location.dirname.mkpath