Compare commits
No commits in common. "fa65b6b071242cb33938413e341e11997f33ce5f" and "06bb51476b1177b6910e1b6f51e3dccc5ecf79d0" have entirely different histories.
fa65b6b071
...
06bb51476b
@ -4,7 +4,6 @@ use clap::Values;
|
||||
use glob::glob;
|
||||
use crate::store::{self, object::Object};
|
||||
use crate::utils::{self, path};
|
||||
use crate::store::object::object::{Obj, ObjMethods};
|
||||
use crate::utils::nextsyncignore::{self, ignore_file};
|
||||
use crate::utils::path::{normalize_relative, repo_root, path_buf_to_string};
|
||||
|
||||
@ -40,19 +39,35 @@ pub fn add(args: AddArgs) {
|
||||
}
|
||||
};
|
||||
|
||||
// check if the file must be ignored
|
||||
if !args.force && ignore_file(&f, rules.clone(), &mut ignored_f) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let path = repo_root().join(Path::new(&f));
|
||||
match path.exists() {
|
||||
true => {
|
||||
add_entry(path, args.force, &mut added_files, rules.clone(), &mut ignored_f);
|
||||
if path.is_dir() {
|
||||
add_folder_content(path.to_path_buf(), &mut added_files);
|
||||
}
|
||||
added_files.push(f);
|
||||
},
|
||||
false => {
|
||||
if Object::new(path.to_str().unwrap()).exists() {
|
||||
// object is deleted so not present but can still be added for deletion
|
||||
// object is deleted so not a present file but can still be added
|
||||
added_files.push(String::from(f));
|
||||
} else {
|
||||
// try globbing if nothing has been found
|
||||
for entry in try_globbing(path) {
|
||||
add_entry(entry, args.force, &mut added_files, rules.clone(), &mut ignored_f);
|
||||
if path::is_nextsync_config(entry.clone()) {
|
||||
continue;
|
||||
}
|
||||
if !args.force && ignore_file(&path_buf_to_string(entry.clone()), rules.clone(), &mut ignored_f) {
|
||||
continue;
|
||||
}
|
||||
if entry.is_dir() {
|
||||
add_folder_content(entry.to_path_buf(), &mut added_files);
|
||||
}
|
||||
added_files.push(path_buf_to_string(entry.strip_prefix(repo_root()).unwrap().to_path_buf()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -63,41 +78,6 @@ pub fn add(args: AddArgs) {
|
||||
write_added_files(added_files);
|
||||
}
|
||||
|
||||
fn add_entry(entry: PathBuf, force: bool, added_files: &mut Vec<String>, rules: Vec<String>, ignored_f: &mut Vec<String>) {
|
||||
// ignore nextsync config files
|
||||
if path::is_nextsync_config(entry.clone()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// check if the file must be ignored
|
||||
if !force && ignore_file(&path_buf_to_string(entry.clone()), rules, ignored_f) {
|
||||
return;
|
||||
}
|
||||
|
||||
// add the parent if there is one and it is not already created
|
||||
add_parent(entry.clone(), added_files);
|
||||
|
||||
added_files.push(path_buf_to_string(entry.strip_prefix(repo_root()).unwrap().to_path_buf()));
|
||||
if entry.is_dir() {
|
||||
add_folder_content(entry.to_path_buf(), added_files);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn add_parent(entry: PathBuf, added_files: &mut Vec<String>) {
|
||||
let test_parent = entry.strip_prefix(repo_root()).unwrap().parent();
|
||||
if test_parent.is_none() || test_parent.unwrap() == PathBuf::new() {
|
||||
return;
|
||||
}
|
||||
|
||||
let parent = entry.parent().unwrap();
|
||||
|
||||
if !Obj::from_path(parent).exists_on_remote() {
|
||||
add_parent(parent.to_path_buf(), added_files);
|
||||
added_files.push(path_buf_to_string(parent.strip_prefix(repo_root()).unwrap().to_path_buf()));
|
||||
}
|
||||
}
|
||||
|
||||
fn print_ignored_files(ignored_files: Vec<String>) {
|
||||
if ignored_files.len() > 0 {
|
||||
// todo multiple nextsyncignore
|
||||
|
@ -32,7 +32,6 @@ impl PushChange for New {
|
||||
|
||||
match res {
|
||||
Err(ApiError::IncorrectRequest(err)) => {
|
||||
dbg!(&err);
|
||||
eprintln!("fatal: error pushing file '{}': {}", obj.name, err.status());
|
||||
std::process::exit(1);
|
||||
},
|
||||
|
@ -31,7 +31,6 @@ impl PushChange for NewDir {
|
||||
}
|
||||
|
||||
fn push(&self) -> io::Result<()> {
|
||||
dbg!("pushing new dir");
|
||||
let obj = &self.obj;
|
||||
let res = CreateFolder::new()
|
||||
.set_url(obj.path.to_str().unwrap())
|
||||
|
@ -148,7 +148,7 @@ fn add_node(path: &Path, node: &str) -> io::Result<()> {
|
||||
|
||||
root.push(dir);
|
||||
if !root.exists() {
|
||||
//todo!();
|
||||
todo!();
|
||||
}
|
||||
root.push(rest);
|
||||
|
||||
|
@ -22,7 +22,6 @@ pub trait ObjMethods {
|
||||
fn get_obj_path(&self) -> PathBuf;
|
||||
fn get_file_path(&self) -> PathBuf;
|
||||
fn get_relative_file_path(&self) -> PathBuf;
|
||||
fn get_repo_file_path(&self) -> PathBuf;
|
||||
fn get_name(&self) -> String;
|
||||
fn get_hash_path(&self) -> String;
|
||||
fn get_local_obj(&self) -> LocalObj;
|
||||
@ -31,7 +30,6 @@ pub trait ObjMethods {
|
||||
fn rm(&mut self) -> io::Result<()>;
|
||||
fn rm_node(&mut self) -> io::Result<()>;
|
||||
fn rm_node_down(&mut self) -> io::Result<()>;
|
||||
fn exists_on_remote(&mut self) -> bool;
|
||||
}
|
||||
|
||||
pub struct Obj {
|
||||
@ -40,7 +38,6 @@ pub struct Obj {
|
||||
obj_type: ObjType,
|
||||
file_path: PathBuf, // file here is used as both file and directory
|
||||
relative_file_path: PathBuf,
|
||||
repo_file_path: PathBuf,
|
||||
hash_path: String, // hash of the relative path of the file
|
||||
}
|
||||
|
||||
@ -62,10 +59,6 @@ impl ObjMethods for Obj {
|
||||
self.relative_file_path.clone()
|
||||
}
|
||||
|
||||
fn get_repo_file_path(&self) -> PathBuf {
|
||||
self.repo_file_path.clone()
|
||||
}
|
||||
|
||||
fn get_local_obj(&self) -> LocalObj {
|
||||
LocalObj {
|
||||
otype: match self.obj_type {
|
||||
@ -74,7 +67,7 @@ impl ObjMethods for Obj {
|
||||
ObjType::DEFAULT => String::from("default"),
|
||||
},
|
||||
name: self.get_name(),
|
||||
path: self.get_repo_file_path(),
|
||||
path: self.get_file_path(),
|
||||
path_from: None,
|
||||
state: State::New
|
||||
}
|
||||
@ -127,10 +120,6 @@ impl ObjMethods for Obj {
|
||||
eprintln!("rm: tried to do this on Obj");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn exists_on_remote(&mut self) -> bool {
|
||||
PathBuf::from(self.get_hash_path()).exists()
|
||||
}
|
||||
}
|
||||
|
||||
impl ObjMethods for Blob {
|
||||
@ -150,10 +139,6 @@ impl ObjMethods for Blob {
|
||||
self.obj.get_relative_file_path()
|
||||
}
|
||||
|
||||
fn get_repo_file_path(&self) -> PathBuf {
|
||||
self.obj.get_repo_file_path()
|
||||
}
|
||||
|
||||
fn get_local_obj(&self) -> LocalObj {
|
||||
self.obj.get_local_obj()
|
||||
}
|
||||
@ -192,10 +177,6 @@ impl ObjMethods for Blob {
|
||||
fs::remove_file(self.get_file_path())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn exists_on_remote(&mut self) -> bool {
|
||||
self.obj.exists_on_remote()
|
||||
}
|
||||
}
|
||||
|
||||
impl ObjMethods for Tree {
|
||||
@ -215,10 +196,6 @@ impl ObjMethods for Tree {
|
||||
self.obj.get_relative_file_path()
|
||||
}
|
||||
|
||||
fn get_repo_file_path(&self) -> PathBuf {
|
||||
self.obj.get_repo_file_path()
|
||||
}
|
||||
|
||||
fn get_local_obj(&self) -> LocalObj {
|
||||
self.obj.get_local_obj()
|
||||
}
|
||||
@ -267,10 +244,6 @@ impl ObjMethods for Tree {
|
||||
fs::remove_dir_all(self.get_file_path())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn exists_on_remote(&mut self) -> bool {
|
||||
self.obj.exists_on_remote()
|
||||
}
|
||||
}
|
||||
|
||||
impl Obj {
|
||||
@ -281,8 +254,7 @@ impl Obj {
|
||||
file_path: PathBuf::new(),
|
||||
obj_type: ObjType::DEFAULT,
|
||||
hash_path: String::new(),
|
||||
relative_file_path: PathBuf::new(),
|
||||
repo_file_path: PathBuf::new()
|
||||
relative_file_path: PathBuf::new()
|
||||
}
|
||||
}
|
||||
|
||||
@ -313,8 +285,7 @@ impl Obj {
|
||||
false => ObjType::DEFAULT
|
||||
},
|
||||
file_path: abs_path,
|
||||
relative_file_path: path.clone(),
|
||||
repo_file_path: path,
|
||||
relative_file_path: path,
|
||||
hash_path: hash,
|
||||
}
|
||||
}
|
||||
@ -353,8 +324,7 @@ impl Obj {
|
||||
_ => ObjType::DEFAULT
|
||||
},
|
||||
file_path: abs_path,
|
||||
relative_file_path: path.clone(),
|
||||
repo_file_path: path,
|
||||
relative_file_path: path,
|
||||
hash_path: String::from(hash_path),
|
||||
};
|
||||
|
||||
@ -372,7 +342,6 @@ impl Obj {
|
||||
obj_type: ObjType::TREE,
|
||||
file_path: PathBuf::new(),
|
||||
relative_file_path: PathBuf::new(),
|
||||
repo_file_path: PathBuf::new(),
|
||||
hash_path: String::new(),
|
||||
}
|
||||
}
|
||||
|
@ -85,10 +85,10 @@ impl Tree {
|
||||
// create tree object
|
||||
let mut content = format!("{} {}", self.get_name(), date);
|
||||
//create_obj(self.get_hash_path(), &content)?;
|
||||
// todo!();
|
||||
todo!();
|
||||
|
||||
// update date for all parent
|
||||
//todo!();
|
||||
todo!();
|
||||
//if up_parent {
|
||||
// update_dates(path, date)?;
|
||||
//}
|
||||
|
98
tests/add.rs
98
tests/add.rs
@ -1,98 +0,0 @@
|
||||
use std::str;
|
||||
|
||||
mod utils;
|
||||
use utils::{utils::*, client::ClientTest};
|
||||
|
||||
fn line_should_contains(lines: &Vec<String>, nb: usize, str: &str) {
|
||||
|
||||
if lines[nb].find(str).is_none()
|
||||
{
|
||||
eprintln!("'{}' not found in '{}'", str, lines[nb]);
|
||||
dbg!(lines);
|
||||
}
|
||||
|
||||
assert!(lines[nb].find(str).is_some());
|
||||
}
|
||||
|
||||
fn lines_should_not_contains(lines: Vec<String>, str: &str) {
|
||||
|
||||
for line in lines {
|
||||
if line.find("Changes not staged for push").is_some() {
|
||||
return;
|
||||
}
|
||||
|
||||
if line.find(str).is_some() {
|
||||
eprintln!("'{}' found in '{}'", str, line);
|
||||
}
|
||||
assert!(line.find(str).is_none());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn collect_status_lines(client: &mut ClientTest) -> Vec<String> {
|
||||
let out = client.run_cmd("status");
|
||||
|
||||
str::from_utf8(&out.stdout)
|
||||
.unwrap()
|
||||
.split("\n")
|
||||
.map(|s| s.to_owned())
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod push_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn simple_add() {
|
||||
let id = get_random_test_id();
|
||||
let mut client = ClientTest::new(id).init();
|
||||
|
||||
let _ = client.add_file("file1", "foo");
|
||||
client.run_cmd_ok("add file1");
|
||||
|
||||
let lines = collect_status_lines(&mut client);
|
||||
|
||||
// test
|
||||
line_should_contains(&lines, 2, "file1");
|
||||
|
||||
client.clean();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn add_config_file() {
|
||||
let id = get_random_test_id();
|
||||
let mut client = ClientTest::new(id).init();
|
||||
|
||||
let _ = client.add_file("file1", "foo");
|
||||
client.run_cmd_ok("add .nextsync -f");
|
||||
|
||||
let lines = collect_status_lines(&mut client);
|
||||
|
||||
// test
|
||||
lines_should_not_contains(lines, ".nextsync");
|
||||
|
||||
client.clean();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn add_dir_implicit() {
|
||||
let id = get_random_test_id();
|
||||
let mut client = ClientTest::new(id).init();
|
||||
|
||||
let _ = client.add_dir("dir");
|
||||
let _ = client.add_file("dir/file1", "foo");
|
||||
|
||||
// adding the file should add the dir
|
||||
client.run_cmd_ok("add dir/file1");
|
||||
|
||||
let lines = collect_status_lines(&mut client);
|
||||
|
||||
// tests
|
||||
line_should_contains(&lines, 2, "dir");
|
||||
line_should_contains(&lines, 3, "dir/file1");
|
||||
|
||||
client.clean();
|
||||
}
|
||||
|
||||
}
|
@ -1,8 +1,19 @@
|
||||
use rand::{distributions::Alphanumeric, Rng};
|
||||
|
||||
mod utils;
|
||||
use utils::{utils::*, server::ServerTest, client::ClientTest};
|
||||
use utils::{server::ServerTest, client::ClientTest};
|
||||
|
||||
|
||||
fn get_random_test_id() -> String {
|
||||
let mut id: String = rand::thread_rng()
|
||||
.sample_iter(&Alphanumeric)
|
||||
.take(7)
|
||||
.map(char::from)
|
||||
.collect();
|
||||
id.push_str("_nextsync");
|
||||
id.to_owned()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod push_tests {
|
||||
use super::*;
|
||||
@ -51,7 +62,7 @@ mod push_tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn push_dir_explicit() {
|
||||
fn push_dir() {
|
||||
let id = get_random_test_id();
|
||||
let mut server = ServerTest::new(id.clone()).init();
|
||||
let mut client = ClientTest::new(id).init();
|
||||
@ -60,27 +71,7 @@ mod push_tests {
|
||||
let _ = client.add_file("dir/file2", "bar");
|
||||
|
||||
// push dir and file2
|
||||
client.run_cmd_ok("add dir");
|
||||
client.run_cmd_ok("push");
|
||||
|
||||
// tests
|
||||
assert!(server.has_file("dir/file2", "bar"));
|
||||
|
||||
client.clean();
|
||||
server.clean();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn push_dir_implicit() {
|
||||
let id = get_random_test_id();
|
||||
let mut server = ServerTest::new(id.clone()).init();
|
||||
let mut client = ClientTest::new(id).init();
|
||||
|
||||
let _ = client.add_dir("dir");
|
||||
let _ = client.add_file("dir/file2", "bar");
|
||||
|
||||
// push dir and file2
|
||||
client.run_cmd_ok("add dir/file2");
|
||||
client.run_cmd_ok("add file2");
|
||||
client.run_cmd_ok("push");
|
||||
|
||||
// tests
|
||||
|
@ -3,6 +3,3 @@ pub mod server;
|
||||
|
||||
#[path = "utils/client.rs"]
|
||||
pub mod client;
|
||||
|
||||
#[path = "utils/utils.rs"]
|
||||
pub mod utils;
|
||||
|
@ -13,6 +13,7 @@ pub struct ClientTest {
|
||||
|
||||
impl ClientTest {
|
||||
pub fn new(id: String) -> Self {
|
||||
|
||||
// create a directory in /tmp with the given id
|
||||
let mut vol = String::from("/tmp/");
|
||||
vol.push_str(&id);
|
||||
@ -55,7 +56,6 @@ impl ClientTest {
|
||||
pub fn run_cmd_ok(&mut self, args: &str) -> Output {
|
||||
let output = self.run_cmd(args);
|
||||
if !output.status.success() {
|
||||
println!("id: {}", self.test_id.clone());
|
||||
println!("Failed to execute: '{}'", args);
|
||||
println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
|
||||
println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
|
||||
|
@ -93,7 +93,6 @@ impl ServerTest {
|
||||
let full_path = self.volume.clone().join(file);
|
||||
|
||||
if !full_path.exists() {
|
||||
println!("id: {}", self.test_id.clone());
|
||||
eprintln!("File '{}' doesn't exists on the server", file);
|
||||
return false;
|
||||
}
|
||||
@ -102,7 +101,6 @@ impl ServerTest {
|
||||
for line in BufReader::new(f).lines(){
|
||||
if let Ok(line) = line {
|
||||
if line != content {
|
||||
println!("id: {}", self.test_id.clone());
|
||||
eprintln!("File '{}' is not equal, {} != {}", file, line, content);
|
||||
return false;
|
||||
}
|
||||
|
@ -1,11 +0,0 @@
|
||||
use rand::{distributions::Alphanumeric, Rng};
|
||||
|
||||
pub fn get_random_test_id() -> String {
|
||||
let mut id: String = rand::thread_rng()
|
||||
.sample_iter(&Alphanumeric)
|
||||
.take(7)
|
||||
.map(char::from)
|
||||
.collect();
|
||||
id.push_str("_nextsync");
|
||||
id.to_owned()
|
||||
}
|
Loading…
Reference in New Issue
Block a user