Skip to main content

rainier_filesystem/
memory.rs

1//! [`MemoryFilesystem`] — files in this process, for tests.
2
3use std::collections::HashMap;
4use std::sync::Mutex;
5
6use bytes::Bytes;
7use chrono::{DateTime, Utc};
8use rainier_support::{BoxFuture, Result};
9
10use crate::filesystem::{normalise_path, normalise_prefix, Filesystem, Metadata};
11
12/// Files held in memory.
13///
14/// What a test wants: no directory to create, no cleanup to forget, and the
15/// same [`Filesystem`] the application uses in production. It also **enforces
16/// the same path rules**, so a test cannot pass with a path the local or S3
17/// driver would refuse.
18pub struct MemoryFilesystem {
19    files: Mutex<HashMap<String, Entry>>,
20}
21
22#[derive(Debug, Clone)]
23struct Entry {
24    contents: Bytes,
25    last_modified: DateTime<Utc>,
26}
27
28impl Default for MemoryFilesystem {
29    fn default() -> Self {
30        Self::new()
31    }
32}
33
34impl MemoryFilesystem {
35    /// An empty filesystem.
36    pub fn new() -> Self {
37        Self { files: Mutex::new(HashMap::new()) }
38    }
39
40    fn lock(&self) -> std::sync::MutexGuard<'_, HashMap<String, Entry>> {
41        self.files.lock().unwrap_or_else(|poisoned| poisoned.into_inner())
42    }
43
44    /// How many files are held.
45    pub fn len(&self) -> usize {
46        self.lock().len()
47    }
48
49    /// Whether nothing is held.
50    pub fn is_empty(&self) -> bool {
51        self.len() == 0
52    }
53
54    /// Every path, sorted. For asserting on what a test wrote.
55    pub fn paths(&self) -> Vec<String> {
56        let mut paths: Vec<String> = self.lock().keys().cloned().collect();
57        paths.sort();
58        paths
59    }
60}
61
62impl Filesystem for MemoryFilesystem {
63    fn name(&self) -> &str {
64        "memory"
65    }
66
67    fn get<'a>(&'a self, path: &'a str) -> BoxFuture<'a, Result<Option<Bytes>>> {
68        Box::pin(async move {
69            let path = normalise_path(path)?;
70            Ok(self.lock().get(&path).map(|entry| entry.contents.clone()))
71        })
72    }
73
74    fn put<'a>(&'a self, path: &'a str, contents: Bytes) -> BoxFuture<'a, Result<()>> {
75        Box::pin(async move {
76            let path = normalise_path(path)?;
77            self.lock().insert(path, Entry { contents, last_modified: Utc::now() });
78            Ok(())
79        })
80    }
81
82    fn delete<'a>(&'a self, path: &'a str) -> BoxFuture<'a, Result<bool>> {
83        Box::pin(async move {
84            let path = normalise_path(path)?;
85            Ok(self.lock().remove(&path).is_some())
86        })
87    }
88
89    fn exists<'a>(&'a self, path: &'a str) -> BoxFuture<'a, Result<bool>> {
90        Box::pin(async move {
91            let path = normalise_path(path)?;
92            Ok(self.lock().contains_key(&path))
93        })
94    }
95
96    fn metadata<'a>(&'a self, path: &'a str) -> BoxFuture<'a, Result<Option<Metadata>>> {
97        Box::pin(async move {
98            let normalised = normalise_path(path)?;
99            Ok(self.lock().get(&normalised).map(|entry| Metadata {
100                path: normalised.clone(),
101                size: entry.contents.len() as u64,
102                last_modified: Some(entry.last_modified),
103            }))
104        })
105    }
106
107    fn list<'a>(&'a self, prefix: &'a str) -> BoxFuture<'a, Result<Vec<Metadata>>> {
108        Box::pin(async move {
109            let prefix = normalise_prefix(prefix)?;
110            let files = self.lock();
111
112            let mut out: Vec<Metadata> = files
113                .iter()
114                .filter(|(path, _)| {
115                    // Shallow, like the local driver: directly under the prefix
116                    // and no deeper, so the two behave the same in a test.
117                    let Some(rest) = strip_prefix(path, &prefix) else { return false };
118                    !rest.contains('/')
119                })
120                .map(|(path, entry)| Metadata {
121                    path: path.clone(),
122                    size: entry.contents.len() as u64,
123                    last_modified: Some(entry.last_modified),
124                })
125                .collect();
126
127            out.sort_by(|a, b| a.path.cmp(&b.path));
128            Ok(out)
129        })
130    }
131}
132
133impl std::fmt::Debug for MemoryFilesystem {
134    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
135        f.debug_struct("MemoryFilesystem").field("files", &self.len()).finish()
136    }
137}
138
139/// The part of `path` after `prefix`, or `None` if it is not under it.
140fn strip_prefix<'a>(path: &'a str, prefix: &str) -> Option<&'a str> {
141    if prefix.is_empty() {
142        return Some(path);
143    }
144    path.strip_prefix(prefix)?.strip_prefix('/')
145}
146
147#[cfg(test)]
148mod tests {
149    use super::*;
150    use crate::filesystem::FilesystemExt;
151
152    #[tokio::test]
153    async fn a_file_round_trips() {
154        let fs = MemoryFilesystem::new();
155        fs.put_string("a/b.txt", "hello").await.unwrap();
156
157        assert_eq!(fs.get_string("a/b.txt").await.unwrap().as_deref(), Some("hello"));
158        assert!(fs.exists("a/b.txt").await.unwrap());
159        assert_eq!(fs.size("a/b.txt").await.unwrap(), Some(5));
160        assert_eq!(fs.len(), 1);
161    }
162
163    #[tokio::test]
164    async fn a_missing_file_reads_as_none() {
165        let fs = MemoryFilesystem::new();
166
167        assert_eq!(fs.get("absent.txt").await.unwrap(), None);
168        assert!(!fs.exists("absent.txt").await.unwrap());
169    }
170
171    #[tokio::test]
172    async fn deleting_reports_whether_it_was_there() {
173        let fs = MemoryFilesystem::new();
174        fs.put_string("f.txt", "x").await.unwrap();
175
176        assert!(fs.delete("f.txt").await.unwrap());
177        assert!(!fs.delete("f.txt").await.unwrap());
178        assert!(fs.is_empty());
179    }
180
181    #[tokio::test]
182    async fn paths_are_normalised_the_same_way_as_the_real_drivers() {
183        // A test that passes here must pass against local and S3, so the same
184        // rules apply — otherwise the double is more permissive than production.
185        let fs = MemoryFilesystem::new();
186
187        fs.put_string("/a//b.txt", "x").await.unwrap();
188        assert_eq!(fs.paths(), vec!["a/b.txt"]);
189        assert_eq!(fs.get_string("a/b.txt").await.unwrap().as_deref(), Some("x"));
190
191        assert!(fs.put_string("../escape.txt", "x").await.is_err());
192        assert!(fs.get("../escape.txt").await.is_err());
193    }
194
195    #[tokio::test]
196    async fn listing_is_shallow_and_sorted() {
197        let fs = MemoryFilesystem::new();
198        fs.put_string("z.txt", "x").await.unwrap();
199        fs.put_string("a.txt", "x").await.unwrap();
200        fs.put_string("sub/deep.txt", "x").await.unwrap();
201        fs.put_string("sub/deeper/deepest.txt", "x").await.unwrap();
202
203        let root: Vec<String> = fs.list("").await.unwrap().into_iter().map(|m| m.path).collect();
204        assert_eq!(root, vec!["a.txt", "z.txt"]);
205
206        let sub: Vec<String> = fs.list("sub").await.unwrap().into_iter().map(|m| m.path).collect();
207        assert_eq!(sub, vec!["sub/deep.txt"], "one level only");
208    }
209
210    #[tokio::test]
211    async fn a_prefix_is_matched_on_a_separator_not_a_substring() {
212        // `subdirectory/x` must not be listed under the prefix `sub`.
213        let fs = MemoryFilesystem::new();
214        fs.put_string("sub/a.txt", "x").await.unwrap();
215        fs.put_string("subdirectory/b.txt", "x").await.unwrap();
216
217        let listed: Vec<String> =
218            fs.list("sub").await.unwrap().into_iter().map(|m| m.path).collect();
219        assert_eq!(listed, vec!["sub/a.txt"]);
220    }
221
222    #[tokio::test]
223    async fn copying_and_moving_behave() {
224        let fs = MemoryFilesystem::new();
225        fs.put_string("a.txt", "content").await.unwrap();
226
227        fs.copy("a.txt", "b.txt").await.unwrap();
228        assert!(fs.exists("a.txt").await.unwrap() && fs.exists("b.txt").await.unwrap());
229
230        fs.rename("a.txt", "c.txt").await.unwrap();
231        assert!(!fs.exists("a.txt").await.unwrap());
232        assert_eq!(fs.get_string("c.txt").await.unwrap().as_deref(), Some("content"));
233    }
234
235    #[tokio::test]
236    async fn there_is_no_url() {
237        // Nothing serves this, so a URL would 404.
238        assert_eq!(MemoryFilesystem::new().url("a.txt"), None);
239    }
240
241    #[tokio::test]
242    async fn the_port_is_object_safe() {
243        let fs: std::sync::Arc<dyn Filesystem> = std::sync::Arc::new(MemoryFilesystem::new());
244
245        fs.put_string("k.txt", "v").await.unwrap();
246        assert_eq!(fs.get_string("k.txt").await.unwrap().as_deref(), Some("v"));
247    }
248}