Skip to content

Commit

Permalink
chore: add rustic_testing to workspace crates
Browse files Browse the repository at this point in the history
Signed-off-by: simonsan <[email protected]>
  • Loading branch information
simonsan committed Feb 4, 2024
1 parent 8d6e813 commit a2ea02b
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 3 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["crates/core", "crates/backend", "examples/*"]
members = ["crates/core", "crates/backend", "crates/testing", "examples/*"]
resolver = "2"

[workspace.package]
Expand All @@ -9,6 +9,10 @@ rust-version = "1.71.1"
rustic_backend = { path = "crates/backend" }
rustic_core = { path = "crates/core" }
simplelog = "0.12.1"
aho-corasick = "1.1.2"

# dev-dependencies
tempfile = "3.9.0"

# see: https://nnethercote.github.io/perf-book/build-configuration.html
[profile.dev]
Expand Down
2 changes: 1 addition & 1 deletion crates/backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ clap = { version = "4.4.18", optional = true, features = ["derive", "env", "wrap
merge = { version = "0.1.0", optional = true }

# local backend
aho-corasick = "1.1.2"
aho-corasick = { workspace = true }
shell-words = "1.1.0"
walkdir = "2.4.0"

Expand Down
2 changes: 1 addition & 1 deletion crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,4 @@ rustdoc-json = "0.8.9"
rustic_backend = { workspace = true }
rustup-toolchain = "0.1.6"
simplelog = "0.12.1"
tempfile = "3.9.0"
tempfile = { workspace = true }
10 changes: 10 additions & 0 deletions crates/testing/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "rustic_testing"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
aho-corasick = { workspace = true }
once_cell = "1.19.0"
tempfile = { workspace = true }
71 changes: 71 additions & 0 deletions crates/testing/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use aho_corasick::{AhoCorasick, PatternID};
use std::{error::Error, ffi::OsStr};
use tempfile::NamedTempFile;

pub type TestResult<T> = std::result::Result<T, Box<dyn Error>>;

pub fn get_matches<I, P>(patterns: I, output: String) -> TestResult<Vec<(PatternID, usize)>>
where
I: IntoIterator<Item = P>,
P: AsRef<[u8]>,
{
let ac = AhoCorasick::new(patterns)?;
let mut matches = vec![];
for mat in ac.find_iter(output.as_str()) {
add_match_to_vector(&mut matches, mat);
}
Ok(matches)
}

pub fn add_match_to_vector(matches: &mut Vec<(PatternID, usize)>, mat: aho_corasick::Match) {
matches.push((mat.pattern(), mat.end() - mat.start()))
}

pub fn get_temp_file() -> TestResult<NamedTempFile> {
Ok(NamedTempFile::new()?)
}

pub fn files_differ(
path_left: impl AsRef<OsStr>,
path_right: impl AsRef<OsStr>,
) -> TestResult<bool> {
// diff the directories
#[cfg(not(windows))]
{
let proc = std::process::Command::new("diff")
.arg(path_left)
.arg(path_right)
.output()?;

if proc.stdout.is_empty() {
return Ok(false);
}
}

#[cfg(windows)]
{
let proc = std::process::Command::new("fc.exe")
.arg("/L")
.arg(path_left)
.arg(path_right)
.output()?;

let output = String::from_utf8(proc.stdout)?;

dbg!(&output);

let patterns = &["FC: no differences encountered"];
let ac = AhoCorasick::new(patterns)?;
let mut matches = vec![];

for mat in ac.find_iter(output.as_str()) {
matches.push((mat.pattern(), mat.end() - mat.start()));
}

if matches == vec![(PatternID::must(0), 30)] {
return Ok(false);
}
}

Ok(true)
}

0 comments on commit a2ea02b

Please sign in to comment.