Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rust gem] Make cargo test work by default #7608

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Manifest.txt
Expand Up @@ -221,6 +221,7 @@ bundler/lib/bundler/templates/newgem/bin/setup.tt
bundler/lib/bundler/templates/newgem/circleci/config.yml.tt
bundler/lib/bundler/templates/newgem/exe/newgem.tt
bundler/lib/bundler/templates/newgem/ext/newgem/Cargo.toml.tt
bundler/lib/bundler/templates/newgem/ext/newgem/build.rs.tt
bundler/lib/bundler/templates/newgem/ext/newgem/extconf-c.rb.tt
bundler/lib/bundler/templates/newgem/ext/newgem/extconf-rust.rb.tt
bundler/lib/bundler/templates/newgem/ext/newgem/newgem.c.tt
Expand Down
1 change: 1 addition & 0 deletions bundler/lib/bundler/cli/gem.rb
Expand Up @@ -205,6 +205,7 @@ def run
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",
)
Expand Down
Expand Up @@ -12,4 +12,11 @@ publish = false
crate-type = ["cdylib"]

[dependencies]
magnus = { version = "0.6.2" }
magnus = { version = "0.6.3" }
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" }
5 changes: 5 additions & 0 deletions bundler/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(())
}
11 changes: 11 additions & 0 deletions bundler/lib/bundler/templates/newgem/ext/newgem/src/lib.rs.tt
Expand Up @@ -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())
}
}
23 changes: 23 additions & 0 deletions bundler/spec/commands/newgem_spec.rb
Expand Up @@ -1454,6 +1454,7 @@ def create_temporary_dir(dir)
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
Expand Down Expand Up @@ -1482,6 +1483,28 @@ def create_temporary_dir(dir)

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
end

Expand Down
1 change: 1 addition & 0 deletions bundler/spec/support/filters.rb
Expand Up @@ -33,6 +33,7 @@ def inspect
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"]
end