diff --git a/lib/wasi/build/wasitests.rs b/lib/wasi/build/wasitests.rs index 2527c8be6..6d954373e 100644 --- a/lib/wasi/build/wasitests.rs +++ b/lib/wasi/build/wasitests.rs @@ -50,6 +50,10 @@ pub fn compile(file: &str, ignores: &HashSet) -> Option { perm.set_mode(0o766); fs::set_permissions(normal_path, perm).expect("set permissions"); } + let rs_module_name = { + let temp = PathBuf::from(&normalized_name); + temp.file_name().unwrap().to_string_lossy().to_string() + }; let result = Command::new(&normalized_name) .output() @@ -65,15 +69,12 @@ pub fn compile(file: &str, ignores: &HashSet) -> Option { .output() .expect("Failed to compile program to native code"); - let ignored = if ignores.contains(&normalized_name) { + let ignored = if ignores.contains(&rs_module_name) { "\n#[ignore]" } else { "" }; - let rs_module_name = { - let temp = PathBuf::from(&normalized_name); - temp.file_name().unwrap().to_string_lossy().to_string() - }; + let contents = format!( "#[test]{ignore} fn test_{rs_module_name}() {{ @@ -106,7 +107,6 @@ pub fn build() { let mut modules: Vec = Vec::new(); let ignores = read_ignore_list(); - for entry in glob("wasitests/*.rs").unwrap() { match entry { Ok(path) => { @@ -128,7 +128,7 @@ pub fn build() { modules.push("".to_string()); let modfile: String = modules.join("\n"); - let source = fs::read(dbg!(&rust_test_modpath)).unwrap(); + let source = fs::read(&rust_test_modpath).unwrap(); // We only modify the mod file if has changed if source != modfile.as_bytes() { fs::write(&rust_test_modpath, modfile.as_bytes()).unwrap(); diff --git a/lib/wasi/tests/wasitests/create_dir.rs b/lib/wasi/tests/wasitests/create_dir.rs new file mode 100644 index 000000000..1980907f2 --- /dev/null +++ b/lib/wasi/tests/wasitests/create_dir.rs @@ -0,0 +1,10 @@ +#[test] +#[ignore] +fn test_create_dir() { + assert_wasi_output!( + "../../wasitests/create_dir.wasm", + "create_dir", + vec![], + "../../wasitests/create_dir.out" + ); +} diff --git a/lib/wasi/tests/wasitests/file_metadata.rs b/lib/wasi/tests/wasitests/file_metadata.rs new file mode 100644 index 000000000..70bb6de9b --- /dev/null +++ b/lib/wasi/tests/wasitests/file_metadata.rs @@ -0,0 +1,10 @@ +#[test] +#[ignore] +fn test_file_metadata() { + assert_wasi_output!( + "../../wasitests/file_metadata.wasm", + "file_metadata", + vec![], + "../../wasitests/file_metadata.out" + ); +} diff --git a/lib/wasi/tests/wasitests/mod.rs b/lib/wasi/tests/wasitests/mod.rs index 3c1123094..743eed31c 100644 --- a/lib/wasi/tests/wasitests/mod.rs +++ b/lib/wasi/tests/wasitests/mod.rs @@ -5,4 +5,7 @@ // The _common module is not autogenerated. It provides common macros for the wasitests #[macro_use] mod _common; +mod create_dir; +mod file_metadata; mod hello; +mod quine; diff --git a/lib/wasi/tests/wasitests/quine.rs b/lib/wasi/tests/wasitests/quine.rs new file mode 100644 index 000000000..156ded8a4 --- /dev/null +++ b/lib/wasi/tests/wasitests/quine.rs @@ -0,0 +1,9 @@ +#[test] +fn test_quine() { + assert_wasi_output!( + "../../wasitests/quine.wasm", + "quine", + vec![], + "../../wasitests/quine.out" + ); +} diff --git a/lib/wasi/wasitests/create_dir b/lib/wasi/wasitests/create_dir new file mode 100755 index 000000000..f83063c51 Binary files /dev/null and b/lib/wasi/wasitests/create_dir differ diff --git a/lib/wasi/wasitests/create_dir.out b/lib/wasi/wasitests/create_dir.out new file mode 100644 index 000000000..cc1a8aa2c --- /dev/null +++ b/lib/wasi/wasitests/create_dir.out @@ -0,0 +1,5 @@ +Test file exists: false +Dir exists: false +Dir exists: false +Dir exists: false +Success diff --git a/lib/wasi/wasitests/create_dir.rs b/lib/wasi/wasitests/create_dir.rs new file mode 100644 index 000000000..514387b02 --- /dev/null +++ b/lib/wasi/wasitests/create_dir.rs @@ -0,0 +1,37 @@ +use std::fs; +use std::io::{Read, Seek, SeekFrom, Write}; +use std::path::*; + +fn main() { + let mut path = PathBuf::from("wasitests/testing/nested/directories"); + let test_file = path.join("test.file"); + fs::create_dir_all(&path).unwrap(); + { + let mut file = fs::OpenOptions::new() + .read(true) + .write(true) + .truncate(true) + .create(true) + .open(&test_file) + .unwrap(); + + assert_eq!(file.write(b"hello").unwrap(), 5); + + file.flush().unwrap(); + file.seek(SeekFrom::Start(0)).unwrap(); + let mut in_str = String::new(); + file.read_to_string(&mut in_str).unwrap(); + assert_eq!(&in_str, "hello"); + } + fs::remove_file(&test_file).unwrap(); + println!("Test file exists: {}", test_file.exists()); + assert!(!test_file.exists()); + for _ in 0..3 { + fs::remove_dir_all(&path).unwrap(); + println!("Dir exists: {}", path.exists()); + assert!(!path.exists()); + path.pop(); + } + + println!("Success"); +} diff --git a/lib/wasi/wasitests/create_dir.wasm b/lib/wasi/wasitests/create_dir.wasm new file mode 100755 index 000000000..c92d95516 Binary files /dev/null and b/lib/wasi/wasitests/create_dir.wasm differ diff --git a/lib/wasi/wasitests/file_metadata b/lib/wasi/wasitests/file_metadata new file mode 100755 index 000000000..cd3728704 Binary files /dev/null and b/lib/wasi/wasitests/file_metadata differ diff --git a/lib/wasi/wasitests/file_metadata.out b/lib/wasi/wasitests/file_metadata.out new file mode 100644 index 000000000..d49e60f1d --- /dev/null +++ b/lib/wasi/wasitests/file_metadata.out @@ -0,0 +1,2 @@ +is dir: false +file info: FileType(FileType { mode: 33188 }) 419 Ok(SystemTime { tv_sec: 1558132188, tv_nsec: 545288295 }) Ok(SystemTime { tv_sec: 1558132188, tv_nsec: 545243056 }) Ok(SystemTime { tv_sec: 1558132191, tv_nsec: 359031112 }) diff --git a/lib/wasi/wasitests/file_metadata.rs b/lib/wasi/wasitests/file_metadata.rs new file mode 100644 index 000000000..7419a4f82 --- /dev/null +++ b/lib/wasi/wasitests/file_metadata.rs @@ -0,0 +1,17 @@ +use std::fs; +use std::io::Read; + +fn main() { + let mut this_file = + fs::File::open("wasitests/file_metadata.rs").expect("could not find src file"); + let md = this_file.metadata().unwrap(); + println!("is dir: {}", md.is_dir()); + println!( + "file info: {:?} {} {:?} {:?} {:?}", + md.file_type(), + md.len(), + md.modified(), + md.created(), + md.accessed() + ); +} diff --git a/lib/wasi/wasitests/file_metadata.wasm b/lib/wasi/wasitests/file_metadata.wasm new file mode 100755 index 000000000..2e2f702f9 Binary files /dev/null and b/lib/wasi/wasitests/file_metadata.wasm differ diff --git a/lib/wasi/wasitests/ignores.txt b/lib/wasi/wasitests/ignores.txt index 8b1378917..aa48474bf 100644 --- a/lib/wasi/wasitests/ignores.txt +++ b/lib/wasi/wasitests/ignores.txt @@ -1 +1,2 @@ - +file_metadata +create_dir diff --git a/lib/wasi/wasitests/quine b/lib/wasi/wasitests/quine new file mode 100755 index 000000000..409c63048 Binary files /dev/null and b/lib/wasi/wasitests/quine differ diff --git a/lib/wasi/wasitests/quine.out b/lib/wasi/wasitests/quine.out new file mode 100644 index 000000000..13de73d05 --- /dev/null +++ b/lib/wasi/wasitests/quine.out @@ -0,0 +1,11 @@ +use std::fs; +use std::io::Read; + +fn main() { + let mut this_file = fs::File::open("wasitests/quine.rs").expect("could not find src file"); + let md = this_file.metadata().unwrap(); + let mut in_str = String::new(); + this_file.read_to_string(&mut in_str).unwrap(); + println!("{}", in_str); +} + diff --git a/lib/wasi/wasitests/quine.rs b/lib/wasi/wasitests/quine.rs new file mode 100644 index 000000000..6ca65ee8c --- /dev/null +++ b/lib/wasi/wasitests/quine.rs @@ -0,0 +1,10 @@ +use std::fs; +use std::io::Read; + +fn main() { + let mut this_file = fs::File::open("wasitests/quine.rs").expect("could not find src file"); + let md = this_file.metadata().unwrap(); + let mut in_str = String::new(); + this_file.read_to_string(&mut in_str).unwrap(); + println!("{}", in_str); +} diff --git a/lib/wasi/wasitests/quine.wasm b/lib/wasi/wasitests/quine.wasm new file mode 100755 index 000000000..937671762 Binary files /dev/null and b/lib/wasi/wasitests/quine.wasm differ