diff --git a/CHANGELOG.md b/CHANGELOG.md index 18f47ad..3ee3c7d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ ### Fixed - [#38](https://github.com/tweag/nixtract/pull/38) fixed bug where found derivations were parsed incorrectly +- [#42](https://github.com/tweag/nixtract/pull/42) reintroduced the src field in the derivation description ### Changed - [#36](https://github.com/tweag/nixtract/pull/36) moved all nixtract configuration options into a single struct passed to the `nixtract` function diff --git a/src/lib.rs b/src/lib.rs index b07eabb..9594d26 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -229,3 +229,65 @@ pub fn nixtract( Ok(rx.into_iter()) } + +#[cfg(test)] +mod tests { + use super::*; + use core::panic; + use std::fs; + + fn init() { + let _ = env_logger::builder().is_test(true).try_init(); + } + + #[test] + fn test_main_fixtures() -> Result<()> { + init(); + + // For every subdirectory in the tests/fixtures directory + for entry in fs::read_dir("tests/fixtures").unwrap() { + let entry = entry.unwrap(); + let path = entry.path().canonicalize().unwrap(); + if path.is_dir() { + let config = NixtractConfig { + runtime_only: false, + binary_caches: None, + offline: false, + include_nar_info: false, + message_tx: None, + }; + + log::info!("Running test for {:?}", path); + + let test_name = path + .components() + .last() + .unwrap() + .as_os_str() + .to_str() + .unwrap(); + let flake_ref = path.to_str().unwrap(); + let system: Option = None; + let attribute_path: Option = None; + + let mut descriptions = nixtract(flake_ref, system, attribute_path, config).unwrap(); + + match test_name { + "flake-direct-buildInput" => {} + "flake-direct-nativeBuildInput" => {} + "flake-three-levels" => {} + "flake-trivial-rust" => { + assert!(descriptions.any(|d| { + d.src.is_some_and(|s| { + s.git_repo_url == "https://github.com/hello-lang/Rust.git" + }) + })); + } + "flake-trivial" => {} + s => panic!("Unknown test: {}", s), + } + } + } + Ok(()) + } +} diff --git a/src/nix/describe_derivation.nix b/src/nix/describe_derivation.nix index 647e797..718a0a1 100644 --- a/src/nix/describe_derivation.nix +++ b/src/nix/describe_derivation.nix @@ -31,6 +31,17 @@ in name = targetValue.name; parsed_name = (builtins.parseDrvName targetValue.name); attribute_path = targetAttributePath; + + src = + if targetValue ? src.gitRepoUrl && targetValue ? src.rev + then + { + git_repo_url = targetValue.src.gitRepoUrl; + rev = targetValue.src.rev; + } + else + null; + nixpkgs_metadata = { description = (builtins.tryEval (targetValue.meta.description or "")).value;