diff --git a/src/commands/status.rs b/src/commands/status.rs index 68aaeac..753a00d 100644 --- a/src/commands/status.rs +++ b/src/commands/status.rs @@ -191,12 +191,11 @@ fn print_object(obj: &Obj, root_path: &PathBuf, color: impl Fn(&str) -> ColoredS println!( " {}{}", match obj.get_status() { - ObjStatus::Created => - if obj.get_obj_type() == &ObjType::Blob { - color("new file: ") - } else { - color("new: ") - }, + ObjStatus::Created => match *obj.get_obj_type() { + ObjType::Blob => color("new file: "), + ObjType::Tree => color("new dir: "), + ObjType::Obj => color("new: "), + }, ObjStatus::Modified => color("modified: "), ObjStatus::Deleted => color("deleted: "), _ => "unknown".red(), diff --git a/src/store/object.rs b/src/store/object.rs index eda2cee..cf0bf61 100644 --- a/src/store/object.rs +++ b/src/store/object.rs @@ -47,6 +47,7 @@ impl TryFrom for ObjType { #[derive(PartialEq, Clone, Debug)] pub enum ObjStatus { Undefined, + Unchanged, Created, Modified, Moved, @@ -84,9 +85,17 @@ impl Obj { pub fn get_status(&self) -> &ObjStatus { self.status.get_or_init(|| { - // todo!(); - // TODO read path - ObjStatus::Created + let nsobj = self.get_nsobj(); + if !nsobj.exists() { + return ObjStatus::Created; + } + if !self.obj_path.exists() { + return ObjStatus::Deleted; + } + if *self != nsobj { + return ObjStatus::Modified; + } + todo!("Read status"); }) } @@ -119,7 +128,13 @@ impl Obj { if let Ok(root_diff) = binding.strip_prefix(repo_root) { match self.obj_path.strip_prefix(&root_diff) { - Ok(path) => path.to_path_buf(), + Ok(path) => { + if path == PathBuf::from("") { + PathBuf::from("./") + } else { + path.to_path_buf() + } + } Err(_) => { // if cannot strip prefix then we need to go up to the root let mut res_path = PathBuf::new(); @@ -140,6 +155,10 @@ impl Obj { path::to_string(&self.obj_path) } + fn get_nsobj(&self) -> NsObject { + NsObject::from_local_path(&self.obj_path) + } + fn get_nsobj_path(&self) -> NsObjPath { NsObjPath::from(&self.obj_path) } @@ -153,7 +172,7 @@ impl Obj { let mut nsobj_dir = nsobj_path.clone(); nsobj_dir.pop(); if !nsobj_dir.exists() { - std::fs::create_dir_all(nsobj_dir)?; + std::fs::create_dir_all(nsobj_dir)?; } File::create(&nsobj_path)? } diff --git a/tests/common/client.rs b/tests/common/client.rs index 27e7b65..4fafb1e 100644 --- a/tests/common/client.rs +++ b/tests/common/client.rs @@ -6,11 +6,18 @@ use std::io::{self, Write}; use std::path::PathBuf; use std::process::{Command, Output}; use std::str; +use std::sync::LazyLock; + +// Absolute path of the nextsync executable +static EXE_PATH: LazyLock = LazyLock::new(|| { + let mut exe_path = env::current_dir().unwrap(); + exe_path = exe_path.join("target/debug/nextsync"); + exe_path +}); pub struct ClientTest { volume: String, // temp dir for the test pub test_id: String, // name of the test (e.g nextsync_rand) - exe_path: PathBuf, // absolute path of nextsync executable } pub fn get_random_test_id() -> String { @@ -23,6 +30,9 @@ pub fn get_random_test_id() -> String { impl ClientTest { pub fn new(id: &str) -> Self { + // init the EXE_PATH for the first time + let _ = &*EXE_PATH; + let mut test_id = id.to_string(); test_id.push_str("_"); test_id.push_str(&get_random_test_id()); @@ -33,15 +43,13 @@ impl ClientTest { let _ = fs::remove_dir_all(&vol); let _ = fs::create_dir_all(&vol); - // get nextsync path - let mut exe_path = env::current_dir().unwrap(); - exe_path = exe_path.join("target/debug/nextsync"); + // Setup the current dir to the local repo + env::set_current_dir(&vol).unwrap(); // build the client ClientTest { volume: vol, test_id, - exe_path, } } @@ -72,6 +80,14 @@ impl ClientTest { Config::from(Some(&full_path)) } + pub fn set_execution_path(&self, path: &str) { + let mut new_execution_path = self.volume.clone(); + new_execution_path.push_str("/"); + new_execution_path.push_str(path); + + env::set_current_dir(new_execution_path).unwrap(); + } + pub fn ok(self) -> io::Result<()> { fs::remove_dir_all(&self.volume)?; Ok(()) @@ -90,7 +106,7 @@ impl ClientTest { } pub fn exec(&mut self, args: &str) -> Output { - let output = Command::new(self.exe_path.to_str().unwrap()) + let output = Command::new(&*EXE_PATH.to_str().unwrap()) .current_dir(self.volume.clone()) .args(args.split(" ")) .output() diff --git a/tests/status_test.rs b/tests/status_new_test.rs similarity index 97% rename from tests/status_test.rs rename to tests/status_new_test.rs index a7c4f45..101cdf2 100644 --- a/tests/status_test.rs +++ b/tests/status_new_test.rs @@ -89,7 +89,6 @@ fn all_folder_current() -> io::Result<()> { } #[test] -#[ignore] fn relative_path() -> io::Result<()> { let mut client = ClientTest::new("status__all_folder_current").init(); @@ -100,9 +99,9 @@ fn relative_path() -> io::Result<()> { status_expected(&client.get_config(), vec![], vec!["foor", "dir"]); client.exec_ok("add dir"); - // client.set_execution_path("dir"); + client.set_execution_path("dir"); status_expected( - &client.new_config("dir"), + &client.get_config(), vec!["foo", "bar"], vec!["../foor"], );