From 642c3587372cb81d81b43438d40ef6a5574b19b6 Mon Sep 17 00:00:00 2001 From: grimhilt Date: Thu, 22 Feb 2024 14:00:13 +0100 Subject: [PATCH] feat(test): allow multiple tests --- tests/push.rs | 27 +++++++++++++++++++++++++++ tests/utils/client.rs | 20 +++++++++++++------- tests/utils/server.rs | 12 ++++++++---- 3 files changed, 48 insertions(+), 11 deletions(-) diff --git a/tests/push.rs b/tests/push.rs index a5c4978..eeb5af5 100644 --- a/tests/push.rs +++ b/tests/push.rs @@ -1,4 +1,5 @@ use rand::{distributions::Alphanumeric, Rng}; +use std::env; mod utils; use utils::{server::ServerTest, client::ClientTest}; @@ -34,4 +35,30 @@ mod push_tests { client.clean(); server.clean(); } + + #[test] + fn push_update() { + let id = get_random_test_id(); + let mut server = ServerTest::new(id.clone()).init(); + let mut client = ClientTest::new(id).init(); + + // init content of file1 + let _ = client.add_file("file1", "foo"); + client.run_cmd_ok("add file1"); + client.run_cmd_ok("push"); + + // tests + assert!(server.has_file("file1", "foo")); + + // change content of file1 + let _ = client.add_file("file1", "bar"); + client.run_cmd_ok("add file1"); + client.run_cmd_ok("push"); + + // tests + assert!(server.has_file("file1", "bar")); + + client.clean(); + server.clean(); + } } diff --git a/tests/utils/client.rs b/tests/utils/client.rs index 5a33183..2f4072b 100644 --- a/tests/utils/client.rs +++ b/tests/utils/client.rs @@ -5,10 +5,10 @@ use std::env; use std::path::PathBuf; pub struct ClientTest { - user: String, - volume: String, - test_id: String, - exe_path: PathBuf, + user: String, // the nextcloud user + volume: String, // temp dir for the test + test_id: String, // name of the test (e.g nextsync_rand) + exe_path: PathBuf, // absolute path of nextsync executable } impl ClientTest { @@ -23,7 +23,7 @@ impl ClientTest { let mut exe_path = env::current_dir().unwrap(); exe_path = exe_path.join("target/debug/nextsync"); - let _ = env::set_current_dir(vol.clone()); + // let _ = env::set_current_dir(vol.clone()); // build the client ClientTest { @@ -46,12 +46,17 @@ impl ClientTest { // set token for request self.run_cmd_ok(&format!("credential add {} {}", self.user, self.user)); - self + } - pub fn clean(self) -> Self { + pub fn clean(mut self) -> Self { let _ = fs::remove_dir_all(&self.volume); + + // set back the current dir + self.exe_path.pop(); + dbg!(self.exe_path.clone()); + let _ = env::set_current_dir(self.exe_path.clone()); self } @@ -68,6 +73,7 @@ impl ClientTest { 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"); diff --git a/tests/utils/server.rs b/tests/utils/server.rs index d6eee43..38a6409 100644 --- a/tests/utils/server.rs +++ b/tests/utils/server.rs @@ -93,17 +93,21 @@ impl ServerTest { let full_path = self.volume.clone().join(file); if !full_path.exists() { - eprintln!("File '{}' does't exists", file); + eprintln!("File '{}' does't exists on the server", file); return false; } - let file = File::open(full_path).unwrap(); - for line in BufReader::new(file).lines(){ + let f = File::open(full_path).unwrap(); + for line in BufReader::new(f).lines(){ if let Ok(line) = line { + if line != content { + eprintln!("File '{}' is not equal, {} != {}", file, line, content); + return false; + } return line == content; } } - return false; + return true; } }