From 7180647d26d9ea442486b688c7ef301d0f372011 Mon Sep 17 00:00:00 2001 From: grimhilt Date: Mon, 11 Mar 2024 14:47:05 +0100 Subject: [PATCH] test(pull): add test for pull --- tests/pull.rs | 26 ++++++++++++++++++++ tests/push.rs | 15 ++++++++---- tests/utils.rs | 3 +++ tests/utils/client.rs | 10 +++++++- tests/utils/files_utils.rs | 25 +++++++++++++++++++ tests/utils/server.rs | 50 ++++++++++++++++++-------------------- 6 files changed, 96 insertions(+), 33 deletions(-) create mode 100644 tests/pull.rs create mode 100644 tests/utils/files_utils.rs diff --git a/tests/pull.rs b/tests/pull.rs new file mode 100644 index 0000000..8470563 --- /dev/null +++ b/tests/pull.rs @@ -0,0 +1,26 @@ +mod utils; +use utils::{utils::*, status_utils::*, server::ServerTest, client::ClientTest}; + + +#[cfg(test)] +mod push_tests { + use super::*; + + #[test] + fn simple_pull() { + let id = get_random_test_id(); + let mut server = ServerTest::new(id.clone()); + server.init(); + let mut client = ClientTest::new(id).init(); + + let _ = server.add_file("file1", "foo"); + client.run_cmd_ok("pull"); + + // tests + assert!(client.has_file("file1", "foo")); + + client.clean(); + server.clean(); + } + +} diff --git a/tests/push.rs b/tests/push.rs index 16536c5..1994ecd 100644 --- a/tests/push.rs +++ b/tests/push.rs @@ -10,7 +10,8 @@ mod push_tests { #[test] fn simple_push() { let id = get_random_test_id(); - let mut server = ServerTest::new(id.clone()).init(); + let mut server = ServerTest::new(id.clone()); + server.init(); let mut client = ClientTest::new(id).init(); let _ = client.add_file("file1", "foo"); @@ -30,7 +31,8 @@ mod push_tests { #[test] fn push_update() { let id = get_random_test_id(); - let mut server = ServerTest::new(id.clone()).init(); + let mut server = ServerTest::new(id.clone()); + server.init(); let mut client = ClientTest::new(id).init(); // init content of file1 @@ -64,7 +66,8 @@ mod push_tests { #[test] fn push_dir_explicit() { let id = get_random_test_id(); - let mut server = ServerTest::new(id.clone()).init(); + let mut server = ServerTest::new(id.clone()); + server.init(); let mut client = ClientTest::new(id).init(); let _ = client.add_dir("dir"); @@ -89,7 +92,8 @@ mod push_tests { #[test] fn push_dir_implicit() { let id = get_random_test_id(); - let mut server = ServerTest::new(id.clone()).init(); + let mut server = ServerTest::new(id.clone()); + server.init(); let mut client = ClientTest::new(id).init(); let _ = client.add_dir("dir"); @@ -114,7 +118,8 @@ mod push_tests { #[test] fn push_all() { let id = get_random_test_id(); - let mut server = ServerTest::new(id.clone()).init(); + let mut server = ServerTest::new(id.clone()); + server.init(); let mut client = ClientTest::new(id).init(); let _ = client.add_file("file1", "foo"); diff --git a/tests/utils.rs b/tests/utils.rs index 7aaacdd..a6942e8 100644 --- a/tests/utils.rs +++ b/tests/utils.rs @@ -9,3 +9,6 @@ pub mod utils; #[path = "utils/status_utils.rs"] pub mod status_utils; + +#[path = "utils/files_utils.rs"] +pub mod files_utils; diff --git a/tests/utils/client.rs b/tests/utils/client.rs index b244e28..170f643 100644 --- a/tests/utils/client.rs +++ b/tests/utils/client.rs @@ -1,10 +1,12 @@ use std::str; use std::process::{Command, Output}; use std::fs::{self, File}; -use std::io::Write; +use std::io::{Write, BufReader, BufRead}; use std::env; use std::path::PathBuf; +use super::files_utils::has_files; + pub struct ClientTest { user: String, // the nextcloud user volume: String, // temp dir for the test @@ -92,6 +94,12 @@ impl ClientTest { 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"); diff --git a/tests/utils/files_utils.rs b/tests/utils/files_utils.rs new file mode 100644 index 0000000..f619307 --- /dev/null +++ b/tests/utils/files_utils.rs @@ -0,0 +1,25 @@ +use std::io::{BufReader, BufRead, Write}; +use std::fs::{File}; +use std::path::PathBuf; + +pub fn has_files(full_path: PathBuf, file: &str, content: &str, test_id: String) -> bool +{ + if !full_path.exists() { + println!("id: {}", test_id.clone()); + eprintln!("File '{}' doesn't exists on the server", file); + return false; + } + + let f = File::open(full_path).unwrap(); + for line in BufReader::new(f).lines(){ + if let Ok(line) = line { + if line != content { + println!("id: {}", test_id); + eprintln!("File '{}' is not equal, {} != {}", file, line, content); + return false; + } + return line == content; + } + } + return true; +} diff --git a/tests/utils/server.rs b/tests/utils/server.rs index 1af3ed5..af7b989 100644 --- a/tests/utils/server.rs +++ b/tests/utils/server.rs @@ -1,10 +1,12 @@ use std::process::Command; use std::os::unix::fs::PermissionsExt; use std::fs::{self, File, Permissions}; -use std::io::{BufReader, BufRead}; +use std::io::{BufReader, BufRead, Write}; use std::env; use std::path::PathBuf; +use super::files_utils::has_files; + #[cfg(test)] pub struct ServerTest { user: String, @@ -25,15 +27,17 @@ impl ServerTest { } } - pub fn init(mut self) -> Self { + pub fn init(&mut self) -> &mut ServerTest{ self.add_dir(self.test_id.clone()); self.volume = self.volume.join(self.test_id.clone()); - self.sync_test() + self.sync_test(); + self } - pub fn clean(mut self) -> Self { + pub fn clean(&mut self) -> &mut ServerTest{ self.remove_dir(self.test_id.clone()); - self.sync_root() + self.sync_root(); + self } pub fn add_dir(&mut self, path: String) -> &mut ServerTest { @@ -54,6 +58,16 @@ impl ServerTest { self } + pub fn add_file(&mut self, name: &str, content: &str) -> std::io::Result<()> { + let mut full_path = self.volume.clone(); + full_path.push(name); + + let mut file = File::create(full_path)?; + file.write_all(content.as_bytes())?; + self.sync_test(); + Ok(()) + } + pub fn remove_dir(&mut self, path: String) -> &mut ServerTest { let mut full_path = self.volume.clone(); full_path.push(path); @@ -62,16 +76,16 @@ impl ServerTest { self } - fn sync_root(self) -> Self { + fn sync_root(&self) -> &Self { self.sync("") } - fn sync_test(self) -> Self { + fn sync_test(&self) -> &Self { let test_id = self.test_id.clone(); self.sync(&test_id) } - fn sync(self, path: &str) -> Self { + fn sync(&self, path: &str) -> &Self { // perform the occ files:scan command inside the nextcloud docker container let nextcloud_docker = "master-nextcloud-1"; @@ -91,25 +105,7 @@ impl ServerTest { pub fn has_file(&mut self, file: &str, content: &str) -> bool { 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; - } - - let f = File::open(full_path).unwrap(); - 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; - } - return line == content; - } - } - return true; + has_files(full_path, file, content, self.test_id.clone()) } }