#[macro_export]
macro_rules! assert_vfs_setup {
($vfs:expr $(, $func:expr )?) => {{
let vfs = $vfs;
let abs = match vfs.abs(testing::TEST_TEMP_DIR) {
Ok(x) => x,
_ => panic_msg!("assert_vfs_setup!", "failed to get absolute path", testing::TEST_TEMP_DIR),
};
#[allow(unused_variables)]
let func_name: Option<&str> = None;
$( let func_name = Some($func); )?
let tmpdir = abs.mash(match func_name {
Some(name) => name.as_ref(),
None => function_fqn!(),
});
if tmpdir == abs {
panic_msg!("assert_vfs_setup!", "function name is empty", &tmpdir);
}
if vfs.remove_all(&tmpdir).is_err() {
panic_msg!("assert_vfs_setup!", "failed while removing directory", &tmpdir);
}
assert_vfs_mkdir_p!(vfs, &tmpdir);
(vfs, tmpdir)
}};
}
#[macro_export]
macro_rules! assert_vfs_copyfile {
($vfs:expr, $from:expr, $to:expr) => {
let src = match $vfs.abs($from) {
Ok(x) => x,
_ => panic_msg!("assert_vfs_copyfile!", "failed to get absolute src path", $from),
};
let dst = match $vfs.abs($to) {
Ok(x) => x,
_ => panic_msg!("assert_vfs_copyfile!", "failed to get absolute dst path", $to),
};
if !$vfs.exists(&src) {
panic_msg!("assert_vfs_copyfile!", "doesn't exist", &src);
} else if !$vfs.is_file(&src) {
panic_msg!("assert_vfs_copyfile!", "is not a file", &src);
} else {
match $vfs.copy(&src, &dst) {
Ok(_) => match $vfs.read_all(&src) {
Ok(x) => match $vfs.read_all(&dst) {
Ok(y) => {
if &x != &y {
panic_compare_msg!("assert_vfs_copyfile!", "src data doesn't match dst", &x, &y);
}
},
_ => panic_msg!("assert_vfs_copyfile!", "failed reading dst file", &dst),
},
_ => panic_msg!("assert_vfs_copyfile!", "failed reading src file", &src),
},
_ => panic_msg!("assert_vfs_copyfile!", "failed while copying src file", &src),
};
if !$vfs.is_file(&dst) {
panic_msg!("assert_vfs_copyfile!", "dst doesn't exist", &dst);
}
}
};
}
#[macro_export]
macro_rules! assert_vfs_exists {
($vfs:expr, $path:expr) => {
let target = match $vfs.abs($path) {
Ok(x) => x,
_ => panic_msg!("assert_vfs_exists!", "failed to get absolute path", $path),
};
if !$vfs.exists(&target) {
panic_msg!("assert_vfs_exists!", "doesn't exist", &target);
}
};
}
#[macro_export]
macro_rules! assert_vfs_no_exists {
($vfs:expr, $path:expr) => {
let target = match $vfs.abs($path) {
Ok(x) => x,
_ => panic_msg!("assert_vfs_no_exists!", "failed to get absolute path", $path),
};
if $vfs.exists(&target) {
panic_msg!("assert_vfs_no_exists!", "still exists", &target);
}
};
}
#[macro_export]
macro_rules! assert_vfs_is_dir {
($vfs:expr, $path:expr) => {
let target = match $vfs.abs($path) {
Ok(x) => x,
_ => panic_msg!("assert_vfs_is_dir!", "failed to get absolute path", $path),
};
if $vfs.exists(&target) {
if !$vfs.is_dir(&target) {
panic_msg!("assert_vfs_is_dir!", "exists but is not a directory", &target);
}
} else {
panic_msg!("assert_vfs_is_dir!", "doesn't exist", &target);
}
};
}
#[macro_export]
macro_rules! assert_vfs_no_dir {
($vfs:expr, $path:expr) => {
let target = match $vfs.abs($path) {
Ok(x) => x,
_ => panic_msg!("assert_vfs_no_dir!", "failed to get absolute path", $path),
};
if $vfs.exists(&target) {
if !$vfs.is_dir(&target) {
panic_msg!("assert_vfs_no_dir!", "exists and is not a directory", &target);
} else {
panic_msg!("assert_vfs_no_dir!", "directory still exists", &target);
}
}
};
}
#[macro_export]
macro_rules! assert_vfs_is_file {
($vfs:expr, $path:expr) => {
let target = match $vfs.abs($path) {
Ok(x) => x,
_ => panic_msg!("assert_vfs_is_file!", "failed to get absolute path", $path),
};
if $vfs.exists(&target) {
if !$vfs.is_file(&target) {
panic_msg!("assert_vfs_is_file!", "exists but is not a file", &target);
}
} else {
panic_msg!("assert_vfs_is_file!", "doesn't exist", &target);
}
};
}
#[macro_export]
macro_rules! assert_vfs_no_file {
($vfs:expr, $path:expr) => {
let target = match $vfs.abs($path) {
Ok(x) => x,
_ => panic_msg!("assert_vfs_no_file!", "failed to get absolute path", $path),
};
if $vfs.exists(&target) {
if !$vfs.is_file(&target) {
panic_msg!("assert_vfs_no_file!", "exists and is not a file", &target);
} else {
panic_msg!("assert_vfs_no_file!", "file still exists", &target);
}
}
};
}
#[macro_export]
macro_rules! assert_vfs_is_symlink {
($vfs:expr, $path:expr) => {
let target = match $vfs.abs($path) {
Ok(x) => x,
_ => panic_msg!("assert_vfs_is_symlink!", "failed to get absolute path", $path),
};
if $vfs.exists(&target) {
if !$vfs.is_symlink(&target) {
panic_msg!("assert_vfs_is_link!", "exists but is not a symlink", &target);
}
} else {
panic_msg!("assert_vfs_is_symlink!", "symlink doesn't exist", &target);
}
};
}
#[macro_export]
macro_rules! assert_vfs_no_symlink {
($vfs:expr, $path:expr) => {
let target = match $vfs.abs($path) {
Ok(x) => x,
_ => panic_msg!("assert_vfs_no_symlink!", "failed to get absolute path", $path),
};
if $vfs.exists(&target) {
if $vfs.is_symlink(&target) {
panic_msg!("assert_vfs_no_symlink!", "exists and is a symlink", &target);
}
}
};
}
#[macro_export]
macro_rules! assert_vfs_mkdir_m {
($vfs:expr, $path:expr, $mode:expr) => {
let target = match $vfs.abs($path) {
Ok(x) => x,
_ => panic_msg!("assert_vfs_mkdir_m!", "failed to get absolute path", $path),
};
match $vfs.mkdir_m(&target, $mode) {
Ok(x) => {
if &x != &target {
panic_compare_msg!(
"assert_vfs_mkdir_m!",
"created directory path doesn't match the target",
&x,
&target
);
}
match $vfs.mode(&target) {
Ok(x) => {
if x != $mode {
panic_compare_msg!(
"assert_vfs_mkdir_m!",
"created directory mode doesn't match the target",
&x,
&target
);
}
},
Err(e) => panic!("assert_vfs_mkdir_m!: mode failure for {}", e.to_string()),
};
},
Err(e) => panic!("assert_vfs_mkdir_m!: {}", e.to_string()),
};
if !$vfs.is_dir(&target) {
panic_msg!("assert_vfs_mkdir_m!", "failed to create directory", &target);
}
};
}
#[macro_export]
macro_rules! assert_vfs_mkdir_p {
($vfs:expr, $path:expr) => {
let target = match $vfs.abs($path) {
Ok(x) => x,
_ => panic_msg!("assert_vfs_mkdir_p!", "failed to get absolute path", $path),
};
match $vfs.mkdir_p(&target) {
Ok(x) => {
if &x != &target {
panic_compare_msg!(
"assert_vfs_mkdir_p!",
"created directory path doesn't match the target",
&x,
&target
);
}
},
Err(e) => panic!("assert_vfs_mkdir_p!: {}", e.to_string()),
};
if !$vfs.is_dir(&target) {
panic_msg!("assert_vfs_mkdir_p!", "failed to create directory", &target);
}
};
}
#[macro_export]
macro_rules! assert_vfs_mkfile {
($vfs:expr, $path:expr) => {
let target = match $vfs.abs($path) {
Ok(x) => x,
_ => panic_msg!("assert_vfs_mkfile!", "failed to get absolute path", $path),
};
if $vfs.exists(&target) {
if !$vfs.is_file(&target) {
panic_msg!("assert_vfs_mkfile!", "is not a file", &target);
}
} else {
match $vfs.mkfile(&target) {
Ok(x) => {
if &x != &target {
panic_compare_msg!(
"assert_vfs_mkfile!",
"created file path doesn't match the target",
&x,
&target
);
}
},
_ => panic_msg!("assert_vfs_mkfile!", "failed while creating file", &target),
};
if !$vfs.is_file(&target) {
panic_msg!("assert_vfs_mkfile!", "file doesn't exist", &target);
}
}
};
}
#[macro_export]
macro_rules! assert_vfs_read_all {
($vfs:expr, $path:expr, $data:expr) => {
let target = match $vfs.abs($path) {
Ok(x) => x,
_ => panic_msg!("assert_vfs_read_all!", "failed to get absolute path", $path),
};
if !$vfs.is_file(&target) {
panic_msg!("assert_vfs_read_all!", "file doesn't exist or is not a file", &target);
}
match $vfs.read_all(&target) {
Ok(data) => {
if data != $data {
panic_msg!(
"assert_vfs_read_all!",
format!("read data doesn't equal given data\n read: {}\n expected: {}", data, $data),
&target
);
}
},
_ => panic_msg!("assert_vfs_read_all!", "failed while reading file", &target),
};
};
}
#[macro_export]
macro_rules! assert_vfs_readlink {
($vfs:expr, $path:expr, $target:expr) => {
let link = match $vfs.abs($path) {
Ok(x) => x,
_ => panic_msg!("assert_vfs_readlink!", "failed to get absolute path", $path),
};
if !$vfs.is_symlink(&link) {
panic_msg!("assert_vfs_readlink!", "file doesn't exist or is not a symlink", &link);
}
match $vfs.readlink(&link) {
Ok(x) => {
if x.to_string().unwrap() != $target.to_string().unwrap() {
panic_msg!("assert_vfs_readlink!", "link target doesn't equal given path", &x);
}
},
_ => panic_msg!("assert_vfs_readlink!", "failed while reading link", &link),
};
};
}
#[macro_export]
macro_rules! assert_vfs_readlink_abs {
($vfs:expr, $path:expr, $data:expr) => {
let link = match $vfs.abs($path) {
Ok(x) => x,
_ => panic_msg!("assert_vfs_readlink_abs!", "failed to get absolute path", $path),
};
let target = match $vfs.abs($data) {
Ok(x) => x,
_ => panic_msg!("assert_vfs_readlink_abs!", "failed to get absolute path", $data),
};
if !$vfs.is_symlink(&link) {
panic_msg!("assert_vfs_readlink_abs!", "file doesn't exist or is not a symlink", &link);
}
match $vfs.readlink_abs(&link) {
Ok(x) => {
if !target.has_suffix(&x) {
panic_msg!("assert_vfs_readlink_abs!", "link target doesn't equal given path", &x);
}
},
_ => panic_msg!("assert_vfs_readlink_abs!", "failed while reading link", &link),
};
};
}
#[macro_export]
macro_rules! assert_vfs_remove {
($vfs:expr, $path:expr) => {
let target = match $vfs.abs($path) {
Ok(x) => x,
_ => panic_msg!("assert_vfs_remove!", "failed to get absolute path", $path),
};
if $vfs.exists(&target) {
if !$vfs.is_dir(&target) {
if $vfs.remove(&target).is_err() {
panic_msg!("assert_vfs_remove!", "failed removing file", &target);
}
if $vfs.exists(&target) {
panic_msg!("assert_vfs_remove!", "file still exists", &target);
}
} else {
if $vfs.remove(&target).is_err() {
panic_msg!("assert_vfs_remove!", "failed removing directory", &target);
}
if $vfs.exists(&target) {
panic_msg!("assert_vfs_remove!", "directory still exists", &target);
}
}
}
};
}
#[macro_export]
macro_rules! assert_vfs_remove_all {
($vfs:expr, $path:expr) => {
let target = match $vfs.abs($path) {
Ok(x) => x,
_ => panic_msg!("assert_vfs_remove_all!", "failed to get absolute path", $path),
};
if $vfs.remove_all(&target).is_err() {
panic_msg!("assert_vfs_remove_all!", "failed while removing", &target);
}
if $vfs.exists(&target) {
panic_msg!("assert_vfs_remove_all!", "still exists", &target);
}
};
}
#[macro_export]
macro_rules! assert_vfs_symlink {
($vfs:expr, $link:expr, $target:expr) => {
let link = match $vfs.abs($link) {
Ok(x) => x,
_ => panic_msg!("assert_vfs_symlink!", "failed to get absolute path", $link),
};
if $vfs.exists(&link) {
if !$vfs.is_symlink(&link) {
panic_msg!("assert_vfs_symlink!", "is not a symlink", &link);
}
} else {
match $vfs.symlink(&link, $target) {
Ok(x) => {
if &x != &link {
panic_compare_msg!("assert_vfs_symlink!", "created link path doesn't match", &x, &link);
}
},
_ => panic_msg!("assert_vfs_symlink!", "failed while creating symlink", &link),
};
if !$vfs.is_symlink(&link) {
panic_msg!("assert_vfs_symlink!", "symlink doesn't exist", &link);
}
}
};
}
#[macro_export]
macro_rules! assert_vfs_write_all {
($vfs:expr, $path:expr, $data:expr) => {
let target = match $vfs.abs($path) {
Ok(x) => x,
_ => panic_msg!("assert_vfs_write_all!", "failed to get absolute path", $path),
};
if $vfs.exists(&target) {
if !$vfs.is_file(&target) {
panic_msg!("assert_vfs_write_all!", "is not a file", &target);
}
} else {
match $vfs.write_all(&target, $data) {
Ok(_) => {
if !$vfs.is_file(&target) {
panic_msg!("assert_vfs_write_all!", "is not a file", &target);
}
},
_ => panic_msg!("assert_vfs_write_all!", "failed while writing file", &target),
};
}
};
}
#[macro_export]
macro_rules! panic_msg {
($name:expr, $msg:expr, $target:expr) => {
panic!("\n{}: {}\n target: {}\n", $name, $msg, format!("{:?}", $target))
};
}
#[macro_export]
macro_rules! panic_compare_msg {
($name:expr, $msg:expr, $actual:expr, $target:expr) => {
panic!(
"\n{}: {}\n actual: {}\n target: {}\n",
$name,
$msg,
format!("{:?}", $actual),
format!("{:?}", $target)
)
};
}
#[cfg(test)]
mod tests
{
use crate::prelude::*;
#[test]
fn test_vfs_setup()
{
let (vfs, tmpdir) = assert_vfs_setup!(Vfs::memfs());
let expected =
vfs.root().mash(testing::TEST_TEMP_DIR).mash("rivia::testing::assert::tests::test_vfs_setup");
assert_eq!(&tmpdir, &expected);
assert_vfs_exists!(vfs, &expected);
let (vfs, tmpdir) = assert_vfs_setup!(Vfs::memfs(), "foobar_vfs_setup");
let expected = vfs.root().mash(testing::TEST_TEMP_DIR).mash("foobar_vfs_setup");
assert_eq!(&tmpdir, &expected);
assert_vfs_exists!(vfs, &expected);
}
#[test]
fn test_assert_vfs_copyfile()
{
let (vfs, tmpdir) = assert_vfs_setup!(Vfs::memfs());
let file1 = tmpdir.mash("file1");
let file2 = tmpdir.mash("file2");
let dir1 = tmpdir.mash("dir1");
assert_vfs_mkdir_p!(vfs, &dir1);
let result = testing::capture_panic(|| {
assert_vfs_copyfile!(vfs, "", "");
});
assert_eq!(
result.unwrap_err().to_string(),
"\nassert_vfs_copyfile!: failed to get absolute src path\n target: \"\"\n"
);
let result = testing::capture_panic(|| {
assert_vfs_copyfile!(vfs, "foo", "");
});
assert_eq!(
result.unwrap_err().to_string(),
"\nassert_vfs_copyfile!: failed to get absolute dst path\n target: \"\"\n"
);
let result = testing::capture_panic(|| {
assert_vfs_copyfile!(vfs, &file1, &file2);
});
assert_eq!(
result.unwrap_err().to_string(),
format!("\nassert_vfs_copyfile!: doesn't exist\n target: {:?}\n", &file1)
);
let result = testing::capture_panic(|| {
assert_vfs_copyfile!(vfs, &dir1, &file2);
});
assert_eq!(
result.unwrap_err().to_string(),
format!("\nassert_vfs_copyfile!: is not a file\n target: {:?}\n", &dir1)
);
assert_vfs_write_all!(vfs, &file1, "this is a test");
assert_vfs_copyfile!(vfs, &file1, &file2);
}
#[test]
fn test_assert_vfs_exists_and_no_exists()
{
let (vfs, tmpdir) = assert_vfs_setup!(Vfs::memfs());
{
let file = tmpdir.mash("file");
assert_vfs_no_exists!(vfs, &file);
assert!(!vfs.exists(&file));
assert_vfs_mkfile!(vfs, &file);
assert_vfs_exists!(vfs, &file);
assert!(vfs.exists(&file));
assert_vfs_remove!(vfs, &file);
assert_vfs_no_exists!(vfs, &file);
assert!(!vfs.exists(&file));
}
{
let dir1 = tmpdir.mash("dir1");
assert_vfs_no_exists!(vfs, &dir1);
assert!(!vfs.exists(&dir1));
assert_vfs_mkdir_p!(vfs, &dir1);
assert_vfs_exists!(vfs, &dir1);
assert!(vfs.exists(&dir1));
assert_vfs_remove_all!(vfs, &dir1);
assert_vfs_no_exists!(vfs, &dir1);
assert!(!vfs.exists(&dir1));
}
let result = testing::capture_panic(|| {
assert_vfs_exists!(vfs, "");
});
assert_eq!(
result.unwrap_err().to_string(),
"\nassert_vfs_exists!: failed to get absolute path\n target: \"\"\n"
);
let file1 = tmpdir.mash("file1");
let result = testing::capture_panic(|| {
assert_vfs_exists!(vfs, &file1);
});
assert_eq!(
result.unwrap_err().to_string(),
format!("\nassert_vfs_exists!: doesn't exist\n target: {:?}\n", &file1)
);
let result = testing::capture_panic(|| {
assert_vfs_no_exists!(vfs, "");
});
assert_eq!(
result.unwrap_err().to_string(),
"\nassert_vfs_no_exists!: failed to get absolute path\n target: \"\"\n"
);
assert_vfs_mkfile!(vfs, &file1);
let result = testing::capture_panic(|| {
assert_vfs_no_exists!(vfs, &file1);
});
assert_eq!(
result.unwrap_err().to_string(),
format!("\nassert_vfs_no_exists!: still exists\n target: {:?}\n", &file1)
);
assert_vfs_remove_all!(vfs, &tmpdir);
}
#[test]
fn test_assert_vfs_is_dir_no_dir()
{
let (vfs, tmpdir) = assert_vfs_setup!(Vfs::memfs());
let dir1 = tmpdir.mash("dir1");
let dir2 = tmpdir.mash("dir2");
assert_vfs_no_dir!(vfs, &dir1);
assert!(!vfs.is_dir(&dir1));
assert_vfs_mkdir_p!(vfs, &dir1);
assert_vfs_is_dir!(vfs, &dir1);
assert!(vfs.is_dir(&dir1));
let result = testing::capture_panic(|| {
assert_vfs_is_dir!(vfs, "");
});
assert_eq!(
result.unwrap_err().to_string(),
"\nassert_vfs_is_dir!: failed to get absolute path\n target: \"\"\n"
);
let result = testing::capture_panic(|| {
assert_vfs_is_dir!(vfs, &dir2);
});
assert_eq!(
result.unwrap_err().to_string(),
format!("\nassert_vfs_is_dir!: doesn't exist\n target: {:?}\n", &dir2)
);
let result = testing::capture_panic(|| {
assert_vfs_no_dir!(vfs, "");
});
assert_eq!(
result.unwrap_err().to_string(),
"\nassert_vfs_no_dir!: failed to get absolute path\n target: \"\"\n"
);
let result = testing::capture_panic(|| {
assert_vfs_no_dir!(vfs, &dir1);
});
assert_eq!(
result.unwrap_err().to_string(),
format!("\nassert_vfs_no_dir!: directory still exists\n target: {:?}\n", &dir1)
);
assert_vfs_remove_all!(vfs, &tmpdir);
}
#[test]
fn test_assert_vfs_is_file_no_file()
{
let (vfs, tmpdir) = assert_vfs_setup!(Vfs::memfs());
let file1 = tmpdir.mash("file1");
let file2 = tmpdir.mash("file2");
assert_vfs_no_file!(vfs, &file1);
assert!(!vfs.is_file(&file1));
assert_vfs_mkfile!(vfs, &file1);
assert_vfs_is_file!(vfs, &file1);
assert!(vfs.is_file(&file1));
let result = testing::capture_panic(|| {
assert_vfs_is_file!(vfs, "");
});
assert_eq!(
result.unwrap_err().to_string(),
"\nassert_vfs_is_file!: failed to get absolute path\n target: \"\"\n"
);
let result = testing::capture_panic(|| {
assert_vfs_is_file!(vfs, &file2);
});
assert_eq!(
result.unwrap_err().to_string(),
format!("\nassert_vfs_is_file!: doesn't exist\n target: {:?}\n", &file2)
);
let result = testing::capture_panic(|| {
assert_vfs_no_file!(vfs, "");
});
assert_eq!(
result.unwrap_err().to_string(),
"\nassert_vfs_no_file!: failed to get absolute path\n target: \"\"\n"
);
let result = testing::capture_panic(|| {
assert_vfs_no_file!(vfs, &file1);
});
assert_eq!(
result.unwrap_err().to_string(),
format!("\nassert_vfs_no_file!: file still exists\n target: {:?}\n", &file1)
);
assert_vfs_remove_all!(vfs, &tmpdir);
}
#[test]
fn test_assert_vfs_is_symlink_no_symlink()
{
let (vfs, tmpdir) = assert_vfs_setup!(Vfs::memfs());
let file1 = tmpdir.mash("file1");
let link1 = tmpdir.mash("link1");
assert_vfs_no_symlink!(vfs, &file1);
assert!(!vfs.is_symlink(&file1));
assert_vfs_symlink!(vfs, &link1, &file1);
assert_vfs_is_symlink!(vfs, &link1);
assert!(vfs.is_symlink(&link1));
let result = testing::capture_panic(|| {
assert_vfs_is_symlink!(vfs, "");
});
assert_eq!(
result.unwrap_err().to_string(),
"\nassert_vfs_is_symlink!: failed to get absolute path\n target: \"\"\n"
);
let result = testing::capture_panic(|| {
assert_vfs_is_symlink!(vfs, &file1);
});
assert_eq!(
result.unwrap_err().to_string(),
format!("\nassert_vfs_is_symlink!: symlink doesn't exist\n target: {:?}\n", &file1)
);
let result = testing::capture_panic(|| {
assert_vfs_no_symlink!(vfs, "");
});
assert_eq!(
result.unwrap_err().to_string(),
"\nassert_vfs_no_symlink!: failed to get absolute path\n target: \"\"\n"
);
let result = testing::capture_panic(|| {
assert_vfs_no_symlink!(vfs, &link1);
});
assert_eq!(
result.unwrap_err().to_string(),
format!("\nassert_vfs_no_symlink!: exists and is a symlink\n target: {:?}\n", &link1)
);
assert_vfs_mkfile!(vfs, &file1);
assert_vfs_no_symlink!(vfs, &file1);
assert_vfs_remove_all!(vfs, &tmpdir);
}
#[test]
fn test_assert_vfs_mkdir_m()
{
let vfs = Memfs::new();
let file1 = vfs.root().mash("file1");
let dir1 = vfs.root().mash("dir1");
assert_vfs_mkfile!(vfs, &file1);
let result = testing::capture_panic(|| {
assert_vfs_mkdir_m!(vfs, "", 0o40777);
});
assert_eq!(
result.unwrap_err().to_string(),
"\nassert_vfs_mkdir_m!: failed to get absolute path\n target: \"\"\n"
);
let result = testing::capture_panic(|| {
assert_vfs_mkdir_m!(vfs, &file1, 0o40777);
});
assert_eq!(
result.unwrap_err().to_string(),
format!("assert_vfs_mkdir_m!: Target path is not a directory: {}", &file1.display())
);
assert_vfs_no_dir!(vfs, &dir1);
assert_vfs_mkdir_m!(vfs, &dir1, 0o40777);
assert_eq!(vfs.mode(&dir1).unwrap(), 0o40777);
assert_vfs_is_dir!(vfs, &dir1);
}
#[test]
fn test_assert_vfs_mkdir_p()
{
let (vfs, tmpdir) = assert_vfs_setup!(Vfs::memfs());
let file1 = tmpdir.mash("file1");
let dir1 = tmpdir.mash("dir1");
assert_vfs_mkfile!(vfs, &file1);
let result = testing::capture_panic(|| {
assert_vfs_mkdir_p!(vfs, "");
});
assert_eq!(
result.unwrap_err().to_string(),
"\nassert_vfs_mkdir_p!: failed to get absolute path\n target: \"\"\n"
);
let result = testing::capture_panic(|| {
assert_vfs_mkdir_p!(vfs, &file1);
});
assert_eq!(
result.unwrap_err().to_string(),
format!("assert_vfs_mkdir_p!: Target path is not a directory: {}", &file1.display())
);
assert_vfs_no_dir!(vfs, &dir1);
assert_vfs_mkdir_p!(vfs, &dir1);
assert_vfs_is_dir!(vfs, &dir1);
assert_vfs_remove_all!(vfs, &tmpdir);
}
#[test]
fn test_assert_vfs_mkfile()
{
let (vfs, tmpdir) = assert_vfs_setup!(Vfs::memfs());
let file1 = tmpdir.mash("file1");
let dir1 = tmpdir.mash("dir1");
assert_vfs_mkdir_p!(vfs, &dir1);
let result = testing::capture_panic(|| {
assert_vfs_mkfile!(vfs, "");
});
assert_eq!(
result.unwrap_err().to_string(),
"\nassert_vfs_mkfile!: failed to get absolute path\n target: \"\"\n"
);
let result = testing::capture_panic(|| {
assert_vfs_mkfile!(vfs, &dir1);
});
assert_eq!(
result.unwrap_err().to_string(),
format!("\nassert_vfs_mkfile!: is not a file\n target: \"{}\"\n", dir1.display())
);
assert_vfs_no_file!(vfs, &file1);
assert_vfs_mkfile!(vfs, &file1);
assert_vfs_is_file!(vfs, &file1);
assert_vfs_remove_all!(vfs, &tmpdir);
}
#[test]
fn test_assert_vfs_read_all()
{
let (vfs, tmpdir) = assert_vfs_setup!(Vfs::memfs());
let result = testing::capture_panic(|| {
assert_vfs_read_all!(vfs, "", "");
});
assert_eq!(
result.unwrap_err().to_string(),
"\nassert_vfs_read_all!: failed to get absolute path\n target: \"\"\n"
);
let result = testing::capture_panic(|| {
assert_vfs_read_all!(vfs, &tmpdir, "");
});
assert_eq!(
result.unwrap_err().to_string(),
format!(
"\nassert_vfs_read_all!: file doesn't exist or is not a file\n target: \"{}\"\n",
tmpdir.display()
)
);
let file = tmpdir.mash("file");
assert_vfs_write_all!(vfs, &file, "foobar 1");
let result = testing::capture_panic(|| {
assert_vfs_read_all!(vfs, &file, "foobar");
});
assert_eq!(
result.unwrap_err().to_string(),
format!(
"\nassert_vfs_read_all!: read data doesn't equal given data\n read: foobar 1\n expected: foobar\n target: \"{}\"\n",
file.display()
)
);
assert_vfs_read_all!(vfs, &file, "foobar 1".to_string());
assert_vfs_remove_all!(vfs, &tmpdir);
}
#[test]
fn test_assert_vfs_readlink()
{
let (vfs, tmpdir) = assert_vfs_setup!(Vfs::memfs());
let dir = tmpdir.mash("dir");
let link = dir.mash("link");
let file = tmpdir.mash("file");
assert_vfs_mkdir_p!(vfs, &dir);
assert_vfs_mkfile!(vfs, &file);
let result = testing::capture_panic(|| {
assert_vfs_readlink!(vfs, "", &file);
});
assert_eq!(
result.unwrap_err().to_string(),
"\nassert_vfs_readlink!: failed to get absolute path\n target: \"\"\n"
);
let result = testing::capture_panic(|| {
assert_vfs_readlink!(vfs, &dir, &file);
});
assert_eq!(
result.unwrap_err().to_string(),
format!(
"\nassert_vfs_readlink!: file doesn't exist or is not a symlink\n target: \"{}\"\n",
dir.display()
)
);
assert_vfs_no_symlink!(vfs, &link);
assert_vfs_symlink!(vfs, &link, &file);
assert_vfs_is_symlink!(vfs, &link);
assert_vfs_readlink!(vfs, &link, PathBuf::from("..").mash("file"));
assert_vfs_remove_all!(vfs, &tmpdir);
}
#[test]
fn test_assert_vfs_readlink_abs()
{
let (vfs, tmpdir) = assert_vfs_setup!(Vfs::memfs());
let dir = tmpdir.mash("dir");
let link = dir.mash("link");
let file = tmpdir.mash("file");
assert_vfs_mkdir_p!(vfs, &dir);
assert_vfs_mkfile!(vfs, &file);
let result = testing::capture_panic(|| {
assert_vfs_readlink_abs!(vfs, "", &file);
});
assert_eq!(
result.unwrap_err().to_string(),
"\nassert_vfs_readlink_abs!: failed to get absolute path\n target: \"\"\n"
);
let result = testing::capture_panic(|| {
assert_vfs_readlink_abs!(vfs, &link, PathBuf::new());
});
assert_eq!(
result.unwrap_err().to_string(),
"\nassert_vfs_readlink_abs!: failed to get absolute path\n target: \"\"\n"
);
let result = testing::capture_panic(|| {
assert_vfs_readlink_abs!(vfs, &dir, &file);
});
assert_eq!(
result.unwrap_err().to_string(),
format!(
"\nassert_vfs_readlink_abs!: file doesn't exist or is not a symlink\n target: \"{}\"\n",
dir.display()
)
);
assert_vfs_no_symlink!(vfs, &link);
assert_vfs_symlink!(vfs, &link, &file);
assert_vfs_is_symlink!(vfs, &link);
assert_vfs_readlink_abs!(vfs, &link, &file);
assert_vfs_remove_all!(vfs, &tmpdir);
}
#[test]
fn test_assert_vfs_remove()
{
let (vfs, tmpdir) = assert_vfs_setup!(Vfs::memfs());
let file1 = tmpdir.mash("file1");
assert_vfs_remove!(vfs, &file1);
assert_vfs_mkfile!(vfs, &file1);
assert_vfs_is_file!(vfs, &file1);
assert_vfs_remove!(vfs, &file1);
assert_vfs_no_file!(vfs, &file1);
let result = testing::capture_panic(|| {
assert_vfs_remove!(vfs, "");
});
assert_eq!(
result.unwrap_err().to_string(),
"\nassert_vfs_remove!: failed to get absolute path\n target: \"\"\n"
);
assert_vfs_mkfile!(vfs, &file1);
let result = testing::capture_panic(|| {
assert_vfs_remove!(vfs, &tmpdir);
});
assert_eq!(
result.unwrap_err().to_string(),
format!("\nassert_vfs_remove!: failed removing directory\n target: {:?}\n", &tmpdir)
);
assert_vfs_remove_all!(vfs, &tmpdir);
}
#[test]
fn test_assert_vfs_remove_all()
{
let (vfs, tmpdir) = assert_vfs_setup!(Vfs::memfs());
let file1 = tmpdir.mash("file1");
assert_vfs_mkfile!(vfs, &file1);
assert_vfs_is_file!(vfs, &file1);
assert_vfs_remove_all!(vfs, &tmpdir);
assert_vfs_no_dir!(vfs, &tmpdir);
let result = testing::capture_panic(|| {
assert_vfs_remove_all!(vfs, "");
});
assert_eq!(
result.unwrap_err().to_string(),
"\nassert_vfs_remove_all!: failed to get absolute path\n target: \"\"\n"
);
}
#[test]
fn test_assert_vfs_symlink()
{
let (vfs, tmpdir) = assert_vfs_setup!(Vfs::memfs());
let dir1 = tmpdir.mash("dir1");
let file1 = dir1.mash("file1");
let link1 = tmpdir.mash("link1");
assert_vfs_mkdir_p!(vfs, &dir1);
assert_vfs_mkfile!(vfs, &file1);
let result = testing::capture_panic(|| {
assert_vfs_symlink!(vfs, "", "");
});
assert_eq!(
result.unwrap_err().to_string(),
"\nassert_vfs_symlink!: failed to get absolute path\n target: \"\"\n"
);
let result = testing::capture_panic(|| {
assert_vfs_symlink!(vfs, &dir1, "");
});
assert_eq!(
result.unwrap_err().to_string(),
format!("\nassert_vfs_symlink!: is not a symlink\n target: \"{}\"\n", dir1.display())
);
assert_vfs_no_symlink!(vfs, &link1);
assert_vfs_symlink!(vfs, &link1, &file1);
assert_vfs_is_symlink!(vfs, &link1);
assert_vfs_remove_all!(vfs, &tmpdir);
}
#[test]
fn test_assert_vfs_write_all()
{
let (vfs, tmpdir) = assert_vfs_setup!(Vfs::memfs());
let result = testing::capture_panic(|| {
assert_vfs_write_all!(vfs, "", "");
});
assert_eq!(
result.unwrap_err().to_string(),
"\nassert_vfs_write_all!: failed to get absolute path\n target: \"\"\n"
);
let result = testing::capture_panic(|| {
assert_vfs_write_all!(vfs, &tmpdir, "");
});
assert_eq!(
result.unwrap_err().to_string(),
format!("\nassert_vfs_write_all!: is not a file\n target: \"{}\"\n", &tmpdir.display())
);
let file = tmpdir.mash("foo");
assert_vfs_write_all!(vfs, &file, b"foobar 1");
assert_vfs_read_all!(vfs, &file, "foobar 1".to_string());
assert_vfs_remove_all!(vfs, &tmpdir);
}
}