mirror of
https://github.com/veeso/termscp.git
synced 2026-06-08 14:18:41 +02:00
Fixed symlink test; tests
This commit is contained in:
@@ -30,7 +30,7 @@ use std::time::SystemTime;
|
|||||||
///
|
///
|
||||||
/// FsEntry represents a generic entry in a directory
|
/// FsEntry represents a generic entry in a directory
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone, std::fmt::Debug)]
|
||||||
pub enum FsEntry {
|
pub enum FsEntry {
|
||||||
Directory(FsDirectory),
|
Directory(FsDirectory),
|
||||||
File(FsFile)
|
File(FsFile)
|
||||||
@@ -40,7 +40,7 @@ pub enum FsEntry {
|
|||||||
///
|
///
|
||||||
/// Directory provides an interface to file system directories
|
/// Directory provides an interface to file system directories
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone, std::fmt::Debug)]
|
||||||
pub struct FsDirectory {
|
pub struct FsDirectory {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub last_change_time: SystemTime,
|
pub last_change_time: SystemTime,
|
||||||
@@ -57,7 +57,7 @@ pub struct FsDirectory {
|
|||||||
///
|
///
|
||||||
/// FsFile provides an interface to file system files
|
/// FsFile provides an interface to file system files
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone, std::fmt::Debug)]
|
||||||
pub struct FsFile {
|
pub struct FsFile {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub last_change_time: SystemTime,
|
pub last_change_time: SystemTime,
|
||||||
|
|||||||
@@ -121,12 +121,7 @@ impl Localhost {
|
|||||||
if !self.file_exists(file) {
|
if !self.file_exists(file) {
|
||||||
return Err(HostError::NoSuchFileOrDirectory);
|
return Err(HostError::NoSuchFileOrDirectory);
|
||||||
}
|
}
|
||||||
match OpenOptions::new()
|
match OpenOptions::new().create(false).read(true).write(false).open(file) {
|
||||||
.create(false)
|
|
||||||
.read(true)
|
|
||||||
.write(false)
|
|
||||||
.open(file)
|
|
||||||
{
|
|
||||||
Ok(f) => Ok(f),
|
Ok(f) => Ok(f),
|
||||||
Err(_) => Err(HostError::FileNotAccessible),
|
Err(_) => Err(HostError::FileNotAccessible),
|
||||||
}
|
}
|
||||||
@@ -136,12 +131,7 @@ impl Localhost {
|
|||||||
///
|
///
|
||||||
/// Open file for write
|
/// Open file for write
|
||||||
pub fn open_file_write(&self, file: &Path) -> Result<File, HostError> {
|
pub fn open_file_write(&self, file: &Path) -> Result<File, HostError> {
|
||||||
match OpenOptions::new()
|
match OpenOptions::new().create(true).write(true).truncate(true).open(file) {
|
||||||
.create(true)
|
|
||||||
.write(true)
|
|
||||||
.truncate(true)
|
|
||||||
.open(file)
|
|
||||||
{
|
|
||||||
Ok(f) => Ok(f),
|
Ok(f) => Ok(f),
|
||||||
Err(_) => match self.file_exists(file) {
|
Err(_) => match self.file_exists(file) {
|
||||||
true => Err(HostError::ReadonlyFile),
|
true => Err(HostError::ReadonlyFile),
|
||||||
@@ -171,7 +161,6 @@ impl Localhost {
|
|||||||
if let Ok(entry) = entry {
|
if let Ok(entry) = entry {
|
||||||
let path: PathBuf = entry.path();
|
let path: PathBuf = entry.path();
|
||||||
let attr: Metadata = fs::metadata(path.clone()).unwrap();
|
let attr: Metadata = fs::metadata(path.clone()).unwrap();
|
||||||
let is_symlink: bool = attr.file_type().is_symlink();
|
|
||||||
// Get user stuff
|
// Get user stuff
|
||||||
let user: Option<String> = match get_user_by_uid(attr.uid()) {
|
let user: Option<String> = match get_user_by_uid(attr.uid()) {
|
||||||
Some(user) => Some(String::from(user.name().to_str().unwrap_or(""))),
|
Some(user) => Some(String::from(user.name().to_str().unwrap_or(""))),
|
||||||
@@ -193,15 +182,9 @@ impl Localhost {
|
|||||||
last_access_time: attr.accessed().unwrap_or(SystemTime::UNIX_EPOCH),
|
last_access_time: attr.accessed().unwrap_or(SystemTime::UNIX_EPOCH),
|
||||||
creation_time: attr.created().unwrap_or(SystemTime::UNIX_EPOCH),
|
creation_time: attr.created().unwrap_or(SystemTime::UNIX_EPOCH),
|
||||||
readonly: attr.permissions().readonly(),
|
readonly: attr.permissions().readonly(),
|
||||||
symlink: match is_symlink {
|
symlink: match fs::read_link(path) {
|
||||||
true => {
|
|
||||||
// Read link
|
|
||||||
match fs::read_link(path) {
|
|
||||||
Ok(p) => Some(p),
|
Ok(p) => Some(p),
|
||||||
Err(_) => None,
|
Err(_) => None,
|
||||||
}
|
|
||||||
}
|
|
||||||
false => None,
|
|
||||||
},
|
},
|
||||||
user: user,
|
user: user,
|
||||||
group: group,
|
group: group,
|
||||||
@@ -222,15 +205,9 @@ impl Localhost {
|
|||||||
readonly: attr.permissions().readonly(),
|
readonly: attr.permissions().readonly(),
|
||||||
size: attr.len() as usize,
|
size: attr.len() as usize,
|
||||||
ftype: extension,
|
ftype: extension,
|
||||||
symlink: match is_symlink {
|
symlink: match fs::read_link(path) {
|
||||||
true => {
|
|
||||||
// Read link
|
|
||||||
match fs::read_link(path) {
|
|
||||||
Ok(p) => Some(p),
|
Ok(p) => Some(p),
|
||||||
Err(_) => None,
|
Err(_) => None,
|
||||||
}
|
|
||||||
}
|
|
||||||
false => None,
|
|
||||||
},
|
},
|
||||||
user: user,
|
user: user,
|
||||||
group: group,
|
group: group,
|
||||||
@@ -318,9 +295,10 @@ mod tests {
|
|||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
use std::fs::File;
|
||||||
|
|
||||||
#[cfg(any(unix, macos, linux))]
|
#[cfg(any(unix, macos, linux))]
|
||||||
use std::os::unix::fs::PermissionsExt;
|
use std::os::unix::fs::{PermissionsExt, symlink};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(any(unix, macos, linux))]
|
#[cfg(any(unix, macos, linux))]
|
||||||
@@ -381,8 +359,8 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
#[cfg(any(unix, macos, linux))]
|
#[cfg(any(unix, macos, linux))]
|
||||||
fn test_host_localhost_change_dir() {
|
fn test_host_localhost_change_dir() {
|
||||||
let mut host: Localhost = Localhost::new(PathBuf::from("/usr")).ok().unwrap();
|
let mut host: Localhost = Localhost::new(PathBuf::from("/dev")).ok().unwrap();
|
||||||
let new_dir: PathBuf = PathBuf::from("/usr");
|
let new_dir: PathBuf = PathBuf::from("/dev");
|
||||||
assert!(host.change_wrkdir(new_dir.clone()).is_ok());
|
assert!(host.change_wrkdir(new_dir.clone()).is_ok());
|
||||||
// Verify new files
|
// Verify new files
|
||||||
// Scan dir
|
// Scan dir
|
||||||
@@ -453,6 +431,38 @@ mod tests {
|
|||||||
assert!(host.open_file_write(file.path()).is_err());
|
assert!(host.open_file_write(file.path()).is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(any(unix, macos, linux))]
|
||||||
|
#[test]
|
||||||
|
fn test_host_localhost_symlinks() {
|
||||||
|
let tmpdir: tempfile::TempDir = tempfile::TempDir::new().unwrap();
|
||||||
|
// Create sample file
|
||||||
|
assert!(File::create(format!("{}/foo.txt", tmpdir.path().display()).as_str()).is_ok());
|
||||||
|
// Create symlink
|
||||||
|
assert!(symlink(format!("{}/foo.txt", tmpdir.path().display()), format!("{}/bar.txt", tmpdir.path().display())).is_ok());
|
||||||
|
// Get dir
|
||||||
|
let host: Localhost = Localhost::new(PathBuf::from(tmpdir.path())).ok().unwrap();
|
||||||
|
let files: Vec<FsEntry> = host.list_dir();
|
||||||
|
println!("Entries {:?}", files);
|
||||||
|
// Verify files
|
||||||
|
let foo_file: &FsEntry = files.get(0).unwrap();
|
||||||
|
match foo_file {
|
||||||
|
FsEntry::File(foo_file) => {
|
||||||
|
assert_eq!(foo_file.name, String::from("foo.txt"));
|
||||||
|
assert!(foo_file.symlink.is_none());
|
||||||
|
},
|
||||||
|
_ => panic!("expected entry 0 to be file: {:?}", foo_file)
|
||||||
|
};
|
||||||
|
// Verify simlink
|
||||||
|
let bar_file: &FsEntry = files.get(1).unwrap();
|
||||||
|
match bar_file {
|
||||||
|
FsEntry::File(bar_file) => {
|
||||||
|
assert_eq!(bar_file.name, String::from("bar.txt"));
|
||||||
|
assert_eq!(*bar_file.symlink.as_ref().unwrap(), PathBuf::from(format!("{}/foo.txt", tmpdir.path().display())));
|
||||||
|
},
|
||||||
|
_ => panic!("expected entry 1 to be file: {:?}", bar_file)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/// ### create_sample_file
|
/// ### create_sample_file
|
||||||
///
|
///
|
||||||
/// Create a sample file
|
/// Create a sample file
|
||||||
|
|||||||
Reference in New Issue
Block a user