sprawl-guard 0.1.0

Repository sprawl checker CLI.
use std::path::Path;

use assert_fs::TempDir;
use indoc::indoc;

use super::common::*;

mod when_explaining_rust_cfg_test_preprocessing {
    use super::*;

    #[test]
    fn it_reports_the_status_and_excluded_item_count() {
        let root = TempDir::new().unwrap();
        write_config(
            &root,
            indoc! {r#"
                [languages.rust]
                enabled = true
            "#},
        );
        write_file(
            &root,
            "src/lib.rs",
            indoc! {r#"
                fn production() {}

                #[cfg(test)]
                fn fixture() {}
            "#},
        );

        let output = sprawl_guard(&root)
            .arg("explain")
            .arg(Path::new("src/lib.rs"))
            .output()
            .unwrap();

        assert_exit_code(&output, exit_codes::SUCCESS);
        assert_eq!(
            stdout(&output),
            indoc! {"
                src/lib.rs: production source (Rust); file LOC limit: 400; directory leaf-file limit: 8; rust cfg(test): parsed; excluded top-level items: 1
            "}
        );
        assert_eq!(stderr(&output), "");
    }
}

mod when_explaining_matching_overrides {
    use super::*;

    #[test]
    fn it_reports_the_final_limits_and_override_stack() {
        let root = TempDir::new().unwrap();
        write_config(
            &root,
            indoc! {r#"
                [limits]
                max_lines_per_file = { code_non_test = 1 }
                max_leaf_files_per_directory = { production = 1 }

                [languages.rust]
                enabled = true

                [[overrides]]
                paths = ["src/**"]
                max_lines_per_file = { code_non_test = 2 }
                max_leaf_files_per_directory = { production = 3 }
                reason = "package exception"

                [[overrides]]
                paths = ["src/legacy/**"]
                max_lines_per_file = { code_non_test = 4 }
                reason = "legacy exception"
            "#},
        );
        write_file(&root, "src/legacy/lib.rs", "fn production() {}\n");

        let output = sprawl_guard(&root)
            .arg("explain")
            .arg(Path::new("src/legacy/lib.rs"))
            .output()
            .unwrap();

        assert_exit_code(&output, exit_codes::SUCCESS);
        assert_eq!(
            stdout(&output),
            indoc! {"
                src/legacy/lib.rs: production source (Rust); file LOC limit: 4; directory leaf-file limit: 3; matched file overrides: #1 paths=[src/**], #2 paths=[src/legacy/**]; matched directory overrides: #1 paths=[src/**], #2 paths=[src/legacy/**]; rust cfg(test): skipped by prefilter; excluded top-level items: 0
            "}
        );
        assert_eq!(stderr(&output), "");
    }

    #[test]
    fn it_reports_directory_only_override_matches() {
        let root = TempDir::new().unwrap();
        write_config(
            &root,
            indoc! {r#"
                [limits]
                max_leaf_files_per_directory = { production = 1 }

                [languages.rust]
                enabled = true

                [[overrides]]
                paths = ["src"]
                max_leaf_files_per_directory = { production = 3 }
                reason = "small module hub"
            "#},
        );
        write_file(&root, "src/lib.rs", "fn production() {}\n");

        let output = sprawl_guard(&root)
            .arg("explain")
            .arg(Path::new("src/lib.rs"))
            .output()
            .unwrap();

        assert_exit_code(&output, exit_codes::SUCCESS);
        assert_eq!(
            stdout(&output),
            indoc! {"
                src/lib.rs: production source (Rust); file LOC limit: 400; directory leaf-file limit: 3; matched directory overrides: #1 paths=[src]; rust cfg(test): skipped by prefilter; excluded top-level items: 0
            "}
        );
        assert_eq!(stderr(&output), "");
    }
}