use nextsync::config::config::Config; use rand::{distributions::Alphanumeric, Rng}; use std::env; use std::fs::{self, File, OpenOptions}; use std::io::{self, Write}; use std::path::PathBuf; use std::process::{Command, Output}; use std::str; use std::sync::OnceLock; // Absolute path of the nextsync executable static EXE_PATH: OnceLock = OnceLock::new(); pub struct ClientTest { volume: String, // temp dir for the test pub test_id: String, // name of the test (e.g nextsync_rand) } pub fn get_random_test_id() -> String { rand::thread_rng() .sample_iter(&Alphanumeric) .take(4) .map(char::from) .collect() } impl ClientTest { pub fn new(id: &str) -> Self { let _ = EXE_PATH.get_or_init(|| { let mut exe_path = env::current_dir().unwrap(); exe_path = exe_path.join("target/debug/nextsync"); exe_path }); let mut test_id = id.to_string(); test_id.push_str("_"); test_id.push_str(&get_random_test_id()); // create a directory in /tmp with the given id let mut vol = String::from("/tmp/nextsync/"); vol.push_str(&test_id); let _ = fs::remove_dir_all(&vol); let _ = fs::create_dir_all(&vol); // Setup the current dir to the local repo env::set_current_dir(&vol).unwrap(); // build the client ClientTest { volume: vol, test_id, } } pub fn init(mut self) -> Self { self.exec_ok("init"); println!("========== {} ========== ", &self.test_id); // 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 get_config(&self) -> Config { Config::from(Some(&self.volume)) } pub fn new_config(&self, path: &str) -> Config { let mut full_path = self.volume.clone(); full_path.push_str("/"); full_path.push_str(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<()> { fs::remove_dir_all(&self.volume)?; Ok(()) } pub fn exec_ok(&mut self, args: &str) -> Output { let output = self.exec(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 exec(&mut self, args: &str) -> Output { let output = Command::new(EXE_PATH.get().unwrap().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 add_ignore_rule(&self, rule: &str) { let mut nsignore_path = self.volume.clone(); nsignore_path.push_str("/.nsignore"); let mut file = OpenOptions::new() .write(true) .create(true) .append(true) .open(nsignore_path) .unwrap(); let _ = writeln!(file, "{rule}").unwrap(); } // 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()) // } }