2016-02-01 07:43:26 -05:00
|
|
|
# frozen_string_literal: true
|
2021-05-12 05:41:32 -04:00
|
|
|
require_relative "test_case"
|
2013-07-09 19:21:36 -04:00
|
|
|
require "rubygems/stub_specification"
|
|
|
|
|
|
|
|
class TestStubSpecification < Gem::TestCase
|
2020-10-15 00:25:27 -04:00
|
|
|
FOO = File.expand_path File.join("specifications", "foo-0.0.1-x86-mswin32.gemspec"), __dir__
|
|
|
|
BAR = File.expand_path File.join("specifications", "bar-0.0.2.gemspec"), __dir__
|
2013-07-09 19:21:36 -04:00
|
|
|
|
2013-10-15 20:14:16 -04:00
|
|
|
def setup
|
|
|
|
super
|
|
|
|
|
2020-10-15 00:25:27 -04:00
|
|
|
@base_dir = __dir__
|
|
|
|
@gems_dir = File.join __dir__, 'gem'
|
2015-11-11 23:50:06 -05:00
|
|
|
@foo = Gem::StubSpecification.gemspec_stub FOO, @base_dir, @gems_dir
|
2013-10-15 20:14:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_initialize
|
|
|
|
assert_equal "foo", @foo.name
|
|
|
|
assert_equal Gem::Version.new("0.0.1"), @foo.version
|
|
|
|
assert_equal Gem::Platform.new("mswin32"), @foo.platform
|
|
|
|
assert_equal ["lib", "lib/f oo/ext"], @foo.require_paths
|
2013-10-18 17:56:18 -04:00
|
|
|
assert @foo.stubbed?
|
2013-10-15 20:14:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_initialize_extension
|
|
|
|
stub = stub_with_extension
|
|
|
|
|
2014-09-13 23:30:02 -04:00
|
|
|
assert_equal 'stub_e', stub.name
|
|
|
|
assert_equal v(2), stub.version
|
|
|
|
assert_equal Gem::Platform::RUBY, stub.platform
|
|
|
|
assert_equal [stub.extension_dir, 'lib'], stub.require_paths
|
|
|
|
assert_equal %w[ext/stub_e/extconf.rb], stub.extensions
|
2013-07-09 19:21:36 -04:00
|
|
|
end
|
|
|
|
|
2017-01-23 21:38:57 -05:00
|
|
|
def test_initialize_version
|
|
|
|
stub = stub_with_version
|
|
|
|
|
|
|
|
assert_equal 'stub_v', stub.name
|
|
|
|
assert_equal v(2), stub.version
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_initialize_with_empty_version
|
|
|
|
stub = stub_without_version
|
|
|
|
|
|
|
|
assert_equal 'stub_v', stub.name
|
|
|
|
assert_equal v(0), stub.version
|
|
|
|
end
|
|
|
|
|
2013-10-15 20:14:16 -04:00
|
|
|
def test_initialize_missing_stubline
|
2015-11-11 23:50:06 -05:00
|
|
|
stub = Gem::StubSpecification.gemspec_stub(BAR, @base_dir, @gems_dir)
|
2013-07-09 19:21:36 -04:00
|
|
|
assert_equal "bar", stub.name
|
|
|
|
assert_equal Gem::Version.new("0.0.2"), stub.version
|
|
|
|
assert_equal Gem::Platform.new("ruby"), stub.platform
|
|
|
|
assert_equal ["lib"], stub.require_paths
|
2013-10-18 17:56:18 -04:00
|
|
|
assert !stub.stubbed?
|
2013-07-09 19:21:36 -04:00
|
|
|
end
|
|
|
|
|
2013-10-15 20:14:16 -04:00
|
|
|
def test_contains_requirable_file_eh
|
|
|
|
stub = stub_without_extension
|
|
|
|
code_rb = File.join stub.gem_dir, 'lib', 'code.rb'
|
|
|
|
FileUtils.mkdir_p File.dirname code_rb
|
|
|
|
FileUtils.touch code_rb
|
|
|
|
|
|
|
|
assert stub.contains_requirable_file? 'code'
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_contains_requirable_file_eh_extension
|
2019-06-01 05:45:11 -04:00
|
|
|
skip "I guess making the stub match the running platform should work" if Gem.java_platform?
|
2013-10-15 20:14:16 -04:00
|
|
|
stub_with_extension do |stub|
|
2019-06-29 07:16:49 -04:00
|
|
|
_, err = capture_output do
|
2014-09-13 23:30:02 -04:00
|
|
|
refute stub.contains_requirable_file? 'nonexistent'
|
2013-10-15 20:14:16 -04:00
|
|
|
end
|
|
|
|
|
2017-10-07 21:32:18 -04:00
|
|
|
expected = "Ignoring stub_e-2 because its extensions are not built. " +
|
2014-12-06 19:53:01 -05:00
|
|
|
"Try: gem pristine stub_e --version 2\n"
|
2013-10-15 20:14:16 -04:00
|
|
|
|
2014-09-13 23:30:02 -04:00
|
|
|
assert_equal expected, err
|
2013-10-15 20:14:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_full_require_paths
|
|
|
|
stub = stub_with_extension
|
|
|
|
|
|
|
|
expected = [
|
2013-11-18 19:34:13 -05:00
|
|
|
File.join(stub.full_gem_path, 'lib'),
|
2015-02-05 08:42:45 -05:00
|
|
|
stub.extension_dir,
|
2013-10-15 20:14:16 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
assert_equal expected, stub.full_require_paths
|
|
|
|
end
|
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
def test_lib_dirs_glob
|
|
|
|
stub = stub_without_extension
|
|
|
|
|
|
|
|
assert_equal File.join(stub.full_gem_path, 'lib'), stub.lib_dirs_glob
|
|
|
|
end
|
|
|
|
|
2017-10-07 21:32:18 -04:00
|
|
|
def test_lib_dirs_glob_with_extension
|
|
|
|
stub = stub_with_extension
|
|
|
|
|
|
|
|
assert_equal File.join(stub.full_gem_path, 'lib'), stub.lib_dirs_glob
|
|
|
|
end
|
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
def test_matches_for_glob
|
|
|
|
stub = stub_without_extension
|
|
|
|
code_rb = File.join stub.gem_dir, 'lib', 'code.rb'
|
|
|
|
FileUtils.mkdir_p File.dirname code_rb
|
|
|
|
FileUtils.touch code_rb
|
|
|
|
|
|
|
|
assert_equal code_rb, stub.matches_for_glob('code*').first
|
|
|
|
end
|
|
|
|
|
2017-10-20 05:03:37 -04:00
|
|
|
def test_matches_for_glob_with_bundler_inline
|
|
|
|
stub = stub_with_extension
|
|
|
|
code_rb = File.join stub.gem_dir, 'lib', 'code.rb'
|
|
|
|
FileUtils.mkdir_p File.dirname code_rb
|
|
|
|
FileUtils.touch code_rb
|
|
|
|
|
|
|
|
stub.stub(:raw_require_paths, nil) do
|
|
|
|
assert_equal code_rb, stub.matches_for_glob('code*').first
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-13 23:30:02 -04:00
|
|
|
def test_missing_extensions_eh
|
2019-06-01 05:45:11 -04:00
|
|
|
skip "I guess making the stub match the running platform should work" if Gem.java_platform?
|
2014-09-13 23:30:02 -04:00
|
|
|
stub = stub_with_extension do |s|
|
|
|
|
extconf_rb = File.join s.gem_dir, s.extensions.first
|
|
|
|
FileUtils.mkdir_p File.dirname extconf_rb
|
|
|
|
|
2018-02-05 21:58:35 -05:00
|
|
|
File.open extconf_rb, 'w' do |f|
|
2014-09-13 23:30:02 -04:00
|
|
|
f.write <<-'RUBY'
|
2018-02-05 21:58:35 -05:00
|
|
|
File.open 'Makefile', 'w' do |f|
|
2014-09-13 23:30:02 -04:00
|
|
|
f.puts "clean:\n\techo clean"
|
|
|
|
f.puts "default:\n\techo built"
|
|
|
|
f.puts "install:\n\techo installed"
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert stub.missing_extensions?
|
|
|
|
|
|
|
|
stub.build_extensions
|
|
|
|
|
|
|
|
refute stub.missing_extensions?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_missing_extensions_eh_default_gem
|
|
|
|
spec = new_default_spec 'default', 1
|
|
|
|
spec.extensions << 'extconf.rb'
|
|
|
|
|
2018-02-05 21:58:35 -05:00
|
|
|
File.open spec.loaded_from, 'w' do |io|
|
2014-09-13 23:30:02 -04:00
|
|
|
io.write spec.to_ruby_for_cache
|
|
|
|
end
|
|
|
|
|
2015-11-11 23:50:06 -05:00
|
|
|
default_spec = Gem::StubSpecification.gemspec_stub spec.loaded_from, spec.base_dir, spec.gems_dir
|
2014-09-13 23:30:02 -04:00
|
|
|
|
|
|
|
refute default_spec.missing_extensions?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_missing_extensions_eh_none
|
|
|
|
refute @foo.missing_extensions?
|
|
|
|
end
|
|
|
|
|
2013-07-09 19:21:36 -04:00
|
|
|
def test_to_spec
|
2014-09-13 23:30:02 -04:00
|
|
|
real_foo = util_spec @foo.name, @foo.version
|
|
|
|
real_foo.activate
|
|
|
|
|
|
|
|
assert_equal @foo.version, Gem.loaded_specs[@foo.name].version,
|
|
|
|
'sanity check'
|
|
|
|
|
|
|
|
assert_same real_foo, @foo.to_spec
|
|
|
|
end
|
|
|
|
|
2014-12-06 19:53:01 -05:00
|
|
|
def test_to_spec_with_other_specs_loaded_does_not_warn
|
|
|
|
real_foo = util_spec @foo.name, @foo.version
|
|
|
|
real_foo.activate
|
2015-11-11 23:50:06 -05:00
|
|
|
bar = Gem::StubSpecification.gemspec_stub BAR, real_foo.base_dir, real_foo.gems_dir
|
2014-12-06 19:53:01 -05:00
|
|
|
refute_predicate Gem.loaded_specs, :empty?
|
|
|
|
assert bar.to_spec
|
|
|
|
end
|
|
|
|
|
2014-09-13 23:30:02 -04:00
|
|
|
def test_to_spec_activated
|
2013-10-15 20:14:16 -04:00
|
|
|
assert @foo.to_spec.is_a?(Gem::Specification)
|
|
|
|
assert_equal "foo", @foo.to_spec.name
|
2015-11-11 23:50:06 -05:00
|
|
|
refute @foo.to_spec.instance_variable_get :@ignored
|
2014-09-13 23:30:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_to_spec_missing_extensions
|
|
|
|
stub = stub_with_extension
|
|
|
|
|
2019-06-29 07:16:49 -04:00
|
|
|
capture_output do
|
2014-09-13 23:30:02 -04:00
|
|
|
stub.contains_requirable_file? 'nonexistent'
|
|
|
|
end
|
|
|
|
|
|
|
|
assert stub.to_spec.instance_variable_get :@ignored
|
2013-07-09 19:21:36 -04:00
|
|
|
end
|
2013-10-15 20:14:16 -04:00
|
|
|
|
2017-01-23 21:38:57 -05:00
|
|
|
def stub_with_version
|
|
|
|
spec = File.join @gemhome, 'specifications', 'stub_e-2.gemspec'
|
2018-02-05 21:58:35 -05:00
|
|
|
File.open spec, 'w' do |io|
|
2017-01-23 21:38:57 -05:00
|
|
|
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
|
|
|
|
STUB
|
|
|
|
|
|
|
|
io.flush
|
|
|
|
|
|
|
|
stub = Gem::StubSpecification.gemspec_stub io.path, @gemhome, File.join(@gemhome, 'gems')
|
|
|
|
|
|
|
|
yield stub if block_given?
|
|
|
|
|
|
|
|
return stub
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def stub_without_version
|
|
|
|
spec = File.join @gemhome, 'specifications', 'stub-2.gemspec'
|
2018-02-05 21:58:35 -05:00
|
|
|
File.open spec, 'w' do |io|
|
2017-01-23 21:38:57 -05:00
|
|
|
io.write <<-STUB
|
|
|
|
# -*- encoding: utf-8 -*-
|
|
|
|
# stub: stub_v ruby lib
|
|
|
|
|
|
|
|
Gem::Specification.new do |s|
|
|
|
|
s.name = 'stub_v'
|
|
|
|
s.version = ""
|
|
|
|
end
|
|
|
|
STUB
|
|
|
|
|
|
|
|
io.flush
|
|
|
|
|
|
|
|
stub = Gem::StubSpecification.gemspec_stub io.path, @gemhome, File.join(@gemhome, 'gems')
|
|
|
|
|
|
|
|
yield stub if block_given?
|
|
|
|
|
|
|
|
return stub
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-15 20:14:16 -04:00
|
|
|
def stub_with_extension
|
|
|
|
spec = File.join @gemhome, 'specifications', 'stub_e-2.gemspec'
|
2018-02-05 21:58:35 -05:00
|
|
|
File.open spec, 'w' do |io|
|
2013-10-15 20:14:16 -04:00
|
|
|
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']
|
2013-10-19 20:31:12 -04:00
|
|
|
s.installed_by_version = '2.2'
|
2013-07-09 19:21:36 -04:00
|
|
|
end
|
2013-10-15 20:14:16 -04:00
|
|
|
STUB
|
|
|
|
|
|
|
|
io.flush
|
|
|
|
|
2015-11-11 23:50:06 -05:00
|
|
|
stub = Gem::StubSpecification.gemspec_stub io.path, @gemhome, File.join(@gemhome, 'gems')
|
2013-10-15 20:14:16 -04:00
|
|
|
|
|
|
|
yield stub if block_given?
|
|
|
|
|
|
|
|
return stub
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def stub_without_extension
|
|
|
|
spec = File.join @gemhome, 'specifications', 'stub-2.gemspec'
|
2018-02-05 21:58:35 -05:00
|
|
|
File.open spec, 'w' do |io|
|
2013-10-15 20:14:16 -04:00
|
|
|
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
|
|
|
|
STUB
|
|
|
|
|
|
|
|
io.flush
|
|
|
|
|
2015-11-11 23:50:06 -05:00
|
|
|
stub = Gem::StubSpecification.gemspec_stub io.path, @gemhome, File.join(@gemhome, 'gems')
|
2013-10-15 20:14:16 -04:00
|
|
|
|
|
|
|
yield stub if block_given?
|
|
|
|
|
|
|
|
return stub
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|