Skip to content

Commit 644015d

Browse files
authored
Merge pull request #1846 from chengr4/unit-test
Add unit test for `fill_libraries`
2 parents 8de4945 + 2bea1c8 commit 644015d

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

collector/src/toolchain.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,3 +598,50 @@ fn get_lib_dir_from_rustc(rustc: &Path) -> anyhow::Result<PathBuf> {
598598

599599
Ok(Path::new(sysroot_path.as_ref().trim()).join("lib"))
600600
}
601+
602+
#[cfg(test)]
603+
mod tests {
604+
use super::*;
605+
606+
#[test]
607+
fn fill_libraries() {
608+
let mut components = ToolchainComponents::default();
609+
610+
// create mock dir and libraries
611+
let temp_dir: tempfile::TempDir = tempfile::tempdir().unwrap();
612+
let lib_rustc_path = create_temp_lib_path("librustc_driver.so", &temp_dir);
613+
let lib_std_path = create_temp_lib_path("libstd.so", &temp_dir);
614+
let lib_test_path = create_temp_lib_path("libtest.so", &temp_dir);
615+
let lib_new_llvm_path =
616+
create_temp_lib_path("libLLVM.so.18.1-rust-1.78.0-nightly", &temp_dir);
617+
618+
components.fill_libraries(temp_dir.path()).unwrap();
619+
620+
assert_eq!(components.lib_rustc, Some(lib_rustc_path));
621+
assert_eq!(components.lib_std, Some(lib_std_path));
622+
assert_eq!(components.lib_test, Some(lib_test_path));
623+
assert_eq!(components.lib_llvm, Some(lib_new_llvm_path));
624+
}
625+
626+
#[test]
627+
fn fill_old_llvm_library() {
628+
let mut components = ToolchainComponents::default();
629+
let lib_old_llvm = "libLLVM-17-rust-1.76.0-stable.so";
630+
631+
// create mock dir and libraries
632+
let temp_dir: tempfile::TempDir = tempfile::tempdir().unwrap();
633+
let lib_old_llvm_path = create_temp_lib_path(lib_old_llvm, &temp_dir);
634+
635+
components.fill_libraries(temp_dir.path()).unwrap();
636+
637+
assert_eq!(components.lib_llvm, Some(lib_old_llvm_path));
638+
}
639+
640+
fn create_temp_lib_path(lib_name: &str, temp_dir: &tempfile::TempDir) -> PathBuf {
641+
let lib_path = temp_dir.path().join(lib_name);
642+
// create mock file
643+
File::create(&lib_path).unwrap();
644+
645+
lib_path
646+
}
647+
}

0 commit comments

Comments
 (0)