feat: Store.parse_store_path()

(cherry picked from commit 6b92d4164b94d5030929dfb56577b1fd8d62e067)
This commit is contained in:
Robert Hensing 2024-12-17 09:54:28 +01:00
parent c9d54ccbc6
commit c986c09b8c
2 changed files with 55 additions and 0 deletions

View file

@ -71,3 +71,17 @@ impl Drop for StorePath {
}
}
}
#[cfg(test)]
mod tests {
#[test]
#[cfg(nix_at_least = "2.26" /* get_storedir */)]
fn store_path_name() {
let mut store = crate::store::Store::open("dummy://", []).unwrap();
let store_dir = store.get_storedir().unwrap();
let store_path_string =
format!("{store_dir}/rdd4pnr4x9rqc9wgbibhngv217w2xvxl-bash-interactive-5.2p26");
let store_path = store.parse_store_path(store_path_string.as_str()).unwrap();
assert_eq!(store_path.name().unwrap(), "bash-interactive-5.2p26");
}
}

View file

@ -10,6 +10,8 @@ use std::ptr::null_mut;
use std::ptr::NonNull;
use std::sync::{Arc, Mutex, Weak};
use crate::path::StorePath;
/* TODO make Nix itself thread safe */
lazy_static! {
static ref INIT: Result<()> = unsafe {
@ -187,6 +189,21 @@ impl Store {
r
}
#[doc(alias = "nix_store_parse_path")]
pub fn parse_store_path(&mut self, path: &str) -> Result<StorePath> {
let path = CString::new(path)?;
unsafe {
let store_path = check_call!(raw::store_parse_path(
&mut self.context,
self.inner.ptr(),
path.as_ptr()
))?;
let store_path =
NonNull::new(store_path).expect("nix_store_parse_path returned a null pointer");
Ok(StorePath::new_raw(store_path))
}
}
pub fn weak_ref(&self) -> StoreWeak {
StoreWeak {
inner: Arc::downgrade(&self.inner),
@ -240,6 +257,30 @@ mod tests {
assert_eq!(uri, "https://cache.nixos.org");
}
#[test]
#[cfg(nix_at_least = "2.26" /* get_storedir */)]
fn parse_store_path_ok() {
let mut store = crate::store::Store::open("dummy://", []).unwrap();
let store_dir = store.get_storedir().unwrap();
let store_path_string =
format!("{store_dir}/rdd4pnr4x9rqc9wgbibhngv217w2xvxl-bash-interactive-5.2p26");
let store_path = store.parse_store_path(store_path_string.as_str()).unwrap();
assert_eq!(store_path.name().unwrap(), "bash-interactive-5.2p26");
}
#[test]
fn parse_store_path_fail() {
let mut store = crate::store::Store::open("dummy://", []).unwrap();
let store_path_string = format!("bash-interactive-5.2p26");
let r = store.parse_store_path(store_path_string.as_str());
match r {
Err(e) => {
assert!(e.to_string().contains("bash-interactive-5.2p26"));
}
_ => panic!("Expected error"),
}
}
#[test]
fn weak_ref() {
let mut store = Store::open("auto", HashMap::new()).unwrap();