tests(add): introduce basics tests for add

This commit is contained in:
grimhilt 2024-09-08 17:59:39 +02:00
parent baeef1a33a
commit e66dc8d408
4 changed files with 75 additions and 10 deletions

49
tests/add_test.rs Normal file
View File

@ -0,0 +1,49 @@
mod common;
use common::client::ClientTest;
use nextsync::store::indexer::Indexer;
use nextsync::commands::status::{get_obj_changes, StatusArgs};
use nextsync::config::config::Config;
use std::io;
use std::path::PathBuf;
const DEFAULT_STATUS_ARG: StatusArgs = StatusArgs { nostyle: false };
fn indexed_expected(indexer: &Indexer, expected: Vec<&str>) {
let objs = indexer.get_indexed_objs();
assert_eq!(objs.len(), expected.len());
for obj in expected {
assert!(objs
.iter()
.position(|e| { e.path == PathBuf::from(obj) })
.is_some());
}
}
#[test]
fn add_ignored_file() -> io::Result<()> {
let mut client = ClientTest::new("add__simple_file").init();
client.add_ignore_rule("foo");
client.add_file("foo", "foo")?;
let mut indexer = Indexer::new(client.get_config().get_root_unsafe());
client.exec_ok("add foo");
let _ = indexer.load();
indexed_expected(&indexer, vec![]);
client.exec_ok("add foo -f");
let _ = indexer.load();
indexed_expected(&indexer, vec!["foo"]);
client.ok()
}
// add double globbing
// add all
// add folders
// add part of folders
// add all folder
// add automatic ignored if is tracked

View File

@ -1,4 +1,5 @@
use nextsync::config::config::Config; use nextsync::config::config::Config;
use std::fs::OpenOptions;
use rand::{distributions::Alphanumeric, Rng}; use rand::{distributions::Alphanumeric, Rng};
use std::env; use std::env;
use std::fs::{self, File}; use std::fs::{self, File};
@ -125,6 +126,18 @@ impl ClientTest {
Ok(()) 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 { // pub fn has_file(&mut self, file: &str, content: &str) -> bool {
// let full_path = PathBuf::from(self.volume.clone()).join(file); // let full_path = PathBuf::from(self.volume.clone()).join(file);

4
tests/reset_test.rs Normal file
View File

@ -0,0 +1,4 @@
// reset all
// reset file
// reset folder
// reset unknown

View File

@ -8,7 +8,7 @@ use std::path::PathBuf;
const DEFAULT_STATUS_ARG: StatusArgs = StatusArgs { nostyle: false }; const DEFAULT_STATUS_ARG: StatusArgs = StatusArgs { nostyle: false };
fn status_exepected(config: &Config, staged: Vec<&str>, not_staged: Vec<&str>) { fn status_expected(config: &Config, staged: Vec<&str>, not_staged: Vec<&str>) {
let res = get_obj_changes(&DEFAULT_STATUS_ARG, config); let res = get_obj_changes(&DEFAULT_STATUS_ARG, config);
assert_eq!(res.staged.len(), staged.len()); assert_eq!(res.staged.len(), staged.len());
@ -36,16 +36,15 @@ fn simple_file() -> io::Result<()> {
let mut client = ClientTest::new("status__simple_file").init(); let mut client = ClientTest::new("status__simple_file").init();
client.add_file("foo", "foo")?; client.add_file("foo", "foo")?;
status_exepected(&client.get_config(), vec![], vec!["foo"]); status_expected(&client.get_config(), vec![], vec!["foo"]);
client.exec_ok("add foo"); client.exec_ok("add foo");
status_exepected(&client.get_config(), vec!["foo"], vec![]); status_expected(&client.get_config(), vec!["foo"], vec![]);
client.ok() client.ok()
} }
#[test] #[test]
#[ignore]
fn all_folder() -> io::Result<()> { fn all_folder() -> io::Result<()> {
let mut client = ClientTest::new("status__all_folder").init(); let mut client = ClientTest::new("status__all_folder").init();
@ -53,10 +52,10 @@ fn all_folder() -> io::Result<()> {
client.add_file("dir/foo", "foo")?; client.add_file("dir/foo", "foo")?;
client.add_file("dir/bar", "bar")?; client.add_file("dir/bar", "bar")?;
client.add_file("foo", "foo")?; client.add_file("foo", "foo")?;
status_exepected(&client.get_config(), vec![], vec!["foo", "dir"]); status_expected(&client.get_config(), vec![], vec!["foo", "dir"]);
client.exec_ok("add dir"); client.exec_ok("add dir");
status_exepected(&client.get_config(), vec!["dir"], vec!["foo"]); status_expected(&client.get_config(), vec!["dir/foo", "dir/bar"], vec!["foo"]);
client.ok() client.ok()
} }
@ -70,10 +69,10 @@ fn all_folder_current() -> io::Result<()> {
client.add_file("dir/foo", "foo")?; client.add_file("dir/foo", "foo")?;
client.add_file("dir/bar", "bar")?; client.add_file("dir/bar", "bar")?;
client.add_file("foor", "foor")?; client.add_file("foor", "foor")?;
status_exepected(&client.get_config(), vec![], vec!["foor", "dir"]); status_expected(&client.get_config(), vec![], vec!["foor", "dir"]);
client.exec_ok("add dir"); client.exec_ok("add dir");
status_exepected( status_expected(
&Config::from(Some(&String::from("./dir"))), &Config::from(Some(&String::from("./dir"))),
vec!["foor", "bar"], vec!["foor", "bar"],
vec!["../foo"], vec!["../foo"],
@ -90,10 +89,10 @@ fn part_of_folder() -> io::Result<()> {
client.add_file("dir/foo", "foo")?; client.add_file("dir/foo", "foo")?;
client.add_file("dir/bar", "bar")?; client.add_file("dir/bar", "bar")?;
client.add_file("foor", "foor")?; client.add_file("foor", "foor")?;
status_exepected(&client.get_config(), vec![], vec!["foor", "dir"]); status_expected(&client.get_config(), vec![], vec!["foor", "dir"]);
client.exec_ok("add dir/foo"); client.exec_ok("add dir/foo");
status_exepected( status_expected(
&client.get_config(), &client.get_config(),
vec!["dir/foo"], vec!["dir/foo"],
vec!["foor", "dir/bar"], vec!["foor", "dir/bar"],