refactor(tvix/cli): force outside of output configuration helper
Change-Id: I28357fe131cefedcef9761b08a72f675f4a10789 Reviewed-on: https://cl.tvl.fyi/c/depot/+/7939 Reviewed-by: flokli <flokli@flokli.de> Autosubmit: tazjin <tazjin@tvl.su> Tested-by: BuildkiteCI
This commit is contained in:
		
							parent
							
								
									8ec8d03175
								
							
						
					
					
						commit
						e4bb750b3b
					
				
					 1 changed files with 36 additions and 54 deletions
				
			
		| 
						 | 
					@ -164,39 +164,15 @@ fn construct_output_hash(digest: &str, algo: &str, hash_mode: Option<&str>) -> R
 | 
				
			||||||
/// parameters, including invalid ones.
 | 
					/// parameters, including invalid ones.
 | 
				
			||||||
fn populate_output_configuration(
 | 
					fn populate_output_configuration(
 | 
				
			||||||
    drv: &mut Derivation,
 | 
					    drv: &mut Derivation,
 | 
				
			||||||
    vm: &mut VM,
 | 
					    hash: Option<String>,      // in nix: outputHash
 | 
				
			||||||
    hash: Option<&Value>,      // in nix: outputHash
 | 
					    hash_algo: Option<String>, // in nix: outputHashAlgo
 | 
				
			||||||
    hash_algo: Option<&Value>, // in nix: outputHashAlgo
 | 
					    hash_mode: Option<String>, // in nix: outputHashmode
 | 
				
			||||||
    hash_mode: Option<&Value>, // in nix: outputHashmode
 | 
					 | 
				
			||||||
) -> Result<(), ErrorKind> {
 | 
					) -> Result<(), ErrorKind> {
 | 
				
			||||||
    match (hash, hash_algo, hash_mode) {
 | 
					    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()),
 | 
					            None => return Err(Error::ConflictingOutputTypes.into()),
 | 
				
			||||||
            Some(out) => {
 | 
					            Some(out) => {
 | 
				
			||||||
                let algo = strong_coerce_to_string(
 | 
					                out.hash = Some(construct_output_hash(&digest, &algo, hash_mode.as_deref())?);
 | 
				
			||||||
                    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(),
 | 
					 | 
				
			||||||
                )?);
 | 
					 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -309,10 +285,22 @@ mod derivation_builtins {
 | 
				
			||||||
        // Configure fixed-output derivations if required.
 | 
					        // Configure fixed-output derivations if required.
 | 
				
			||||||
        populate_output_configuration(
 | 
					        populate_output_configuration(
 | 
				
			||||||
            &mut drv,
 | 
					            &mut drv,
 | 
				
			||||||
            vm,
 | 
					            input
 | 
				
			||||||
            input.select("outputHash"),
 | 
					                .select("outputHash")
 | 
				
			||||||
            input.select("outputHashAlgo"),
 | 
					                .map(|v| strong_coerce_to_string(vm, v, "evaluating the `outputHash` parameter"))
 | 
				
			||||||
            input.select("outputHashMode"),
 | 
					                .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() {
 | 
					        for (name, value) in input.into_iter_sorted() {
 | 
				
			||||||
| 
						 | 
					@ -545,10 +533,9 @@ mod tests {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    #[test]
 | 
					    #[test]
 | 
				
			||||||
    fn populate_output_config_std() {
 | 
					    fn populate_output_config_std() {
 | 
				
			||||||
        let mut vm = fake_vm();
 | 
					 | 
				
			||||||
        let mut drv = Derivation::default();
 | 
					        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");
 | 
					            .expect("populate_output_configuration() should succeed");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        assert_eq!(drv, Derivation::default(), "derivation should be unchanged");
 | 
					        assert_eq!(drv, Derivation::default(), "derivation should be unchanged");
 | 
				
			||||||
| 
						 | 
					@ -556,18 +543,16 @@ mod tests {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    #[test]
 | 
					    #[test]
 | 
				
			||||||
    fn populate_output_config_fod() {
 | 
					    fn populate_output_config_fod() {
 | 
				
			||||||
        let mut vm = fake_vm();
 | 
					 | 
				
			||||||
        let mut drv = Derivation::default();
 | 
					        let mut drv = Derivation::default();
 | 
				
			||||||
        drv.outputs.insert("out".to_string(), Default::default());
 | 
					        drv.outputs.insert("out".to_string(), Default::default());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        let hash = Value::String(
 | 
					        populate_output_configuration(
 | 
				
			||||||
            "0000000000000000000000000000000000000000000000000000000000000000".into(),
 | 
					            &mut drv,
 | 
				
			||||||
        );
 | 
					            Some("0000000000000000000000000000000000000000000000000000000000000000".into()),
 | 
				
			||||||
 | 
					            Some("sha256".into()),
 | 
				
			||||||
        let algo = Value::String("sha256".into());
 | 
					            None,
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
        populate_output_configuration(&mut drv, &mut vm, Some(&hash), Some(&algo), None)
 | 
					        .expect("populate_output_configuration() should succeed");
 | 
				
			||||||
            .expect("populate_output_configuration() should succeed");
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        let expected = Hash {
 | 
					        let expected = Hash {
 | 
				
			||||||
            algo: "sha256".into(),
 | 
					            algo: "sha256".into(),
 | 
				
			||||||
| 
						 | 
					@ -579,19 +564,16 @@ mod tests {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    #[test]
 | 
					    #[test]
 | 
				
			||||||
    fn populate_output_config_fod_recursive() {
 | 
					    fn populate_output_config_fod_recursive() {
 | 
				
			||||||
        let mut vm = fake_vm();
 | 
					 | 
				
			||||||
        let mut drv = Derivation::default();
 | 
					        let mut drv = Derivation::default();
 | 
				
			||||||
        drv.outputs.insert("out".to_string(), Default::default());
 | 
					        drv.outputs.insert("out".to_string(), Default::default());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        let hash = Value::String(
 | 
					        populate_output_configuration(
 | 
				
			||||||
            "0000000000000000000000000000000000000000000000000000000000000000".into(),
 | 
					            &mut drv,
 | 
				
			||||||
        );
 | 
					            Some("0000000000000000000000000000000000000000000000000000000000000000".into()),
 | 
				
			||||||
 | 
					            Some("sha256".into()),
 | 
				
			||||||
        let algo = Value::String("sha256".into());
 | 
					            Some("recursive".into()),
 | 
				
			||||||
        let mode = Value::String("recursive".into());
 | 
					        )
 | 
				
			||||||
 | 
					        .expect("populate_output_configuration() should succeed");
 | 
				
			||||||
        populate_output_configuration(&mut drv, &mut vm, Some(&hash), Some(&algo), Some(&mode))
 | 
					 | 
				
			||||||
            .expect("populate_output_configuration() should succeed");
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        let expected = Hash {
 | 
					        let expected = Hash {
 | 
				
			||||||
            algo: "r:sha256".into(),
 | 
					            algo: "r:sha256".into(),
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue