Skip to content

Commit

Permalink
[casr-cluster] --diff option. Get diff between two directories with r…
Browse files Browse the repository at this point in the history
…eports.
  • Loading branch information
anfedotoff committed Jan 22, 2024
1 parent 4d18827 commit 454f32b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 11 deletions.
51 changes: 41 additions & 10 deletions casr/src/bin/casr-cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,22 +279,26 @@ fn deduplication(indir: &Path, outdir: Option<PathBuf>, jobs: usize) -> Result<(
Ok((before, after))
}

/// Merge new reports from input directory into output directory
/// Merge unique reports from `new` directory into `prev` directory.
/// If `diff` directory is set unique (`new` \ `prev`) reports are saved
/// in `diff` directory.
///
/// # Arguments
///
/// * `input` - path to directory with new CASR reports
/// * `new` - path to directory with new CASR reports
///
/// * `output` - path to output directory with CASR reports
/// * `prev` - path to directory with previous CASR reports
///
/// * `diff` - optional: path to save unique (`new` \ `prev`) reports
///
/// # Return value
///
/// Number of merged reports
fn merge_dirs(input: &Path, output: &Path) -> Result<u64> {
let dir = fs::read_dir(output).with_context(|| {
fn merge_or_diff(new: &Path, prev: &Path, diff: Option<&Path>) -> Result<u64> {
let dir = fs::read_dir(prev).with_context(|| {
format!(
"Error occurred while opening directory with CASR reports. Directory: {}",
output.display()
prev.display()

Check warning on line 301 in casr/src/bin/casr-cluster.rs

View check run for this annotation

Codecov / codecov/patch

casr/src/bin/casr-cluster.rs#L301

Added line #L301 was not covered by tests
)
})?;

Expand All @@ -309,19 +313,26 @@ fn merge_dirs(input: &Path, output: &Path) -> Result<u64> {
}
}

let dir = fs::read_dir(input).with_context(|| {
let dir = fs::read_dir(new).with_context(|| {
format!(
"Error occurred while opening directory with CASR reports. Directory: {}",
input.display()
new.display()

Check warning on line 319 in casr/src/bin/casr-cluster.rs

View check run for this annotation

Codecov / codecov/patch

casr/src/bin/casr-cluster.rs#L319

Added line #L319 was not covered by tests
)
})?;

let save_dir = if let Some(diff) = diff {
fs::create_dir(diff)?;
diff
} else {
prev
};

let mut new: u64 = 0;
for entry in dir.flatten() {
if entry.path().extension().is_some() && entry.path().extension().unwrap() == "casrep" {
if let Ok(trace) = stacktrace(entry.path().as_path()) {
if mainhash.insert(trace) {
let target = Path::new(&output).join(entry.file_name());
let target = Path::new(&save_dir).join(entry.file_name());
if target.exists() {
eprintln!(
"File with name {} already exists in OUTPUT_DIR.",
Expand Down Expand Up @@ -408,6 +419,18 @@ fn main() -> Result<()> {
INPUT_DIR will be added to OUTPUT_DIR.",
),
)
.arg(
Arg::new("diff")
.long("diff")
.action(ArgAction::Set)
.num_args(3)
.value_parser(clap::value_parser!(PathBuf))
.value_names(["NEW_DIR", "PREV_DIR", "DIFF_DIR"])
.help(
"Compute NEW_DIR \\ PREV_DIR. Save new CASR reports from \
NEW_DIR into DIFF_DIR.",
),
)
.arg(
Arg::new("ignore")
.long("ignore")
Expand Down Expand Up @@ -474,12 +497,20 @@ fn main() -> Result<()> {
println!("Number of reports after deduplication: {after}");
} else if matches.contains_id("merge") {
let paths: Vec<&PathBuf> = matches.get_many::<PathBuf>("merge").unwrap().collect();
let new = merge_dirs(paths[0], paths[1])?;
let new = merge_or_diff(paths[0], paths[1], None)?;
println!(
"Merged {} new reports into {} directory",
new,
paths[1].display()
);
} else if matches.contains_id("diff") {
let paths: Vec<&PathBuf> = matches.get_many::<PathBuf>("diff").unwrap().collect();
let new = merge_or_diff(paths[0], paths[1], Some(paths[2]))?;
println!(
"Diff of {} new reports is saved into {} directory",
new,
paths[2].display()
);
}

Ok(())
Expand Down
22 changes: 21 additions & 1 deletion casr/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2603,9 +2603,11 @@ fn test_casr_cluster_d_and_m() {
let paths = [
abs_path("tests/casr_tests/casrep/dedup/in"),
abs_path("tests/tmp_tests_casr/dedup_out"),
abs_path("tests/tmp_tests_casr/dedup_diff"),
];

let _ = fs::remove_dir_all(&paths[1]);
let _ = fs::remove_dir_all(&paths[2]);

let output = Command::new(*EXE_CASR_CLUSTER.read().unwrap())
.args(["-d", &paths[0], &paths[1]])
Expand Down Expand Up @@ -2648,7 +2650,25 @@ fn test_casr_cluster_d_and_m() {
"Something went wrong while merging directories"
);

let _ = std::fs::remove_dir_all(&paths[1]);
// Test --diff option
dirvec = match fs::read_dir(&paths[1]) {
Ok(vec) => vec,
Err(why) => {
panic!("{:?}", why.kind());
}
};
let casrep = dirvec.next().unwrap().unwrap().path();
let _ = std::fs::remove_file(casrep);
let output = Command::new(*EXE_CASR_CLUSTER.read().unwrap())
.args(["--diff", &paths[0], &paths[1], &paths[2]])
.output()
.expect("failed to start casr-cluster");
let out = String::from_utf8_lossy(&output.stdout);
println!("{}", out);
assert!(
out.contains("Diff of 1 new reports") && (fs::read_dir(&paths[2]).unwrap().count() == 1),
"Something went wrong while diffing directories"
);
}

#[test]
Expand Down

0 comments on commit 454f32b

Please sign in to comment.