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

@ -4,7 +4,7 @@ use clap::{Args, Parser, Subcommand};
mod bindings; mod bindings;
mod cli; mod cli;
mod make_nar; pub mod make_nar;
pub mod path_info; pub mod path_info;
pub mod push; pub mod push;
pub mod store; pub mod store;

View file

@ -15,10 +15,10 @@ use crate::store::Store;
pub struct MakeNar<'a> { pub struct MakeNar<'a> {
path_info: &'a PathInfo, path_info: &'a PathInfo,
store: Arc<Store>, store: Arc<Store>,
nar_hasher: Sha256, pub nar_hasher: Sha256,
/// hash of compressed nar file /// hash of compressed nar file
file_hasher: Sha256, file_hasher: Sha256,
nar_size: u64, pub nar_size: u64,
file_size: u64, file_size: u64,
} }

View file

@ -1,12 +1,20 @@
#![allow(dead_code)]
use std::sync::Arc;
use nixcp::store::Store; 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 struct Context {
pub store: Store, pub store: Arc<Store>,
} }
impl Context { impl Context {
fn new() -> Self { 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 } 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::path::PathBuf;
use std::process::Command; use std::process::Command;
mod common; use crate::common::{HELLO, HELLO_DRV, HELLO_PATH};
const HELLO: &str = "github:nixos/nixpkgs?ref=f771eb401a46846c1aebd20552521b233dd7e18b#hello"; mod common;
const HELLO_DRV: &str = "iqbwkm8mjjjlmw6x6ry9rhzin2cp9372-hello-2.12.1.drv";
#[tokio::test] #[tokio::test]
async fn path_info_from_package() { async fn path_info_from_package() {
@ -27,7 +26,7 @@ async fn path_info_from_path() {
.status() .status()
.unwrap(); .unwrap();
let ctx = common::context(); 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) let path_info = PathInfo::from_derivation(&path, &ctx.store)
.await .await
.expect("get pathinfo from package"); .expect("get pathinfo from package");