about summary refs log tree commit diff
path: root/tvix/nix-compat/src/nar/writer/test.rs
blob: d7f18a49af344243e40299e10132650d87fc24b4 (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
121
122
123
124
125
126
127
128
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")]
#[tokio::test]
async fn symlink_async() {
    let mut buf = vec![];

    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")]
#[tokio::test]
async fn file_async() {
    use std::io::Cursor;

    let mut buf = vec![];

    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 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")]
#[tokio::test]
async fn complicated_async() {
    use std::io::Cursor;

    let mut buf = vec![];

    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 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 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());
}