fix(status): get status in obj and fix tests env dir

This commit is contained in:
grimhilt 2024-09-14 13:59:00 +02:00
parent a69a71d843
commit cd7b225145
4 changed files with 53 additions and 20 deletions

View File

@ -191,11 +191,10 @@ fn print_object(obj: &Obj, root_path: &PathBuf, color: impl Fn(&str) -> ColoredS
println!( println!(
" {}{}", " {}{}",
match obj.get_status() { match obj.get_status() {
ObjStatus::Created => ObjStatus::Created => match *obj.get_obj_type() {
if obj.get_obj_type() == &ObjType::Blob { ObjType::Blob => color("new file: "),
color("new file: ") ObjType::Tree => color("new dir: "),
} else { ObjType::Obj => color("new: "),
color("new: ")
}, },
ObjStatus::Modified => color("modified: "), ObjStatus::Modified => color("modified: "),
ObjStatus::Deleted => color("deleted: "), ObjStatus::Deleted => color("deleted: "),

View File

@ -47,6 +47,7 @@ impl TryFrom<u8> for ObjType {
#[derive(PartialEq, Clone, Debug)] #[derive(PartialEq, Clone, Debug)]
pub enum ObjStatus { pub enum ObjStatus {
Undefined, Undefined,
Unchanged,
Created, Created,
Modified, Modified,
Moved, Moved,
@ -84,9 +85,17 @@ impl Obj {
pub fn get_status(&self) -> &ObjStatus { pub fn get_status(&self) -> &ObjStatus {
self.status.get_or_init(|| { self.status.get_or_init(|| {
// todo!(); let nsobj = self.get_nsobj();
// TODO read path if !nsobj.exists() {
ObjStatus::Created 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) { if let Ok(root_diff) = binding.strip_prefix(repo_root) {
match self.obj_path.strip_prefix(&root_diff) { 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(_) => { Err(_) => {
// if cannot strip prefix then we need to go up to the root // if cannot strip prefix then we need to go up to the root
let mut res_path = PathBuf::new(); let mut res_path = PathBuf::new();
@ -140,6 +155,10 @@ impl Obj {
path::to_string(&self.obj_path) path::to_string(&self.obj_path)
} }
fn get_nsobj(&self) -> NsObject {
NsObject::from_local_path(&self.obj_path)
}
fn get_nsobj_path(&self) -> NsObjPath { fn get_nsobj_path(&self) -> NsObjPath {
NsObjPath::from(&self.obj_path) NsObjPath::from(&self.obj_path)
} }

View File

@ -6,11 +6,18 @@ use std::io::{self, Write};
use std::path::PathBuf; use std::path::PathBuf;
use std::process::{Command, Output}; use std::process::{Command, Output};
use std::str; use std::str;
use std::sync::LazyLock;
// Absolute path of the nextsync executable
static EXE_PATH: LazyLock<PathBuf> = LazyLock::new(|| {
let mut exe_path = env::current_dir().unwrap();
exe_path = exe_path.join("target/debug/nextsync");
exe_path
});
pub struct ClientTest { pub struct ClientTest {
volume: String, // temp dir for the test volume: String, // temp dir for the test
pub test_id: String, // name of the test (e.g nextsync_rand) 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 { pub fn get_random_test_id() -> String {
@ -23,6 +30,9 @@ pub fn get_random_test_id() -> String {
impl ClientTest { impl ClientTest {
pub fn new(id: &str) -> Self { pub fn new(id: &str) -> Self {
// init the EXE_PATH for the first time
let _ = &*EXE_PATH;
let mut test_id = id.to_string(); let mut test_id = id.to_string();
test_id.push_str("_"); test_id.push_str("_");
test_id.push_str(&get_random_test_id()); test_id.push_str(&get_random_test_id());
@ -33,15 +43,13 @@ impl ClientTest {
let _ = fs::remove_dir_all(&vol); let _ = fs::remove_dir_all(&vol);
let _ = fs::create_dir_all(&vol); let _ = fs::create_dir_all(&vol);
// get nextsync path // Setup the current dir to the local repo
let mut exe_path = env::current_dir().unwrap(); env::set_current_dir(&vol).unwrap();
exe_path = exe_path.join("target/debug/nextsync");
// build the client // build the client
ClientTest { ClientTest {
volume: vol, volume: vol,
test_id, test_id,
exe_path,
} }
} }
@ -72,6 +80,14 @@ impl ClientTest {
Config::from(Some(&full_path)) 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<()> { pub fn ok(self) -> io::Result<()> {
fs::remove_dir_all(&self.volume)?; fs::remove_dir_all(&self.volume)?;
Ok(()) Ok(())
@ -90,7 +106,7 @@ impl ClientTest {
} }
pub fn exec(&mut self, args: &str) -> Output { 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()) .current_dir(self.volume.clone())
.args(args.split(" ")) .args(args.split(" "))
.output() .output()

View File

@ -89,7 +89,6 @@ fn all_folder_current() -> io::Result<()> {
} }
#[test] #[test]
#[ignore]
fn relative_path() -> io::Result<()> { fn relative_path() -> io::Result<()> {
let mut client = ClientTest::new("status__all_folder_current").init(); 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"]); status_expected(&client.get_config(), vec![], vec!["foor", "dir"]);
client.exec_ok("add dir"); client.exec_ok("add dir");
// client.set_execution_path("dir"); client.set_execution_path("dir");
status_expected( status_expected(
&client.new_config("dir"), &client.get_config(),
vec!["foo", "bar"], vec!["foo", "bar"],
vec!["../foor"], vec!["../foor"],
); );