about summary refs log tree commit diff
path: root/tvix/derivation/src
diff options
context:
space:
mode:
authorFlorian Klink <flokli@flokli.de>2023-01-06T22·05+0100
committerclbot <clbot@tvl.fyi>2023-01-07T08·05+0000
commit1e2c859840f1d73ccf8a956647a15c0436f4bdd0 (patch)
treed705e34333a37740964997950ebc11213af2ec38 /tvix/derivation/src
parentf04829a1bb98c95ddf65c03474d46e9a30ae37ce (diff)
feat(tvix/derivation): derive Default for Derivation r/5623
Some of the fields in a Derivation struct stay empty, and manually
creating BTreeMap or vec for it is annoying.

Derive Default instead, so we can use the defaults instead of writing
more by hand.

Change-Id: I5d41b4b55c8187cb101eb4266451a470008e0067
Reviewed-on: https://cl.tvl.fyi/c/depot/+/7788
Reviewed-by: tazjin <tazjin@tvl.su>
Autosubmit: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
Diffstat (limited to 'tvix/derivation/src')
-rw-r--r--tvix/derivation/src/derivation.rs2
-rw-r--r--tvix/derivation/src/tests/mod.rs51
2 files changed, 21 insertions, 32 deletions
diff --git a/tvix/derivation/src/derivation.rs b/tvix/derivation/src/derivation.rs
index 04035b6b955d..54e0bbf82ec2 100644
--- a/tvix/derivation/src/derivation.rs
+++ b/tvix/derivation/src/derivation.rs
@@ -7,7 +7,7 @@ use std::{collections::BTreeMap, fmt, fmt::Write};
 use tvix_store::nixbase32::NIXBASE32;
 use tvix_store::store_path::{ParseStorePathError, StorePath, STORE_DIR};
 
-#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
+#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
 pub struct Derivation {
     #[serde(rename = "args")]
     pub arguments: Vec<String>,
diff --git a/tvix/derivation/src/tests/mod.rs b/tvix/derivation/src/tests/mod.rs
index fb88e1beb2bf..cb98a27cdfe4 100644
--- a/tvix/derivation/src/tests/mod.rs
+++ b/tvix/derivation/src/tests/mod.rs
@@ -1,6 +1,5 @@
 use crate::derivation::Derivation;
 use crate::output::{Hash, Output};
-use std::collections::BTreeMap;
 use std::fs::File;
 use std::io::Read;
 use std::path::Path;
@@ -191,8 +190,14 @@ fn output_paths(name: &str, drv_path: &str) {
 #[test]
 fn output_path_construction() {
     // create the bar derivation
+    let mut bar_drv = Derivation {
+        builder: ":".to_string(),
+        system: ":".to_string(),
+        ..Default::default()
+    };
+
     // assemble bar env
-    let mut bar_env: BTreeMap<String, String> = BTreeMap::new();
+    let bar_env = &mut bar_drv.environment;
     bar_env.insert("builder".to_string(), ":".to_string());
     bar_env.insert("name".to_string(), "bar".to_string());
     bar_env.insert("out".to_string(), "".to_string()); // will be calculated
@@ -205,8 +210,7 @@ fn output_path_construction() {
     bar_env.insert("system".to_string(), ":".to_string());
 
     // assemble bar outputs
-    let mut bar_outputs: BTreeMap<String, Output> = BTreeMap::new();
-    bar_outputs.insert(
+    bar_drv.outputs.insert(
         "out".to_string(),
         Output {
             path: "".to_string(), // will be calculated
@@ -218,17 +222,6 @@ fn output_path_construction() {
         },
     );
 
-    // assemble bar itself
-    let mut bar_drv = Derivation {
-        arguments: vec![],
-        builder: ":".to_string(),
-        environment: bar_env,
-        input_derivations: BTreeMap::new(),
-        input_sources: vec![],
-        outputs: bar_outputs,
-        system: ":".to_string(),
-    };
-
     // calculate bar output paths
     let bar_calc_result = bar_drv.calculate_output_paths(
         "bar",
@@ -254,8 +247,15 @@ fn output_path_construction() {
         .calculate_derivation_path("bar")
         .expect("must succeed");
 
+    // create foo derivation
+    let mut foo_drv = Derivation {
+        builder: ":".to_string(),
+        system: ":".to_string(),
+        ..Default::default()
+    };
+
     // assemble foo env
-    let mut foo_env: BTreeMap<String, String> = BTreeMap::new();
+    let foo_env = &mut foo_drv.environment;
     foo_env.insert("bar".to_string(), bar_output_path.to_string());
     foo_env.insert("builder".to_string(), ":".to_string());
     foo_env.insert("name".to_string(), "foo".to_string());
@@ -263,8 +263,7 @@ fn output_path_construction() {
     foo_env.insert("system".to_string(), ":".to_string());
 
     // asssemble foo outputs
-    let mut foo_outputs: BTreeMap<String, Output> = BTreeMap::new();
-    foo_outputs.insert(
+    foo_drv.outputs.insert(
         "out".to_string(),
         Output {
             path: "".to_string(), // will be calculated
@@ -273,19 +272,9 @@ fn output_path_construction() {
     );
 
     // assemble foo input_derivations
-    let mut foo_input_derivations: BTreeMap<String, Vec<String>> = BTreeMap::new();
-    foo_input_derivations.insert(bar_drv_path.to_absolute_path(), vec!["out".to_string()]);
-
-    // assemble foo itself
-    let mut foo_drv = Derivation {
-        arguments: vec![],
-        builder: ":".to_string(),
-        environment: foo_env,
-        input_derivations: foo_input_derivations,
-        input_sources: vec![],
-        outputs: foo_outputs,
-        system: ":".to_string(),
-    };
+    foo_drv
+        .input_derivations
+        .insert(bar_drv_path.to_absolute_path(), vec!["out".to_string()]);
 
     // calculate foo output paths
     let foo_calc_result = foo_drv.calculate_output_paths(