-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add rustic_testing to workspace crates
Signed-off-by: simonsan <[email protected]>
- Loading branch information
Showing
5 changed files
with
88 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |