From e4bb750b3bcdac4c9c138af2828e32587ec2a5ca Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sun, 29 Jan 2023 15:52:48 +0300 Subject: refactor(tvix/cli): force outside of output configuration helper Change-Id: I28357fe131cefedcef9761b08a72f675f4a10789 Reviewed-on: https://cl.tvl.fyi/c/depot/+/7939 Reviewed-by: flokli Autosubmit: tazjin Tested-by: BuildkiteCI --- tvix/cli/src/derivation.rs | 90 +++++++++++++++++++--------------------------- 1 file changed, 36 insertions(+), 54 deletions(-) (limited to 'tvix/cli/src') diff --git a/tvix/cli/src/derivation.rs b/tvix/cli/src/derivation.rs index 012294375e11..17211baa1c3e 100644 --- a/tvix/cli/src/derivation.rs +++ b/tvix/cli/src/derivation.rs @@ -164,39 +164,15 @@ fn construct_output_hash(digest: &str, algo: &str, hash_mode: Option<&str>) -> R /// parameters, including invalid ones. fn populate_output_configuration( drv: &mut Derivation, - vm: &mut VM, - hash: Option<&Value>, // in nix: outputHash - hash_algo: Option<&Value>, // in nix: outputHashAlgo - hash_mode: Option<&Value>, // in nix: outputHashmode + hash: Option, // in nix: outputHash + hash_algo: Option, // in nix: outputHashAlgo + hash_mode: Option, // in nix: outputHashmode ) -> Result<(), ErrorKind> { match (hash, hash_algo, hash_mode) { - (Some(hash), Some(algo), hash_mode) => match drv.outputs.get_mut("out") { + (Some(digest), Some(algo), hash_mode) => match drv.outputs.get_mut("out") { None => return Err(Error::ConflictingOutputTypes.into()), Some(out) => { - let algo = strong_coerce_to_string( - vm, - &algo, - "evaluating outputHashAlgo of a derivation", - )?; - - let digest_str = - strong_coerce_to_string(vm, &hash, "evaluating outputHash of a derivation")?; - - let hash_mode = match hash_mode { - None => None, - Some(mode) => Some(strong_coerce_to_string( - vm, - &mode, - "evaluating outputHashMode of a derivation", - )?), - }; - - // construct out.hash - out.hash = Some(construct_output_hash( - &digest_str, - &algo, - hash_mode.as_deref(), - )?); + out.hash = Some(construct_output_hash(&digest, &algo, hash_mode.as_deref())?); } }, @@ -309,10 +285,22 @@ mod derivation_builtins { // Configure fixed-output derivations if required. populate_output_configuration( &mut drv, - vm, - input.select("outputHash"), - input.select("outputHashAlgo"), - input.select("outputHashMode"), + input + .select("outputHash") + .map(|v| strong_coerce_to_string(vm, v, "evaluating the `outputHash` parameter")) + .transpose()?, + input + .select("outputHashAlgo") + .map(|v| { + strong_coerce_to_string(vm, v, "evaluating the `outputHashAlgo` parameter") + }) + .transpose()?, + input + .select("outputHashMode") + .map(|v| { + strong_coerce_to_string(vm, v, "evaluating the `outputHashMode` parameter") + }) + .transpose()?, )?; for (name, value) in input.into_iter_sorted() { @@ -545,10 +533,9 @@ mod tests { #[test] fn populate_output_config_std() { - let mut vm = fake_vm(); let mut drv = Derivation::default(); - populate_output_configuration(&mut drv, &mut vm, None, None, None) + populate_output_configuration(&mut drv, None, None, None) .expect("populate_output_configuration() should succeed"); assert_eq!(drv, Derivation::default(), "derivation should be unchanged"); @@ -556,18 +543,16 @@ mod tests { #[test] fn populate_output_config_fod() { - let mut vm = fake_vm(); let mut drv = Derivation::default(); drv.outputs.insert("out".to_string(), Default::default()); - let hash = Value::String( - "0000000000000000000000000000000000000000000000000000000000000000".into(), - ); - - let algo = Value::String("sha256".into()); - - populate_output_configuration(&mut drv, &mut vm, Some(&hash), Some(&algo), None) - .expect("populate_output_configuration() should succeed"); + populate_output_configuration( + &mut drv, + Some("0000000000000000000000000000000000000000000000000000000000000000".into()), + Some("sha256".into()), + None, + ) + .expect("populate_output_configuration() should succeed"); let expected = Hash { algo: "sha256".into(), @@ -579,19 +564,16 @@ mod tests { #[test] fn populate_output_config_fod_recursive() { - let mut vm = fake_vm(); let mut drv = Derivation::default(); drv.outputs.insert("out".to_string(), Default::default()); - let hash = Value::String( - "0000000000000000000000000000000000000000000000000000000000000000".into(), - ); - - let algo = Value::String("sha256".into()); - let mode = Value::String("recursive".into()); - - populate_output_configuration(&mut drv, &mut vm, Some(&hash), Some(&algo), Some(&mode)) - .expect("populate_output_configuration() should succeed"); + populate_output_configuration( + &mut drv, + Some("0000000000000000000000000000000000000000000000000000000000000000".into()), + Some("sha256".into()), + Some("recursive".into()), + ) + .expect("populate_output_configuration() should succeed"); let expected = Hash { algo: "r:sha256".into(), -- cgit 1.4.1