use std::path::Path;
use std::path::PathBuf;
use std::process;
use globset::{Glob, GlobSetBuilder};
use rust_code_analysis::LANG;
use rust_code_analysis::*;
const REPO: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/", "repositories");
const SNAPSHOT_PATH: &str = concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/",
"repositories/rca-output/snapshots"
);
#[derive(Debug)]
struct Config {
language: Option<LANG>,
}
fn act_on_file(path: PathBuf, cfg: &Config) -> std::io::Result<()> {
let source = if let Some(source) = read_file_with_eol(&path)? {
source
} else {
return Ok(());
};
let language = if let Some(language) = cfg.language {
language
} else if let Some(language) = guess_language(&source, &path).0 {
language
} else {
return Ok(());
};
let funcspace_struct = get_function_spaces(&language, source, &path, None).unwrap();
insta::with_settings!({snapshot_path => Path::new(SNAPSHOT_PATH)
.join(path.strip_prefix(Path::new(REPO)).unwrap())
.parent()
.unwrap(),
prepend_module_to_snapshot => false,
sort_maps => true,
}, {
insta::assert_yaml_snapshot!(
path.file_name().unwrap().to_string_lossy().as_ref(),
funcspace_struct,
{
".spaces[].**.metrics.*.*" => insta::rounded_redaction(3),
".metrics.*.*" => insta::rounded_redaction(3),
".name" => "[filepath]",
}
);
});
Ok(())
}
pub fn compare_rca_output_with_files(repo_name: &str, include: &[&str], exclude: &[&str]) {
let num_jobs = 4;
let cfg = Config { language: None };
let mut gsbi = GlobSetBuilder::new();
for file in include {
gsbi.add(Glob::new(file).unwrap());
}
let mut gsbe = GlobSetBuilder::new();
for file in exclude {
gsbe.add(Glob::new(file).unwrap());
}
let files_data = FilesData {
include: gsbi.build().unwrap(),
exclude: gsbe.build().unwrap(),
paths: vec![Path::new(REPO).join(repo_name)],
};
if let Err(e) = ConcurrentRunner::new(num_jobs, act_on_file).run(cfg, files_data) {
eprintln!("{e:?}");
process::exit(1);
}
}