add nar tests

This commit is contained in:
cy 2025-04-28 17:37:04 -04:00
parent 54d4c714af
commit 09181ae785
5 changed files with 42 additions and 9 deletions

View file

@ -1,12 +1,20 @@
#![allow(dead_code)]
use std::sync::Arc;
use nixcp::store::Store;
pub const HELLO: &str = "github:nixos/nixpkgs?ref=f771eb401a46846c1aebd20552521b233dd7e18b#hello";
pub const HELLO_DRV: &str = "iqbwkm8mjjjlmw6x6ry9rhzin2cp9372-hello-2.12.1.drv";
pub const HELLO_PATH: &str = "/nix/store/9bwryidal9q3g91cjm6xschfn4ikd82q-hello-2.12.1";
pub struct Context {
pub store: Store,
pub store: Arc<Store>,
}
impl Context {
fn new() -> Self {
let store = Store::connect().expect("connect to nix store");
let store = Arc::new(Store::connect().expect("connect to nix store"));
Self { store }
}
}

26
tests/nar.rs Normal file
View file

@ -0,0 +1,26 @@
use crate::common::HELLO_PATH;
use nix_compat::nixbase32;
use nixcp::make_nar::MakeNar;
use nixcp::path_info::PathInfo;
use sha2::Digest;
use tokio::io::AsyncReadExt;
mod common;
#[tokio::test]
async fn nar_size_and_hash() {
let ctx = common::context();
let path_info = PathInfo::from_path(HELLO_PATH, &ctx.store).await.unwrap();
let mut nar = MakeNar::new(&path_info, ctx.store).unwrap();
let mut reader = nar.compress_and_hash().unwrap();
let mut buf = Vec::new();
reader.read_to_end(&mut buf).await.unwrap();
drop(reader);
assert_eq!(nar.nar_size, 234680);
let nar_hash = nar.nar_hasher.finalize();
let real_nar_hash = "08za7nnjda8kpdsd73v3mhykjvp0rsmskwsr37winhmzgm6iw79w";
assert_eq!(nixbase32::encode(nar_hash.as_slice()), real_nar_hash);
}

View file

@ -2,10 +2,9 @@ use nixcp::path_info::PathInfo;
use std::path::PathBuf;
use std::process::Command;
mod common;
use crate::common::{HELLO, HELLO_DRV, HELLO_PATH};
const HELLO: &str = "github:nixos/nixpkgs?ref=f771eb401a46846c1aebd20552521b233dd7e18b#hello";
const HELLO_DRV: &str = "iqbwkm8mjjjlmw6x6ry9rhzin2cp9372-hello-2.12.1.drv";
mod common;
#[tokio::test]
async fn path_info_from_package() {
@ -27,7 +26,7 @@ async fn path_info_from_path() {
.status()
.unwrap();
let ctx = common::context();
let path = PathBuf::from("/nix/store/9bwryidal9q3g91cjm6xschfn4ikd82q-hello-2.12.1");
let path = PathBuf::from(HELLO_PATH);
let path_info = PathInfo::from_derivation(&path, &ctx.store)
.await
.expect("get pathinfo from package");