Elffuss BraidHub

kikocis/continue public

e31fcddb0dec · 959 BRawPermalink
1use std::fs::{self, File};
2use std::io::Write;
3use tempfile::tempdir;
4
5#[derive(Default)]
6pub struct TempDirBuilder {
7 files: Vec<(String, String)>,
8}
9
10impl TempDirBuilder {
11 pub fn new() -> Self {
12 Self::default()
13 }
14
15 pub fn add(&mut self, path: &str, content: &str) -> &mut Self {
16 self.files.push((path.to_string(), content.to_string()));
17 self
18 }
19
20 pub fn create(&self) -> tempfile::TempDir {
21 let temp_dir = tempdir().expect("Failed to create temp dir");
22
23 for (path, content) in &self.files {
24 let file_path = temp_dir.path().join(path);
25 if let Some(dir) = file_path.parent() {
26 fs::create_dir_all(dir).expect("Failed to create directory");
27 }
28 let mut file = File::create(&file_path).expect("Failed to create test file");
29 writeln!(file, "{content}").expect("Failed to write to test file");
30 }
31
32 temp_dir
33 }
34}