feat: initial nix-daemon implementation
This commit is contained in:
225
nix-js/tests/builtins_store.rs
Normal file
225
nix-js/tests/builtins_store.rs
Normal file
@@ -0,0 +1,225 @@
|
||||
mod utils;
|
||||
|
||||
use std::sync::Once;
|
||||
|
||||
use nix_js::value::Value;
|
||||
use utils::eval_result;
|
||||
|
||||
fn init() {
|
||||
static INIT: Once = Once::new();
|
||||
INIT.call_once(|| {
|
||||
unsafe { std::env::set_var("NIX_JS_STORE_MODE", "simulated") };
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_file_simple() {
|
||||
init();
|
||||
|
||||
let result =
|
||||
eval_result(r#"builtins.toFile "hello.txt" "Hello, World!""#).expect("Failed to evaluate");
|
||||
|
||||
match result {
|
||||
Value::String(path) => {
|
||||
assert!(path.contains("-hello.txt"));
|
||||
assert!(std::path::Path::new(&path).exists());
|
||||
|
||||
let contents = std::fs::read_to_string(&path).expect("Failed to read file");
|
||||
assert_eq!(contents, "Hello, World!");
|
||||
}
|
||||
_ => panic!("Expected string, got {:?}", result),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_file_with_references() {
|
||||
init();
|
||||
|
||||
let result = eval_result(
|
||||
r#"
|
||||
let
|
||||
dep = builtins.toFile "dep.txt" "dependency";
|
||||
in
|
||||
builtins.toFile "main.txt" "Reference: ${dep}"
|
||||
"#,
|
||||
)
|
||||
.expect("Failed to evaluate");
|
||||
|
||||
match result {
|
||||
Value::String(path) => {
|
||||
assert!(path.contains("-main.txt"));
|
||||
let contents = std::fs::read_to_string(&path).expect("Failed to read file");
|
||||
assert!(contents.contains("Reference: "));
|
||||
assert!(contents.contains("-dep.txt"));
|
||||
}
|
||||
_ => panic!("Expected string"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_file_invalid_name_with_slash() {
|
||||
init();
|
||||
|
||||
let result = eval_result(r#"builtins.toFile "foo/bar.txt" "content""#);
|
||||
|
||||
assert!(result.is_err());
|
||||
assert!(
|
||||
result
|
||||
.unwrap_err()
|
||||
.to_string()
|
||||
.contains("name cannot contain '/'")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_file_invalid_name_dot() {
|
||||
init();
|
||||
|
||||
let result = eval_result(r#"builtins.toFile "." "content""#);
|
||||
|
||||
assert!(result.is_err());
|
||||
assert!(result.unwrap_err().to_string().contains("invalid name"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_file_invalid_name_dotdot() {
|
||||
init();
|
||||
|
||||
let result = eval_result(r#"builtins.toFile ".." "content""#);
|
||||
|
||||
assert!(result.is_err());
|
||||
assert!(result.unwrap_err().to_string().contains("invalid name"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn store_path_validation_not_in_store() {
|
||||
init();
|
||||
|
||||
let result = eval_result(r#"builtins.storePath "/tmp/foo""#);
|
||||
|
||||
assert!(result.is_err());
|
||||
assert!(
|
||||
result
|
||||
.unwrap_err()
|
||||
.to_string()
|
||||
.contains("not in the Nix store")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn store_path_validation_malformed_hash() {
|
||||
init();
|
||||
|
||||
let dummy_file_result = eval_result(r#"builtins.toFile "dummy.txt" "content""#)
|
||||
.expect("Failed to create dummy file");
|
||||
|
||||
let dummy_path = match dummy_file_result {
|
||||
Value::String(ref p) => p.clone(),
|
||||
_ => panic!("Expected string"),
|
||||
};
|
||||
|
||||
let store_dir = std::path::Path::new(&dummy_path)
|
||||
.parent()
|
||||
.expect("Failed to get parent dir")
|
||||
.to_str()
|
||||
.expect("Failed to convert to string");
|
||||
|
||||
let test_path = format!("{}/invalid-hash-hello", store_dir);
|
||||
let result = eval_result(&format!(r#"builtins.storePath "{}""#, test_path));
|
||||
|
||||
assert!(result.is_err());
|
||||
let err_str = result.unwrap_err().to_string();
|
||||
assert!(
|
||||
err_str.contains("invalid") || err_str.contains("hash"),
|
||||
"Expected hash validation error, got: {}",
|
||||
err_str
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn store_path_validation_missing_name() {
|
||||
init();
|
||||
|
||||
let dummy_file_result = eval_result(r#"builtins.toFile "dummy.txt" "content""#)
|
||||
.expect("Failed to create dummy file");
|
||||
|
||||
let dummy_path = match dummy_file_result {
|
||||
Value::String(ref p) => p.clone(),
|
||||
_ => panic!("Expected string"),
|
||||
};
|
||||
|
||||
let store_dir = std::path::Path::new(&dummy_path)
|
||||
.parent()
|
||||
.expect("Failed to get parent dir")
|
||||
.to_str()
|
||||
.expect("Failed to convert to string");
|
||||
|
||||
let test_path = format!("{}/abcd1234abcd1234abcd1234abcd1234", store_dir);
|
||||
let result = eval_result(&format!(r#"builtins.storePath "{}""#, test_path));
|
||||
|
||||
assert!(result.is_err());
|
||||
let err_str = result.unwrap_err().to_string();
|
||||
assert!(
|
||||
err_str.contains("missing name") || err_str.contains("format"),
|
||||
"Expected missing name error, got: {}",
|
||||
err_str
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_file_curried_application() {
|
||||
init();
|
||||
|
||||
let result = eval_result(
|
||||
r#"
|
||||
let
|
||||
makeFile = builtins.toFile "test.txt";
|
||||
in
|
||||
makeFile "test content"
|
||||
"#,
|
||||
)
|
||||
.expect("Failed to evaluate");
|
||||
|
||||
match result {
|
||||
Value::String(path) => {
|
||||
assert!(path.contains("-test.txt"));
|
||||
let contents = std::fs::read_to_string(&path).expect("Failed to read file");
|
||||
assert_eq!(contents, "test content");
|
||||
}
|
||||
_ => panic!("Expected string"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_file_number_conversion() {
|
||||
init();
|
||||
|
||||
let result = eval_result(r#"builtins.toFile "number.txt" (builtins.toString 42)"#)
|
||||
.expect("Failed to evaluate");
|
||||
|
||||
match result {
|
||||
Value::String(path) => {
|
||||
let contents = std::fs::read_to_string(&path).expect("Failed to read file");
|
||||
assert_eq!(contents, "42");
|
||||
}
|
||||
_ => panic!("Expected string"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_file_list_conversion() {
|
||||
init();
|
||||
|
||||
let result = eval_result(
|
||||
r#"builtins.toFile "list.txt" (builtins.concatStringsSep "\n" ["line1" "line2" "line3"])"#,
|
||||
)
|
||||
.expect("Failed to evaluate");
|
||||
|
||||
match result {
|
||||
Value::String(path) => {
|
||||
let contents = std::fs::read_to_string(&path).expect("Failed to read file");
|
||||
assert_eq!(contents, "line1\nline2\nline3");
|
||||
}
|
||||
_ => panic!("Expected string"),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user