Skip to content

Commit 1fe6567

Browse files
authored
Merge pull request #21289 from Homebrew/patch-typed-strict
patch: `typed: strict`
2 parents 7742f77 + e0d9334 commit 1fe6567

File tree

3 files changed

+43
-32
lines changed

3 files changed

+43
-32
lines changed

Library/Homebrew/patch.rb

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1-
# typed: true # rubocop:todo Sorbet/StrictSigil
1+
# typed: strict
22
# frozen_string_literal: true
33

44
require "resource"
55
require "erb"
66
require "utils/output"
77

8+
Owner = T.type_alias { T.any(Formula, Resource, SoftwareSpec) }
9+
810
# Helper module for creating patches.
911
module Patch
12+
sig {
13+
params(
14+
strip: T.any(Symbol, String),
15+
src: T.nilable(T.any(Symbol, String)),
16+
block: T.nilable(T.proc.bind(Resource::Patch).void),
17+
).returns(T.any(EmbeddedPatch, ExternalPatch))
18+
}
1019
def self.create(strip, src, &block)
1120
case strip
1221
when :DATA
@@ -22,32 +31,38 @@ def self.create(strip, src, &block)
2231
else
2332
ExternalPatch.new(strip, &block)
2433
end
25-
when nil
26-
raise ArgumentError, "nil value for strip"
27-
else
28-
raise ArgumentError, "Unexpected value for strip: #{strip.inspect}"
2934
end
3035
end
3136
end
3237

3338
# An abstract class representing a patch embedded into a formula.
3439
class EmbeddedPatch
3540
include Utils::Output::Mixin
41+
extend T::Helpers
42+
43+
abstract!
3644

45+
sig { params(owner: T.nilable(Owner)).returns(T.nilable(Owner)) }
3746
attr_writer :owner
47+
48+
sig { returns(T.any(String, Symbol)) }
3849
attr_reader :strip
3950

51+
sig { params(strip: T.any(String, Symbol)).void }
4052
def initialize(strip)
41-
@strip = strip
53+
@strip = T.let(strip, T.any(String, Symbol))
54+
@owner = T.let(nil, T.nilable(Owner))
4255
end
4356

4457
sig { returns(T::Boolean) }
4558
def external?
4659
false
4760
end
4861

62+
sig { abstract.returns(String) }
4963
def contents; end
5064

65+
sig { void }
5166
def apply
5267
data = contents.gsub("@@HOMEBREW_PREFIX@@", HOMEBREW_PREFIX)
5368
if data.gsub!("HOMEBREW_PREFIX", HOMEBREW_PREFIX)
@@ -66,15 +81,20 @@ def inspect
6681

6782
# A patch at the `__END__` of a formula file.
6883
class DATAPatch < EmbeddedPatch
84+
sig { returns(T.nilable(Pathname)) }
6985
attr_accessor :path
7086

87+
sig { params(strip: T.any(String, Symbol)).void }
7188
def initialize(strip)
7289
super
73-
@path = nil
90+
@path = T.let(nil, T.nilable(Pathname))
7491
end
7592

76-
sig { returns(String) }
93+
sig { override.returns(String) }
7794
def contents
95+
path = self.path
96+
raise ArgumentError, "DATAPatch#contents called before path was set!" unless path
97+
7898
data = +""
7999
path.open("rb") do |f|
80100
loop do
@@ -91,11 +111,13 @@ def contents
91111

92112
# A string containing a patch.
93113
class StringPatch < EmbeddedPatch
114+
sig { params(strip: T.any(String, Symbol), str: String).void }
94115
def initialize(strip, str)
95116
super(strip)
96-
@str = str
117+
@str = T.let(str, String)
97118
end
98119

120+
sig { override.returns(String) }
99121
def contents
100122
@str
101123
end
@@ -107,27 +129,34 @@ class ExternalPatch
107129

108130
extend Forwardable
109131

110-
attr_reader :resource, :strip
132+
sig { returns(Resource::Patch) }
133+
attr_reader :resource
134+
135+
sig { returns(T.any(String, Symbol)) }
136+
attr_reader :strip
111137

112138
def_delegators :resource,
113139
:url, :fetch, :patch_files, :verify_download_integrity,
114140
:cached_download, :downloaded?, :clear_cache
115141

142+
sig { params(strip: T.any(String, Symbol), block: T.nilable(T.proc.bind(Resource::Patch).void)).void }
116143
def initialize(strip, &block)
117-
@strip = strip
118-
@resource = Resource::Patch.new(&block)
144+
@strip = T.let(strip, T.any(String, Symbol))
145+
@resource = T.let(Resource::Patch.new(&block), Resource::Patch)
119146
end
120147

121148
sig { returns(T::Boolean) }
122149
def external?
123150
true
124151
end
125152

153+
sig { params(owner: T.nilable(Owner)).void }
126154
def owner=(owner)
127155
resource.owner = owner
128156
resource.version(resource.checksum&.hexdigest || ERB::Util.url_encode(resource.url))
129157
end
130158

159+
sig { void }
131160
def apply
132161
base_dir = Pathname.pwd
133162
resource.unpack do

Library/Homebrew/test/patch_spec.rb

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,6 @@
4242
it { is_expected.to be_a DATAPatch }
4343
it(:strip) { expect(patch.strip).to eq(:p1) }
4444
end
45-
46-
it "raises an error for unknown values" do
47-
expect do
48-
described_class.create(Object.new)
49-
end.to raise_error(ArgumentError)
50-
51-
expect do
52-
described_class.create(Object.new, Object.new)
53-
end.to raise_error(ArgumentError)
54-
end
5545
end
5646

5747
describe "#patch_files" do
@@ -81,14 +71,6 @@
8171
end
8272
end
8373

84-
describe EmbeddedPatch do
85-
describe "#new" do
86-
subject(:patch) { described_class.new(:p1) }
87-
88-
it(:inspect) { expect(patch.inspect).to eq("#<EmbeddedPatch: :p1>") }
89-
end
90-
end
91-
9274
describe ExternalPatch do
9375
subject(:patch) { described_class.new(:p1) { url "file:///my.patch" } }
9476

Library/Homebrew/test/resource_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,15 @@
152152
end
153153

154154
describe "#owner" do
155+
let(:owner) { described_class.new("test-owner") }
156+
155157
it "sets the owner" do
156-
owner = Object.new
157158
resource.owner = owner
158159
expect(resource.owner).to eq(owner)
159160
end
160161

161162
it "sets its owner to be the patches' owner" do
162163
resource.patch(:p1) { url "file:///my.patch" }
163-
owner = Object.new
164164
resource.owner = owner
165165
resource.patches.each do |p|
166166
expect(p.resource.owner).to eq(owner)

0 commit comments

Comments
 (0)