File size: 8,375 Bytes
d90101d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | use std::sync::Arc;
use anyhow::Result;
use forge_app::domain::File;
use forge_app::{
DirectoryReaderInfra, EnvironmentInfra, FileDiscoveryService, Walker, WalkerInfra,
};
pub struct ForgeDiscoveryService<F> {
service: Arc<F>,
}
impl<F> ForgeDiscoveryService<F> {
pub fn new(service: Arc<F>) -> Self {
Self { service }
}
}
impl<F: EnvironmentInfra + WalkerInfra> ForgeDiscoveryService<F> {
async fn discover_with_config(&self, config: Walker) -> Result<Vec<File>> {
let files = self.service.walk(config).await?;
Ok(files
.into_iter()
.map(|file| File { path: file.path.clone(), is_dir: file.is_dir() })
.collect())
}
}
#[async_trait::async_trait]
impl<F: EnvironmentInfra + WalkerInfra + DirectoryReaderInfra + Send + Sync> FileDiscoveryService
for ForgeDiscoveryService<F>
{
async fn collect_files(&self, config: Walker) -> Result<Vec<File>> {
self.discover_with_config(config).await
}
async fn list_current_directory(&self) -> Result<Vec<File>> {
let env = self.service.get_environment();
let entries = self.service.list_directory_entries(&env.cwd).await?;
let mut files: Vec<File> = entries
.into_iter()
.filter_map(|(path, is_dir)| {
path.file_name()
.and_then(|n| n.to_str())
.map(|path| File { path: path.to_string(), is_dir })
})
.collect();
// Sort: directories first (alphabetically), then files (alphabetically)
files.sort_by(|a, b| match (a.is_dir, b.is_dir) {
(true, false) => std::cmp::Ordering::Less,
(false, true) => std::cmp::Ordering::Greater,
_ => a.path.cmp(&b.path),
});
Ok(files)
}
}
#[cfg(test)]
mod tests {
use std::path::PathBuf;
use forge_app::domain::Environment;
use forge_app::{EnvironmentInfra, WalkedFile};
use forge_domain::ConfigOperation;
use pretty_assertions::assert_eq;
use super::*;
#[derive(Clone)]
struct MockInfra {
entries: Vec<(PathBuf, bool)>,
cwd: PathBuf,
}
impl MockInfra {
fn new(entries: Vec<(&str, bool)>, cwd: &str) -> Self {
Self {
entries: entries
.into_iter()
.map(|(name, is_dir)| (PathBuf::from(cwd).join(name), is_dir))
.collect(),
cwd: PathBuf::from(cwd),
}
}
}
impl EnvironmentInfra for MockInfra {
type Config = forge_config::ForgeConfig;
fn get_environment(&self) -> Environment {
use fake::{Fake, Faker};
let mut env: Environment = Faker.fake();
env.cwd = self.cwd.clone();
env
}
fn get_config(&self) -> anyhow::Result<forge_config::ForgeConfig> {
Ok(forge_config::ForgeConfig::default())
}
async fn update_environment(&self, _ops: Vec<ConfigOperation>) -> anyhow::Result<()> {
unimplemented!()
}
fn get_env_var(&self, _key: &str) -> Option<String> {
None
}
fn get_env_vars(&self) -> std::collections::BTreeMap<String, String> {
std::collections::BTreeMap::new()
}
}
#[async_trait::async_trait]
impl WalkerInfra for MockInfra {
async fn walk(&self, _config: Walker) -> Result<Vec<WalkedFile>> {
Ok(vec![])
}
}
#[async_trait::async_trait]
impl DirectoryReaderInfra for MockInfra {
async fn list_directory_entries(
&self,
_directory: &std::path::Path,
) -> Result<Vec<(PathBuf, bool)>> {
Ok(self.entries.clone())
}
async fn read_directory_files(
&self,
_directory: &std::path::Path,
_pattern: Option<&str>,
) -> Result<Vec<(PathBuf, String)>> {
Ok(vec![])
}
}
#[tokio::test]
async fn test_list_current_directory_sorts_dirs_first() {
// Fixture: Create entries in random order
let fixture = MockInfra::new(
vec![
("file1.txt", false),
("docs", true),
("config.toml", false),
("src", true),
("README.md", false),
],
"/test",
);
let service = ForgeDiscoveryService::new(Arc::new(fixture));
// Actual: List current directory
let actual = service.list_current_directory().await.unwrap();
// Expected: Directories first (sorted), then files (sorted)
let expected = vec![
File { path: "docs".to_string(), is_dir: true },
File { path: "src".to_string(), is_dir: true },
File { path: "README.md".to_string(), is_dir: false },
File { path: "config.toml".to_string(), is_dir: false },
File { path: "file1.txt".to_string(), is_dir: false },
];
assert_eq!(actual, expected);
}
#[tokio::test]
async fn test_list_current_directory_only_files() {
// Fixture: Only files
let fixture = MockInfra::new(
vec![
("zebra.txt", false),
("alpha.txt", false),
("middle.txt", false),
],
"/test",
);
let service = ForgeDiscoveryService::new(Arc::new(fixture));
// Actual: List current directory
let actual = service.list_current_directory().await.unwrap();
// Expected: Files sorted alphabetically
let expected = vec![
File { path: "alpha.txt".to_string(), is_dir: false },
File { path: "middle.txt".to_string(), is_dir: false },
File { path: "zebra.txt".to_string(), is_dir: false },
];
assert_eq!(actual, expected);
}
#[tokio::test]
async fn test_list_current_directory_only_dirs() {
// Fixture: Only directories
let fixture = MockInfra::new(
vec![("zoo", true), ("apple", true), ("berry", true)],
"/test",
);
let service = ForgeDiscoveryService::new(Arc::new(fixture));
// Actual: List current directory
let actual = service.list_current_directory().await.unwrap();
// Expected: Directories sorted alphabetically
let expected = vec![
File { path: "apple".to_string(), is_dir: true },
File { path: "berry".to_string(), is_dir: true },
File { path: "zoo".to_string(), is_dir: true },
];
assert_eq!(actual, expected);
}
#[tokio::test]
async fn test_list_current_directory_empty() {
// Fixture: Empty directory
let fixture = MockInfra::new(vec![], "/test");
let service = ForgeDiscoveryService::new(Arc::new(fixture));
// Actual: List current directory
let actual = service.list_current_directory().await.unwrap();
// Expected: Empty list
let expected: Vec<File> = vec![];
assert_eq!(actual, expected);
}
#[tokio::test]
async fn test_list_current_directory_alphabetical_edge_cases() {
// Fixture: Test case sensitivity and special characters
let fixture = MockInfra::new(
vec![
(".gitignore", false),
("Zulu", true),
("_underscore", true),
("Apple", true),
("zebra.txt", false),
("apple.txt", false),
],
"/test",
);
let service = ForgeDiscoveryService::new(Arc::new(fixture));
// Actual: List current directory
let actual = service.list_current_directory().await.unwrap();
// Expected: Directories first (case-sensitive sort), then files
let expected = vec![
File { path: "Apple".to_string(), is_dir: true },
File { path: "Zulu".to_string(), is_dir: true },
File { path: "_underscore".to_string(), is_dir: true },
File { path: ".gitignore".to_string(), is_dir: false },
File { path: "apple.txt".to_string(), is_dir: false },
File { path: "zebra.txt".to_string(), is_dir: false },
];
assert_eq!(actual, expected);
}
}
|