Elffuss BraidHub

kikocis/continue public

main · 3.3 KiBRawPermalink
1use std::collections::HashMap;
2use std::fs;
3use std::io::{self, Read};
4use std::path::{Path, PathBuf};
5
6pub fn local_find_gitignores(workspace_dir: &Path) -> io::Result<HashMap<PathBuf, String>> {
7 let mut gitignores = HashMap::new();
8 for entry in fs::read_dir(workspace_dir)? {
9 let entry = entry?;
10 let path = entry.path();
11 if path.is_dir() {
12 gitignores.extend(local_find_gitignores(&path)?);
13 } else {
14 match path.file_name().and_then(|name| name.to_str()) {
15 Some(file_name) if file_name.ends_with(".gitignore") || file_name.ends_with(".continueignore") => {
16 let mut contents = String::new();
17 fs::File::open(&path)?.read_to_string(&mut contents)?;
18 gitignores.insert(path, contents);
19 }
20 _ => {}
21 }
22 }
23 }
24 Ok(gitignores)
25}
26
27fn main() -> io::Result<()> {
28 let workspace_dir = Path::new("path/to/workspace");
29 let gitignore_map = local_find_gitignores(workspace_dir)?;
30
31 // Print out the result (optional)
32 for (path, contents) in gitignore_map {
33 println!("{}:\n{}", path.display(), contents);
34 }
35
36 Ok(())
37}
38
39#[cfg(test)]
40mod tests {
41 use super::*;
42 use std::fs::{self, File};
43 use std::io::Write;
44 use tempfile::tempdir;
45
46 #[test]
47 fn test_local_find_gitignores() -> io::Result<()> {
48 // Create a temporary directory for the test
49 let temp_dir = tempdir()?;
50 let temp_path = temp_dir.path();
51
52 // Create some directories and .gitignore/.continueignore files
53 let dir_structure = [
54 ("dir1", Some(".gitignore"), "node_modules/"),
55 ("dir2", Some(".continueignore"), "target/"),
56 ("dir3", None, ""),
57 ];
58
59 for (dir, file, contents) in dir_structure.iter() {
60 let dir_path = temp_path.join(dir);
61 fs::create_dir(&dir_path)?;
62
63 if let Some(file_name) = file {
64 let file_path = dir_path.join(file_name);
65 let mut file = File::create(file_path)?;
66 writeln!(file, "{}", contents)?;
67 }
68 }
69
70 // Call the function under test
71 let gitignores = local_find_gitignores(temp_path)?;
72
73 // Verify that the returned HashMap contains the correct paths and contents
74 for (dir, file, contents) in dir_structure.iter() {
75 if let Some(file_name) = file {
76 let file_path = temp_path.join(dir).join(file_name);
77 assert!(gitignores.contains_key(&file_path));
78 assert_eq!(gitignores.get(&file_path).unwrap().trim(), *contents);
79 }
80 }
81
82 // Cleanup the temporary directory automatically
83 temp_dir.close()?;
84
85 Ok(())
86 }
87
88 #[test]
89 fn test_in_continue_repo() -> io::Result<()> {
90 // Get the current directory
91 let current_dir = std::env::current_dir()?;
92 let parent_dir = current_dir.parent().unwrap();
93 let gitignores = local_find_gitignores(&parent_dir)?;
94
95 // Verify that the returned HashMap contains the correct paths and contents
96 let top_level_gitignore = parent_dir.join(".gitignore");
97 assert!(gitignores.contains_key(&top_level_gitignore));
98 assert!(gitignores[&top_level_gitignore].contains(".tiktoken_cache"));
99
100 Ok(())
101 }
102}