about summary refs log tree commit diff
path: root/tvix/nix-compat/src/nar/reader/test.rs
blob: fd0d6a9f5afdb9e8500237091a3ebdeaeec7c54a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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"),
    }
}