From 939b6f2fe3f64cace7ddba3549810d6f57f897cd Mon Sep 17 00:00:00 2001 From: grimhilt Date: Thu, 2 May 2024 18:36:09 +0200 Subject: [PATCH] feat: push deletion --- src/commands/push/push_factory.rs | 13 ++++++++++--- src/store/object/object.rs | 7 +++---- src/store/object/tree.rs | 16 ++++++++-------- tests/push.rs | 25 +++++++++++++++++++++++-- tests/utils/client.rs | 8 ++++++++ tests/utils/files_utils.rs | 12 ++++++++++++ tests/utils/server.rs | 6 ++++++ 7 files changed, 70 insertions(+), 17 deletions(-) diff --git a/src/commands/push/push_factory.rs b/src/commands/push/push_factory.rs index 9765e15..811e747 100644 --- a/src/commands/push/push_factory.rs +++ b/src/commands/push/push_factory.rs @@ -73,9 +73,16 @@ pub trait PushChange { }; // check if remote is newest - let last_sync_ts = Blob::from_path(obj.path.clone()) - .saved_remote_ts() - .parse::().unwrap(); + let last_sync_ts = { + if obj.otype == String::from("blob") { + Blob::from_path(obj.path.clone()) + .saved_remote_ts() + .parse::().unwrap() + } else { + // todo timestamp on tree + 99999999999999 + } + }; let remote_ts = obj_data.lastmodified.unwrap().timestamp_millis(); if last_sync_ts < remote_ts { diff --git a/src/store/object/object.rs b/src/store/object/object.rs index f4e56fb..70084df 100644 --- a/src/store/object/object.rs +++ b/src/store/object/object.rs @@ -178,8 +178,7 @@ impl ObjMethods for Blob { fn rm_node(&mut self) -> io::Result<()> { // remove self object and children object - self.rm_node_down(); - + let _ = self.rm_node_down(); self.obj.rm_node() } @@ -246,7 +245,7 @@ impl ObjMethods for Tree { fn rm_node(&mut self) -> io::Result<()> { // remove self object and children object - self.rm_node_down(); + let _ = self.rm_node_down(); self.obj.rm_node() } @@ -336,7 +335,7 @@ impl Obj { pub fn from_line(line: String, base_dir: Option) -> Box { let mut split = line.trim().rsplit(' '); if split.clone().count() != 3 { - eprintln!("fatal: invalid object(s)"); + eprintln!("fatal: invalid object(s) ({})", line.trim()); std::process::exit(1); } diff --git a/src/store/object/tree.rs b/src/store/object/tree.rs index d55de4e..7a681b6 100644 --- a/src/store/object/tree.rs +++ b/src/store/object/tree.rs @@ -11,7 +11,6 @@ pub struct Tree { is_head: bool, } - impl Tree { pub fn new(obj: Obj) -> Self { Tree { @@ -42,9 +41,10 @@ impl Tree { if let Ok(file) = File::open(self.get_obj_path()) { self.buf_reader = Some(BufReader::new(file)); - // skip first line if is head + // skip first line (declaration) if is not head if !self.is_head { - self.next(); + let mut line = String::new(); + self.buf_reader.as_mut().unwrap().read_line(&mut line); } } } @@ -99,11 +99,11 @@ impl Tree { .open(self.get_obj_path())?; // update date for all parent - if up_parent { - if let Err(err) = update_dates(self.get_relative_file_path(), date) { - eprintln!("err: updating parent date of {}: {}", self.get_relative_file_path().display(), err); - } - } + // if up_parent { + // if let Err(err) = update_dates(self.get_relative_file_path(), date) { + // eprintln!("err: updating parent date of {}: {}", self.get_relative_file_path().display(), err); + // } + // } writeln!(file, "{}", content)?; diff --git a/tests/push.rs b/tests/push.rs index cafe671..bac84ac 100644 --- a/tests/push.rs +++ b/tests/push.rs @@ -1,4 +1,3 @@ - mod utils; use utils::{utils::*, status_utils::*, server::ServerTest, client::ClientTest}; @@ -151,7 +150,6 @@ mod push_tests { // remove it let _ = client.remove_file("file1"); client.run_cmd_ok("add file1"); - dbg!(client.get_status()); client.run_cmd_ok("push"); // tests @@ -161,4 +159,27 @@ mod push_tests { client.clean(); server.clean(); } + + #[test] + fn push_dir_deletion() { + let (mut client, mut server) = init_test(); + + // push dir and file2 + let _ = client.add_dir("dir"); + let _ = client.add_file("dir/file2", "bar"); + client.run_cmd_ok("add dir"); + client.run_cmd_ok("push"); + + // tests + assert!(server.has_file("dir/file2", "bar")); + + // push deletion + let _ = client.remove_dir("dir"); + client.run_cmd_ok("add dir"); + client.run_cmd_ok("push"); + assert!(server.has_not_dir("dir")); + + client.clean(); + server.clean(); + } } diff --git a/tests/utils/client.rs b/tests/utils/client.rs index 6a817c5..a84b86a 100644 --- a/tests/utils/client.rs +++ b/tests/utils/client.rs @@ -104,6 +104,14 @@ impl ClientTest { Ok(()) } + pub fn remove_dir(&mut self, name: &str) -> std::io::Result<()> { + let mut path = self.volume.clone(); + path.push_str("/"); + path.push_str(name); + fs::remove_dir_all(path)?; + Ok(()) + } + pub fn has_file(&mut self, file: &str, content: &str) -> bool { let full_path = PathBuf::from(self.volume.clone()).join(file); diff --git a/tests/utils/files_utils.rs b/tests/utils/files_utils.rs index 2d3ea0f..2774310 100644 --- a/tests/utils/files_utils.rs +++ b/tests/utils/files_utils.rs @@ -36,3 +36,15 @@ pub fn has_not_file(full_path: PathBuf, file: &str, test_id: String) -> bool return true; } + +#[cfg(test)] +pub fn has_not_dir(full_path: PathBuf, dir: &str, test_id: String) -> bool +{ + if full_path.exists() { + println!("id: {}", test_id.clone()); + eprintln!("Dir '{}' exists but it shouldn't", dir); + return false; + } + + return true; +} diff --git a/tests/utils/server.rs b/tests/utils/server.rs index ae60570..d9394af 100644 --- a/tests/utils/server.rs +++ b/tests/utils/server.rs @@ -113,5 +113,11 @@ impl ServerTest { let full_path = self.volume.clone().join(file); files_utils::has_not_file(full_path, file, self.test_id.clone()) } + + pub fn has_not_dir(&mut self, dir: &str) -> bool { + let full_path = self.volume.clone().join(dir); + dbg!(full_path.clone()); + files_utils::has_not_file(full_path, dir, self.test_id.clone()) + } }