Refactor common functionality into helpers

This commit is contained in:
Dennis
2024-06-05 10:29:53 +02:00
parent 7f7aa3dc2c
commit 96e90ce321
4 changed files with 358 additions and 0 deletions
View File
+16
View File
@@ -0,0 +1,16 @@
use std::{fs::{self, File}, io::Read, path::PathBuf};
use log::debug;
pub fn get_file_as_byte_vec(path: &PathBuf) -> Vec<u8> {
debug!("opening {path:?}");
let mut f = File::open(path).expect("no file found");
let metadata = fs::metadata(path).expect("unable to read metadata");
let mut buffer = vec![0; metadata.len() as usize];
match f.read(&mut buffer) {
Ok(l) => assert_eq!(metadata.len() as usize, l, "data was not completely read"),
Err(e) => panic!("Error: {e}"),
}
buffer
}
+78
View File
@@ -0,0 +1,78 @@
use std::{env, fs, path::PathBuf};
use log::debug;
use crate::common::{file_util::get_file_as_byte_vec, lexicographic_file_walker::LexicographicFileWalker};
pub fn md5_of_osrm_executables() -> chksum_md5::MD5 {
// create OSRM digest before any tests are executed since cucumber-rs doesn't have @beforeAll
let exe_path = env::current_exe().expect("failed to get current exe path");
let path = exe_path
.ancestors()
.find(|p| p.ends_with("target"))
.expect("compiled cucumber test executable resides in a directory tree with the root name 'target'")
.parent().unwrap();
// TODO: Remove after migration to Rust build dir
let build_path = path.join("build");
// TODO: Remove after migration to Rust build dir
let mut dependencies = Vec::new();
// FIXME: the following iterator gymnastics port the exact order and behavior of the JavaScript implementation
let names = [
"osrm-extract",
"osrm-contract",
"osrm-customize",
"osrm-partition",
"osrm_extract",
"osrm_contract",
"osrm_customize",
"osrm_partition",
];
let files: Vec<PathBuf> = fs::read_dir(build_path)
.unwrap()
.filter_map(|e| e.ok())
.map(|dir_entry| dir_entry.path())
.collect();
let iter = names.iter().map(|name| {
files
.iter()
.find(|path_buf| {
path_buf
.file_stem()
.unwrap()
.to_str()
.unwrap()
.contains(name)
})
.cloned()
.expect("file exists and is usable")
});
dependencies.extend(iter);
let profiles_path = path.join("profiles");
debug!("{profiles_path:?}");
dependencies.extend(
LexicographicFileWalker::new(&profiles_path)
.filter(|pb| !pb.to_str().unwrap().contains("examples"))
.filter(|pathbuf| match pathbuf.extension() {
Some(ext) => ext.to_str().unwrap() == "lua",
None => false,
}),
);
let mut md5 = chksum_md5::new();
debug!("md5: {}", md5.digest().to_hex_lowercase());
for path_buf in dependencies {
let data = get_file_as_byte_vec(&path_buf);
if data.is_empty() {
continue;
}
md5.update(data);
// debug!("md5: {}", md5.digest().to_hex_lowercase());
}
md5
}