Skip to content

Commit ab41874

Browse files
committed
Rewrite Source::Rubygems#clear_cache tests without ivar peeking
Replace `instance_variable_get` assertions with behavior-driven checks: object identity confirms memoization is invalidated for all four cached indexes, and a stubbed `Bundler.rubygems.installed_specs` verifies that fresh data is surfaced after the cache is cleared.
1 parent 21a4cc4 commit ab41874

1 file changed

Lines changed: 22 additions & 25 deletions

File tree

spec/bundler/source/rubygems_spec.rb

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -46,44 +46,41 @@
4646
end
4747

4848
describe "#clear_cache" do
49-
it "clears the installed_specs cache" do
49+
it "invalidates memoized indexes so subsequent reads rebuild them" do
5050
source = described_class.new
5151

52-
# Access installed_specs to populate the cache
53-
source.send(:installed_specs)
54-
expect(source.instance_variable_get(:@installed_specs)).not_to be_nil
52+
first_specs = source.specs
53+
first_installed = source.send(:installed_specs)
54+
first_default = source.send(:default_specs)
55+
first_cached = source.send(:cached_specs)
5556

56-
# Expire the cache
57-
source.clear_cache
58-
59-
# Cache should be cleared
60-
expect(source.instance_variable_get(:@installed_specs)).to be_nil
61-
end
57+
expect(source.specs).to equal(first_specs)
58+
expect(source.send(:installed_specs)).to equal(first_installed)
59+
expect(source.send(:default_specs)).to equal(first_default)
60+
expect(source.send(:cached_specs)).to equal(first_cached)
6261

63-
it "clears the default_specs cache" do
64-
source = described_class.new
65-
66-
# Access default_specs to populate the cache
67-
source.send(:default_specs)
68-
expect(source.instance_variable_get(:@default_specs)).not_to be_nil
69-
70-
# Expire the cache
7162
source.clear_cache
7263

73-
# Cache should be cleared
74-
expect(source.instance_variable_get(:@default_specs)).to be_nil
64+
expect(source.specs).not_to equal(first_specs)
65+
expect(source.send(:installed_specs)).not_to equal(first_installed)
66+
expect(source.send(:default_specs)).not_to equal(first_default)
67+
expect(source.send(:cached_specs)).not_to equal(first_cached)
7568
end
7669

77-
it "clears the merged specs cache" do
70+
it "reflects newly-discovered installed gems after clear_cache" do
7871
source = described_class.new
72+
foo = Gem::Specification.new("foo", "1.0.0")
73+
bar = Gem::Specification.new("bar", "1.0.0")
74+
75+
allow(Bundler.rubygems).to receive(:installed_specs).and_return([foo])
76+
expect(source.send(:installed_specs).search("bar")).to be_empty
7977

80-
source.instance_variable_set(:@specs, Bundler::Index.new)
81-
source.instance_variable_set(:@cached_specs, Bundler::Index.new)
78+
allow(Bundler.rubygems).to receive(:installed_specs).and_return([foo, bar])
79+
expect(source.send(:installed_specs).search("bar")).to be_empty
8280

8381
source.clear_cache
8482

85-
expect(source.instance_variable_get(:@specs)).to be_nil
86-
expect(source.instance_variable_get(:@cached_specs)).to be_nil
83+
expect(source.send(:installed_specs).search("bar")).not_to be_empty
8784
end
8885
end
8986

0 commit comments

Comments
 (0)