fallow_engine/
baseline_location.rs1use std::path::Path;
9
10const COMMITTED_BASELINE_DIR: &str = "fallow-baselines";
12
13#[must_use]
17fn cache_dir_warning(baseline: &Path, cache_dir: &Path, root: &Path) -> Option<String> {
18 let absolute =
19 std::env::current_dir().map_or_else(|_| baseline.to_path_buf(), |cwd| cwd.join(baseline));
20 let resolved = crate::write_guard::resolve(&absolute);
21 let cache = crate::write_guard::resolve(cache_dir);
22 if !resolved.starts_with(&cache) {
23 return None;
24 }
25 let name = baseline
26 .file_name()
27 .map_or_else(|| "baseline.json".into(), |name| name.to_string_lossy());
28 let root = crate::write_guard::resolve(root);
29 Some(format!(
30 "Warning: the baseline {} is in the fallow cache directory {}. \
31 Fallow tells Git to ignore all files in this directory, so you cannot commit the baseline. \
32 Save the baseline to a path that you commit, for example {COMMITTED_BASELINE_DIR}/{name}.",
33 display_from(&resolved, &root),
34 display_from(&cache, &root),
35 ))
36}
37
38#[expect(
41 clippy::print_stderr,
42 reason = "the warning is for the person who runs the save, next to the save note"
43)]
44pub fn warn_if_in_cache_dir(baseline: &Path, cache_dir: &Path, root: &Path) {
45 if let Some(warning) = cache_dir_warning(baseline, cache_dir, root) {
46 eprintln!("{warning}");
47 }
48}
49
50fn display_from(path: &Path, root: &Path) -> String {
54 fallow_types::path_util::display_relative(dunce::simplified(root), dunce::simplified(path))
55}
56
57#[cfg(test)]
58mod tests {
59 use super::*;
60
61 #[test]
62 fn baseline_in_the_cache_dir_gets_a_warning() {
63 let dir = tempfile::tempdir().expect("tempdir");
64 let root = dir.path();
65 let cache = root.join(".fallow");
66 let warning = cache_dir_warning(&cache.join("dead-code.json"), &cache, root)
67 .expect("a baseline in the cache directory is flagged");
68 assert!(
69 warning.contains("the baseline .fallow/dead-code.json is in"),
70 "{warning}"
71 );
72 assert!(warning.contains("cache directory .fallow."), "{warning}");
73 assert!(
74 warning.contains("fallow-baselines/dead-code.json"),
75 "{warning}"
76 );
77 }
78
79 #[test]
80 fn displayed_paths_use_forward_slashes() {
81 let root = Path::new("/project");
82 let baseline = root.join(".fallow\\dead-code.json");
83 assert_eq!(display_from(&baseline, root), ".fallow/dead-code.json");
84 }
85
86 #[test]
87 fn baseline_in_a_nested_missing_cache_subdir_gets_a_warning() {
88 let dir = tempfile::tempdir().expect("tempdir");
89 let root = dir.path();
90 let cache = root.join(".fallow");
91 let baseline = cache.join("baselines").join("dupes.json");
92 assert!(cache_dir_warning(&baseline, &cache, root).is_some());
93 }
94
95 #[test]
96 fn baseline_outside_the_cache_dir_gets_no_warning() {
97 let dir = tempfile::tempdir().expect("tempdir");
98 let root = dir.path();
99 let cache = root.join(".fallow");
100 for path in [
101 "fallow-baselines/dead-code.json",
102 ".fallow-baseline.json",
103 "dead-code.json",
104 ] {
105 assert_eq!(
106 cache_dir_warning(&root.join(path), &cache, root),
107 None,
108 "{path}"
109 );
110 }
111 }
112
113 #[test]
114 fn a_dot_dot_path_out_of_the_cache_dir_gets_no_warning() {
115 let dir = tempfile::tempdir().expect("tempdir");
116 let root = dir.path();
117 let cache = root.join(".fallow");
118 let baseline = cache
119 .join("..")
120 .join("fallow-baselines")
121 .join("health.json");
122 assert_eq!(cache_dir_warning(&baseline, &cache, root), None);
123 }
124
125 #[test]
126 fn a_custom_cache_dir_is_honoured() {
127 let dir = tempfile::tempdir().expect("tempdir");
128 let root = dir.path();
129 let cache = root.join(".cache").join("fallow");
130 assert!(cache_dir_warning(&cache.join("x.json"), &cache, root).is_some());
131 assert_eq!(
132 cache_dir_warning(&root.join(".fallow/x.json"), &cache, root),
133 None
134 );
135 }
136}