Skip to main content

fallow_engine/
baseline_location.rs

1//! Warn when a saved baseline lands inside the fallow cache directory.
2//!
3//! Fallow writes a `.gitignore` with `*` into its cache directory, so Git
4//! ignores every file there. A baseline saved into that directory (for example
5//! `--save-baseline .fallow/dead-code.json`) cannot be committed, and a CI run
6//! on a fresh checkout does not find it.
7
8use std::path::Path;
9
10/// The directory that the warning recommends for committed baselines.
11const COMMITTED_BASELINE_DIR: &str = "fallow-baselines";
12
13/// The warning for a baseline at `baseline` when it is inside `cache_dir`, or
14/// `None` when it is not. A relative `baseline` is relative to the working
15/// directory, like the write itself. Symlinks are resolved on both sides.
16#[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/// Print a warning to stderr when `baseline` is inside `cache_dir`. A relative
39/// `baseline` is relative to the working directory, like the write itself.
40#[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
50/// `path` relative to `root` when it is inside it, else the full path, with
51/// `/` as the separator on every platform. A resolved path on Windows has the
52/// `\\?\` prefix, which the full path does not show.
53fn 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}