Skip to content

Commit 7daadae

Browse files
committed
tests: Prefer let(:klass) { Foo } over described_class
- The "explicit" setting on the `RSpec/DescribedClass` cop is still enabled, because `described_class` is bad for Sorbet typing, but instead of applying its autocorrections to specify the full class name everywhere that `described_class` was used, use `let(:klass) { Foo }` so that the repetition per-test isn't massive in tests that use it a lot (say, >5 times). - Changes made with Copilot CLI, agent mode, Claude Opus 4.7.
1 parent f8dc64b commit 7daadae

432 files changed

Lines changed: 4815 additions & 4137 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Library/Homebrew/test/PATH_spec.rb

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,34 @@
44
require "PATH"
55

66
RSpec.describe PATH do
7+
sig { returns(T.class_of(PATH)) }
8+
let(:klass) { PATH }
9+
710
describe "#initialize" do
811
it "can take multiple arguments" do
9-
expect(described_class.new("/path1", "/path2")).to eq("/path1:/path2")
12+
expect(klass.new("/path1", "/path2")).to eq("/path1:/path2")
1013
end
1114

1215
it "can parse a mix of arrays and arguments" do
13-
expect(described_class.new(["/path1", "/path2"], "/path3")).to eq("/path1:/path2:/path3")
16+
expect(klass.new(["/path1", "/path2"], "/path3")).to eq("/path1:/path2:/path3")
1417
end
1518

1619
it "splits an existing PATH" do
17-
expect(described_class.new("/path1:/path2")).to eq(["/path1", "/path2"])
20+
expect(klass.new("/path1:/path2")).to eq(["/path1", "/path2"])
1821
end
1922

2023
it "removes duplicates" do
21-
expect(described_class.new("/path1", "/path1")).to eq("/path1")
24+
expect(klass.new("/path1", "/path1")).to eq("/path1")
2225
end
2326
end
2427

2528
describe "#to_ary" do
2629
it "returns a PATH array" do
27-
expect(described_class.new("/path1", "/path2").to_ary).to eq(["/path1", "/path2"])
30+
expect(klass.new("/path1", "/path2").to_ary).to eq(["/path1", "/path2"])
2831
end
2932

3033
it "does not allow mutating the original" do
31-
path = described_class.new("/path1", "/path2")
34+
path = klass.new("/path1", "/path2")
3235
path.to_ary << "/path3"
3336

3437
expect(path).not_to include("/path3")
@@ -37,53 +40,53 @@
3740

3841
describe "#to_str" do
3942
it "returns a PATH string" do
40-
expect(described_class.new("/path1", "/path2").to_str).to eq("/path1:/path2")
43+
expect(klass.new("/path1", "/path2").to_str).to eq("/path1:/path2")
4144
end
4245
end
4346

4447
describe "#prepend" do
4548
specify(:aggregate_failures) do
46-
expect(described_class.new("/path1").prepend("/path2").to_str).to eq("/path2:/path1")
47-
expect(described_class.new("/path1").prepend("/path1").to_str).to eq("/path1")
49+
expect(klass.new("/path1").prepend("/path2").to_str).to eq("/path2:/path1")
50+
expect(klass.new("/path1").prepend("/path1").to_str).to eq("/path1")
4851
end
4952
end
5053

5154
describe "#append" do
5255
specify(:aggregate_failures) do
53-
expect(described_class.new("/path1").append("/path2").to_str).to eq("/path1:/path2")
54-
expect(described_class.new("/path1").append("/path1").to_str).to eq("/path1")
56+
expect(klass.new("/path1").append("/path2").to_str).to eq("/path1:/path2")
57+
expect(klass.new("/path1").append("/path1").to_str).to eq("/path1")
5558
end
5659
end
5760

5861
describe "#insert" do
5962
specify(:aggregate_failures) do
60-
expect(described_class.new("/path1").insert(0, "/path2").to_str).to eq("/path2:/path1")
61-
expect(described_class.new("/path1").insert(0, "/path2", "/path3")).to eq("/path2:/path3:/path1")
63+
expect(klass.new("/path1").insert(0, "/path2").to_str).to eq("/path2:/path1")
64+
expect(klass.new("/path1").insert(0, "/path2", "/path3")).to eq("/path2:/path3:/path1")
6265
end
6366
end
6467

6568
describe "#==" do
6669
it "always returns false when comparing against something which does not respond to `#to_ary` or `#to_str`" do
67-
expect(described_class.new).not_to eq Object.new
70+
expect(klass.new).not_to eq Object.new
6871
end
6972
end
7073

7174
describe "#include?" do
7275
it "returns true if a path is included", :aggregate_failures do
73-
path = described_class.new("/path1", "/path2")
76+
path = klass.new("/path1", "/path2")
7477
expect(path).to include("/path1")
7578
expect(path).to include("/path2")
76-
expect(described_class.new("/path1", "/path2")).not_to include("/path1:")
79+
expect(klass.new("/path1", "/path2")).not_to include("/path1:")
7780
end
7881

7982
it "returns false if a path is not included" do
80-
expect(described_class.new("/path1")).not_to include("/path2")
83+
expect(klass.new("/path1")).not_to include("/path2")
8184
end
8285
end
8386

8487
describe "#each" do
8588
it "loops through each path" do
86-
enum = described_class.new("/path1", "/path2").each
89+
enum = klass.new("/path1", "/path2").each
8790

8891
expect(enum.next).to eq("/path1")
8992
expect(enum.next).to eq("/path2")
@@ -92,13 +95,13 @@
9295

9396
describe "#select" do
9497
it "returns an object of the same class instead of an Array" do
95-
expect(described_class.new.select { true }).to be_a(described_class)
98+
expect(klass.new.select { true }).to be_a(klass)
9699
end
97100
end
98101

99102
describe "#reject" do
100103
it "returns an object of the same class instead of an Array" do
101-
expect(described_class.new.reject { true }).to be_a(described_class)
104+
expect(klass.new.reject { true }).to be_a(klass)
102105
end
103106
end
104107

@@ -107,13 +110,15 @@
107110
allow(File).to receive(:directory?).with("/path1").and_return(true)
108111
allow(File).to receive(:directory?).with("/path2").and_return(false)
109112

110-
path = described_class.new("/path1", "/path2")
111-
expect(path.existing.to_ary).to eq(["/path1"])
113+
path = klass.new("/path1", "/path2")
114+
existing = path.existing
115+
expect(existing).not_to be_nil
116+
expect(existing&.to_ary).to eq(["/path1"])
112117
expect(path.to_ary).to eq(["/path1", "/path2"])
113118
end
114119

115-
it "returns nil instead of an empty #{described_class}" do
116-
expect(described_class.new.existing).to be_nil
120+
it "returns nil instead of an empty #{PATH}" do
121+
expect(klass.new.existing).to be_nil
117122
end
118123
end
119124
end

Library/Homebrew/test/abstract_command_spec.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
require "abstract_command"
55

66
RSpec.describe Homebrew::AbstractCommand do
7+
let(:klass) { Homebrew::AbstractCommand }
8+
79
describe "subclasses" do
810
before do
9-
test_cat = Class.new(described_class) do
11+
test_cat = Class.new(klass) do
1012
cmd_args do
1113
description "test"
1214
switch "--foo"
@@ -37,7 +39,7 @@ def run; end
3739
end
3840

3941
it "can lookup command" do
40-
expect(described_class.command("test-cat")).to be(TestCat)
42+
expect(klass.command("test-cat")).to be(TestCat)
4143
end
4244

4345
it "removes -cmd suffix from command name" do
@@ -47,15 +49,15 @@ def run; end
4749

4850
describe "when command name is overridden" do
4951
before do
50-
tac = Class.new(described_class) do
52+
tac = Class.new(klass) do
5153
def self.command_name = "t-a-c"
5254
def run; end
5355
end
5456
stub_const("Tac", tac)
5557
end
5658

5759
it "can be looked up by command name" do
58-
expect(described_class.command("t-a-c")).to be(Tac)
60+
expect(klass.command("t-a-c")).to be(Tac)
5961
end
6062
end
6163
end
@@ -67,7 +69,7 @@ def run; end
6769
Dir[File.join(__dir__, "../#{dir}", "*.rb")].each do |file|
6870
filename = File.basename(file, ".rb")
6971
require(file)
70-
command = described_class.command(filename)
72+
command = klass.command(filename)
7173
expect(Pathname(File.join(__dir__, "../#{dir}/#{command.command_name}.rb"))).to exist
7274
end
7375
end

Library/Homebrew/test/abstract_subcommand_spec.rb

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
require "abstract_subcommand"
66

77
RSpec.describe Homebrew::AbstractSubcommand do
8+
let(:klass) { Homebrew::AbstractSubcommand }
9+
810
describe "subclasses" do
911
before do
10-
subcommand = Class.new(described_class) do
12+
subcommand = Class.new(klass) do
1113
subcommand_args aliases: ["ts"], default: true do
1214
usage_banner <<~EOS
1315
`brew test`:
@@ -39,33 +41,33 @@ def run; end
3941
end
4042

4143
it "finds subcommands nested under a command class" do
42-
nested_subcommand = Class.new(described_class) do
44+
nested_subcommand = Class.new(klass) do
4345
subcommand_args { named_args :none }
4446
def run; end
4547
end
4648
stub_const("SubcommandTestCmd::NestedSubcommand", nested_subcommand)
4749
stub_const("OtherSubcommandTestCmd", Class.new(Homebrew::AbstractCommand))
48-
other_subcommand = Class.new(described_class) do
50+
other_subcommand = Class.new(klass) do
4951
subcommand_args { named_args :none }
5052
def run; end
5153
end
5254
stub_const("OtherSubcommandTestCmd::NestedSubcommand", other_subcommand)
5355

54-
expect(described_class.subcommands_for(SubcommandTestCmd)).to include(nested_subcommand)
55-
expect(described_class.subcommands_for(SubcommandTestCmd)).not_to include(other_subcommand)
56+
expect(klass.subcommands_for(SubcommandTestCmd)).to include(nested_subcommand)
57+
expect(klass.subcommands_for(SubcommandTestCmd)).not_to include(other_subcommand)
5658
end
5759

5860
it "defines all subcommands nested under a command class" do
59-
stub_const("SubcommandTestCmd::FirstSubcommand", Class.new(described_class) do
61+
stub_const("SubcommandTestCmd::FirstSubcommand", Class.new(klass) do
6062
subcommand_args { named_args :none }
6163
def run; end
6264
end)
63-
stub_const("SubcommandTestCmd::SecondSubcommand", Class.new(described_class) do
65+
stub_const("SubcommandTestCmd::SecondSubcommand", Class.new(klass) do
6466
subcommand_args { named_args :none }
6567
def run; end
6668
end)
6769

68-
abstract_subcommand = described_class
70+
abstract_subcommand = klass
6971
parser = Homebrew::CLI::Parser.new(SubcommandTestCmd) do
7072
abstract_subcommand.define_all(self, command: SubcommandTestCmd)
7173
end

Library/Homebrew/test/api/cask/cask_struct_generator_spec.rb

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
require "api"
55

66
RSpec.describe Homebrew::API::Cask::CaskStructGenerator do
7+
let(:klass) { Homebrew::API::Cask::CaskStructGenerator }
8+
79
describe ":process_depends_on" do
810
let(:depends_on_non_macos) do
911
{
@@ -18,13 +20,13 @@
1820
let(:depends_on_maximum_macos) { { maximum_macos: MacOSRequirement.new([:sequoia], comparator: "<=") } }
1921

2022
specify :aggregate_failures do
21-
expect(described_class.process_depends_on(depends_on_non_macos)).to eq({ arch: :intel, formula: ["foo"] })
22-
expect(described_class.process_depends_on(depends_on_linux)).to eq({ linux: :any })
23-
expect(described_class.process_depends_on(depends_on_macos_equals)).to eq({ macos: [:sequoia] })
24-
expect(described_class.process_depends_on(depends_on_macos_greater)).to eq({ macos: :sequoia })
25-
expect(described_class.process_depends_on(depends_on_macos_bare)).to eq({ macos: :any })
26-
expect(described_class.process_depends_on({ macos: {} })).to eq({ macos: :any })
27-
expect(described_class.process_depends_on(depends_on_maximum_macos)).to eq({ maximum_macos: :sequoia })
23+
expect(klass.process_depends_on(depends_on_non_macos)).to eq({ arch: :intel, formula: ["foo"] })
24+
expect(klass.process_depends_on(depends_on_linux)).to eq({ linux: :any })
25+
expect(klass.process_depends_on(depends_on_macos_equals)).to eq({ macos: [:sequoia] })
26+
expect(klass.process_depends_on(depends_on_macos_greater)).to eq({ macos: :sequoia })
27+
expect(klass.process_depends_on(depends_on_macos_bare)).to eq({ macos: :any })
28+
expect(klass.process_depends_on({ macos: {} })).to eq({ macos: :any })
29+
expect(klass.process_depends_on(depends_on_maximum_macos)).to eq({ maximum_macos: :sequoia })
2830
end
2931
end
3032

@@ -41,7 +43,7 @@
4143
[:bar, ["arg1", "arg2"], { kwarg1: "value1" }, nil],
4244
[:baz, [], { kwarg1: "value1" }, nil],
4345
]
44-
output = described_class.process_artifacts(input)
46+
output = klass.process_artifacts(input)
4547
expect(output).to eq expected_output
4648
end
4749

@@ -57,7 +59,7 @@
5759
using: :curl,
5860
bar: "baz",
5961
}
60-
output = described_class.process_url_specs(input)
62+
output = klass.process_url_specs(input)
6163
expect(output).to eq expected_output
6264
end
6365
end

Library/Homebrew/test/api/cask_spec.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
require "api"
55

66
RSpec.describe Homebrew::API::Cask do
7+
let(:klass) { Homebrew::API::Cask }
8+
79
let(:cache_dir) { mktmpdir }
810

911
before do
@@ -40,7 +42,7 @@ def mock_curl_download(stdout:)
4042

4143
it "returns the expected cask JSON list" do
4244
mock_curl_download stdout: casks_json
43-
casks_output = described_class.all_casks
45+
casks_output = klass.all_casks
4446
expect(casks_output).to eq casks_hash
4547
end
4648
end
@@ -68,7 +70,7 @@ def mock_curl_download(stdout:)
6870
Checksum.new("00ae1ae330365f3d6e4387776f67a9c4b096da3d4546bd0827b5dcafa985234e"),
6971
any_args,
7072
).and_call_original
71-
described_class.source_download(cask)
73+
klass.source_download(cask)
7274
end
7375
end
7476
end

0 commit comments

Comments
 (0)