test(push): add push remove test

This commit is contained in:
grimhilt 2024-03-31 19:23:32 +02:00
parent 5e43800d6c
commit 1aa02a24af
7 changed files with 89 additions and 24 deletions

View File

@ -32,6 +32,7 @@ pub fn add(args: AddArgs) {
let rules = nextsyncignore::get_rules();
for file in file_vec {
dbg!(&file);
let f = match normalize_relative(file) {
Ok(f) => f,
Err(err) => {

View File

@ -39,7 +39,7 @@ fn collect_status_lines(client: &mut ClientTest) -> Vec<String> {
}
#[cfg(test)]
mod push_tests {
mod add_tests {
use super::*;
#[test]

View File

@ -2,6 +2,14 @@
mod utils;
use utils::{utils::*, status_utils::*, server::ServerTest, client::ClientTest};
fn init_test() -> (ClientTest, ServerTest) {
let id = get_random_test_id();
let mut server = ServerTest::new(id.clone());
server.init();
let client = ClientTest::new(id).init();
(client, server)
}
#[cfg(test)]
mod push_tests {
@ -9,10 +17,7 @@ mod push_tests {
#[test]
fn simple_push() {
let id = get_random_test_id();
let mut server = ServerTest::new(id.clone());
server.init();
let mut client = ClientTest::new(id).init();
let (mut client, mut server) = init_test();
let _ = client.add_file("file1", "foo");
client.run_cmd_ok("add file1");
@ -30,10 +35,7 @@ mod push_tests {
#[test]
fn push_update() {
let id = get_random_test_id();
let mut server = ServerTest::new(id.clone());
server.init();
let mut client = ClientTest::new(id).init();
let (mut client, mut server) = init_test();
// init content of file1
let _ = client.add_file("file1", "foo");
@ -65,10 +67,7 @@ mod push_tests {
#[test]
fn push_dir_explicit() {
let id = get_random_test_id();
let mut server = ServerTest::new(id.clone());
server.init();
let mut client = ClientTest::new(id).init();
let (mut client, mut server) = init_test();
let _ = client.add_dir("dir");
let _ = client.add_file("dir/file2", "bar");
@ -91,10 +90,7 @@ mod push_tests {
#[test]
fn push_dir_implicit() {
let id = get_random_test_id();
let mut server = ServerTest::new(id.clone());
server.init();
let mut client = ClientTest::new(id).init();
let (mut client, mut server) = init_test();
let _ = client.add_dir("dir");
let _ = client.add_file("dir/file2", "bar");
@ -117,10 +113,7 @@ mod push_tests {
#[test]
fn push_all() {
let id = get_random_test_id();
let mut server = ServerTest::new(id.clone());
server.init();
let mut client = ClientTest::new(id).init();
let (mut client, mut server) = init_test();
let _ = client.add_file("file1", "foo");
let _ = client.add_dir("dir");
@ -140,4 +133,32 @@ mod push_tests {
client.clean();
server.clean();
}
#[test]
fn push_file_deletion() {
let (mut client, mut server) = init_test();
let _ = client.add_file("file1", "foo");
// push file1
client.run_cmd_ok("add file1");
client.run_cmd_ok("push");
// tests
assert!(server.has_file("file1", "foo"));
status_should_be_empty(&mut client);
// remove it
let _ = client.remove_file("file1");
client.run_cmd_ok("add file1");
dbg!(client.get_status());
client.run_cmd_ok("push");
// tests
assert!(server.has_not_file("file1"));
status_should_be_empty(&mut client);
client.clean();
server.clean();
}
}

View File

@ -11,7 +11,7 @@ use super::files_utils::has_files;
pub struct ClientTest {
user: String, // the nextcloud user
volume: String, // temp dir for the test
test_id: String, // name of the test (e.g nextsync_rand)
pub test_id: String, // name of the test (e.g nextsync_rand)
exe_path: PathBuf, // absolute path of nextsync executable
}
@ -96,6 +96,15 @@ impl ClientTest {
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(name)?;
Ok(())
}
pub fn has_file(&mut self, file: &str, content: &str) -> bool {
let full_path = PathBuf::from(self.volume.clone()).join(file);

View File

@ -24,3 +24,15 @@ pub fn has_files(full_path: PathBuf, file: &str, content: &str, test_id: String)
}
return true;
}
#[cfg(test)]
pub fn has_not_file(full_path: PathBuf, file: &str, test_id: String) -> bool
{
if full_path.exists() {
println!("id: {}", test_id.clone());
eprintln!("File '{}' exists but it shouldn't", file);
return false;
}
return true;
}

View File

@ -5,13 +5,13 @@ use std::io::Write;
use std::env;
use std::path::PathBuf;
use super::files_utils::has_files;
use super::files_utils::{self, has_files};
#[cfg(test)]
pub struct ServerTest {
user: String,
volume: PathBuf,
test_id: String
pub test_id: String
}
#[cfg(test)]
@ -108,5 +108,10 @@ impl ServerTest {
let full_path = self.volume.clone().join(file);
has_files(full_path, file, content, self.test_id.clone())
}
pub fn has_not_file(&mut self, file: &str) -> bool {
let full_path = self.volume.clone().join(file);
files_utils::has_not_file(full_path, file, self.test_id.clone())
}
}

View File

@ -1,3 +1,4 @@
use super::client::ClientTest;
#[cfg(test)]
pub fn lines_should_not_contains(lines: Vec<String>, str: &str) {
@ -8,3 +9,19 @@ pub fn lines_should_not_contains(lines: Vec<String>, str: &str) {
assert!(line.find(str).is_none());
}
}
#[cfg(test)]
pub fn status_should_be_empty(client: &mut ClientTest) {
let (staged, not_staged) = client.get_status();
if staged.len() != 0 {
eprintln!("id: {}", client.test_id.clone());
eprintln!("Staged should be empty but has '{}'", staged.len());
assert!(staged.len() == 0);
}
if staged.len() != 0 {
eprintln!("id: {}", client.test_id.clone());
eprintln!("Not Staged should be empty but has '{}'", not_staged.len());
assert!(not_staged.len() == 0);
}
}