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