added some test stubs

This commit is contained in:
Robin Müller 2024-02-03 00:44:36 +01:00
parent 475a587f78
commit b760d1ea57
Signed by: muellerr
GPG Key ID: A649FB78196E3849

View File

@ -98,7 +98,8 @@ pub trait VirtualFilestore {
/// This can be more efficient than removing and re-creating a file. /// This can be more efficient than removing and re-creating a file.
fn truncate_file(&self, file_path: &str) -> Result<(), FilestoreError>; fn truncate_file(&self, file_path: &str) -> Result<(), FilestoreError>;
fn remove_dir(&self, file_path: &str, all: bool) -> Result<(), FilestoreError>; fn remove_dir(&self, dir_path: &str, all: bool) -> Result<(), FilestoreError>;
fn create_dir(&self, dir_path: &str) -> Result<(), FilestoreError>;
fn read_data( fn read_data(
&self, &self,
@ -155,7 +156,7 @@ pub mod stdmod {
fn remove_file(&self, file_path: &str) -> Result<(), FilestoreError> { fn remove_file(&self, file_path: &str) -> Result<(), FilestoreError> {
if !self.exists(file_path) { if !self.exists(file_path) {
return Ok(()); return Err(FilestoreError::FileDoesNotExist);
} }
if !self.is_file(file_path) { if !self.is_file(file_path) {
return Err(FilestoreError::IsNotFile); return Err(FilestoreError::IsNotFile);
@ -166,7 +167,7 @@ pub mod stdmod {
fn truncate_file(&self, file_path: &str) -> Result<(), FilestoreError> { fn truncate_file(&self, file_path: &str) -> Result<(), FilestoreError> {
if !self.exists(file_path) { if !self.exists(file_path) {
return Ok(()); return Err(FilestoreError::FileDoesNotExist);
} }
if !self.is_file(file_path) { if !self.is_file(file_path) {
return Err(FilestoreError::IsNotFile); return Err(FilestoreError::IsNotFile);
@ -178,6 +179,14 @@ pub mod stdmod {
Ok(()) Ok(())
} }
fn create_dir(&self, dir_path: &str) -> Result<(), FilestoreError> {
fs::create_dir(dir_path).map_err(|e| FilestoreError::Io {
raw_errno: e.raw_os_error(),
string: e.to_string(),
})?;
Ok(())
}
fn remove_dir(&self, dir_path: &str, all: bool) -> Result<(), FilestoreError> { fn remove_dir(&self, dir_path: &str, all: bool) -> Result<(), FilestoreError> {
if !self.exists(dir_path) { if !self.exists(dir_path) {
return Err(FilestoreError::DirDoesNotExist); return Err(FilestoreError::DirDoesNotExist);
@ -316,7 +325,7 @@ mod tests {
} }
#[test] #[test]
fn test_basic_native_fs_exists() { fn test_basic_native_fs_file_exists() {
let tmpdir = tempdir().expect("creating tmpdir failed"); let tmpdir = tempdir().expect("creating tmpdir failed");
let file_path = tmpdir.path().join("test.txt"); let file_path = tmpdir.path().join("test.txt");
assert!(!NATIVE_FS.exists(file_path.to_str().unwrap())); assert!(!NATIVE_FS.exists(file_path.to_str().unwrap()));
@ -328,6 +337,34 @@ mod tests {
fs::remove_dir_all(tmpdir).expect("clearing tmpdir failed"); fs::remove_dir_all(tmpdir).expect("clearing tmpdir failed");
} }
#[test]
fn test_basic_native_fs_dir_exists() {
let tmpdir = tempdir().expect("creating tmpdir failed");
let dir_path = tmpdir.path().join("testdir");
assert!(!NATIVE_FS.exists(dir_path.to_str().unwrap()));
NATIVE_FS
.create_dir(dir_path.to_str().expect("getting str for file failed"))
.unwrap();
assert!(NATIVE_FS.exists(dir_path.to_str().unwrap()));
assert!(NATIVE_FS.is_dir(dir_path.as_path().to_str().unwrap()));
fs::remove_dir_all(tmpdir).expect("clearing tmpdir failed");
}
#[test]
fn test_basic_native_fs_remove_file() {
let tmpdir = tempdir().expect("creating tmpdir failed");
let file_path = tmpdir.path().join("test.txt");
NATIVE_FS
.create_file(file_path.to_str().expect("getting str for file failed"))
.expect("creating file failed");
assert!(NATIVE_FS.exists(file_path.to_str().unwrap()));
NATIVE_FS
.remove_file(file_path.to_str().unwrap())
.expect("removing file failed");
assert!(!NATIVE_FS.exists(file_path.to_str().unwrap()));
fs::remove_dir_all(tmpdir).expect("clearing tmpdir failed");
}
#[test] #[test]
fn test_basic_native_fs_write() { fn test_basic_native_fs_write() {
let tmpdir = tempdir().expect("creating tmpdir failed"); let tmpdir = tempdir().expect("creating tmpdir failed");
@ -367,4 +404,110 @@ mod tests {
assert_eq!(read_back, write_data); assert_eq!(read_back, write_data);
fs::remove_dir_all(tmpdir).expect("clearing tmpdir failed"); fs::remove_dir_all(tmpdir).expect("clearing tmpdir failed");
} }
#[test]
fn test_truncate_file() {
let tmpdir = tempdir().expect("creating tmpdir failed");
let file_path = tmpdir.path().join("test.txt");
NATIVE_FS
.create_file(file_path.to_str().expect("getting str for file failed"))
.expect("creating file failed");
fs::write(file_path.clone(), [1, 2, 3, 4]).unwrap();
assert_eq!(fs::read(file_path.clone()).unwrap(), [1, 2, 3, 4]);
NATIVE_FS
.truncate_file(file_path.to_str().unwrap())
.unwrap();
assert_eq!(fs::read(file_path.clone()).unwrap(), []);
fs::remove_dir_all(tmpdir).expect("clearing tmpdir failed");
}
#[test]
fn test_remove_dir() {
let tmpdir = tempdir().expect("creating tmpdir failed");
let dir_path = tmpdir.path().join("testdir");
assert!(!NATIVE_FS.exists(dir_path.to_str().unwrap()));
NATIVE_FS
.create_dir(dir_path.to_str().expect("getting str for file failed"))
.unwrap();
assert!(NATIVE_FS.exists(dir_path.to_str().unwrap()));
NATIVE_FS
.remove_dir(dir_path.to_str().unwrap(), false)
.unwrap();
assert!(!NATIVE_FS.exists(dir_path.to_str().unwrap()));
fs::remove_dir_all(tmpdir).expect("clearing tmpdir failed");
}
#[test]
fn test_read_file() {
let tmpdir = tempdir().expect("creating tmpdir failed");
let file_path = tmpdir.path().join("test.txt");
NATIVE_FS
.create_file(file_path.to_str().expect("getting str for file failed"))
.expect("creating file failed");
fs::write(file_path.clone(), [1, 2, 3, 4]).unwrap();
let read_buf: &mut [u8] = &mut [0; 4];
NATIVE_FS
.read_data(file_path.to_str().unwrap(), 0, 4, read_buf)
.unwrap();
assert_eq!([1, 2, 3, 4], read_buf);
NATIVE_FS
.write_data(file_path.to_str().unwrap(), 4, &[5, 6, 7, 8])
.expect("writing to file failed");
NATIVE_FS
.read_data(file_path.to_str().unwrap(), 2, 4, read_buf)
.unwrap();
assert_eq!([3, 4, 5, 6], read_buf);
}
#[test]
fn test_remove_which_does_not_exist() {
let tmpdir = tempdir().expect("creating tmpdir failed");
let file_path = tmpdir.path().join("test.txt");
let result = NATIVE_FS.read_data(file_path.to_str().unwrap(), 0, 4, &mut [0; 4]);
assert!(result.is_err());
let error = result.unwrap_err();
if let FilestoreError::FileDoesNotExist = error {
assert_eq!(error.to_string(), "file does not exist");
} else {
panic!("unexpected error");
}
}
#[test]
fn test_file_already_exists() {
let tmpdir = tempdir().expect("creating tmpdir failed");
let file_path = tmpdir.path().join("test.txt");
let result =
NATIVE_FS.create_file(file_path.to_str().expect("getting str for file failed"));
assert!(result.is_ok());
let result =
NATIVE_FS.create_file(file_path.to_str().expect("getting str for file failed"));
assert!(result.is_err());
let error = result.unwrap_err();
if let FilestoreError::FileAlreadyExists = error {
assert_eq!(error.to_string(), "file already exists");
} else {
panic!("unexpected error");
}
}
#[test]
fn test_remove_file_with_dir_api() {
// TODO
}
#[test]
fn test_remove_dir_with_file_api() {
// TODO
}
#[test]
fn test_remove_dir_which_does_not_exist() {
// TODO
}
#[test]
fn test_byte_conversion_error_when_reading() {
// TODO
}
} }