use anyhow::{Context, Result};
use git2::Repository;
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};
use crate::dependency_resolver::{
build_file_to_feature_map, collect_feature_info, resolve_feature_dependencies,
};
use crate::feature_metadata_detector::{self, FeatureMetadataMap};
use crate::features_toml_parser::{find_features_toml, read_features_toml};
use crate::git_helper::get_all_commits_by_path;
use crate::import_detector::{ImportStatement, build_file_map, scan_file_for_imports};
use crate::models::{Change, Feature, Stats};
use crate::readme_parser::read_readme_info;
fn is_documentation_directory(dir_path: &Path) -> bool {
let dir_name = dir_path
.file_name()
.and_then(|name| name.to_str())
.unwrap_or("");
let doc_dirs = ["docs", "__docs__", ".docs"];
doc_dirs.contains(&dir_name.to_lowercase().as_str())
}
fn is_inside_documentation_directory(dir_path: &Path) -> bool {
for ancestor in dir_path.ancestors().skip(1) {
if is_documentation_directory(ancestor) {
return true;
}
}
false
}
fn is_direct_subfolder_of_features(dir_path: &Path) -> bool {
if let Some(parent) = dir_path.parent()
&& let Some(parent_name) = parent.file_name().and_then(|name| name.to_str())
{
return parent_name == "features";
}
false
}
fn is_ignored_path(dir_path: &Path, ignore_paths: &[PathBuf]) -> bool {
let Ok(canonical) = fs::canonicalize(dir_path) else {
return false;
};
ignore_paths
.iter()
.any(|ignored| canonical.starts_with(ignored))
}
fn find_readme_file(dir_path: &Path) -> Option<std::path::PathBuf> {
let readme_candidates = ["README.md", "README.mdx"];
for candidate in &readme_candidates {
let readme_path = dir_path.join(candidate);
if readme_path.exists() {
return Some(readme_path);
}
}
None
}
fn feature_flag_in_readme(dir_path: &Path) -> Option<bool> {
let readme_path = find_readme_file(dir_path)?;
let content = fs::read_to_string(&readme_path).ok()?;
let stripped = content.strip_prefix("---\n")?;
let end_pos = stripped.find("\n---\n")?;
let yaml_content = &stripped[..end_pos];
let yaml_value = serde_yaml::from_str::<serde_yaml::Value>(yaml_content).ok()?;
let mapping = yaml_value.as_mapping()?;
let feature_value = mapping.get(serde_yaml::Value::String("feature".to_string()))?;
feature_value.as_bool()
}
fn has_feature_flag_in_readme(dir_path: &Path) -> bool {
feature_flag_in_readme(dir_path) == Some(true)
}
fn has_feature_false_in_readme(dir_path: &Path) -> bool {
feature_flag_in_readme(dir_path) == Some(false)
}
fn is_feature_directory(dir_path: &Path) -> bool {
if is_documentation_directory(dir_path) || is_inside_documentation_directory(dir_path) {
return false;
}
if has_feature_false_in_readme(dir_path) {
return false;
}
if is_direct_subfolder_of_features(dir_path) {
return true;
}
has_feature_flag_in_readme(dir_path)
}
pub fn list_files_recursive(dir: &Path, ignore_paths: &[PathBuf]) -> Result<Vec<Feature>> {
let feature_metadata =
feature_metadata_detector::scan_directory_for_feature_metadata(dir).unwrap_or_default();
let mut features =
list_files_recursive_impl(dir, dir, None, None, &feature_metadata, ignore_paths)?;
populate_dependencies(&mut features, dir)?;
Ok(features)
}
pub fn list_files_recursive_with_changes(
dir: &Path,
ignore_paths: &[PathBuf],
) -> Result<Vec<Feature>> {
let all_commits = get_all_commits_by_path(dir).unwrap_or_default();
let feature_metadata =
feature_metadata_detector::scan_directory_for_feature_metadata(dir).unwrap_or_default();
let mut features = list_files_recursive_impl(
dir,
dir,
Some(&all_commits),
None,
&feature_metadata,
ignore_paths,
)?;
populate_dependencies(&mut features, dir)?;
Ok(features)
}
fn populate_dependencies(features: &mut [Feature], base_path: &Path) -> Result<()> {
let file_map = build_file_map(base_path);
let mut feature_info_list = Vec::new();
collect_feature_info(features, None, &mut feature_info_list);
let file_to_feature_map = build_file_to_feature_map(&feature_info_list, base_path);
let mut feature_path_to_name_map = HashMap::new();
for info in &feature_info_list {
feature_path_to_name_map.insert(info.path.to_string_lossy().to_string(), info.name.clone());
}
let mut feature_imports: HashMap<String, Vec<ImportStatement>> = HashMap::new();
for feature_info in &feature_info_list {
let feature_path = base_path.join(&feature_info.path);
let imports = scan_feature_directory_for_imports(&feature_path);
feature_imports.insert(feature_info.path.to_string_lossy().to_string(), imports);
}
populate_dependencies_recursive(
features,
base_path,
&feature_imports,
&file_to_feature_map,
&feature_path_to_name_map,
&file_map,
);
Ok(())
}
fn scan_feature_directory_for_imports(feature_path: &Path) -> Vec<ImportStatement> {
let mut all_imports = Vec::new();
if let Ok(entries) = fs::read_dir(feature_path) {
let mut paths: Vec<_> = entries.flatten().map(|e| e.path()).collect();
paths.sort_by(|a, b| {
let name_a = a.file_name().unwrap_or_default().to_string_lossy();
let name_b = b.file_name().unwrap_or_default().to_string_lossy();
name_a.cmp(&name_b)
});
for path in paths {
if is_documentation_directory(&path) {
continue;
}
if path.is_file() {
if let Ok(imports) = scan_file_for_imports(&path) {
all_imports.extend(imports);
}
} else if path.is_dir() {
let dir_name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
if dir_name == "features" {
continue;
}
if is_feature_directory(&path) {
continue;
}
let nested_imports = scan_feature_directory_for_imports(&path);
all_imports.extend(nested_imports);
}
}
}
all_imports
}
fn populate_dependencies_recursive(
features: &mut [Feature],
base_path: &Path,
feature_imports: &HashMap<String, Vec<ImportStatement>>,
file_to_feature_map: &HashMap<std::path::PathBuf, String>,
feature_path_to_name_map: &HashMap<String, String>,
file_map: &HashMap<String, std::path::PathBuf>,
) {
for feature in features {
if let Some(imports) = feature_imports.get(&feature.path) {
let feature_path = std::path::PathBuf::from(&feature.path);
let dependencies = resolve_feature_dependencies(
&feature.name,
&feature_path,
base_path,
imports,
file_to_feature_map,
feature_path_to_name_map,
file_map,
);
feature.dependencies = dependencies;
}
if !feature.features.is_empty() {
populate_dependencies_recursive(
&mut feature.features,
base_path,
feature_imports,
file_to_feature_map,
feature_path_to_name_map,
file_map,
);
}
}
}
fn read_decision_files(feature_path: &Path) -> Result<Vec<String>> {
let mut decisions = Vec::new();
let decision_paths = [
feature_path.join(".docs").join("decisions"),
feature_path.join("__docs__").join("decisions"),
];
for decisions_dir in &decision_paths {
if decisions_dir.exists() && decisions_dir.is_dir() {
let entries = fs::read_dir(decisions_dir).with_context(|| {
format!(
"could not read decisions directory `{}`",
decisions_dir.display()
)
})?;
let mut decision_paths_vec = Vec::new();
for entry in entries {
let entry = entry?;
let path = entry.path();
if path.is_file()
&& let Some(file_name) = path.file_name()
{
let file_name_str = file_name.to_string_lossy();
if file_name_str.ends_with(".md") && file_name_str != "README.md" {
decision_paths_vec.push(path);
}
}
}
decision_paths_vec.sort_by(|a, b| {
let name_a = a.file_name().unwrap_or_default().to_string_lossy();
let name_b = b.file_name().unwrap_or_default().to_string_lossy();
name_a.cmp(&name_b)
});
for path in decision_paths_vec {
let content = fs::read_to_string(&path).with_context(|| {
format!("could not read decision file `{}`", path.display())
})?;
decisions.push(content);
}
break; }
}
Ok(decisions)
}
fn count_files(feature_path: &Path, nested_feature_paths: &[String]) -> usize {
let mut file_count = 0;
if let Ok(entries) = fs::read_dir(feature_path) {
let mut paths: Vec<_> = entries.flatten().map(|e| e.path()).collect();
paths.sort_by(|a, b| {
let name_a = a.file_name().unwrap_or_default().to_string_lossy();
let name_b = b.file_name().unwrap_or_default().to_string_lossy();
name_a.cmp(&name_b)
});
for path in paths {
let path_str = path.to_string_lossy().to_string();
if is_documentation_directory(&path) {
continue;
}
if nested_feature_paths
.iter()
.any(|nfp| path_str.starts_with(nfp))
{
continue;
}
if path.is_file() {
file_count += 1;
} else if path.is_dir() {
file_count += count_files(&path, nested_feature_paths);
}
}
}
file_count
}
fn count_lines(feature_path: &Path, nested_feature_paths: &[String]) -> usize {
let mut line_count = 0;
if let Ok(entries) = fs::read_dir(feature_path) {
let mut paths: Vec<_> = entries.flatten().map(|e| e.path()).collect();
paths.sort_by(|a, b| {
let name_a = a.file_name().unwrap_or_default().to_string_lossy();
let name_b = b.file_name().unwrap_or_default().to_string_lossy();
name_a.cmp(&name_b)
});
for path in paths {
let path_str = path.to_string_lossy().to_string();
if is_documentation_directory(&path) {
continue;
}
if nested_feature_paths
.iter()
.any(|nfp| path_str.starts_with(nfp))
{
continue;
}
if path.is_file() {
if let Ok(content) = fs::read_to_string(&path) {
line_count += content.lines().count();
}
} else if path.is_dir() {
line_count += count_lines(&path, nested_feature_paths);
}
}
}
line_count
}
fn count_todos(feature_path: &Path, nested_feature_paths: &[String]) -> usize {
let mut todo_count = 0;
if let Ok(entries) = fs::read_dir(feature_path) {
let mut paths: Vec<_> = entries.flatten().map(|e| e.path()).collect();
paths.sort_by(|a, b| {
let name_a = a.file_name().unwrap_or_default().to_string_lossy();
let name_b = b.file_name().unwrap_or_default().to_string_lossy();
name_a.cmp(&name_b)
});
for path in paths {
let path_str = path.to_string_lossy().to_string();
if is_documentation_directory(&path) {
continue;
}
if nested_feature_paths
.iter()
.any(|nfp| path_str.starts_with(nfp))
{
continue;
}
if path.is_file() {
if let Ok(content) = fs::read_to_string(&path) {
for line in content.lines() {
let line_upper = line.to_uppercase();
if line_upper.contains("TODO") {
todo_count += 1;
}
}
}
} else if path.is_dir() {
todo_count += count_todos(&path, nested_feature_paths);
}
}
}
todo_count
}
fn get_commit_affected_paths(repo: &Repository, commit_hash: &str) -> Vec<String> {
let Ok(oid) = git2::Oid::from_str(commit_hash) else {
return Vec::new();
};
let Ok(commit) = repo.find_commit(oid) else {
return Vec::new();
};
let mut paths = Vec::new();
if commit.parent_count() == 0 {
if let Ok(tree) = commit.tree() {
collect_all_tree_paths(repo, &tree, "", &mut paths);
}
return paths;
}
let Ok(tree) = commit.tree() else {
return Vec::new();
};
let Ok(parent) = commit.parent(0) else {
return Vec::new();
};
let Ok(parent_tree) = parent.tree() else {
return Vec::new();
};
if let Ok(diff) = repo.diff_tree_to_tree(Some(&parent_tree), Some(&tree), None) {
let _ = diff.foreach(
&mut |delta, _| {
if let Some(path) = delta.new_file().path()
&& let Some(path_str) = path.to_str()
{
paths.push(path_str.to_string());
}
if let Some(path) = delta.old_file().path()
&& let Some(path_str) = path.to_str()
&& !paths.contains(&path_str.to_string())
{
paths.push(path_str.to_string());
}
true
},
None,
None,
None,
);
}
paths
}
fn collect_all_tree_paths(
repo: &Repository,
tree: &git2::Tree,
prefix: &str,
paths: &mut Vec<String>,
) {
for entry in tree.iter() {
if let Some(name) = entry.name() {
let path = if prefix.is_empty() {
name.to_string()
} else {
format!("{}/{}", prefix, name)
};
paths.push(path.clone());
if entry.kind() == Some(git2::ObjectType::Tree)
&& let Ok(obj) = entry.to_object(repo)
&& let Ok(subtree) = obj.peel_to_tree()
{
collect_all_tree_paths(repo, &subtree, &path, paths);
}
}
}
}
fn compute_stats_from_changes(
changes: &[Change],
feature_path: &Path,
nested_features: &[Feature],
) -> Option<Stats> {
if changes.is_empty() {
return None;
}
let nested_feature_paths: Vec<String> =
nested_features.iter().map(|f| f.path.clone()).collect();
let repo = Repository::discover(feature_path).ok();
let feature_relative_path = if let Some(ref r) = repo {
if let Ok(canonical_path) = std::fs::canonicalize(feature_path) {
if let Some(workdir) = r.workdir() {
canonical_path
.strip_prefix(workdir)
.ok()
.map(|p| p.to_string_lossy().to_string())
} else {
None
}
} else {
None
}
} else {
None
};
let filtered_changes: Vec<&Change> = changes
.iter()
.filter(|change| {
let Some(ref r) = repo else {
return true;
};
let Some(ref feature_rel_path) = feature_relative_path else {
return true;
};
let affected_files = get_commit_affected_paths(r, &change.hash);
affected_files.iter().any(|file_path| {
let in_feature = file_path.starts_with(feature_rel_path);
let in_nested = nested_feature_paths.iter().any(|nested_path| {
if let Ok(nested_canonical) = std::fs::canonicalize(nested_path)
&& let Some(workdir) = r.workdir()
&& let Ok(nested_rel) = nested_canonical.strip_prefix(workdir)
{
let nested_rel_str = nested_rel.to_string_lossy();
return file_path.starts_with(nested_rel_str.as_ref());
}
false
});
in_feature && !in_nested
})
})
.collect();
let mut commits = std::collections::BTreeMap::new();
commits.insert(
"total_commits".to_string(),
serde_json::json!(filtered_changes.len()),
);
let mut authors_count: HashMap<String, usize> = HashMap::new();
for change in &filtered_changes {
*authors_count.entry(change.author_name.clone()).or_insert(0) += 1;
}
commits.insert(
"authors_count".to_string(),
serde_json::json!(authors_count),
);
let mut count_by_type: HashMap<String, usize> = HashMap::new();
for change in &filtered_changes {
let commit_type = extract_commit_type(&change.title);
*count_by_type.entry(commit_type).or_insert(0) += 1;
}
commits.insert(
"count_by_type".to_string(),
serde_json::json!(count_by_type),
);
if let Some(first) = filtered_changes.first() {
commits.insert(
"first_commit_date".to_string(),
serde_json::json!(first.date.clone()),
);
}
if let Some(last) = filtered_changes.last() {
commits.insert(
"last_commit_date".to_string(),
serde_json::json!(last.date.clone()),
);
}
let files_count = count_files(feature_path, &nested_feature_paths);
let lines_count = count_lines(feature_path, &nested_feature_paths);
let todos_count = count_todos(feature_path, &nested_feature_paths);
Some(Stats {
files_count: Some(files_count),
lines_count: Some(lines_count),
todos_count: Some(todos_count),
commits,
coverage: None,
})
}
fn extract_commit_type(title: &str) -> String {
let known_types = [
"feat", "fix", "docs", "style", "refactor", "perf", "test", "build", "ci", "chore",
"revert",
];
if let Some(colon_pos) = title.find(':') {
let prefix = &title[..colon_pos];
let type_part = if let Some(paren_pos) = prefix.find('(') {
&prefix[..paren_pos]
} else {
prefix
};
let type_part = type_part.trim().to_lowercase();
if known_types.contains(&type_part.as_str()) {
return type_part;
}
}
"other".to_string()
}
fn process_feature_directory(
path: &Path,
base_path: &Path,
name: &str,
changes_map: Option<&HashMap<String, Vec<Change>>>,
parent_owner: Option<&str>,
feature_metadata_map: &FeatureMetadataMap,
ignore_paths: &[PathBuf],
) -> Result<Feature> {
let (title, owner, description, mut meta) = if let Some(toml_path) = find_features_toml(path) {
if let Ok(toml_data) = read_features_toml(&toml_path) {
(
toml_data.name,
toml_data.owner.unwrap_or_default(),
toml_data.description.unwrap_or_default(),
toml_data.meta,
)
} else {
(
None,
String::new(),
String::new(),
std::collections::BTreeMap::new(),
)
}
} else {
let readme_info = if let Some(readme_path) = find_readme_file(path) {
read_readme_info(&readme_path)?
} else {
use crate::readme_parser::ReadmeInfo;
ReadmeInfo {
title: None,
owner: "".to_string(),
description: "".to_string(),
meta: std::collections::BTreeMap::new(),
}
};
(
readme_info.title,
readme_info.owner,
readme_info.description,
readme_info.meta,
)
};
meta.remove("feature");
let relative_path = path
.strip_prefix(base_path)
.unwrap_or(path)
.to_string_lossy()
.to_string();
if let Some(metadata_map) = feature_metadata_map.get(&relative_path) {
for (metadata_key, flags) in metadata_map {
let flags_json: Vec<serde_json::Value> = flags
.iter()
.map(|flag_map| {
let json_map: serde_json::Map<String, serde_json::Value> = flag_map
.iter()
.map(|(k, v)| (k.clone(), serde_json::Value::String(v.clone())))
.collect();
serde_json::Value::Object(json_map)
})
.collect();
meta.entry(metadata_key.clone())
.and_modify(|existing| {
if let serde_json::Value::Array(arr) = existing {
arr.extend(flags_json.clone());
}
})
.or_insert_with(|| serde_json::Value::Array(flags_json));
}
}
let changes = if let Some(map) = changes_map {
get_changes_for_path(path, map).unwrap_or_default()
} else {
Vec::new()
};
let decisions = read_decision_files(path).unwrap_or_default();
let (actual_owner, is_owner_inherited) = if owner.is_empty() {
if let Some(parent) = parent_owner {
(parent.to_string(), true)
} else {
("".to_string(), false)
}
} else {
(owner.clone(), false)
};
let nested_features_path = path.join("features");
let mut nested_features = if nested_features_path.exists()
&& nested_features_path.is_dir()
&& !is_ignored_path(&nested_features_path, ignore_paths)
{
list_files_recursive_impl(
&nested_features_path,
base_path,
changes_map,
Some(&actual_owner),
feature_metadata_map,
ignore_paths,
)
.unwrap_or_default()
} else {
Vec::new()
};
let entries = fs::read_dir(path)
.with_context(|| format!("could not read directory `{}`", path.display()))?;
let mut entries: Vec<_> = entries.collect::<Result<_, _>>()?;
entries.sort_by_key(|entry| entry.path());
for entry in entries {
let entry_path = entry.path();
let entry_name = entry_path.file_name().unwrap().to_string_lossy();
if entry_path.is_dir()
&& entry_name != "features" && !is_documentation_directory(&entry_path)
&& !is_ignored_path(&entry_path, ignore_paths)
{
if has_feature_flag_in_readme(&entry_path) {
let nested_feature = process_feature_directory(
&entry_path,
base_path,
&entry_name,
changes_map,
Some(&actual_owner),
feature_metadata_map,
ignore_paths,
)?;
nested_features.push(nested_feature);
} else {
let deeper_features = list_files_recursive_impl(
&entry_path,
base_path,
changes_map,
Some(&actual_owner),
feature_metadata_map,
ignore_paths,
)?;
nested_features.extend(deeper_features);
}
}
}
let nested_feature_paths: Vec<String> =
nested_features.iter().map(|f| f.path.clone()).collect();
let files_count = count_files(path, &nested_feature_paths);
let lines_count = count_lines(path, &nested_feature_paths);
let todos_count = count_todos(path, &nested_feature_paths);
let stats =
if let Some(change_stats) = compute_stats_from_changes(&changes, path, &nested_features) {
Some(change_stats)
} else {
Some(Stats {
files_count: Some(files_count),
lines_count: Some(lines_count),
todos_count: Some(todos_count),
commits: std::collections::BTreeMap::new(),
coverage: None,
})
};
let relative_path = path
.strip_prefix(base_path)
.unwrap_or(path)
.to_string_lossy()
.to_string();
Ok(Feature {
name: title.unwrap_or_else(|| name.to_string()),
description,
owner: actual_owner,
is_owner_inherited,
path: relative_path,
features: nested_features,
meta,
changes,
decisions,
stats,
dependencies: Vec::new(), })
}
fn list_files_recursive_impl(
dir: &Path,
base_path: &Path,
changes_map: Option<&HashMap<String, Vec<Change>>>,
parent_owner: Option<&str>,
feature_metadata_map: &FeatureMetadataMap,
ignore_paths: &[PathBuf],
) -> Result<Vec<Feature>> {
let entries = fs::read_dir(dir)
.with_context(|| format!("could not read directory `{}`", dir.display()))?;
let mut entries: Vec<_> = entries.collect::<Result<_, _>>()?;
entries.sort_by_key(|entry| entry.path());
let mut features: Vec<Feature> = Vec::new();
for entry in entries {
let path = entry.path();
let name = path.file_name().unwrap().to_string_lossy();
if path.is_dir() {
if is_ignored_path(&path, ignore_paths) {
continue;
}
if is_feature_directory(&path) {
let feature = process_feature_directory(
&path,
base_path,
&name,
changes_map,
parent_owner,
feature_metadata_map,
ignore_paths,
)?;
features.push(feature);
} else if !is_documentation_directory(&path)
&& !is_inside_documentation_directory(&path)
{
let new_features = list_files_recursive_impl(
&path,
base_path,
changes_map,
parent_owner,
feature_metadata_map,
ignore_paths,
)?;
features.extend(new_features);
}
}
}
Ok(features)
}
fn get_changes_for_path(
path: &Path,
changes_map: &HashMap<String, Vec<Change>>,
) -> Result<Vec<Change>> {
let canonical_path = std::fs::canonicalize(path)?;
let repo = Repository::discover(path)?;
let repo_workdir = repo
.workdir()
.context("repository has no working directory")?;
let relative_path = canonical_path
.strip_prefix(repo_workdir)
.context("path is not within repository")?;
let relative_path_str = relative_path.to_string_lossy().to_string();
Ok(changes_map
.get(&relative_path_str)
.cloned()
.unwrap_or_default())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_extract_commit_type() {
assert_eq!(extract_commit_type("feat: add new feature"), "feat");
assert_eq!(extract_commit_type("fix: resolve bug"), "fix");
assert_eq!(extract_commit_type("docs: update README"), "docs");
assert_eq!(extract_commit_type("style: format code"), "style");
assert_eq!(
extract_commit_type("refactor: improve structure"),
"refactor"
);
assert_eq!(extract_commit_type("perf: optimize performance"), "perf");
assert_eq!(extract_commit_type("test: add unit tests"), "test");
assert_eq!(extract_commit_type("build: update dependencies"), "build");
assert_eq!(extract_commit_type("ci: fix CI pipeline"), "ci");
assert_eq!(extract_commit_type("chore: update gitignore"), "chore");
assert_eq!(
extract_commit_type("revert: undo previous commit"),
"revert"
);
assert_eq!(extract_commit_type("feat(auth): add login"), "feat");
assert_eq!(
extract_commit_type("fix(api): resolve endpoint issue"),
"fix"
);
assert_eq!(
extract_commit_type("docs(readme): update instructions"),
"docs"
);
assert_eq!(extract_commit_type("FEAT: uppercase type"), "feat");
assert_eq!(extract_commit_type("Fix: mixed case"), "fix");
assert_eq!(extract_commit_type("DOCS: all caps"), "docs");
assert_eq!(extract_commit_type("random commit message"), "other");
assert_eq!(extract_commit_type("update: not conventional"), "other");
assert_eq!(
extract_commit_type("feature: close but not standard"),
"other"
);
assert_eq!(extract_commit_type("no colon here"), "other");
assert_eq!(extract_commit_type(""), "other");
assert_eq!(extract_commit_type("feat:no space after colon"), "feat");
assert_eq!(extract_commit_type("feat : extra spaces"), "feat");
assert_eq!(
extract_commit_type("feat(scope)(weird): nested parens"),
"feat"
);
}
fn write_readme(dir: &Path, content: &str) {
fs::write(dir.join("README.md"), content).unwrap();
}
#[test]
fn test_feature_flag_in_readme_true() {
let temp_dir = tempfile::TempDir::new().unwrap();
write_readme(temp_dir.path(), "---\nfeature: true\n---\n\n# Feature\n");
assert_eq!(feature_flag_in_readme(temp_dir.path()), Some(true));
assert!(has_feature_flag_in_readme(temp_dir.path()));
assert!(!has_feature_false_in_readme(temp_dir.path()));
}
#[test]
fn test_feature_flag_in_readme_false() {
let temp_dir = tempfile::TempDir::new().unwrap();
write_readme(temp_dir.path(), "---\nfeature: false\n---\n\n# Feature\n");
assert_eq!(feature_flag_in_readme(temp_dir.path()), Some(false));
assert!(!has_feature_flag_in_readme(temp_dir.path()));
assert!(has_feature_false_in_readme(temp_dir.path()));
}
#[test]
fn test_feature_flag_in_readme_missing() {
let temp_dir = tempfile::TempDir::new().unwrap();
assert_eq!(feature_flag_in_readme(temp_dir.path()), None);
assert!(!has_feature_flag_in_readme(temp_dir.path()));
assert!(!has_feature_false_in_readme(temp_dir.path()));
write_readme(temp_dir.path(), "---\nowner: team\n---\n\n# Feature\n");
assert_eq!(feature_flag_in_readme(temp_dir.path()), None);
assert!(!has_feature_flag_in_readme(temp_dir.path()));
assert!(!has_feature_false_in_readme(temp_dir.path()));
}
#[test]
fn test_is_feature_directory_direct_subfolder_of_features() {
let temp_dir = tempfile::TempDir::new().unwrap();
let features_dir = temp_dir.path().join("features");
let feature_dir = features_dir.join("my-feature");
fs::create_dir_all(&feature_dir).unwrap();
assert!(is_feature_directory(&feature_dir));
}
#[test]
fn test_is_feature_directory_skips_when_feature_false_even_under_features_folder() {
let temp_dir = tempfile::TempDir::new().unwrap();
let features_dir = temp_dir.path().join("features");
let feature_dir = features_dir.join("my-feature");
fs::create_dir_all(&feature_dir).unwrap();
write_readme(&feature_dir, "---\nfeature: false\n---\n\n# My Feature\n");
assert!(!is_feature_directory(&feature_dir));
}
#[test]
fn test_is_feature_directory_readme_feature_true_outside_features_folder() {
let temp_dir = tempfile::TempDir::new().unwrap();
let feature_dir = temp_dir.path().join("some-dir");
fs::create_dir_all(&feature_dir).unwrap();
write_readme(&feature_dir, "---\nfeature: true\n---\n\n# Some Dir\n");
assert!(is_feature_directory(&feature_dir));
}
#[test]
fn test_is_feature_directory_readme_feature_false_outside_features_folder() {
let temp_dir = tempfile::TempDir::new().unwrap();
let feature_dir = temp_dir.path().join("some-dir");
fs::create_dir_all(&feature_dir).unwrap();
write_readme(&feature_dir, "---\nfeature: false\n---\n\n# Some Dir\n");
assert!(!is_feature_directory(&feature_dir));
}
#[test]
fn test_list_files_recursive_skips_feature_but_keeps_sub_children() {
let temp_dir = tempfile::TempDir::new().unwrap();
let base = temp_dir.path();
let features_dir = base.join("features");
let skipped_dir = features_dir.join("skipped-feature");
fs::create_dir_all(&skipped_dir).unwrap();
write_readme(
&skipped_dir,
"---\nfeature: false\nowner: skipped-owner\n---\n\n# Skipped Feature\n",
);
let nested_via_features_folder = skipped_dir.join("features").join("child-a");
fs::create_dir_all(&nested_via_features_folder).unwrap();
let nested_via_readme = skipped_dir.join("child-b");
fs::create_dir_all(&nested_via_readme).unwrap();
write_readme(
&nested_via_readme,
"---\nfeature: true\nowner: child-b-owner\n---\n\n# Child B\n",
);
let features = list_files_recursive(base, &[]).unwrap();
fn collect_names(features: &[Feature], out: &mut Vec<String>) {
for feature in features {
out.push(feature.name.clone());
collect_names(&feature.features, out);
}
}
let mut names = Vec::new();
collect_names(&features, &mut names);
assert!(!names.contains(&"skipped-feature".to_string()));
assert!(!names.contains(&"Skipped Feature".to_string()));
assert!(names.contains(&"child-a".to_string()));
let child_b = features
.iter()
.find(|f| f.name == "Child B")
.expect("child-b feature should be present");
assert_eq!(child_b.owner, "child-b-owner");
}
}