| 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::{ |
| 6 |
bail, bindings::RunScriptOptions, cargo_metadata::CrateConfigSupplier, |
| 7 |
library_mode::generate_bindings, Context, Result, |
| 8 |
}; |
| 9 |
use camino::{Utf8Path, Utf8PathBuf}; |
| 10 |
use std::env; |
| 11 |
use std::process::Command; |
| 12 |
use uniffi_testing::UniFFITestHelper; |
| 13 |
|
| 14 |
/// Run Kotlin tests for a UniFFI test fixture |
| 15 |
pub fn run_test(tmp_dir: &str, fixture_name: &str, script_file: &str) -> Result<()> { |
| 16 |
run_script( |
| 17 |
tmp_dir, |
| 18 |
fixture_name, |
| 19 |
script_file, |
| 20 |
vec![], |
| 21 |
&RunScriptOptions::default(), |
| 22 |
) |
| 23 |
} |
| 24 |
|
| 25 |
/// Run a Kotlin script |
| 26 |
/// |
| 27 |
/// This function will set things up so that the script can import the UniFFI bindings for a crate |
| 28 |
pub fn run_script( |
| 29 |
tmp_dir: &str, |
| 30 |
crate_name: &str, |
| 31 |
script_file: &str, |
| 32 |
args: Vec<String>, |
| 33 |
options: &RunScriptOptions, |
| 34 |
) -> Result<()> { |
| 35 |
let script_path = Utf8Path::new(script_file); |
| 36 |
let test_helper = UniFFITestHelper::new(crate_name)?; |
| 37 |
let out_dir = test_helper.create_out_dir(tmp_dir, script_path)?; |
| 38 |
let cdylib_path = test_helper.copy_cdylib_to_out_dir(&out_dir)?; |
| 39 |
|
| 40 |
generate_bindings( |
| 41 |
&cdylib_path, |
| 42 |
None, |
| 43 |
&super::KotlinBindingGenerator, |
| 44 |
&CrateConfigSupplier::from(test_helper.cargo_metadata()), |
| 45 |
None, |
| 46 |
&out_dir, |
| 47 |
false, |
| 48 |
)?; |
| 49 |
let jar_file = build_jar(crate_name, &out_dir, options)?; |
| 50 |
|
| 51 |
let mut command = kotlinc_command(options); |
| 52 |
command |
| 53 |
.arg("-classpath") |
| 54 |
.arg(calc_classpath(vec![&out_dir, &jar_file])) |
| 55 |
// Enable runtime assertions, for easy testing etc. |
| 56 |
.arg("-J-ea") |
| 57 |
// Our test scripts should not produce any warnings. |
| 58 |
.arg("-Werror") |
| 59 |
.arg("-script") |
| 60 |
.arg(script_path) |
| 61 |
.args(if args.is_empty() { |
| 62 |
vec![] |
| 63 |
} else { |
| 64 |
std::iter::once(String::from("--")).chain(args).collect() |
| 65 |
}); |
| 66 |
|
| 67 |
let status = command |
| 68 |
.spawn() |
| 69 |
.context("Failed to spawn `kotlinc` to run Kotlin script")? |
| 70 |
.wait() |
| 71 |
.context("Failed to wait for `kotlinc` when running Kotlin script")?; |
| 72 |
if !status.success() { |
| 73 |
bail!("running `kotlinc` failed") |
| 74 |
} |
| 75 |
Ok(()) |
| 76 |
} |
| 77 |
|
| 78 |
/// Generate kotlin bindings for the given namespace, then use the kotlin |
| 79 |
/// command-line tools to compile them into a .jar file. |
| 80 |
fn build_jar( |
| 81 |
crate_name: &str, |
| 82 |
out_dir: &Utf8Path, |
| 83 |
options: &RunScriptOptions, |
| 84 |
) -> Result<Utf8PathBuf> { |
| 85 |
let mut jar_file = Utf8PathBuf::from(out_dir); |
| 86 |
jar_file.push(format!("{crate_name}.jar")); |
| 87 |
let sources = glob::glob(out_dir.join("**/*.kt").as_str())? |
| 88 |
.flatten() |
| 89 |
.map(|p| String::from(p.to_string_lossy())) |
| 90 |
.collect::<Vec<String>>(); |
| 91 |
if sources.is_empty() { |
| 92 |
bail!("No kotlin sources found in {out_dir}") |
| 93 |
} |
| 94 |
|
| 95 |
let mut command = kotlinc_command(options); |
| 96 |
command |
| 97 |
// Our generated bindings should not produce any warnings; fail tests if they do. |
| 98 |
.arg("-Werror") |
| 99 |
.arg("-d") |
| 100 |
.arg(&jar_file) |
| 101 |
.arg("-classpath") |
| 102 |
.arg(calc_classpath(vec![])) |
| 103 |
.args(sources); |
| 104 |
|
| 105 |
let status = command |
| 106 |
.spawn() |
| 107 |
.context("Failed to spawn `kotlinc` to compile the bindings")? |
| 108 |
.wait() |
| 109 |
.context("Failed to wait for `kotlinc` when compiling the bindings")?; |
| 110 |
if !status.success() { |
| 111 |
bail!("running `kotlinc` failed") |
| 112 |
} |
| 113 |
Ok(jar_file) |
| 114 |
} |
| 115 |
|
| 116 |
fn kotlinc_command(options: &RunScriptOptions) -> Command { |
| 117 |
let mut command = Command::new("kotlinc"); |
| 118 |
if !options.show_compiler_messages { |
| 119 |
command.arg("-nowarn"); |
| 120 |
} |
| 121 |
command |
| 122 |
} |
| 123 |
|
| 124 |
fn calc_classpath(extra_paths: Vec<&Utf8Path>) -> String { |
| 125 |
extra_paths |
| 126 |
.into_iter() |
| 127 |
.map(|p| p.to_string()) |
| 128 |
// Add the system classpath as a component, using the fact that env::var returns an Option, |
| 129 |
// which implement Iterator |
| 130 |
.chain(env::var("CLASSPATH")) |
| 131 |
.collect::<Vec<String>>() |
| 132 |
.join(":") |
| 133 |
} |