Skip to content

Commit

Permalink
add mapdir regression test; add testfs; add arg passing system
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark McCaskey committed May 21, 2019
1 parent 7734001 commit 6b81ec0
Show file tree
Hide file tree
Showing 31 changed files with 7,978 additions and 4 deletions.
64 changes: 63 additions & 1 deletion lib/wasi/build/wasitests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,35 @@ pub fn compile(file: &str, ignores: &HashSet<String>) -> Option<String> {
""
};

let src_code = fs::read_to_string(file).expect("read src file");
let args = extract_args_from_source_file(&src_code);

let mapdir_args = if let Some(a) = args {
if !a.mapdir.is_empty() {
let mut out_str = String::new();
out_str.push_str("vec![");
for (alias, real_dir) in a.mapdir {
out_str.push_str(&format!(
"(\"{}\".to_string(), \"{}\".to_string()),",
alias, real_dir
));
}
out_str.push_str("]");
out_str
} else {
"vec![]".to_string()
}
} else {
"vec![]".to_string()
};

let contents = format!(
"#[test]{ignore}
fn test_{rs_module_name}() {{
assert_wasi_output!(
\"../../{module_path}\",
\"{rs_module_name}\",
vec![],
{mapdir_args},
\"../../{test_output_path}\"
);
}}
Expand All @@ -90,6 +112,7 @@ fn test_{rs_module_name}() {{
module_path = wasm_out_name,
rs_module_name = rs_module_name,
test_output_path = format!("{}.out", normalized_name),
mapdir_args = mapdir_args,
);
let rust_test_filepath = format!(
concat!(env!("CARGO_MANIFEST_DIR"), "/tests/{}.rs"),
Expand Down Expand Up @@ -143,3 +166,42 @@ fn read_ignore_list() -> HashSet<String> {
.map(|v| v.to_lowercase())
.collect()
}

struct Args {
pub mapdir: Vec<(String, String)>,
}

/// Pulls args to the program out of a comment at the top of the file starting with "// Args:"
fn extract_args_from_source_file(source_code: &str) -> Option<Args> {
if source_code.starts_with("// Args:") {
let mut args = Args { mapdir: vec![] };
for arg_line in source_code
.lines()
.skip(1)
.take_while(|line| line.starts_with("// "))
{
let tokenized = arg_line
.split_whitespace()
.skip(1)
.map(String::from)
.collect::<Vec<String>>();
match tokenized[1].as_ref() {
"mapdir" => {
if let [alias, real_dir] = &tokenized[2].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]
);
}
}
e => {
eprintln!("WARN: comment arg: {} is not supported", e);
}
}
}
return Some(args);
}
None
}
2 changes: 1 addition & 1 deletion lib/wasi/src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ pub fn fd_readdir(
for entry in entries.iter().skip(cookie as usize) {
cur_cookie += 1;
let entry_path = entry.path();
let entry_path = wasi_try!(entry_path.file_stem().ok_or(__WASI_EIO));
let entry_path = wasi_try!(entry_path.file_name().ok_or(__WASI_EIO));
let entry_path_str = entry_path.to_string_lossy();
let namlen = entry_path_str.len();
debug!("Returning dirent for {}", entry_path_str);
Expand Down
5 changes: 3 additions & 2 deletions lib/wasi/tests/wasitests/_common.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
macro_rules! assert_wasi_output {
($file:expr, $name:expr, $args:expr, $expected:expr) => {{
($file:expr, $name:expr, $mapdir_args:expr, $expected:expr) => {{
use wasmer_dev_utils::stdio::StdioCapturer;
use wasmer_runtime_core::{backend::Compiler, Func};
use wasmer_wasi::generate_import_object;
Expand Down Expand Up @@ -32,7 +32,8 @@ macro_rules! assert_wasi_output {
let module = wasmer_runtime_core::compile_with(&wasm_bytes[..], &get_compiler())
.expect("WASM can't be compiled");

let import_object = generate_import_object(vec![], vec![], vec![".".to_string()], vec![]);
let import_object =
generate_import_object(vec![], vec![], vec![".".to_string()], $mapdir_args);

let instance = module
.instantiate(&import_object)
Expand Down
9 changes: 9 additions & 0 deletions lib/wasi/tests/wasitests/mapdir.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#[test]
fn test_mapdir() {
assert_wasi_output!(
"../../wasitests/mapdir.wasm",
"mapdir",
vec![],
"../../wasitests/mapdir.out"
);
}
1 change: 1 addition & 0 deletions lib/wasi/tests/wasitests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ mod _common;
mod create_dir;
mod file_metadata;
mod hello;
mod mapdir;
mod quine;
Binary file added lib/wasi/wasitests/mapdir
Binary file not shown.
6 changes: 6 additions & 0 deletions lib/wasi/wasitests/mapdir.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"wasitests/test_fs/hamlet/README.md"
"wasitests/test_fs/hamlet/act1"
"wasitests/test_fs/hamlet/act2"
"wasitests/test_fs/hamlet/act3"
"wasitests/test_fs/hamlet/act4"
"wasitests/test_fs/hamlet/act5"
20 changes: 20 additions & 0 deletions lib/wasi/wasitests/mapdir.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Args:
// mapdir: .:wasitests/test_fs/hamlet

use std::fs;

fn main() {
#[cfg(not(target = "wasi"))]
let read_dir = fs::read_dir("wasitests/test_fs/hamlet").unwrap();
#[cfg(target = "wasi")]
let read_dir = fs::read_dir(".").unwrap();
let mut out = vec![];
for entry in read_dir {
out.push(format!("{:?}", entry.unwrap().path()));
}
out.sort();

for p in out {
println!("{}", p);
}
}
Binary file added lib/wasi/wasitests/mapdir.wasm
Binary file not shown.
7 changes: 7 additions & 0 deletions lib/wasi/wasitests/test_fs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Test FS

This is a test "file system" used in some of the WASI integration tests.

It's just a bunch of files in a tree.

If you make changes here, please regenerate the tests with `make wasitests`!
3 changes: 3 additions & 0 deletions lib/wasi/wasitests/test_fs/hamlet/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Hamlet split in to acts and scenes

From: http://shakespeare.mit.edu/hamlet/full.html
Loading

0 comments on commit 6b81ec0

Please sign in to comment.