about summary refs log tree commit diff
path: root/tvix/nix_cli/src/bin/nix-store.rs
blob: e1568fff73f07294f7cf3b543fbfc11cc6b0405c (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
fn main() {
    main_args(std::env::args().collect()).unwrap_or_else(|e| e.exit());
}

pub fn main_args(args: Vec<String>) -> clap::Result<NixResult> {
    let matches = clap::App::new("nix-store")
        .subcommand(clap::App::new("--add").arg(clap::Arg::new("FILE").required(true).index(1)))
        .try_get_matches_from(args.iter())?;
    if let Some(add) = matches.subcommand_matches("--add") {
        let file = add.value_of("FILE").expect("--add needs a file");
        let file_contents =
            std::fs::read_to_string(file).expect(&format!("file {} does not exist", file));
        Ok(NixResult::FileAddedToStore {
            content: file_contents,
        })
    } else {
        panic!("read some arguments that we do not know: {:?}", args)
    }
}

#[derive(Debug, Eq, PartialEq)]
pub enum NixResult {
    FileAddedToStore { content: String },
}

#[cfg(test)]
mod integration_tests {
    use std::collections::VecDeque;
    use std::io::Write;

    use super::*;

    #[derive(Debug)]
    enum NixOutput {
        Err {
            status: i32,
            stdout: String,
            stderr: String,
        },
        Ok {
            stdout: String,
            stderr: String,
        },
    }

    fn run_nix_command(cmd: &str, args: Vec<String>) -> NixOutput {
        let out = std::process::Command::new(cmd)
            .args(args)
            .output()
            .expect(&format!("could not run {}", cmd));
        match out.status.code().expect("no status code!") {
            0 => NixOutput::Ok {
                stdout: String::from_utf8_lossy(&out.stdout).trim_end().to_string(),
                stderr: String::from_utf8_lossy(&out.stderr).trim_end().to_string(),
            },
            status => NixOutput::Err {
                status,
                stdout: String::from_utf8_lossy(&out.stdout).trim_end().to_string(),
                stderr: String::from_utf8_lossy(&out.stderr).trim_end().to_string(),
            },
        }
    }

    fn nix_nix_store<'a>(args: Vec<String>) -> NixResult {
        match run_nix_command("nix-store", args) {
            err @ NixOutput::Err { .. } => panic!("nix-store --add failed: {:#?}", err),
            NixOutput::Ok { stdout, .. } => NixResult::FileAddedToStore {
                content: std::fs::read_to_string(&stdout)
                    .expect(&format!("cannot open {} as store file", stdout)),
            },
        }
    }

    fn tvix_nix_store<'a>(args: Vec<String>) -> NixResult {
        eprintln!("running tvix with arguments {:?}", args);
        let mut args = VecDeque::from(args);
        args.push_front("tvix-store".to_string());
        super::main_args(Vec::from(args))
            .unwrap_or_else(|e| panic!("clap command line parsing failed:\n{}", e))
    }

    #[test]
    fn test_nix_store_add() {
        let file_content = "I am a copied file";
        let mut tempfile = tempfile::NamedTempFile::new().expect("cannot create temp file");
        tempfile
            .write_all(file_content.as_bytes())
            .expect("could not write to tempfile");
        assert_eq!(
            tvix_nix_store(vec![
                "--add".to_string(),
                tempfile.path().as_os_str().to_string_lossy().into_owned()
            ]),
            nix_nix_store(vec![
                "--add".to_string(),
                tempfile.path().as_os_str().to_string_lossy().into_owned()
            ]),
            "added file contents were not the same"
        );

        // make sure the tempfile lives till here
        drop(tempfile)
    }
}