datafusion_ffi/tests/
utils.rs1use std::path::Path;
19
20use abi_stable::library::RootModule;
21use datafusion_common::{DataFusionError, Result};
22
23use crate::tests::ForeignLibraryModuleRef;
24
25pub fn compute_library_path<M: RootModule>(
30 target_path: &Path,
31) -> std::io::Result<std::path::PathBuf> {
32 let debug_dir = target_path.join("debug");
33 let release_dir = target_path.join("release");
34 let ci_dir = target_path.join("ci");
35
36 let debug_path = M::get_library_path(&debug_dir.join("deps"));
37 let release_path = M::get_library_path(&release_dir.join("deps"));
38 let ci_path = M::get_library_path(&ci_dir.join("deps"));
39
40 let all_paths = vec![
41 (debug_dir.clone(), debug_path),
42 (release_dir, release_path),
43 (ci_dir, ci_path),
44 ];
45
46 let best_path = all_paths
47 .into_iter()
48 .filter(|(_, path)| path.exists())
49 .filter_map(|(dir, path)| path.metadata().map(|m| (dir, m)).ok())
50 .filter_map(|(dir, meta)| meta.modified().map(|m| (dir, m)).ok())
51 .max_by_key(|(_, date)| *date)
52 .map(|(dir, _)| dir)
53 .unwrap_or(debug_dir);
54
55 Ok(best_path)
56}
57
58pub fn get_module() -> Result<ForeignLibraryModuleRef> {
59 let expected_version = crate::version();
60
61 let crate_root = Path::new(env!("CARGO_MANIFEST_DIR"));
62 let target_dir = crate_root
63 .parent()
64 .expect("Failed to find crate parent")
65 .parent()
66 .expect("Failed to find workspace root")
67 .join("target");
68
69 let library_path =
73 compute_library_path::<ForeignLibraryModuleRef>(target_dir.as_path())
74 .map_err(|e| DataFusionError::External(Box::new(e)))?
75 .join("deps");
76
77 let module = ForeignLibraryModuleRef::load_from_directory(&library_path)
79 .map_err(|e| DataFusionError::External(Box::new(e)))?;
80
81 assert_eq!(
82 module
83 .version()
84 .expect("Unable to call version on FFI module")(),
85 expected_version
86 );
87
88 Ok(module)
89}