Skip to content

Commit

Permalink
tests/integration/cli: add tests for create-{exe,obj} & --object-form…
Browse files Browse the repository at this point in the history
…at flag

Test that serialized and symbol formats work both in create-exe and
create-obj.
  • Loading branch information
epilys committed Aug 18, 2022
1 parent c73d3dd commit f3a237d
Showing 1 changed file with 171 additions and 2 deletions.
173 changes: 171 additions & 2 deletions tests/integration/cli/tests/create_exe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ struct WasmerCreateExe {
native_executable_path: PathBuf,
/// Compiler with which to compile the Wasm.
compiler: Compiler,
/// Extra CLI flags
extra_cli_flags: Vec<&'static str>,
}

impl Default for WasmerCreateExe {
Expand All @@ -40,17 +42,19 @@ impl Default for WasmerCreateExe {
wasm_path: PathBuf::from(create_exe_test_wasm_path()),
native_executable_path,
compiler: Compiler::Cranelift,
extra_cli_flags: vec![],
}
}
}

impl WasmerCreateExe {
fn run(&self) -> anyhow::Result<()> {
fn run(&self) -> anyhow::Result<Vec<u8>> {
let output = Command::new(&self.wasmer_path)
.current_dir(&self.current_dir)
.arg("create-exe")
.arg(&self.wasm_path.canonicalize()?)
.arg(&self.compiler.to_flag())
.args(self.extra_cli_flags.iter())
.arg("-o")
.arg(&self.native_executable_path)
.output()?;
Expand All @@ -64,7 +68,66 @@ impl WasmerCreateExe {
.expect("stderr is not utf8! need to handle arbitrary bytes")
);
}
Ok(())
Ok(output.stdout)
}
}

/// Data used to run the `wasmer compile` command.
#[derive(Debug)]
struct WasmerCreateObj {
/// The directory to operate in.
current_dir: PathBuf,
/// Path to wasmer executable used to run the command.
wasmer_path: PathBuf,
/// Path to the Wasm file to compile.
wasm_path: PathBuf,
/// Path to the object file produced by compiling the Wasm.
output_object_path: PathBuf,
/// Compiler with which to compile the Wasm.
compiler: Compiler,
/// Extra CLI flags
extra_cli_flags: Vec<&'static str>,
}

impl Default for WasmerCreateObj {
fn default() -> Self {
#[cfg(not(windows))]
let output_object_path = PathBuf::from("wasm.o");
#[cfg(windows)]
let output_object_path = PathBuf::from("wasm.obj");
Self {
current_dir: std::env::current_dir().unwrap(),
wasmer_path: get_wasmer_path(),
wasm_path: PathBuf::from(create_exe_test_wasm_path()),
output_object_path,
compiler: Compiler::Cranelift,
extra_cli_flags: vec![],
}
}
}

impl WasmerCreateObj {
fn run(&self) -> anyhow::Result<Vec<u8>> {
let output = Command::new(&self.wasmer_path)
.current_dir(&self.current_dir)
.arg("create-obj")
.arg(&self.wasm_path.canonicalize()?)
.arg(&self.compiler.to_flag())
.args(self.extra_cli_flags.iter())
.arg("-o")
.arg(&self.output_object_path)
.output()?;

if !output.status.success() {
bail!(
"wasmer create-obj failed with: stdout: {}\n\nstderr: {}",
std::str::from_utf8(&output.stdout)
.expect("stdout is not utf8! need to handle arbitrary bytes"),
std::str::from_utf8(&output.stderr)
.expect("stderr is not utf8! need to handle arbitrary bytes")
);
}
Ok(output.stdout)
}
}

Expand Down Expand Up @@ -160,3 +223,109 @@ fn create_exe_works_with_file() -> anyhow::Result<()> {

Ok(())
}

#[test]
fn create_exe_serialized_works() -> anyhow::Result<()> {
let temp_dir = tempfile::tempdir()?;
let operating_dir: PathBuf = temp_dir.path().to_owned();

let wasm_path = operating_dir.join(create_exe_test_wasm_path());
#[cfg(not(windows))]
let executable_path = operating_dir.join("wasm.out");
#[cfg(windows)]
let executable_path = operating_dir.join("wasm.exe");

let output: Vec<u8> = WasmerCreateExe {
current_dir: operating_dir.clone(),
wasm_path,
native_executable_path: executable_path.clone(),
compiler: Compiler::Cranelift,
extra_cli_flags: vec!["--object-format", "serialized"],
..Default::default()
}
.run()
.context("Failed to create-exe wasm with Wasmer")?;

let result = run_code(
&operating_dir,
&executable_path,
&["--eval".to_string(), "function greet(name) { return JSON.stringify('Hello, ' + name); }; print(greet('World'));".to_string()],
)
.context("Failed to run generated executable")?;
let result_lines = result.lines().collect::<Vec<&str>>();
assert_eq!(result_lines, vec!["\"Hello, World\""],);

let output_str = String::from_utf8_lossy(&output);
assert!(
output_str.contains("erialized"),
"create-exe output doesn't mention `serialized` format keyword:\n{}",
output_str
);

Ok(())
}

fn create_obj(args: Vec<&'static str>, keyword_needle: &str, keyword: &str) -> anyhow::Result<()> {
let temp_dir = tempfile::tempdir()?;
let operating_dir: PathBuf = temp_dir.path().to_owned();

let wasm_path = operating_dir.join(create_exe_test_wasm_path());

#[cfg(not(windows))]
let object_path = operating_dir.join("wasm.o");
#[cfg(windows)]
let object_path = operating_dir.join("wasm.obj");

let output: Vec<u8> = WasmerCreateObj {
current_dir: operating_dir.clone(),
wasm_path,
output_object_path: object_path.clone(),
compiler: Compiler::Cranelift,
extra_cli_flags: args,
..Default::default()
}
.run()
.context("Failed to create-obj wasm with Wasmer")?;

assert!(
object_path.exists(),
"create-obj successfully completed but object output file `{}` missing",
object_path.display()
);
let mut object_header_path = object_path.clone();
object_header_path.set_extension("h");
assert!(
object_header_path.exists(),
"create-obj successfully completed but object output header file `{}` missing",
object_header_path.display()
);

let output_str = String::from_utf8_lossy(&output);
assert!(
output_str.contains(keyword_needle),
"create-obj output doesn't mention `{}` format keyword:\n{}",
keyword,
output_str
);

Ok(())
}

#[test]
fn create_obj_default() -> anyhow::Result<()> {
create_obj(vec![], "ymbols", "symbols")
}

#[test]
fn create_obj_symbols() -> anyhow::Result<()> {
create_obj(vec!["--object-format", "symbols"], "ymbols", "symbols")
}

#[test]
fn create_obj_serialized() -> anyhow::Result<()> {
create_obj(
vec!["--object-format", "serialized"],
"erialized",
"serialized",
)
}

0 comments on commit f3a237d

Please sign in to comment.