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

improve wasitest infra and fix fseek test #563

Merged
merged 4 commits into from
Jul 15, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ generate-emtests:
WASM_EMSCRIPTEN_GENERATE_EMTESTS=1 cargo build -p wasmer-emscripten-tests --release

generate-wasitests:
WASM_WASI_GENERATE_WASITESTS=1 cargo build -p wasmer-wasi-tests --release
WASM_WASI_GENERATE_WASITESTS=1 cargo build -p wasmer-wasi-tests --release -vv

generate: generate-spectests generate-emtests generate-wasitests

Expand Down
53 changes: 45 additions & 8 deletions lib/wasi-tests/build/wasitests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ static BANNER: &str = "// !!! THIS IS A GENERATED FILE !!!
// Files autogenerated with cargo build (build/wasitests.rs).\n";

pub fn compile(file: &str, ignores: &HashSet<String>) -> Option<String> {
dbg!(file);
let mut output_path = PathBuf::from(file);
output_path.set_extension("out");

Expand All @@ -31,12 +30,14 @@ pub fn compile(file: &str, ignores: &HashSet<String>) -> Option<String> {
nn
};

Command::new("rustc")
println!("Compiling program {} to native", file);
let native_out = Command::new("rustc")
.arg(file)
.arg("-o")
.arg(&normalized_name)
.output()
.expect("Failed to compile program to native code");
print_info_on_error(&native_out, "COMPILATION FAILED");

#[cfg(unix)]
{
Expand All @@ -57,18 +58,29 @@ pub fn compile(file: &str, ignores: &HashSet<String>) -> Option<String> {
let result = Command::new(&normalized_name)
.output()
.expect("Failed to execute native program");
print_info_on_error(&result, "NATIVE PROGRAM FAILED");

std::fs::remove_file(&normalized_name).expect("could not delete executable");
let wasm_out_name = format!("{}.wasm", &normalized_name);

Command::new("rustc")
let wasm_compilation_out = Command::new("rustc")
.arg("+nightly")
.arg("--target=wasm32-wasi")
.arg("-C")
.arg("opt-level=s")
.arg(file)
.arg("-o")
.arg(&wasm_out_name)
.output()
.expect("Failed to compile program to native code");
print_info_on_error(&wasm_compilation_out, "WASM COMPILATION");

// to prevent commiting huge binary blobs forever
let wasm_strip_out = Command::new("wasm-strip")
.arg(&wasm_out_name)
.output()
.expect("Failed to strip compiled wasm module");
print_info_on_error(&wasm_strip_out, "STRIPPING WASM");

let ignored = if ignores.contains(&rs_module_name) {
"\n#[ignore]"
Expand Down Expand Up @@ -193,25 +205,36 @@ fn extract_args_from_source_file(source_code: &str) -> Option<Args> {
{
let tokenized = arg_line
.split_whitespace()
// skip trailing space
.skip(1)
.map(String::from)
.collect::<Vec<String>>();
match tokenized[1].as_ref() {
let command_name = {
let mut cn = tokenized[0].clone();
assert_eq!(
cn.pop(),
Some(':'),
"Final character of argname must be a colon"
);
cn
};

match command_name.as_ref() {
"mapdir" => {
if let [alias, real_dir] = &tokenized[2].split(':').collect::<Vec<&str>>()[..] {
if let [alias, real_dir] = &tokenized[1].split(':').collect::<Vec<&str>>()[..] {
args.mapdir.push((alias.to_string(), real_dir.to_string()));
} else {
eprintln!(
"Parse error in mapdir {} not parsed correctly",
&tokenized[2]
&tokenized[1]
);
}
}
"env" => {
if let [name, val] = &tokenized[2].split('=').collect::<Vec<&str>>()[..] {
if let [name, val] = &tokenized[1].split('=').collect::<Vec<&str>>()[..] {
args.envvars.push((name.to_string(), val.to_string()));
} else {
eprintln!("Parse error in env {} not parsed correctly", &tokenized[2]);
eprintln!("Parse error in env {} not parsed correctly", &tokenized[1]);
}
}
e => {
Expand All @@ -223,3 +246,17 @@ fn extract_args_from_source_file(source_code: &str) -> Option<Args> {
}
None
}

fn print_info_on_error(output: &std::process::Output, context: &str) {
if !output.status.success() {
println!("{}", context);
println!(
"stdout:\n{}",
std::str::from_utf8(&output.stdout[..]).unwrap()
);
eprintln!(
"stderr:\n{}",
std::str::from_utf8(&output.stderr[..]).unwrap()
);
}
}
5 changes: 5 additions & 0 deletions lib/wasi-tests/tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Most of the files here are generated.

`_common.rs` is a file containing a macro that the generated tests use to avoid code duplication.

If you want to add new features, edit `_common.rs` and `wasi-tests/build/wasitests.rs` to use the changed macro.
2 changes: 1 addition & 1 deletion lib/wasi-tests/tests/wasitests/envvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ fn test_envvar() {
"../../wasitests/envvar.wasm",
"envvar",
vec![],
vec![],
vec!["DOG=1".to_string(), "CAT=2".to_string(),],
"../../wasitests/envvar.out"
);
}
Binary file modified lib/wasi-tests/wasitests/create_dir.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/envvar.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/file_metadata.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/fs_sandbox_test.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/fseek.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/hello.wasm
Binary file not shown.
11 changes: 8 additions & 3 deletions lib/wasi-tests/wasitests/mapdir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
use std::fs;

fn main() {
// #[cfg(not(target_os = "wasi"))]
// let read_dir = fs::read_dir("wasitests/test_fs/hamlet").unwrap();
// #[cfg(target_os = "wasi")]
#[cfg(not(target_os = "wasi"))]
let cur_dir = std::env::current_dir().unwrap();
#[cfg(not(target_os = "wasi"))]
std::env::set_current_dir("wasitests/test_fs/hamlet").unwrap();

let read_dir = fs::read_dir(".").unwrap();
let mut out = vec![];
for entry in read_dir {
Expand All @@ -17,4 +19,7 @@ fn main() {
for p in out {
println!("{}", p);
}
// return to the current directory
#[cfg(not(target_os = "wasi"))]
std::env::set_current_dir(cur_dir).unwrap();
}
Binary file modified lib/wasi-tests/wasitests/mapdir.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/quine.wasm
Binary file not shown.
3 changes: 2 additions & 1 deletion rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
ignore = [
"src/spectests",
"src/emtests",
"src/emscripten-tests",
"src/wasi-tests",
]