// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2022 The Gleam contributors

use std::path::PathBuf;

pub fn main() {
    println!("cargo:rerun-if-changed=cases");

    let mut module = "// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2024 The Gleam contributors

//! This file is generated by build.rs
//! Do not edit it directly, instead add new test cases to ./cases
"
    .to_string();

    let cases = PathBuf::from("./cases");

    let mut names: Vec<_> = std::fs::read_dir(&cases)
        .unwrap()
        .map(|entry| entry.unwrap().file_name().into_string().unwrap())
        .collect();
    names.sort();

    for name in names {
        let path = cases.join(&name);
        let path = path.to_str().unwrap().replace('\\', "/");
        module.push_str(
            "
#[rustfmt::skip]
#[test]
fn ",
        );
        module.push_str(&name);
        module.push_str("() {\n");
        module.push_str("    let output = crate::prepare(\"");
        module.push_str(&path);
        module.push_str(
            "\");
    insta::assert_snapshot!(
        \"",
        );
        module.push_str(&name);
        module.push_str(
            "\",
        output,
        \"",
        );
        module.push_str(&path);
        module.push_str(
            "\",
    );
}
",
        );
    }

    let out = PathBuf::from("./src/generated_tests.rs");
    std::fs::write(out, module).unwrap();
}
