use std::env;
use std::fs::{self, create_dir_all};
use std::path::{Path, PathBuf};
use tempfile::TempDir;
#[allow(dead_code)]
pub fn setup_temp_dir() -> TempDir {
TempDir::new().expect("Failed to create temp dir")
}
#[allow(dead_code)]
pub fn create_test_manifest(dir: &Path, name: &str, version: &str) -> PathBuf {
create_dir_all(dir).expect("Failed to create directory");
let manifest_path = dir.join("typst.toml");
let content = format!(
r#"[package]
name = "{}"
version = "{}"
entrypoint = "main.typ"
authors = ["Test Author"]
license = "MIT"
description = "Test package"
[tool.utpm]
exclude = []
"#,
name, version
);
fs::write(&manifest_path, content).expect("Failed to write test manifest");
manifest_path
}
#[allow(dead_code)]
pub fn create_custom_manifest(dir: &Path, content: &str) -> PathBuf {
create_dir_all(dir).expect("Failed to create directory");
let manifest_path = dir.join("typst.toml");
fs::write(&manifest_path, content).expect("Failed to write test manifest");
manifest_path
}
#[allow(dead_code)]
pub fn create_test_entrypoint(dir: &Path) -> PathBuf {
let main_path = dir.join("main.typ");
fs::write(&main_path, "// Test entrypoint\n#let hello = \"world\"")
.expect("Failed to write test entrypoint");
main_path
}
#[allow(dead_code)]
pub fn create_test_package(dir: &Path, name: &str, version: &str) -> PathBuf {
create_test_manifest(dir, name, version);
create_test_entrypoint(dir);
let src_dir = dir.join("src");
create_dir_all(&src_dir).expect("Failed to create src dir");
let examples_dir = dir.join("examples");
create_dir_all(&examples_dir).expect("Failed to create examples dir");
fs::write(examples_dir.join("example.typ"), "// Example file")
.expect("Failed to write example file");
dir.to_path_buf()
}
#[allow(dead_code)]
pub fn setup_test_env(temp_dir: &Path) {
unsafe {
env::set_var("UTPM_DATA_DIR", temp_dir.join("data"));
env::set_var("UTPM_CACHE_DIR", temp_dir.join("cache"));
env::set_var("UTPM_CURRENT_DIR", temp_dir.join("current"));
}
create_dir_all(temp_dir.join("data/typst/packages")).ok();
create_dir_all(temp_dir.join("cache/typst/packages")).ok();
create_dir_all(temp_dir.join("current")).ok();
}
#[allow(dead_code)]
pub fn cleanup_test_env() {
unsafe {
env::remove_var("UTPM_DATA_DIR");
env::remove_var("UTPM_CACHE_DIR");
env::remove_var("UTPM_CURRENT_DIR");
env::remove_var("UTPM_DEBUG");
}
}
#[allow(dead_code)]
pub fn read_file_string(path: &Path) -> String {
fs::read_to_string(path).expect("Failed to read file")
}
#[allow(dead_code)]
pub fn assert_file_exists(path: &Path) {
assert!(path.exists(), "File should exist: {:?}", path);
assert!(path.is_file(), "Path should be a file: {:?}", path);
}
#[allow(dead_code)]
pub fn assert_dir_exists(path: &Path) {
assert!(path.exists(), "Directory should exist: {:?}", path);
assert!(path.is_dir(), "Path should be a directory: {:?}", path);
}
#[allow(dead_code)]
pub fn assert_not_exists(path: &Path) {
assert!(!path.exists(), "Path should not exist: {:?}", path);
}