Skip to content

Commit

Permalink
Merge pull request #8117 from rubygems/deivid-rodriguez/gem-pristine-…
Browse files Browse the repository at this point in the history
…etc-twice

Fix `gem pristine etc` resetting gem twice sometimes
  • Loading branch information
deivid-rodriguez authored Oct 10, 2024
2 parents 24fed07 + 5c279ac commit 4241c72
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 43 deletions.
2 changes: 1 addition & 1 deletion lib/rubygems/basic_specification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def ignored?
end

def default_gem?
loaded_from &&
!loaded_from.nil? &&
File.dirname(loaded_from) == Gem.default_specifications_dir
end

Expand Down
21 changes: 11 additions & 10 deletions lib/rubygems/stub_specification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,7 @@ def initialize(filename, base_dir, gems_dir, default_gem)
# True when this gem has been activated

def activated?
@activated ||=
begin
loaded = Gem.loaded_specs[name]
loaded && loaded.version == version
end
@activated ||= !loaded_spec.nil?
end

def default_gem?
Expand Down Expand Up @@ -187,11 +183,7 @@ def full_name
# The full Gem::Specification for this gem, loaded from evalling its gemspec

def spec
@spec ||= if @data
loaded = Gem.loaded_specs[name]
loaded if loaded && loaded.version == version
end

@spec ||= loaded_spec if @data
@spec ||= Gem::Specification.load(loaded_from)
end
alias_method :to_spec, :spec
Expand Down Expand Up @@ -231,4 +223,13 @@ def <=>(other) # :nodoc:
def sort_obj # :nodoc:
[name, version, Gem::Platform.sort_priority(platform)]
end

private

def loaded_spec
spec = Gem.loaded_specs[name]
return unless spec && spec.version == version && spec.default_gem? == default_gem?

spec
end
end
93 changes: 61 additions & 32 deletions test/rubygems/test_gem_stub_specification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,35 @@ def test_to_spec
assert_same real_foo, @foo.to_spec
end

def test_to_spec_default
bar_default_spec = File.join(@gemhome, "specifications", "default", "bar-0.0.2.gemspec")
File.open bar_default_spec, "w" do |io|
io.write <<~STUB
# -*- encoding: utf-8 -*-
# stub: bar 0.0.2 ruby lib
Gem::Specification.new do |s|
s.name = "bar"
s.version = "0.0.2"
s.platform = "ruby"
s.require_paths = ["lib"]
s.summary = "A very bar gem"
end
STUB
end

bar = Gem::StubSpecification.gemspec_stub BAR, @base_dir, @gems_dir
bar_default = Gem::StubSpecification.default_gemspec_stub bar_default_spec, @base_dir, @gems_dir

real_bar = util_spec bar_default.name, bar_default.version
real_bar.activate

assert_equal bar.version, Gem.loaded_specs[bar.name].version,
"sanity check"

refute_same real_bar, bar_default.to_spec
end

def test_to_spec_with_other_specs_loaded_does_not_warn
real_foo = util_spec @foo.name, @foo.version
real_foo.activate
Expand All @@ -182,14 +211,14 @@ def test_to_spec_with_other_specs_loaded_does_not_warn
def stub_with_version
spec = File.join @gemhome, "specifications", "stub_e-2.gemspec"
File.open spec, "w" do |io|
io.write <<-STUB
# -*- encoding: utf-8 -*-
# stub: stub_v 2 ruby lib
io.write <<~STUB
# -*- encoding: utf-8 -*-
# stub: stub_v 2 ruby lib
Gem::Specification.new do |s|
s.name = 'stub_v'
s.version = Gem::Version.new '2'
end
Gem::Specification.new do |s|
s.name = 'stub_v'
s.version = Gem::Version.new '2'
end
STUB

io.flush
Expand All @@ -205,14 +234,14 @@ def stub_with_version
def stub_without_version
spec = File.join @gemhome, "specifications", "stub-2.gemspec"
File.open spec, "w" do |io|
io.write <<-STUB
# -*- encoding: utf-8 -*-
# stub: stub_v ruby lib
io.write <<~STUB
# -*- encoding: utf-8 -*-
# stub: stub_v ruby lib
Gem::Specification.new do |s|
s.name = 'stub_v'
s.version = ""
end
Gem::Specification.new do |s|
s.name = 'stub_v'
s.version = ""
end
STUB

io.flush
Expand All @@ -228,17 +257,17 @@ def stub_without_version
def stub_with_extension
spec = File.join @gemhome, "specifications", "stub_e-2.gemspec"
File.open spec, "w" do |io|
io.write <<-STUB
# -*- encoding: utf-8 -*-
# stub: stub_e 2 ruby lib
# stub: ext/stub_e/extconf.rb
Gem::Specification.new do |s|
s.name = 'stub_e'
s.version = Gem::Version.new '2'
s.extensions = ['ext/stub_e/extconf.rb']
s.installed_by_version = '2.2'
end
io.write <<~STUB
# -*- encoding: utf-8 -*-
# stub: stub_e 2 ruby lib
# stub: ext/stub_e/extconf.rb
Gem::Specification.new do |s|
s.name = 'stub_e'
s.version = Gem::Version.new '2'
s.extensions = ['ext/stub_e/extconf.rb']
s.installed_by_version = '2.2'
end
STUB

io.flush
Expand All @@ -254,14 +283,14 @@ def stub_with_extension
def stub_without_extension
spec = File.join @gemhome, "specifications", "stub-2.gemspec"
File.open spec, "w" do |io|
io.write <<-STUB
# -*- encoding: utf-8 -*-
# stub: stub 2 ruby lib
io.write <<~STUB
# -*- encoding: utf-8 -*-
# stub: stub 2 ruby lib
Gem::Specification.new do |s|
s.name = 'stub'
s.version = Gem::Version.new '2'
end
Gem::Specification.new do |s|
s.name = 'stub'
s.version = Gem::Version.new '2'
end
STUB

io.flush
Expand Down

0 comments on commit 4241c72

Please sign in to comment.