Debian Codegraph @

third_party/rust/uniffi_bindgen/src/bindings/ruby/test.rs

raw
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 use crate::cargo_metadata::CrateConfigSupplier;
6 use crate::library_mode::generate_bindings;
7 use anyhow::{bail, Context, Result};
8 use camino::Utf8Path;
9 use std::env;
10 use std::ffi::OsString;
11 use std::process::{Command, Stdio};
12 use uniffi_testing::UniFFITestHelper;
13
14 /// Run Ruby tests for a UniFFI test fixture
15 pub fn run_test(tmp_dir: &str, fixture_name: &str, script_file: &str) -> Result<()> {
16 let status = test_script_command(tmp_dir, fixture_name, script_file)?
17 .spawn()
18 .context("Failed to spawn `ruby` when running script")?
19 .wait()
20 .context("Failed to wait for `ruby` when running script")?;
21 if !status.success() {
22 bail!("running `ruby` failed");
23 }
24 Ok(())
25 }
26
27 /// Create a `Command` instance that runs a test script
28 pub fn test_script_command(
29 tmp_dir: &str,
30 fixture_name: &str,
31 script_file: &str,
32 ) -> Result<Command> {
33 let script_path = Utf8Path::new(script_file).canonicalize_utf8()?;
34 let test_helper = UniFFITestHelper::new(fixture_name)?;
35 let out_dir = test_helper.create_out_dir(tmp_dir, &script_path)?;
36 let cdylib_path = test_helper.copy_cdylib_to_out_dir(&out_dir)?;
37 generate_bindings(
38 &cdylib_path,
39 None,
40 &super::RubyBindingGenerator,
41 &CrateConfigSupplier::from(test_helper.cargo_metadata()),
42 None,
43 &out_dir,
44 false,
45 )?;
46
47 let rubypath = env::var_os("RUBYLIB").unwrap_or_else(|| OsString::from(""));
48 let rubypath = env::join_paths(
49 env::split_paths(&rubypath).chain(vec![out_dir.to_path_buf().into_std_path_buf()]),
50 )?;
51
52 let mut command = Command::new("ruby");
53 command
54 .current_dir(out_dir)
55 .env("RUBYLIB", rubypath)
56 .arg(script_path)
57 .stderr(Stdio::inherit())
58 .stdout(Stdio::inherit());
59 Ok(command)
60 }