Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Jun 8, 2021
1 parent 6e2903f commit 50be2ed
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion tests/wasi-wast/wasi/tests/path_rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::fs;
use std::io::{Read, Write};
use std::path::PathBuf;

fn main() {
fn run_with_toplevel_dir() {
#[cfg(not(target_os = "wasi"))]
let mut base = PathBuf::from("test_fs");
#[cfg(target_os = "wasi")]
Expand Down Expand Up @@ -73,3 +73,47 @@ fn main() {
println!("{}", test_str);
std::fs::remove_file(file_to_rename_to).unwrap();
}

fn run_with_sub_dir() {
#[cfg(not(target_os = "wasi"))]
let base = PathBuf::from("test_fs");
#[cfg(target_os = "wasi")]
let mut base = PathBuf::from("temp");

//make a sub-directory
fs::create_dir(base.join("sub"));

let file_to_create = base.join("sub/path_rename_file.txt");
let file_to_rename_to = base.join("sub/path_renamed_file.txt");

{
let mut f = std::fs::OpenOptions::new()
.create_new(true)
.write(true)
.open(&file_to_create)
.unwrap();

let string = "Hello world";
let bytes: Vec<u8> = string.bytes().collect();
f.write_all(&bytes[..]).unwrap();
}

std::fs::rename(&file_to_create, &file_to_rename_to).unwrap();
let mut file = fs::File::open(&file_to_rename_to).expect("Could not open file");
if file_to_create.exists() {
println!("The original file still exists!");
return;
} else {
println!("The original file does not still exist!");
}

if !file_to_rename_to.exists() {
println!("The moved file does not exist!");
return;
}
}

fn main() {
run_with_toplevel_dir();
run_with_sub_dir();
}

0 comments on commit 50be2ed

Please sign in to comment.