#![allow(dead_code)]
use std::path::PathBuf;
use tempfile::TempDir;
pub struct TestProject {
_temp_dir: TempDir,
pub root: PathBuf,
}
impl TestProject {
pub fn new() -> anyhow::Result<Self> {
let temp_dir = TempDir::new()?;
let root = temp_dir.path().to_path_buf();
Ok(Self {
_temp_dir: temp_dir,
root,
})
}
pub fn create_file(&self, path: &str, contents: &str) -> anyhow::Result<PathBuf> {
let file_path = self.root.join(path);
if let Some(parent) = file_path.parent() {
std::fs::create_dir_all(parent)?;
}
std::fs::write(&file_path, contents)?;
Ok(file_path)
}
pub fn create_dir(&self, path: &str) -> anyhow::Result<PathBuf> {
let dir_path = self.root.join(path);
std::fs::create_dir_all(&dir_path)?;
Ok(dir_path)
}
pub fn path(&self, relative: &str) -> PathBuf {
self.root.join(relative)
}
pub fn file_exists(&self, path: &str) -> bool {
self.root.join(path).exists()
}
pub fn read_file(&self, path: &str) -> anyhow::Result<String> {
Ok(std::fs::read_to_string(self.root.join(path))?)
}
}
pub struct MockRedis {
}
impl MockRedis {
pub fn new() -> Self {
Self {}
}
}
pub fn minimal_config() -> serde_json::Value {
serde_json::json!({
"name": "test-robot",
"version": "0.1.0",
"nodes": [],
"drivers": [],
"types": []
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_project_creation() {
let project = TestProject::new().unwrap();
assert!(project.root.exists());
}
#[test]
fn test_file_creation() {
let project = TestProject::new().unwrap();
project.create_file("test.txt", "hello").unwrap();
assert!(project.file_exists("test.txt"));
assert_eq!(project.read_file("test.txt").unwrap(), "hello");
}
#[test]
fn test_nested_file_creation() {
let project = TestProject::new().unwrap();
project.create_file("foo/bar/test.txt", "nested").unwrap();
assert!(project.file_exists("foo/bar/test.txt"));
}
}