rcom/tests/local_fs.rs
2026-12-05 23:30:06 +03:00

189 lines
5.8 KiB
Rust

use std::fs;
use std::path::Path;
use rcom::domain::{EntryKind, Location};
use rcom::fs::{FsError, LocalFs, ReadFileSystem};
use tempfile::TempDir;
#[tokio::test]
async fn local_fs_lists_an_empty_directory() {
let fixture = TempDir::new().unwrap();
let fs = LocalFs::new();
let entries = fs.list(&Location::local(fixture.path())).await.unwrap();
assert!(entries.is_empty());
}
#[tokio::test]
async fn local_fs_lists_real_names_kinds_metadata_and_symlink_target() {
let fixture = TempDir::new().unwrap();
fs::create_dir(fixture.path().join("folder")).unwrap();
fs::write(fixture.path().join("notes.txt"), b"notes").unwrap();
#[cfg(unix)]
std::os::unix::fs::symlink("notes.txt", fixture.path().join("notes-link")).unwrap();
let mut entries = LocalFs
.list(&Location::local(fixture.path()))
.await
.unwrap();
entries.sort_by(|left, right| left.name.cmp(&right.name));
let folder = entries.iter().find(|entry| entry.name == "folder").unwrap();
assert_eq!(folder.kind, EntryKind::Directory);
assert_eq!(folder.size, None);
assert!(folder.modified.is_some());
let file = entries
.iter()
.find(|entry| entry.name == "notes.txt")
.unwrap();
assert_eq!(file.kind, EntryKind::File);
assert_eq!(file.size, Some(5));
assert!(file.modified.is_some());
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
fs::set_permissions(
fixture.path().join("notes.txt"),
fs::Permissions::from_mode(0o640),
)
.unwrap();
let file = LocalFs
.metadata(&Location::local(fixture.path().join("notes.txt")))
.await
.unwrap();
assert_eq!(file.permissions, Some(0o640));
let link = entries
.iter()
.find(|entry| entry.name == "notes-link")
.unwrap();
assert_eq!(link.kind, EntryKind::Symlink);
assert_eq!(link.link_target.as_deref(), Some(Path::new("notes.txt")));
assert_eq!(link.size, None);
}
}
#[tokio::test]
async fn local_fs_metadata_and_canonicalize_return_typed_locations() {
let fixture = TempDir::new().unwrap();
fs::create_dir(fixture.path().join("nested")).unwrap();
let location = Location::local(fixture.path().join("nested").join(".."));
let canonical = LocalFs.canonicalize(&location).await.unwrap();
let metadata = LocalFs
.metadata(&Location::local(fixture.path().join("nested")))
.await
.unwrap();
assert_eq!(
canonical,
Location::local(fs::canonicalize(fixture.path()).unwrap())
);
assert_eq!(metadata.name, "nested");
assert_eq!(metadata.kind, EntryKind::Directory);
}
#[tokio::test]
async fn local_fs_maps_missing_and_file_instead_of_directory() {
let fixture = TempDir::new().unwrap();
let file = fixture.path().join("plain.txt");
fs::write(&file, b"plain").unwrap();
let missing = Location::local(fixture.path().join("missing"));
let missing_error = LocalFs.list(&missing).await.unwrap_err();
assert_eq!(missing_error, FsError::NotFound);
assert_eq!(
LocalFs.canonicalize(&missing).await.unwrap_err(),
FsError::NotFound
);
assert_eq!(
LocalFs.metadata(&missing).await.unwrap_err(),
FsError::NotFound
);
assert_eq!(
LocalFs.list(&Location::local(&file)).await.unwrap_err(),
FsError::NotADirectory
);
let error = LocalFs.list(&missing).await.unwrap_err();
assert!(
!error
.to_string()
.contains(&fixture.path().display().to_string())
);
assert!(
!error
.user_message()
.contains(&fixture.path().display().to_string())
);
}
#[tokio::test]
async fn local_fs_read_prefix_obeys_zero_exact_and_shorter_limits() {
let fixture = TempDir::new().unwrap();
let file = fixture.path().join("data.bin");
fs::write(&file, b"abcd").unwrap();
let location = Location::local(&file);
for (limit, expected, truncated) in [
(0, &b""[..], true),
(1, &b"a"[..], true),
(3, &b"abc"[..], true),
(4, &b"abcd"[..], false),
(8, &b"abcd"[..], false),
] {
let prefix = LocalFs.read_prefix(&location, limit).await.unwrap();
assert_eq!(prefix.data, expected, "limit {limit}");
assert_eq!(prefix.truncated, truncated, "limit {limit}");
assert!(prefix.data.len() <= limit);
}
fs::write(&file, b"").unwrap();
let empty = LocalFs.read_prefix(&location, 0).await.unwrap();
assert!(empty.data.is_empty());
assert!(!empty.truncated);
}
#[cfg(unix)]
#[tokio::test]
async fn local_fs_preserves_non_utf8_names_for_listing_metadata_and_reading() {
use std::os::unix::ffi::{OsStrExt, OsStringExt};
let fixture = TempDir::new().unwrap();
let name = std::ffi::OsString::from_vec(vec![b'f', 0x80, b'o']);
let path = fixture.path().join(&name);
fs::write(&path, b"raw").unwrap();
let entries = LocalFs
.list(&Location::local(fixture.path()))
.await
.unwrap();
let metadata = LocalFs.metadata(&Location::local(&path)).await.unwrap();
let prefix = LocalFs
.read_prefix(&Location::local(&path), 2)
.await
.unwrap();
assert_eq!(entries.len(), 1);
assert_eq!(
entries[0].name.as_os_str().as_bytes(),
name.as_os_str().as_bytes()
);
assert_eq!(metadata.name, name);
assert_eq!(prefix.data, b"ra");
assert!(prefix.truncated);
}
#[tokio::test]
async fn local_fs_rejects_an_ssh_location_without_using_the_local_path() {
let location = Location::ssh(rcom::domain::HostAlias::new("edge"), "/etc");
let error = LocalFs.list(&location).await.unwrap_err();
assert!(matches!(error, FsError::Other(_)));
assert!(!error.to_string().contains("/etc"));
}