diff options
| author | Ian Ker-Seymer <hello@ianks.com> | 2024-04-24 22:52:52 -0400 |
|---|---|---|
| committer | git <svn-admin@ruby-lang.org> | 2026-02-13 06:28:11 +0000 |
| commit | 27204dc8ccd028d4c670be572d2c70abf41f1ce2 (patch) | |
| tree | 91bff6e66d4bfb829721ed07b4785953e77a51aa | |
| parent | 5d40a6d5875cd740912e115eb6cfa869bb9c875f (diff) | |
[ruby/rubygems] [rust gem] Make cargo test work by default
https://github.com/ruby/rubygems/commit/9420974db7
| -rw-r--r-- | lib/bundler/cli/gem.rb | 1 | ||||
| -rw-r--r-- | lib/bundler/templates/newgem/ext/newgem/Cargo.toml.tt | 7 | ||||
| -rw-r--r-- | lib/bundler/templates/newgem/ext/newgem/build.rs.tt | 5 | ||||
| -rw-r--r-- | lib/bundler/templates/newgem/ext/newgem/src/lib.rs.tt | 11 | ||||
| -rw-r--r-- | spec/bundler/commands/newgem_spec.rb | 23 | ||||
| -rw-r--r-- | spec/bundler/support/filters.rb | 1 |
6 files changed, 48 insertions, 0 deletions
diff --git a/lib/bundler/cli/gem.rb b/lib/bundler/cli/gem.rb index e681326401..0ff0f5e11f 100644 --- a/lib/bundler/cli/gem.rb +++ b/lib/bundler/cli/gem.rb @@ -228,6 +228,7 @@ module Bundler templates.merge!( "Cargo.toml.tt" => "Cargo.toml", "ext/newgem/Cargo.toml.tt" => "ext/#{name}/Cargo.toml", + "ext/newgem/build.rs.tt" => "ext/#{name}/build.rs", "ext/newgem/extconf-rust.rb.tt" => "ext/#{name}/extconf.rb", "ext/newgem/src/lib.rs.tt" => "ext/#{name}/src/lib.rs", ) diff --git a/lib/bundler/templates/newgem/ext/newgem/Cargo.toml.tt b/lib/bundler/templates/newgem/ext/newgem/Cargo.toml.tt index c0dc63fbfa..142957a348 100644 --- a/lib/bundler/templates/newgem/ext/newgem/Cargo.toml.tt +++ b/lib/bundler/templates/newgem/ext/newgem/Cargo.toml.tt @@ -13,3 +13,10 @@ crate-type = ["cdylib"] [dependencies] magnus = { version = "0.8.2" } +rb-sys = { version = "0.9", features = ["stable-api-compiled-fallback"] } + +[build-dependencies] +rb-sys-env = "0.1.2" + +[dev-dependencies] +rb-sys-test-helpers = { version = "0.2.0" } diff --git a/lib/bundler/templates/newgem/ext/newgem/build.rs.tt b/lib/bundler/templates/newgem/ext/newgem/build.rs.tt new file mode 100644 index 0000000000..8d812a4a3c --- /dev/null +++ b/lib/bundler/templates/newgem/ext/newgem/build.rs.tt @@ -0,0 +1,5 @@ +pub fn main() -> Result<(), Box<dyn std::error::Error>> { + let _rb_env = rb_sys_env::activate()?; + + Ok(()) +} diff --git a/lib/bundler/templates/newgem/ext/newgem/src/lib.rs.tt b/lib/bundler/templates/newgem/ext/newgem/src/lib.rs.tt index ba234529a3..8f32a93ab8 100644 --- a/lib/bundler/templates/newgem/ext/newgem/src/lib.rs.tt +++ b/lib/bundler/templates/newgem/ext/newgem/src/lib.rs.tt @@ -10,3 +10,14 @@ fn init(ruby: &Ruby) -> Result<(), Error> { module.define_singleton_method("hello", function!(hello, 1))?; Ok(()) } + +#[cfg(test)] +mod tests { + use rb_sys_test_helpers::ruby_test; + + #[ruby_test] + fn test_hello() { + use magnus::RString; + assert_eq!(12, RString::new("rust ❤️ ruby").length()) + } +} diff --git a/spec/bundler/commands/newgem_spec.rb b/spec/bundler/commands/newgem_spec.rb index 26e27968b3..c078ed84aa 100644 --- a/spec/bundler/commands/newgem_spec.rb +++ b/spec/bundler/commands/newgem_spec.rb @@ -1733,6 +1733,7 @@ RSpec.describe "bundle gem" do expect(bundled_app("#{gem_name}/ext/#{gem_name}/Cargo.toml")).to exist expect(bundled_app("#{gem_name}/ext/#{gem_name}/extconf.rb")).to exist expect(bundled_app("#{gem_name}/ext/#{gem_name}/src/lib.rs")).to exist + expect(bundled_app("#{gem_name}/ext/#{gem_name}/build.rs")).to exist end it "includes rake-compiler, rb_sys gems and required_rubygems_version constraint" do @@ -1761,6 +1762,28 @@ RSpec.describe "bundle gem" do expect(bundled_app("#{gem_name}/Rakefile").read).to eq(rakefile) end + + it "configures the crate such that `cargo test` works", :ruby_repo, :mri_only do + env = setup_rust_env + gem_path = bundled_app(gem_name) + result = sys_exec("cargo test", env: env, dir: gem_path) + + expect(result).to include("1 passed") + end + + def setup_rust_env + skip "rust toolchain of mingw is broken" if RUBY_PLATFORM.match?("mingw") + + env = { + "CARGO_HOME" => ENV.fetch("CARGO_HOME", File.join(ENV["HOME"], ".cargo")), + "RUSTUP_HOME" => ENV.fetch("RUSTUP_HOME", File.join(ENV["HOME"], ".rustup")), + "RUSTUP_TOOLCHAIN" => ENV.fetch("RUSTUP_TOOLCHAIN", "stable"), + } + + system(env, "cargo", "-V", out: IO::NULL, err: [:child, :out]) + skip "cargo not present" unless $?.success? + env + end end context "--ext parameter set with go" do diff --git a/spec/bundler/support/filters.rb b/spec/bundler/support/filters.rb index 6e94b10e32..c6b60b5d52 100644 --- a/spec/bundler/support/filters.rb +++ b/spec/bundler/support/filters.rb @@ -29,6 +29,7 @@ RSpec.configure do |config| config.filter_run_excluding jruby_only: RUBY_ENGINE != "jruby" config.filter_run_excluding truffleruby_only: RUBY_ENGINE != "truffleruby" config.filter_run_excluding man: Gem.win_platform? + config.filter_run_excluding mri_only: RUBY_ENGINE != "ruby" config.filter_run_when_matching :focus unless ENV["CI"] |
