Skip to content

Commit

Permalink
- Add unit test for format guessing
Browse files Browse the repository at this point in the history
- Fix clippy lints
- Add --open to compare cli
- Propagate comparison-errors to stdout
- Bump version to 0.3.2
  • Loading branch information
Christopher Regali committed Jun 27, 2023
1 parent e52cc11 commit b432ec6
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 15 deletions.
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description = "A flexible rule-based file and folder comparison tool and crate i
repository = "https://github.com/VolumeGraphics/havocompare"
homepage = "https://github.com/VolumeGraphics/havocompare"
documentation = "https://docs.rs/havocompare"
version = "0.3.1"
version = "0.3.2"
edition = "2021"
license = "MIT"
authors = ["Volume Graphics GmbH"]
Expand Down Expand Up @@ -34,7 +34,7 @@ serde_json = "1.0"
glob = "0.3"
test-log = {version="0.2", features=["trace"]}
strsim = "0.10"
itertools = "0.10"
itertools = "0.11"
tera = "1.17"
sha2 = "0.10"
data-encoding = "2.3"
Expand All @@ -45,6 +45,8 @@ rayon = "1.6"
enable-ansi-support = "0.2"
tempfile = "3.3"
fs_extra = "1.3"
opener = "0.6"
anyhow = "1.0"

[dev-dependencies]
env_logger = "0.10"
Expand Down
27 changes: 25 additions & 2 deletions src/csv/preprocessing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,29 @@ mod tests {
.unwrap()
}

fn setup_table_two(delimiters: Option<Delimiters>) -> Table {
let delimiters = delimiters.unwrap_or_default();
Table::from_reader(
File::open("tests/csv/data/defects_headers.csv").unwrap(),
&delimiters,
)
.unwrap()
}

#[test]
fn test_extract_headers_two() {
let mut table = setup_table_two(None);
extract_headers(&mut table).unwrap();
assert_eq!(
table.columns.first().unwrap().header.as_deref().unwrap(),
"Entry"
);
assert_eq!(
table.columns.last().unwrap().header.as_deref().unwrap(),
"Radius"
);
}

#[test]
fn test_extract_headers() {
let mut table = setup_table(None);
Expand Down Expand Up @@ -260,7 +283,7 @@ mod tests {
.unwrap()
.rows
.iter()
.all(|v| *v == csv::Value::deleted()));
.all(|v| *v == Value::deleted()));
}

#[test]
Expand All @@ -278,7 +301,7 @@ mod tests {
.unwrap()
.rows
.iter()
.all(|v| *v == csv::Value::deleted()));
.all(|v| *v == Value::deleted()));
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/csv/tokenizer/guess_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub(crate) fn guess_format_from_reader<R: Read + Seek>(

let bufreader = BufReader::new(&mut input);
debug!("Guessing format from reader...");
for line in bufreader.lines().filter_map(|l| l.ok()) {
for line in bufreader.lines().map_while(Result::ok) {
debug!("Guessing format from line: '{}'", line.as_str());
format = guess_format_from_line(line.as_str(), format.0)?;
debug!("Current format: {:?}", format);
Expand Down
2 changes: 1 addition & 1 deletion src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub fn compare_files<P: AsRef<Path>>(
.lines()
.enumerate()
.filter_map(|l| l.1.ok().map(|a| (l.0, a)))
.zip(nominal.lines().filter_map(|l| l.ok()))
.zip(nominal.lines().map_while(Result::ok))
.filter(|((_, a), n)|
exclusion_list.iter().all(|exc| !exc.is_match(a)) && exclusion_list.iter().all(|exc| !exc.is_match(n))
)
Expand Down
29 changes: 20 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::path::Path;
use anyhow::anyhow;
use clap::Parser;
use havocompare::{compare_folders, get_schema, validate_config};
use tracing::Level;
use tracing::{info, Level};
use tracing_subscriber::FmtSubscriber;

const DEFAULT_REPORT_FOLDER: &str = "report";
Expand All @@ -18,6 +20,9 @@ enum Commands {
/// Optional: Folder to store the report to, if not set the default location will be chosen.
#[arg(short, long = "report_path", default_value_t = DEFAULT_REPORT_FOLDER.to_string())]
report_config: String,
/// Open the report immediately after comparison
#[arg(short, long)]
open: bool
},

/// Export the JsonSchema for the config files
Expand All @@ -38,7 +43,7 @@ struct Arguments {
command: Commands,
}

fn main() {
fn main() -> Result<(), vg_errortools::MainError> {
let args = Arguments::parse();
let level = if args.verbose {
Level::DEBUG
Expand All @@ -59,28 +64,34 @@ fn main() {
"{}",
get_schema().expect("Error occurred writing json schema")
);
std::process::exit(0);
Ok(())
}
Commands::Compare {
compare_config,
nominal,
actual,
report_config,
open
} => {
let report_path = Path::new(report_config.as_str());
let result =
compare_folders(nominal, actual, compare_config, report_config).unwrap_or(false);
compare_folders(nominal, actual, compare_config, report_path)?;
if open {
info!("Opening report");
opener::open(report_path.join("index.html")).expect("Could not open report!");
}
if result {
std::process::exit(0);
Ok(())
} else {
std::process::exit(1);
Err(anyhow!("Comparison failed!").into())
}
}
Commands::Validate { compare_config } => {
if validate_config(compare_config) {
std::process::exit(0);
Ok(())
} else {
std::process::exit(1);
Err(anyhow!("Validation failed!").into())
}
}
};
}
}

0 comments on commit b432ec6

Please sign in to comment.