diff options
author | Florian Klink <flokli@flokli.de> | 2023-10-18T12·27+0100 |
---|---|---|
committer | flokli <flokli@flokli.de> | 2023-10-18T12·31+0000 |
commit | 05bca22121ad223fa44062391e0418b0975766db (patch) | |
tree | 6f841139a7a899d948e85fe4c850c4e5c490d81c /tvix/nix-compat/src | |
parent | e64acba454c7b3e988793ad388601c82904c2036 (diff) |
feat(tvix/nix-compat/nar/reader): add tests r/6856
Change-Id: If0abaf74a3b7d201af8972e54e3e34b20b8c0bc4 Reviewed-on: https://cl.tvl.fyi/c/depot/+/9764 Tested-by: BuildkiteCI Reviewed-by: Connor Brewster <cbrewster@hey.com>
Diffstat (limited to 'tvix/nix-compat/src')
-rw-r--r-- | tvix/nix-compat/src/nar/reader/mod.rs | 2 | ||||
-rw-r--r-- | tvix/nix-compat/src/nar/reader/test.rs | 120 | ||||
-rw-r--r-- | tvix/nix-compat/src/nar/tests/complicated.nar | bin | 0 -> 840 bytes | |||
-rw-r--r-- | tvix/nix-compat/src/nar/tests/helloworld.nar | bin | 0 -> 128 bytes | |||
-rw-r--r-- | tvix/nix-compat/src/nar/tests/symlink.nar | bin | 0 -> 136 bytes |
5 files changed, 122 insertions, 0 deletions
diff --git a/tvix/nix-compat/src/nar/reader/mod.rs b/tvix/nix-compat/src/nar/reader/mod.rs index e0b2e1c84574..09af41866508 100644 --- a/tvix/nix-compat/src/nar/reader/mod.rs +++ b/tvix/nix-compat/src/nar/reader/mod.rs @@ -14,6 +14,8 @@ use std::io::{ use crate::nar::wire; mod read; +#[cfg(test)] +mod test; pub type Reader<'a> = dyn Read + Send + 'a; diff --git a/tvix/nix-compat/src/nar/reader/test.rs b/tvix/nix-compat/src/nar/reader/test.rs new file mode 100644 index 000000000000..fd0d6a9f5afd --- /dev/null +++ b/tvix/nix-compat/src/nar/reader/test.rs @@ -0,0 +1,120 @@ +use std::io::Read; + +use crate::nar; + +#[test] +fn symlink() { + let mut f = std::io::Cursor::new(include_bytes!("../tests/symlink.nar")); + let node = nar::reader::open(&mut f).unwrap(); + + match node { + nar::reader::Node::Symlink { target } => { + assert_eq!( + &b"/nix/store/somewhereelse"[..], + &target, + "target must match" + ); + } + _ => panic!("unexpected type"), + } +} + +#[test] +fn file() { + let mut f = std::io::Cursor::new(include_bytes!("../tests/helloworld.nar")); + let node = nar::reader::open(&mut f).unwrap(); + + match node { + nar::reader::Node::File { + executable, + mut reader, + } => { + assert!(!executable); + let mut buf = vec![]; + reader.read_to_end(&mut buf).expect("read must succeed"); + assert_eq!(&b"Hello World!"[..], &buf); + } + _ => panic!("unexpected type"), + } +} + +#[test] +fn complicated() { + let mut f = std::io::Cursor::new(include_bytes!("../tests/complicated.nar")); + let node = nar::reader::open(&mut f).unwrap(); + + match node { + nar::reader::Node::Directory(mut dir_reader) => { + // first entry is .keep, an empty regular file. + let entry = dir_reader + .next() + .expect("next must succeed") + .expect("must be some"); + + assert_eq!(&b".keep"[..], &entry.name); + + match entry.node { + nar::reader::Node::File { + executable, + mut reader, + } => { + assert!(!executable); + assert_eq!(reader.read(&mut [0]).unwrap(), 0); + } + _ => panic!("unexpected type for .keep"), + } + + // second entry is aa, a symlink to /nix/store/somewhereelse + let entry = dir_reader + .next() + .expect("next must be some") + .expect("must be some"); + + assert_eq!(&b"aa"[..], &entry.name); + + match entry.node { + nar::reader::Node::Symlink { target } => { + assert_eq!(&b"/nix/store/somewhereelse"[..], &target); + } + _ => panic!("unexpected type for aa"), + } + + // third entry is a directory called "keep" + let entry = dir_reader + .next() + .expect("next must be some") + .expect("must be some"); + + assert_eq!(&b"keep"[..], &entry.name); + + match entry.node { + nar::reader::Node::Directory(mut subdir_reader) => { + // first entry is .keep, an empty regular file. + let entry = subdir_reader + .next() + .expect("next must succeed") + .expect("must be some"); + + // … it contains a single .keep, an empty regular file. + assert_eq!(&b".keep"[..], &entry.name); + + match entry.node { + nar::reader::Node::File { + executable, + mut reader, + } => { + assert!(!executable); + assert_eq!(reader.read(&mut [0]).unwrap(), 0); + } + _ => panic!("unexpected type for keep/.keep"), + } + } + _ => panic!("unexpected type for keep/.keep"), + } + + // reading more entries yields None (and we actually must read until this) + assert!(dir_reader.next().expect("must succeed").is_none()); + } + _ => panic!("unexpected type"), + } +} diff --git a/tvix/nix-compat/src/nar/tests/complicated.nar b/tvix/nix-compat/src/nar/tests/complicated.nar new file mode 100644 index 000000000000..6a137f5fbb6b --- /dev/null +++ b/tvix/nix-compat/src/nar/tests/complicated.nar Binary files differdiff --git a/tvix/nix-compat/src/nar/tests/helloworld.nar b/tvix/nix-compat/src/nar/tests/helloworld.nar new file mode 100644 index 000000000000..2e1268115205 --- /dev/null +++ b/tvix/nix-compat/src/nar/tests/helloworld.nar Binary files differdiff --git a/tvix/nix-compat/src/nar/tests/symlink.nar b/tvix/nix-compat/src/nar/tests/symlink.nar new file mode 100644 index 000000000000..7990e4ad5bc2 --- /dev/null +++ b/tvix/nix-compat/src/nar/tests/symlink.nar Binary files differ |