From 3207391fdbf41361a94c1b6355424abe4a28104e Mon Sep 17 00:00:00 2001 From: grimhilt Date: Sun, 10 Mar 2024 23:06:32 +0100 Subject: [PATCH] test(push): check that object are locally created when pushed --- tests/add.rs | 1 - tests/push.rs | 23 +++++++++++++++++++- tests/utils.rs | 3 +++ tests/utils/client.rs | 43 +++++++++++++++++++++++++++++++++++++ tests/utils/status_utils.rs | 9 ++++++++ 5 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 tests/utils/status_utils.rs diff --git a/tests/add.rs b/tests/add.rs index bae10c9..9b84561 100644 --- a/tests/add.rs +++ b/tests/add.rs @@ -26,7 +26,6 @@ fn lines_should_not_contains(lines: Vec, str: &str) { } assert!(line.find(str).is_none()); } - } fn collect_status_lines(client: &mut ClientTest) -> Vec { diff --git a/tests/push.rs b/tests/push.rs index cdda442..926bc71 100644 --- a/tests/push.rs +++ b/tests/push.rs @@ -1,6 +1,6 @@ mod utils; -use utils::{utils::*, server::ServerTest, client::ClientTest}; +use utils::{utils::*, status_utils::*, server::ServerTest, client::ClientTest}; #[cfg(test)] @@ -19,6 +19,9 @@ mod push_tests { // tests assert!(server.has_file("file1", "foo")); + let (staged, not_staged) = client.get_status(); + lines_should_not_contains(staged, "file1"); + lines_should_not_contains(not_staged, "file1"); client.clean(); server.clean(); @@ -38,6 +41,10 @@ mod push_tests { // tests assert!(server.has_file("file1", "foo")); + let (staged, not_staged) = client.get_status(); + lines_should_not_contains(staged, "file1"); + lines_should_not_contains(not_staged, "file1"); + // change content of file1 let _ = client.add_file("file1", "bar"); client.run_cmd_ok("add file1"); @@ -45,6 +52,10 @@ mod push_tests { // tests assert!(server.has_file("file1", "bar")); + let (staged, not_staged) = client.get_status(); + lines_should_not_contains(staged, "file1"); + lines_should_not_contains(not_staged, "file1"); + client.clean(); server.clean(); @@ -65,6 +76,11 @@ mod push_tests { // tests assert!(server.has_file("dir/file2", "bar")); + let (staged, not_staged) = client.get_status(); + lines_should_not_contains(staged.clone(), "file2"); + lines_should_not_contains(staged, "foo"); + lines_should_not_contains(not_staged.clone(), "file2"); + lines_should_not_contains(not_staged, "foo"); client.clean(); server.clean(); @@ -85,6 +101,11 @@ mod push_tests { // tests assert!(server.has_file("dir/file2", "bar")); + let (staged, not_staged) = client.get_status(); + lines_should_not_contains(staged.clone(), "file2"); + lines_should_not_contains(staged, "foo"); + lines_should_not_contains(not_staged.clone(), "file2"); + lines_should_not_contains(not_staged, "foo"); client.clean(); server.clean(); diff --git a/tests/utils.rs b/tests/utils.rs index 29dafbb..7aaacdd 100644 --- a/tests/utils.rs +++ b/tests/utils.rs @@ -6,3 +6,6 @@ pub mod client; #[path = "utils/utils.rs"] pub mod utils; + +#[path = "utils/status_utils.rs"] +pub mod status_utils; diff --git a/tests/utils/client.rs b/tests/utils/client.rs index 333cf1e..b244e28 100644 --- a/tests/utils/client.rs +++ b/tests/utils/client.rs @@ -1,3 +1,4 @@ +use std::str; use std::process::{Command, Output}; use std::fs::{self, File}; use std::io::Write; @@ -90,4 +91,46 @@ impl ClientTest { file.write_all(content.as_bytes())?; Ok(()) } + + /// 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); + } } diff --git a/tests/utils/status_utils.rs b/tests/utils/status_utils.rs new file mode 100644 index 0000000..e9cbf55 --- /dev/null +++ b/tests/utils/status_utils.rs @@ -0,0 +1,9 @@ + +pub fn lines_should_not_contains(lines: Vec, str: &str) { + for line in lines { + if line.find(str).is_some() { + eprintln!("'{}' found in '{}'", str, line); + } + assert!(line.find(str).is_none()); + } +}