about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorian Klink <flokli@flokli.de>2023-10-18T12·27+0100
committerflokli <flokli@flokli.de>2023-10-18T12·31+0000
commit42d3c1a74811bbe20ec8dd10d92cc28739b7fdc5 (patch)
treebe413e1071fbc40bf151a70ee67853870ee75046
parent05bca22121ad223fa44062391e0418b0975766db (diff)
test(tvix/nix-compat/nar/writer/sync): add tests r/6857
Change-Id: I04c556e7b57f20dd33f71b0379e6104e1702a8ee
Reviewed-on: https://cl.tvl.fyi/c/depot/+/9785
Reviewed-by: Connor Brewster <cbrewster@hey.com>
Autosubmit: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
-rw-r--r--tvix/nix-compat/default.nix2
-rw-r--r--tvix/nix-compat/src/nar/writer/mod.rs3
-rw-r--r--tvix/nix-compat/src/nar/writer/test.rs133
3 files changed, 138 insertions, 0 deletions
diff --git a/tvix/nix-compat/default.nix b/tvix/nix-compat/default.nix
index 7f51438eb70d..d6169f1339cf 100644
--- a/tvix/nix-compat/default.nix
+++ b/tvix/nix-compat/default.nix
@@ -2,4 +2,6 @@
 
 depot.tvix.crates.workspaceMembers.nix-compat.build.override {
   runTests = true;
+  # make sure we also enable async here, so run the tests behind that feature flag.
+  features = [ "default" "async" ];
 }
diff --git a/tvix/nix-compat/src/nar/writer/mod.rs b/tvix/nix-compat/src/nar/writer/mod.rs
index bf81ccd4df32..fe8ccccb3787 100644
--- a/tvix/nix-compat/src/nar/writer/mod.rs
+++ b/tvix/nix-compat/src/nar/writer/mod.rs
@@ -2,5 +2,8 @@ pub use sync::*;
 
 pub mod sync;
 
+#[cfg(test)]
+mod test;
+
 #[cfg(feature = "async")]
 pub mod r#async;
diff --git a/tvix/nix-compat/src/nar/writer/test.rs b/tvix/nix-compat/src/nar/writer/test.rs
new file mode 100644
index 000000000000..7b1dd1a2a96e
--- /dev/null
+++ b/tvix/nix-compat/src/nar/writer/test.rs
@@ -0,0 +1,133 @@
+use crate::nar;
+
+#[test]
+fn symlink() {
+    let mut buf = vec![];
+    let node = nar::writer::open(&mut buf).unwrap();
+
+    node.symlink("/nix/store/somewhereelse".as_bytes()).unwrap();
+
+    assert_eq!(include_bytes!("../tests/symlink.nar"), buf.as_slice());
+}
+
+#[cfg(feature = "async")]
+#[test]
+fn symlink_async() {
+    let mut buf = vec![];
+
+    futures::executor::block_on(async {
+        let node = nar::writer::r#async::open(&mut buf).await.unwrap();
+        node.symlink("/nix/store/somewhereelse".as_bytes())
+            .await
+            .unwrap();
+    });
+
+    assert_eq!(include_bytes!("../tests/symlink.nar"), buf.as_slice());
+}
+
+#[test]
+fn file() {
+    let mut buf = vec![];
+    let node = nar::writer::open(&mut buf).unwrap();
+
+    let file_contents = "Hello World!".to_string();
+    node.file(
+        false,
+        file_contents.len() as u64,
+        &mut std::io::Cursor::new(file_contents),
+    )
+    .unwrap();
+
+    assert_eq!(include_bytes!("../tests/helloworld.nar"), buf.as_slice());
+}
+
+#[cfg(feature = "async")]
+#[test]
+fn file_async() {
+    let mut buf = vec![];
+
+    futures::executor::block_on(async {
+        let node = nar::writer::r#async::open(&mut buf).await.unwrap();
+
+        let file_contents = "Hello World!".to_string();
+        node.file(
+            false,
+            file_contents.len() as u64,
+            &mut futures::io::Cursor::new(file_contents),
+        )
+        .await
+        .unwrap();
+    });
+
+    assert_eq!(include_bytes!("../tests/helloworld.nar"), buf.as_slice());
+}
+
+#[test]
+fn complicated() {
+    let mut buf = vec![];
+    let node = nar::writer::open(&mut buf).unwrap();
+
+    let mut dir_node = node.directory().unwrap();
+
+    let e = dir_node.entry(".keep".as_bytes()).unwrap();
+    e.file(false, 0, &mut std::io::Cursor::new([]))
+        .expect("read .keep must succeed");
+
+    let e = dir_node.entry("aa".as_bytes()).unwrap();
+    e.symlink("/nix/store/somewhereelse".as_bytes())
+        .expect("symlink must succeed");
+
+    let e = dir_node.entry("keep".as_bytes()).unwrap();
+    let mut subdir_node = e.directory().expect("directory must succeed");
+
+    let e_sub = subdir_node
+        .entry(".keep".as_bytes())
+        .expect("subdir entry must succeed");
+    e_sub.file(false, 0, &mut std::io::Cursor::new([])).unwrap();
+
+    // close the subdir, and then the dir, which is required.
+    subdir_node.close().unwrap();
+    dir_node.close().unwrap();
+
+    assert_eq!(include_bytes!("../tests/complicated.nar"), buf.as_slice());
+}
+
+#[cfg(feature = "async")]
+#[test]
+fn complicated_async() {
+    let mut buf = vec![];
+
+    futures::executor::block_on(async {
+        let node = nar::writer::r#async::open(&mut buf).await.unwrap();
+
+        let mut dir_node = node.directory().await.unwrap();
+
+        let e = dir_node.entry(".keep".as_bytes()).await.unwrap();
+        e.file(false, 0, &mut futures::io::Cursor::new([]))
+            .await
+            .expect("read .keep must succeed");
+
+        let e = dir_node.entry("aa".as_bytes()).await.unwrap();
+        e.symlink("/nix/store/somewhereelse".as_bytes())
+            .await
+            .expect("symlink must succeed");
+
+        let e = dir_node.entry("keep".as_bytes()).await.unwrap();
+        let mut subdir_node = e.directory().await.expect("directory must succeed");
+
+        let e_sub = subdir_node
+            .entry(".keep".as_bytes())
+            .await
+            .expect("subdir entry must succeed");
+        e_sub
+            .file(false, 0, &mut futures::io::Cursor::new([]))
+            .await
+            .unwrap();
+
+        // close the subdir, and then the dir, which is required.
+        subdir_node.close().await.unwrap();
+        dir_node.close().await.unwrap();
+    });
+
+    assert_eq!(include_bytes!("../tests/complicated.nar"), buf.as_slice());
+}