yosh_plugin_manager/
lockfile.rs1use std::io::Write;
2use std::path::Path;
3
4use serde::{Deserialize, Serialize};
5use tempfile::NamedTempFile;
6
7#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
8pub struct LockFile {
9 #[serde(default)]
10 pub plugin: Vec<LockEntry>,
11}
12
13#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
14pub struct LockEntry {
15 pub name: String,
16 pub path: String,
17 #[serde(default = "default_true")]
18 pub enabled: bool,
19 #[serde(skip_serializing_if = "Option::is_none")]
20 pub capabilities: Option<Vec<String>>,
21 pub sha256: String,
22 pub source: String,
23 #[serde(skip_serializing_if = "Option::is_none")]
24 pub version: Option<String>,
25}
26
27fn default_true() -> bool {
28 true
29}
30
31pub fn load_lockfile(path: &Path) -> Result<LockFile, String> {
32 if !path.exists() {
33 return Ok(LockFile { plugin: Vec::new() });
34 }
35 let content = std::fs::read_to_string(path)
36 .map_err(|e| format!("{}: {}", path.display(), e))?;
37 toml::from_str(&content)
38 .map_err(|e| format!("{}: {}", path.display(), e))
39}
40
41pub fn save_lockfile(path: &Path, lockfile: &LockFile) -> Result<(), String> {
42 let content = toml::to_string_pretty(lockfile)
43 .map_err(|e| format!("serialize lock file: {}", e))?;
44 let parent = path.parent().ok_or_else(|| {
45 format!("{}: no parent directory", path.display())
46 })?;
47 std::fs::create_dir_all(parent)
48 .map_err(|e| format!("{}: {}", parent.display(), e))?;
49 let mut tmp = NamedTempFile::new_in(parent)
50 .map_err(|e| format!("{}: {}", path.display(), e))?;
51 tmp.write_all(content.as_bytes())
52 .map_err(|e| format!("{}: {}", path.display(), e))?;
53 tmp.persist(path)
54 .map_err(|e| format!("{}: {}", path.display(), e))?;
55 Ok(())
56}
57
58#[cfg(test)]
59mod tests {
60 use super::*;
61
62 fn sample_entry() -> LockEntry {
63 LockEntry {
64 name: "git-status".into(),
65 path: "~/.yosh/plugins/git-status/libgit_status.dylib".into(),
66 enabled: true,
67 capabilities: Some(vec!["variables:read".into(), "io".into()]),
68 sha256: "abc123".into(),
69 source: "github:user/repo".into(),
70 version: Some("1.2.3".into()),
71 }
72 }
73
74 #[test]
75 fn round_trip() {
76 let dir = tempfile::tempdir().unwrap();
77 let lock_path = dir.path().join("plugins.lock");
78 let original = LockFile { plugin: vec![sample_entry()] };
79 save_lockfile(&lock_path, &original).unwrap();
80 let loaded = load_lockfile(&lock_path).unwrap();
81 assert_eq!(original, loaded);
82 }
83
84 #[test]
85 fn load_nonexistent_returns_empty() {
86 let lf = load_lockfile(Path::new("/nonexistent/plugins.lock")).unwrap();
87 assert!(lf.plugin.is_empty());
88 }
89
90 #[test]
91 fn save_creates_parent_dirs() {
92 let dir = tempfile::tempdir().unwrap();
93 let lock_path = dir.path().join("sub/dir/plugins.lock");
94 let lf = LockFile { plugin: vec![sample_entry()] };
95 save_lockfile(&lock_path, &lf).unwrap();
96 assert!(lock_path.exists());
97 }
98
99 #[test]
100 fn local_entry_without_version() {
101 let entry = LockEntry {
102 name: "local-tool".into(),
103 path: "~/.yosh/plugins/liblocal.dylib".into(),
104 enabled: true,
105 capabilities: Some(vec!["io".into()]),
106 sha256: "def456".into(),
107 source: "local:~/.yosh/plugins/liblocal.dylib".into(),
108 version: None,
109 };
110 let dir = tempfile::tempdir().unwrap();
111 let lock_path = dir.path().join("plugins.lock");
112 let original = LockFile { plugin: vec![entry] };
113 save_lockfile(&lock_path, &original).unwrap();
114 let loaded = load_lockfile(&lock_path).unwrap();
115 assert_eq!(original, loaded);
116 assert!(loaded.plugin[0].version.is_none());
117 }
118
119 #[test]
120 fn partial_write_only_successful_entries() {
121 let dir = tempfile::tempdir().unwrap();
122 let lock_path = dir.path().join("plugins.lock");
123 let lf = LockFile {
124 plugin: vec![sample_entry()],
125 };
126 save_lockfile(&lock_path, &lf).unwrap();
127 let loaded = load_lockfile(&lock_path).unwrap();
128 assert_eq!(loaded.plugin.len(), 1);
129 }
130
131 #[test]
132 fn empty_lockfile() {
133 let dir = tempfile::tempdir().unwrap();
134 let lock_path = dir.path().join("plugins.lock");
135 let lf = LockFile { plugin: vec![] };
136 save_lockfile(&lock_path, &lf).unwrap();
137 let loaded = load_lockfile(&lock_path).unwrap();
138 assert!(loaded.plugin.is_empty());
139 }
140}