use std::str; use std::process::{Command, Output}; use std::fs::{self, File}; use std::io::Write; use std::env; use std::path::PathBuf; use super::files_utils::has_files; #[cfg(test)] pub struct ClientTest { user: String, // the nextcloud user 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 } #[cfg(test)] 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); let _ = fs::create_dir(vol.clone()); // get nextsync path let mut exe_path = env::current_dir().unwrap(); exe_path = exe_path.join("target/debug/nextsync"); // build the client ClientTest { user: String::from("admin"), volume: vol, test_id: id, exe_path } } pub fn init(mut self) -> Self { self.run_cmd_ok("init"); // set remote url let url = String::from(format!("{}@nextcloud.local/{}", self.user, self.test_id)); self.run_cmd_ok(&format!("remote add origin {}", url)); // set force_unsecure as debug server has not certificate self.run_cmd_ok("config set force_insecure true"); // set token for request self.run_cmd_ok(&format!("credential add {} {}", self.user, self.user)); self } pub fn clean(self) -> Self { let _ = fs::remove_dir_all(&self.volume); self } 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)); } assert!(output.status.success()); output } pub fn run_cmd(&mut self, args: &str) -> Output { let output = Command::new(self.exe_path.to_str().unwrap()) .current_dir(self.volume.clone()) .args(args.split(" ")) .output() .expect("Could not execute nextsync command"); return output; } pub fn add_dir(&mut self, name: &str) -> std::io::Result<()> { let mut path = self.volume.clone(); path.push_str("/"); path.push_str(name); let _ = fs::create_dir_all(path)?; Ok(()) } pub fn add_file(&mut self, name: &str, content: &str) -> std::io::Result<()> { let mut path = self.volume.clone(); path.push_str("/"); path.push_str(name); let mut file = File::create(path)?; file.write_all(content.as_bytes())?; Ok(()) } pub fn remove_file(&mut self, name: &str) -> std::io::Result<()> { let mut path = self.volume.clone(); path.push_str("/"); path.push_str(name); fs::remove_file(path)?; 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); has_files(full_path, file, content, self.test_id.clone()) } /// get the files given by the status command in two vector (staged and not staged) pub fn get_status(&mut self) -> (Vec, Vec) { let out = self.run_cmd("status"); let lines: Vec = str::from_utf8(&out.stdout) .unwrap() .split("\n") .map(|s| s.to_owned()) .collect(); let mut staged = vec![]; let mut not_staged = vec![]; let mut in_staged = true; let mut counter = 0; for line in lines { if line.find("not staged").is_some() { in_staged = false; counter = 1; continue; } // skip two first line as there are not files if counter < 2 { counter += 1; continue; } if line == String::from("") { continue; } if in_staged { staged.push(line); } else { not_staged.push(line); } } return (staged, not_staged); } }