Skip to content

Commit

Permalink
implemnt exclude directories or files
Browse files Browse the repository at this point in the history
  • Loading branch information
dancespiele committed May 17, 2020
1 parent 14eed5e commit d97b09c
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 51 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[package]
name = "spielrs_diff"
description= "It is a library which compare two tree direcories asynchronously"
version = "0.1.0"
documentation = "https://docs.rs/crate/spielrs_diff/0.1.0"
version = "0.2.0"
documentation = "https://docs.rs/crate/spielrs_diff/0.2.0"
authors = ["Francisco Jesus Navarro Cortes <[email protected]>"]
readme = "README.md"
repository = "https://github.com/spielrs/spielrs-diff"
Expand Down
1 change: 1 addition & 0 deletions mocks/dir_five/hello.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("This line will be printed.")
1 change: 1 addition & 0 deletions mocks/dir_five/hello.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello world
3 changes: 3 additions & 0 deletions mocks/dir_five/vlang/hello.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println("hello world")
}
1 change: 1 addition & 0 deletions mocks/dir_five/vlang/purpose/feature.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
new feature
10 changes: 10 additions & 0 deletions src/diff.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
pub struct Diff {
/// directory to compare
pub dir: String,
/// comparation directory
pub dir_comp: String,
/// exclude directories or files from the comparation
pub excluding: Option<Vec<String>>,
/// exclude recursively or only the from the root path
pub recursive_excluding: bool,
}
96 changes: 74 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,64 @@
//!
//! ## Example
//! ```rust
//! use spielrs_diff::dir_diff;
//! use spielrs_diff::{dir_diff, diff::Diff};
//!
//! #[tokio::test]
//! async fn should_return_true_if_both_dir_tree_are_different() {
//! let diff = dir_diff(
//! "./mocks/dir_one".to_string(),
//! "./mocks/dir_three".to_string(),
//! )
//! let diff = dir_diff(Diff {
//! dir: "./mocks/dir_one".to_string(),
//! dir_comp: "./mocks/dir_five".to_string(),
//! excluding: Some(vec!["purpose".to_string()]),
//! recursive_excluding: true,
//! })
//! .await;
//!
//! assert_eq!(diff, true);
//! }
//! ```
//!
//!
pub mod diff;
pub mod tree;

use diff::Diff;
use tree::{Tree, TreeBuilder};

/// Compare two tree directories and return true if both are different
/// You can exclude directories or files in the comparation only from the root path
/// of both or recursively
///
/// # Example
/// ```rust
/// use spielrs_diff::dir_diff;
/// use spielrs_diff::{dir_diff, diff::Diff};
///
/// #[tokio::test]
/// async fn should_return_true_if_both_dir_tree_are_different() {
/// let diff = dir_diff(
/// "./mocks/dir_one".to_string(),
/// "./mocks/dir_three".to_string(),
/// )
/// let diff = dir_diff(Diff {
/// dir: "./mocks/dir_one".to_string(),
/// dir_comp: "./mocks/dir_five".to_string(),
/// excluding: Some(vec!["purpose".to_string()]),
/// recursive_excluding: true,
/// })
/// .await;
///
/// assert_eq!(diff, true);
/// }
/// ```
///
pub async fn dir_diff(dir_path: String, dir_path_comp: String) -> bool {
let tree_one: Vec<Tree> = Tree::build_tree(dir_path).await;
let tree_two: Vec<Tree> = Tree::build_tree(dir_path_comp).await;
pub async fn dir_diff(diff_options: Diff) -> bool {
let tree_one: Vec<Tree> = Tree::build_tree(
diff_options.dir,
diff_options.excluding.clone(),
diff_options.recursive_excluding,
)
.await;
let tree_two: Vec<Tree> = Tree::build_tree(
diff_options.dir_comp,
diff_options.excluding,
diff_options.recursive_excluding,
)
.await;
if Tree::tree_diff(tree_one.clone(), tree_two.clone()) {
return true;
}
Expand All @@ -63,26 +81,60 @@ pub async fn dir_diff(dir_path: String, dir_path_comp: String) -> bool {

#[tokio::test]
async fn should_return_true_if_both_dir_tree_are_different() {
let diff = dir_diff(
"./mocks/dir_one".to_string(),
"./mocks/dir_three".to_string(),
)
let diff = dir_diff(Diff {
dir: "./mocks/dir_one".to_string(),
dir_comp: "./mocks/dir_three".to_string(),
excluding: None,
recursive_excluding: false,
})
.await;
assert_eq!(diff, true);
}

#[tokio::test]
async fn should_return_false_if_both_dir_tree_are_equal() {
let diff = dir_diff("./mocks/dir_one".to_string(), "./mocks/dir_two".to_string()).await;
let diff = dir_diff(Diff {
dir: "./mocks/dir_one".to_string(),
dir_comp: "./mocks/dir_two".to_string(),
excluding: None,
recursive_excluding: false,
})
.await;
assert_eq!(diff, false);
}

#[tokio::test]
async fn should_return_true_if_both_dir_tree_have_different_content() {
let diff = dir_diff(
"./mocks/dir_one".to_string(),
"./mocks/dir_four".to_string(),
)
let diff = dir_diff(Diff {
dir: "./mocks/dir_one".to_string(),
dir_comp: "./mocks/dir_four".to_string(),
excluding: None,
recursive_excluding: false,
})
.await;
assert_eq!(diff, true);
}

#[tokio::test]
async fn should_return_false_if_both_dir_have_different_subdir_excluded_recursively() {
let diff = dir_diff(Diff {
dir: "./mocks/dir_one".to_string(),
dir_comp: "./mocks/dir_five".to_string(),
excluding: Some(vec!["purpose".to_string()]),
recursive_excluding: true,
})
.await;
assert_eq!(diff, false);
}

#[tokio::test]
async fn should_return_true_if_both_dir_have_different_subdir_excluded_not_recursively() {
let diff = dir_diff(Diff {
dir: "./mocks/dir_one".to_string(),
dir_comp: "./mocks/dir_five".to_string(),
excluding: Some(vec!["purpose".to_string()]),
recursive_excluding: false,
})
.await;
assert_eq!(diff, true);
}
Loading

0 comments on commit d97b09c

Please sign in to comment.