From 31443d21eed5986d8b2968951e3e38283008b892 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Thu, 11 Aug 2022 19:10:23 +0300 Subject: chore(tvix): move nix-store CLI scaffolding to subfolder For some reason a top-level Rust project ended up in this location, which is incompatible with the actual project structure that's being prepared for merge right now. Change-Id: I9d919ad72fc7e4e4d8cbb9899e7f8d90fa7ca87a Reviewed-on: https://cl.tvl.fyi/c/depot/+/6060 Tested-by: BuildkiteCI Reviewed-by: flokli Autosubmit: tazjin --- tvix/nix_cli/src/bin/nix-store.rs | 104 ++++++++++++++++++++++++++++++++++++++ tvix/nix_cli/src/main.rs | 3 ++ 2 files changed, 107 insertions(+) create mode 100644 tvix/nix_cli/src/bin/nix-store.rs create mode 100644 tvix/nix_cli/src/main.rs (limited to 'tvix/nix_cli/src') diff --git a/tvix/nix_cli/src/bin/nix-store.rs b/tvix/nix_cli/src/bin/nix-store.rs new file mode 100644 index 000000000000..e1568fff73f0 --- /dev/null +++ b/tvix/nix_cli/src/bin/nix-store.rs @@ -0,0 +1,104 @@ +fn main() { + main_args(std::env::args().collect()).unwrap_or_else(|e| e.exit()); +} + +pub fn main_args(args: Vec) -> clap::Result { + 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) -> 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) -> 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) -> 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) + } +} diff --git a/tvix/nix_cli/src/main.rs b/tvix/nix_cli/src/main.rs new file mode 100644 index 000000000000..40086e6f27ee --- /dev/null +++ b/tvix/nix_cli/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, tvix!"); +} -- cgit 1.4.1