summaryrefslogtreecommitdiff
path: root/test/rubygems/test_gem_ext_cargo_builder.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/rubygems/test_gem_ext_cargo_builder.rb')
-rw-r--r--test/rubygems/test_gem_ext_cargo_builder.rb117
1 files changed, 55 insertions, 62 deletions
diff --git a/test/rubygems/test_gem_ext_cargo_builder.rb b/test/rubygems/test_gem_ext_cargo_builder.rb
index 2da98c03e2..5faf3e2480 100644
--- a/test/rubygems/test_gem_ext_cargo_builder.rb
+++ b/test/rubygems/test_gem_ext_cargo_builder.rb
@@ -1,46 +1,28 @@
# frozen_string_literal: true
-require_relative 'helper'
-require 'rubygems/ext'
+require_relative "helper"
+require "rubygems/ext"
+require "open3"
class TestGemExtCargoBuilder < Gem::TestCase
def setup
super
@rust_envs = {
- 'CARGO_HOME' => ENV.fetch('CARGO_HOME', File.join(@orig_env['HOME'], '.cargo')),
- 'RUSTUP_HOME' => ENV.fetch('RUSTUP_HOME', File.join(@orig_env['HOME'], '.rustup')),
+ "CARGO_HOME" => ENV.fetch("CARGO_HOME", File.join(@orig_env["HOME"], ".cargo")),
+ "RUSTUP_HOME" => ENV.fetch("RUSTUP_HOME", File.join(@orig_env["HOME"], ".rustup")),
}
end
def setup_rust_gem(name)
- @ext = File.join(@tempdir, 'ext')
- @dest_path = File.join(@tempdir, 'prefix')
+ @ext = File.join(@tempdir, "ext")
+ @dest_path = File.join(@tempdir, "prefix")
@fixture_dir = Pathname.new(File.expand_path("test_gem_ext_cargo_builder/#{name}/", __dir__))
FileUtils.mkdir_p @dest_path
FileUtils.cp_r(@fixture_dir.to_s, @ext)
end
- def test_build_staticlib
- skip_unsupported_platforms!
- setup_rust_gem "rust_ruby_example"
-
- content = @fixture_dir.join('Cargo.toml').read.gsub("cdylib", "staticlib")
- File.write(File.join(@ext, 'Cargo.toml'), content)
-
- output = []
-
- Dir.chdir @ext do
- ENV.update(@rust_envs)
- spec = Gem::Specification.new 'rust_ruby_example', '0.1.0'
- builder = Gem::Ext::CargoBuilder.new(spec)
- assert_raise(Gem::Ext::CargoBuilder::DylibNotFoundError) do
- builder.build nil, @dest_path, output
- end
- end
- end
-
def test_build_cdylib
skip_unsupported_platforms!
setup_rust_gem "rust_ruby_example"
@@ -49,42 +31,43 @@ class TestGemExtCargoBuilder < Gem::TestCase
Dir.chdir @ext do
ENV.update(@rust_envs)
- spec = Gem::Specification.new 'rust_ruby_example', '0.1.0'
- builder = Gem::Ext::CargoBuilder.new(spec)
- builder.build nil, @dest_path, output
+ builder = Gem::Ext::CargoBuilder.new
+ builder.build "Cargo.toml", @dest_path, output
end
output = output.join "\n"
- bundle = File.join(@dest_path, "release/rust_ruby_example.#{RbConfig::CONFIG['DLEXT']}")
+ bundle = File.join(@dest_path, "rust_ruby_example.#{RbConfig::CONFIG["DLEXT"]}")
- assert_match "Finished release [optimized] target(s)", output
- assert_ffi_handle bundle, 'Init_rust_ruby_example'
- rescue Exception => e
+ assert_match(/Finished/, output)
+ assert_match(/release/, output)
+ assert_ffi_handle bundle, "Init_rust_ruby_example"
+ rescue StandardError => e
pp output if output
raise(e)
end
- def test_build_dev_profile
+ def test_rubygems_cfg_passed_to_rustc
skip_unsupported_platforms!
setup_rust_gem "rust_ruby_example"
-
+ version_slug = Gem::VERSION.tr(".", "_")
output = []
+ replace_in_rust_file("src/lib.rs", "rubygems_x_x_x", "rubygems_#{version_slug}")
+
Dir.chdir @ext do
ENV.update(@rust_envs)
- spec = Gem::Specification.new 'rust_ruby_example', '0.1.0'
- builder = Gem::Ext::CargoBuilder.new(spec)
- builder.profile = :dev
- builder.build nil, @dest_path, output
+ builder = Gem::Ext::CargoBuilder.new
+ builder.build "Cargo.toml", @dest_path, output
end
output = output.join "\n"
- bundle = File.join(@dest_path, "debug/rust_ruby_example.#{RbConfig::CONFIG['DLEXT']}")
+ bundle = File.join(@dest_path, "rust_ruby_example.#{RbConfig::CONFIG["DLEXT"]}")
- assert_match "Finished dev [unoptimized + debuginfo] target(s)", output
- assert_ffi_handle bundle, 'Init_rust_ruby_example'
- rescue Exception => e
+ assert_ffi_handle bundle, "hello_from_rubygems"
+ assert_ffi_handle bundle, "hello_from_rubygems_version"
+ refute_ffi_handle bundle, "should_never_exist"
+ rescue StandardError => e
pp output if output
raise(e)
@@ -94,35 +77,30 @@ class TestGemExtCargoBuilder < Gem::TestCase
skip_unsupported_platforms!
setup_rust_gem "rust_ruby_example"
- output = []
-
- FileUtils.rm(File.join(@ext, 'src/lib.rs'))
+ FileUtils.rm(File.join(@ext, "src/lib.rs"))
error = assert_raise(Gem::InstallError) do
Dir.chdir @ext do
ENV.update(@rust_envs)
- spec = Gem::Specification.new 'rust_ruby_example', '0.1.0'
- builder = Gem::Ext::CargoBuilder.new(spec)
- builder.build nil, @dest_path, output
+ builder = Gem::Ext::CargoBuilder.new
+ builder.build "Cargo.toml", @dest_path, []
end
end
- output = output.join "\n"
-
- assert_match 'cargo failed', error.message
+ assert_match(/cargo\s.*\sfailed/, error.message)
end
def test_full_integration
skip_unsupported_platforms!
setup_rust_gem "rust_ruby_example"
- require 'open3'
+ require "open3"
Dir.chdir @ext do
- require 'tmpdir'
+ require "tmpdir"
env_for_subprocess = @rust_envs.merge("GEM_HOME" => Gem.paths.home)
- gem = [env_for_subprocess, *ruby_with_rubygems_in_load_path, File.expand_path('../../bin/gem', __dir__)]
+ gem = [env_for_subprocess, *ruby_with_rubygems_in_load_path, File.expand_path("../../exe/gem", __dir__)]
Dir.mktmpdir("rust_ruby_example") do |dir|
built_gem = File.expand_path(File.join(dir, "rust_ruby_example.gem"))
@@ -141,10 +119,10 @@ class TestGemExtCargoBuilder < Gem::TestCase
setup_rust_gem "custom_name"
Dir.chdir @ext do
- require 'tmpdir'
+ require "tmpdir"
env_for_subprocess = @rust_envs.merge("GEM_HOME" => Gem.paths.home)
- gem = [env_for_subprocess, *ruby_with_rubygems_in_load_path, File.expand_path('../../bin/gem', __dir__)]
+ gem = [env_for_subprocess, *ruby_with_rubygems_in_load_path, File.expand_path("../../exe/gem", __dir__)]
Dir.mktmpdir("custom_name") do |dir|
built_gem = File.expand_path(File.join(dir, "custom_name.gem"))
@@ -162,17 +140,32 @@ class TestGemExtCargoBuilder < Gem::TestCase
private
def skip_unsupported_platforms!
- pend "jruby not supported" if java_platform?
- pend "truffleruby not supported (yet)" if RUBY_ENGINE == 'truffleruby'
- pend "mswin not supported (yet)" if /mswin/ =~ RUBY_PLATFORM && ENV.key?('GITHUB_ACTIONS')
- system(@rust_envs, 'cargo', '-V', out: IO::NULL, err: [:child, :out])
- pend 'cargo not present' unless $?.success?
+ pend "jruby not supported" if Gem.java_platform?
+ pend "truffleruby not supported (yet)" if RUBY_ENGINE == "truffleruby"
+ system(@rust_envs, "cargo", "-V", out: IO::NULL, err: [:child, :out])
+ pend "cargo not present" unless $?.success?
pend "ruby.h is not provided by ruby repo" if ruby_repo?
+ pend "rust toolchain of mingw is broken" if mingw_windows?
end
def assert_ffi_handle(bundle, name)
- require 'fiddle'
+ require "fiddle"
dylib_handle = Fiddle.dlopen bundle
assert_nothing_raised { dylib_handle[name] }
+ ensure
+ dylib_handle&.close
+ end
+
+ def refute_ffi_handle(bundle, name)
+ require "fiddle"
+ dylib_handle = Fiddle.dlopen bundle
+ assert_raise { dylib_handle[name] }
+ ensure
+ dylib_handle&.close
+ end
+
+ def replace_in_rust_file(name, from, to)
+ content = @fixture_dir.join(name).read.gsub(from, to)
+ File.write(File.join(@ext, name), content)
end
end