mod common;
use std::io::Cursor;
use std::path::PathBuf;
use ::common::mount::MountError;
#[tokio::test]
async fn test_mv_file() {
let (mut mount, _, _, _temp) = common::setup_test_env().await;
mount
.add(&PathBuf::from("/old.txt"), Cursor::new(b"data".to_vec()))
.await
.unwrap();
mount
.mv(&PathBuf::from("/old.txt"), &PathBuf::from("/new.txt"))
.await
.unwrap();
let result = mount.cat(&PathBuf::from("/old.txt")).await;
assert!(result.is_err());
let data = mount.cat(&PathBuf::from("/new.txt")).await.unwrap();
assert_eq!(data, b"data");
}
#[tokio::test]
async fn test_mv_file_to_subdir() {
let (mut mount, _, _, _temp) = common::setup_test_env().await;
mount
.add(&PathBuf::from("/file.txt"), Cursor::new(b"data".to_vec()))
.await
.unwrap();
mount
.mv(
&PathBuf::from("/file.txt"),
&PathBuf::from("/subdir/file.txt"),
)
.await
.unwrap();
let result = mount.cat(&PathBuf::from("/file.txt")).await;
assert!(result.is_err());
let data = mount.cat(&PathBuf::from("/subdir/file.txt")).await.unwrap();
assert_eq!(data, b"data");
}
#[tokio::test]
async fn test_mv_directory() {
let (mut mount, _, _, _temp) = common::setup_test_env().await;
mount
.add(
&PathBuf::from("/olddir/file1.txt"),
Cursor::new(b"data1".to_vec()),
)
.await
.unwrap();
mount
.add(
&PathBuf::from("/olddir/file2.txt"),
Cursor::new(b"data2".to_vec()),
)
.await
.unwrap();
mount
.mv(&PathBuf::from("/olddir"), &PathBuf::from("/newdir"))
.await
.unwrap();
let result = mount.ls(&PathBuf::from("/olddir")).await;
assert!(result.is_err());
let items = mount.ls(&PathBuf::from("/newdir")).await.unwrap();
assert_eq!(items.len(), 2);
let data = mount
.cat(&PathBuf::from("/newdir/file1.txt"))
.await
.unwrap();
assert_eq!(data, b"data1");
}
#[tokio::test]
async fn test_mv_not_found() {
let (mut mount, _, _, _temp) = common::setup_test_env().await;
let result = mount
.mv(
&PathBuf::from("/nonexistent.txt"),
&PathBuf::from("/new.txt"),
)
.await;
assert!(matches!(result, Err(MountError::PathNotFound(_))));
}
#[tokio::test]
async fn test_mv_already_exists() {
let (mut mount, _, _, _temp) = common::setup_test_env().await;
mount
.add(&PathBuf::from("/file1.txt"), Cursor::new(b"data1".to_vec()))
.await
.unwrap();
mount
.add(&PathBuf::from("/file2.txt"), Cursor::new(b"data2".to_vec()))
.await
.unwrap();
let result = mount
.mv(&PathBuf::from("/file1.txt"), &PathBuf::from("/file2.txt"))
.await;
assert!(matches!(result, Err(MountError::PathAlreadyExists(_))));
}
#[tokio::test]
async fn test_mv_into_self() {
let (mut mount, _, _, _temp) = common::setup_test_env().await;
mount.mkdir(&PathBuf::from("/parent")).await.unwrap();
mount
.add(
&PathBuf::from("/parent/child.txt"),
Cursor::new(b"data".to_vec()),
)
.await
.unwrap();
let result = mount
.mv(&PathBuf::from("/parent"), &PathBuf::from("/parent/nested"))
.await;
assert!(matches!(result, Err(MountError::MoveIntoSelf { .. })));
let result = mount
.mv(&PathBuf::from("/parent"), &PathBuf::from("/parent"))
.await;
assert!(matches!(result, Err(MountError::MoveIntoSelf { .. })));
let items = mount.ls(&PathBuf::from("/parent")).await.unwrap();
assert_eq!(items.len(), 1);
let data = mount
.cat(&PathBuf::from("/parent/child.txt"))
.await
.unwrap();
assert_eq!(data, b"data");
}