#[cfg(any(test, feature = "test-support"))]
use std::collections::BTreeMap;
use std::path::Path;
#[cfg(any(test, feature = "test-support"))]
use std::path::PathBuf;
#[cfg(any(test, feature = "test-support"))]
use std::sync::{Mutex, OnceLock};
#[cfg(any(test, feature = "test-support"))]
static FILE_PROBES: OnceLock<Mutex<BTreeMap<PathBuf, usize>>> = OnceLock::new();
pub fn is_generated_file(project_root: &Path, path: &Path) -> bool {
if relative_shape_is_generated(project_root, path) {
return true;
}
let absolute = if path.is_absolute() {
path.to_path_buf()
} else {
project_root.join(path)
};
#[cfg(any(test, feature = "test-support"))]
bump_file_probe_count(&absolute);
crate::file_kind::file_head_has_generated_marker(&absolute)
}
pub fn is_generated_file_from_source(project_root: &Path, path: &Path, source: &str) -> bool {
relative_shape_is_generated(project_root, path) || source_has_generated_marker(source)
}
pub fn relative_shape_is_generated(project_root: &Path, path: &Path) -> bool {
crate::file_kind::relative_shape_is_generated(project_root, path)
}
pub fn is_generated_file_with_cached_hint(
project_root: &Path,
relative_file: &str,
cached_hint: Option<bool>,
) -> bool {
cached_hint.unwrap_or_else(|| is_generated_file(project_root, Path::new(relative_file)))
}
pub fn path_has_generated_shape(path: &Path) -> bool {
crate::file_kind::path_has_generated_shape(path)
}
#[cfg(any(test, feature = "test-support"))]
fn debug_file_probes() -> &'static Mutex<BTreeMap<PathBuf, usize>> {
FILE_PROBES.get_or_init(|| Mutex::new(BTreeMap::new()))
}
#[cfg(any(test, feature = "test-support"))]
fn bump_file_probe_count(path: &Path) {
if let Ok(mut probes) = debug_file_probes().lock() {
*probes.entry(path.to_path_buf()).or_default() += 1;
}
}
#[cfg(any(test, feature = "test-support"))]
#[doc(hidden)]
pub fn reset_file_probe_count_for_debug(project_root: &Path) {
let project_root =
std::fs::canonicalize(project_root).unwrap_or_else(|_| project_root.to_path_buf());
if let Ok(mut probes) = debug_file_probes().lock() {
probes.retain(|path, _| !path.starts_with(&project_root));
}
}
#[cfg(any(test, feature = "test-support"))]
#[doc(hidden)]
pub fn file_probe_count_for_debug(project_root: &Path) -> usize {
let project_root =
std::fs::canonicalize(project_root).unwrap_or_else(|_| project_root.to_path_buf());
debug_file_probes()
.lock()
.map(|probes| {
probes
.iter()
.filter(|(path, _)| path.starts_with(&project_root))
.map(|(_, count)| *count)
.sum()
})
.unwrap_or_default()
}
fn source_has_generated_marker(source: &str) -> bool {
crate::file_kind::head_has_generated_marker(source.as_bytes())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn path_shape_catches_conservative_generated_names() {
assert!(path_has_generated_shape(Path::new("src/gen/types.ts")));
assert!(path_has_generated_shape(Path::new(
"src/__generated__/api.ts"
)));
assert!(path_has_generated_shape(Path::new("src/foo_pb.ts")));
assert!(path_has_generated_shape(Path::new("src/foo_pb.d.ts")));
assert!(path_has_generated_shape(Path::new("src/foo.generated.ts")));
assert!(!path_has_generated_shape(Path::new("src/general.ts")));
}
#[test]
fn content_marker_only_checks_file_prefix() {
assert!(source_has_generated_marker(
"// Code generated by protoc-gen-es. DO NOT EDIT.\nexport const x = 1;"
));
assert!(source_has_generated_marker(
"/* eslint-disable */\n// emitted by codegen\nexport const x = 1;"
));
assert!(!source_has_generated_marker(
"export const handwritten = true;\n// 2\n// 3\n// 4\n// 5\n// DO NOT EDIT"
));
}
}
#[cfg(test)]
mod audit_regressions {
#[cfg(windows)]
#[test]
fn root_and_path_in_different_windows_spellings() {
use std::path::Path;
let root = Path::new(r"\\?\C:\work\gen\proj");
assert!(super::relative_shape_is_generated(
root,
Path::new(r"C:\work\gen\proj\gen\schema_pb.ts")
));
assert!(!super::relative_shape_is_generated(
root,
Path::new(r"c:\Work\Gen\Proj\src\main.ts")
));
assert!(!super::relative_shape_is_generated(
Path::new(r"C:\work\gen\proj"),
Path::new(r"\\?\C:\work\gen\proj\src\main.ts")
));
}
#[test]
fn generated_parent_is_not_project_content() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path().join("gen/myapp");
std::fs::create_dir_all(root.join("src/generated")).unwrap();
let file = root.join("src/main.rs");
std::fs::write(&file, "fn main() {}\n").unwrap();
assert!(!super::is_generated_file_from_source(
&root,
&file,
"fn main() {}"
));
assert!(super::is_generated_file_from_source(
&root,
&root.join("src/generated/types.rs"),
""
));
assert!(!super::is_generated_file(&root, &file));
assert!(!super::is_generated_file(
&root,
std::path::Path::new("src/main.rs")
));
assert!(super::is_generated_file(
&root,
&root.join("src/generated/types.rs")
));
}
}