test: add integration tests for scan, session config and cache lifecycle
tests/scan.rs: 3 tests exercising scan_from_root via the public API — finds git repos, skips hidden directories, and respects max_depth. Note: tempfile::tempdir() generates hidden paths (.tmpXXXXXX) on this system, so each test creates a visible scan_root/ subdirectory to avoid filter_entry silently skipping the walk root. tests/session_config.rs: 2 tests for SessionConfig::load_from_project — loads a .tmuxido.toml written into a temp dir and returns None when the file is absent. tests/cache_lifecycle.rs: round-trip test that saves a ProjectCache and reloads it, verifying the projects list survives serialisation. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
bfdc1e2093
commit
9198c481c0
13
tests/cache_lifecycle.rs
Normal file
13
tests/cache_lifecycle.rs
Normal file
@ -0,0 +1,13 @@
|
||||
use std::collections::HashMap;
|
||||
use std::path::PathBuf;
|
||||
use tmuxido::cache::ProjectCache;
|
||||
|
||||
#[test]
|
||||
fn should_save_and_reload_cache() {
|
||||
let projects = vec![PathBuf::from("/tmp/test_tmuxido_project")];
|
||||
let cache = ProjectCache::new(projects.clone(), HashMap::new());
|
||||
cache.save().unwrap();
|
||||
|
||||
let loaded = ProjectCache::load().unwrap().unwrap();
|
||||
assert_eq!(loaded.projects, projects);
|
||||
}
|
||||
65
tests/scan.rs
Normal file
65
tests/scan.rs
Normal file
@ -0,0 +1,65 @@
|
||||
use std::fs;
|
||||
use tempfile::tempdir;
|
||||
use tmuxido::config::Config;
|
||||
use tmuxido::scan_from_root;
|
||||
use tmuxido::session::SessionConfig;
|
||||
|
||||
fn make_config(max_depth: usize) -> Config {
|
||||
Config {
|
||||
paths: vec![],
|
||||
max_depth,
|
||||
cache_enabled: true,
|
||||
cache_ttl_hours: 24,
|
||||
default_session: SessionConfig { windows: vec![] },
|
||||
}
|
||||
}
|
||||
|
||||
/// `tempfile::tempdir()` creates hidden dirs (e.g. `/tmp/.tmpXXXXXX`) on this
|
||||
/// system, which `scan_from_root`'s `filter_entry` would skip. Create a
|
||||
/// visible subdirectory to use as the actual scan root.
|
||||
fn make_scan_root() -> (tempfile::TempDir, std::path::PathBuf) {
|
||||
let dir = tempdir().unwrap();
|
||||
let root = dir.path().join("scan_root");
|
||||
fs::create_dir_all(&root).unwrap();
|
||||
(dir, root)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_find_git_repos_in_temp_dir() {
|
||||
let (_dir, root) = make_scan_root();
|
||||
fs::create_dir_all(root.join("foo/.git")).unwrap();
|
||||
fs::create_dir_all(root.join("bar/.git")).unwrap();
|
||||
|
||||
let config = make_config(5);
|
||||
let (projects, _) = scan_from_root(&root, &config).unwrap();
|
||||
|
||||
assert_eq!(projects.len(), 2);
|
||||
assert!(projects.iter().any(|p| p.ends_with("foo")));
|
||||
assert!(projects.iter().any(|p| p.ends_with("bar")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_not_descend_into_hidden_dirs() {
|
||||
let (_dir, root) = make_scan_root();
|
||||
fs::create_dir_all(root.join(".hidden/repo/.git")).unwrap();
|
||||
|
||||
let config = make_config(5);
|
||||
let (projects, _) = scan_from_root(&root, &config).unwrap();
|
||||
|
||||
assert!(projects.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_respect_max_depth() {
|
||||
let (_dir, root) = make_scan_root();
|
||||
// Shallow: project/.git at depth 2 from root — found with max_depth=2
|
||||
fs::create_dir_all(root.join("project/.git")).unwrap();
|
||||
// Deep: nested/deep/project/.git at depth 4 — excluded with max_depth=2
|
||||
fs::create_dir_all(root.join("nested/deep/project/.git")).unwrap();
|
||||
|
||||
let config = make_config(2);
|
||||
let (projects, _) = scan_from_root(&root, &config).unwrap();
|
||||
|
||||
assert_eq!(projects.len(), 1);
|
||||
assert!(projects[0].ends_with("project"));
|
||||
}
|
||||
26
tests/session_config.rs
Normal file
26
tests/session_config.rs
Normal file
@ -0,0 +1,26 @@
|
||||
use std::fs;
|
||||
use tempfile::tempdir;
|
||||
use tmuxido::session::SessionConfig;
|
||||
|
||||
#[test]
|
||||
fn should_load_project_session_config() {
|
||||
let dir = tempdir().unwrap();
|
||||
let config_content = r#"
|
||||
[[windows]]
|
||||
name = "editor"
|
||||
panes = ["nvim ."]
|
||||
"#;
|
||||
fs::write(dir.path().join(".tmuxido.toml"), config_content).unwrap();
|
||||
|
||||
let result = SessionConfig::load_from_project(dir.path()).unwrap();
|
||||
assert!(result.is_some());
|
||||
let config = result.unwrap();
|
||||
assert_eq!(config.windows[0].name, "editor");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_return_none_when_no_project_config() {
|
||||
let dir = tempdir().unwrap();
|
||||
let result = SessionConfig::load_from_project(dir.path()).unwrap();
|
||||
assert!(result.is_none());
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user