teeny_compiler/compiler/
mod.rs1pub mod backend;
19pub mod driver;
21pub mod target;
23
24pub fn default_cache_dir() -> String {
34 if let Ok(dir) = std::env::var("TEENYC_CACHE_DIR") {
35 return dir;
36 }
37
38 match std::env::current_exe() {
39 Ok(exe) => sibling_cache_dir(&exe).unwrap_or_else(|| "/tmp/teenyc_cache".to_string()),
40 Err(_) => "/tmp/teenyc_cache".to_string(),
41 }
42}
43
44fn sibling_cache_dir(exe: &std::path::Path) -> Option<String> {
46 let package_root = exe.parent()?.parent()?;
47 let candidate = package_root.join("cache");
48 candidate
49 .is_dir()
50 .then(|| candidate.to_string_lossy().into_owned())
51}
52
53#[cfg(test)]
54mod cache_dir_tests {
55 use std::fs;
56 use std::time::{SystemTime, UNIX_EPOCH};
57
58 use super::*;
59
60 fn tmp_root(name: &str) -> std::path::PathBuf {
61 let suffix = SystemTime::now()
62 .duration_since(UNIX_EPOCH)
63 .unwrap()
64 .as_nanos();
65 std::env::temp_dir().join(format!("teeny-compiler-cache-dir-test-{name}-{suffix}"))
66 }
67
68 #[test]
69 fn finds_sibling_cache_dir_when_present() {
70 let root = tmp_root("present");
71 let bin_dir = root.join("bin");
72 fs::create_dir_all(&bin_dir).unwrap();
73 fs::create_dir_all(root.join("cache")).unwrap();
74 let exe = bin_dir.join("myapp");
75
76 let found = sibling_cache_dir(&exe).expect("cache dir should be found");
77 assert_eq!(found, root.join("cache").to_string_lossy());
78
79 let _ = fs::remove_dir_all(&root);
80 }
81
82 #[test]
83 fn none_when_cache_dir_missing() {
84 let root = tmp_root("missing");
85 let bin_dir = root.join("bin");
86 fs::create_dir_all(&bin_dir).unwrap();
87 let exe = bin_dir.join("myapp");
88
89 assert!(sibling_cache_dir(&exe).is_none());
90
91 let _ = fs::remove_dir_all(&root);
92 }
93
94 #[test]
95 fn none_when_exe_has_no_grandparent() {
96 assert!(sibling_cache_dir(std::path::Path::new("myapp")).is_none());
97 }
98}