Skip to content

Commit

Permalink
fix: test
Browse files Browse the repository at this point in the history
  • Loading branch information
veeso committed Nov 2, 2024
1 parent 6396fbc commit df57e8b
Show file tree
Hide file tree
Showing 3 changed files with 227 additions and 5 deletions.
10 changes: 5 additions & 5 deletions remotefs-fuse/src/driver/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ where
return Ok(stat);
}

let path_info = self.path_info(file_name);
let path_info = Self::path_info(file_name);

let file = self.remote(|remote| remote.stat(&path_info.path))?;

Expand All @@ -137,7 +137,7 @@ where
}

/// Get the path information for a given `file_name`.
fn path_info(&self, file_name: &U16CStr) -> PathInfo {
fn path_info(file_name: &U16CStr) -> PathInfo {
let p = PathBuf::from(file_name.to_string_lossy());
let parent = p
.parent()
Expand Down Expand Up @@ -708,7 +708,7 @@ where
} else if create_disposition == FILE_OPEN || create_disposition == FILE_OPEN_IF {
if create_options & FILE_NON_DIRECTORY_FILE > 0 {
debug!("create file: {file_name:?}");
let path_info = self.path_info(file_name);
let path_info = Self::path_info(file_name);
if let Err(err) = self.write(
&File {
path: path_info.path,
Expand Down Expand Up @@ -744,7 +744,7 @@ where
// create directory
debug!("create directory: {file_name:?}");
let stat = {
let path_info = self.path_info(file_name);
let path_info = Self::path_info(file_name);

if let Err(err) = self
.remote(|remote| remote.create_dir(&path_info.path, UnixPex::from(0o755)))
Expand Down Expand Up @@ -1297,7 +1297,7 @@ where
) -> OperationResult<()> {
info!("move_file({file_name:?}, {new_file_name:?}, {replace_if_existing:?}, {context:?})");

let dest = self.path_info(new_file_name);
let dest = Self::path_info(new_file_name);
// check if destination exists
if !replace_if_existing
&& self
Expand Down
97 changes: 97 additions & 0 deletions remotefs-fuse/src/driver/windows/test.rs
Original file line number Diff line number Diff line change
@@ -1 +1,98 @@
use std::path::{Path, PathBuf};

use pretty_assertions::{assert_eq, assert_ne};
use remotefs::fs::{FileType, Metadata, UnixPex};
use remotefs::File;
use remotefs_memory::MemoryFs;
use widestring::U16CString;

use super::Driver;
use crate::driver::windows::ROOT_ID;

#[test]
fn test_should_get_file_index() {
let index = Driver::<MemoryFs>::file_index(&File {
path: PathBuf::from("C:\\Users\\user\\Desktop\\file.txt"),
metadata: Default::default(),
});
assert_ne!(index, ROOT_ID);

let index = Driver::<MemoryFs>::file_index(&File {
path: PathBuf::from("/"),
metadata: Default::default(),
});

assert_eq!(index, ROOT_ID);
}

#[test]
fn test_should_get_filename() {
let filename = Driver::<MemoryFs>::file_name(Path::new("C:\\Users\\user\\Desktop\\file.txt"));
let expected = U16CString::from_str("file.txt").unwrap().to_ucstring();
assert_eq!(filename, expected);
}

#[test]
fn test_should_make_attributes_from_file() {
let file = File {
path: PathBuf::from("C:\\Users\\user\\Desktop\\file.txt"),
metadata: Metadata::default().file_type(FileType::File),
};

let attributes = Driver::<MemoryFs>::attributes_from_file(&file);
assert_eq!(attributes & winapi::um::winnt::FILE_ATTRIBUTE_DIRECTORY, 0);
assert_eq!(
attributes & winapi::um::winnt::FILE_ATTRIBUTE_NORMAL,
winapi::um::winnt::FILE_ATTRIBUTE_NORMAL
);
assert_eq!(attributes & winapi::um::winnt::FILE_ATTRIBUTE_READONLY, 0);

let file = File {
path: PathBuf::from("C:\\Users\\user\\Desktop"),
metadata: Metadata::default().file_type(FileType::Directory),
};

let attributes = Driver::<MemoryFs>::attributes_from_file(&file);
assert_eq!(
attributes & winapi::um::winnt::FILE_ATTRIBUTE_DIRECTORY,
winapi::um::winnt::FILE_ATTRIBUTE_DIRECTORY
);

let file = File {
path: PathBuf::from("C:\\Users\\user\\Desktop"),
metadata: Metadata::default()
.file_type(FileType::File)
.mode(UnixPex::from(0o444)),
};

let attributes = Driver::<MemoryFs>::attributes_from_file(&file);
assert_eq!(
attributes & winapi::um::winnt::FILE_ATTRIBUTE_READONLY,
winapi::um::winnt::FILE_ATTRIBUTE_READONLY
);

let file = File {
path: PathBuf::from("C:\\Users\\user\\Desktop\\.gitignore"),
metadata: Metadata::default().file_type(FileType::File),
};

let attributes = Driver::<MemoryFs>::attributes_from_file(&file);
assert_eq!(
attributes & winapi::um::winnt::FILE_ATTRIBUTE_HIDDEN,
winapi::um::winnt::FILE_ATTRIBUTE_HIDDEN
);
}

#[test]
fn test_should_get_path_info() {
let p = U16CString::from_str("/dev/null").unwrap();

let path_info = Driver::<MemoryFs>::path_info(&p);

assert_eq!(path_info.path, PathBuf::from("/dev/null"));
assert_eq!(
path_info.file_name,
U16CString::from_str("/dev/null").unwrap().to_ucstring()
);
assert_eq!(path_info.parent, PathBuf::from("/dev"));
}
125 changes: 125 additions & 0 deletions remotefs-fuse/src/mount/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,3 +326,128 @@ impl FromStr for MountOption {
}
}
}

#[cfg(test)]
mod test {

use pretty_assertions::assert_eq;

use super::*;

#[test]
fn test_should_convert_str_to_option() {
#[cfg(unix)]
assert_eq!(
MountOption::from_str("uid=1000").unwrap(),
MountOption::Uid(1000)
);
#[cfg(unix)]
assert_eq!(
MountOption::from_str("gid=1000").unwrap(),
MountOption::Gid(1000)
);
#[cfg(unix)]
assert_eq!(
MountOption::from_str("default_mode=0755").unwrap(),
MountOption::DefaultMode(0o755)
);
#[cfg(unix)]
assert_eq!(
MountOption::from_str("fsname=foo").unwrap(),
MountOption::FSName("foo".to_string())
);
#[cfg(unix)]
assert_eq!(
MountOption::from_str("subtype=foo").unwrap(),
MountOption::Subtype("foo".to_string())
);
#[cfg(unix)]
assert_eq!(
MountOption::from_str("custom=foo").unwrap(),
MountOption::Custom("foo".to_string())
);
#[cfg(unix)]
assert_eq!(
MountOption::from_str("allow_other").unwrap(),
MountOption::AllowOther
);
#[cfg(unix)]
assert_eq!(
MountOption::from_str("allow_root").unwrap(),
MountOption::AllowRoot
);
#[cfg(unix)]
assert_eq!(
MountOption::from_str("auto_unmount").unwrap(),
MountOption::AutoUnmount
);
#[cfg(unix)]
assert_eq!(
MountOption::from_str("default_permissions").unwrap(),
MountOption::DefaultPermissions
);
#[cfg(unix)]
assert_eq!(MountOption::from_str("dev").unwrap(), MountOption::Dev);
#[cfg(unix)]
assert_eq!(MountOption::from_str("nodev").unwrap(), MountOption::NoDev);
#[cfg(unix)]
assert_eq!(MountOption::from_str("suid").unwrap(), MountOption::Suid);
#[cfg(unix)]
assert_eq!(
MountOption::from_str("nosuid").unwrap(),
MountOption::NoSuid
);
#[cfg(unix)]
assert_eq!(MountOption::from_str("ro").unwrap(), MountOption::RO);
#[cfg(unix)]
assert_eq!(MountOption::from_str("rw").unwrap(), MountOption::RW);
#[cfg(unix)]
assert_eq!(MountOption::from_str("exec").unwrap(), MountOption::Exec);
#[cfg(unix)]
assert_eq!(
MountOption::from_str("noexec").unwrap(),
MountOption::NoExec
);
#[cfg(unix)]
assert_eq!(MountOption::from_str("atime").unwrap(), MountOption::Atime);
#[cfg(unix)]
assert_eq!(
MountOption::from_str("noatime").unwrap(),
MountOption::NoAtime
);
#[cfg(unix)]
assert_eq!(
MountOption::from_str("dirsync").unwrap(),
MountOption::DirSync
);
#[cfg(unix)]
assert_eq!(MountOption::from_str("sync").unwrap(), MountOption::Sync);
#[cfg(unix)]
assert_eq!(MountOption::from_str("async").unwrap(), MountOption::Async);
#[cfg(windows)]
assert_eq!(
MountOption::from_str("single_thread").unwrap(),
MountOption::SingleThread
);
#[cfg(windows)]
assert_eq!(
MountOption::from_str("flags=1").unwrap(),
MountOption::Flags(1)
);
#[cfg(windows)]
assert_eq!(
MountOption::from_str("timeout=1000").unwrap(),
MountOption::Timeout(Duration::from_millis(1000))
);
#[cfg(windows)]
assert_eq!(
MountOption::from_str("allocation_unit_size=4096").unwrap(),
MountOption::AllocationUnitSize(4096)
);
#[cfg(windows)]
assert_eq!(
MountOption::from_str("sector_size=512").unwrap(),
MountOption::SectorSize(512)
);
}
}

0 comments on commit df57e8b

Please sign in to comment.