char/avril

(wip) why'd you have to go and make things so complicaaaaated?

git clone https://git.t4t.associates/char/avril

Charlotte Somdeslop: consolidate public type and registry test utils7325e8c

main
1.8 KiB51 linesraw
1use std::path::Path;
2use std::process::Command;
3
4use anyhow::{Context, Result, ensure};
5
6pub fn check_public_types(manifest_dir: impl AsRef<Path>, declarations: &[&str]) -> Result<()> {
7    let root = manifest_dir.as_ref();
8    check_types(root.join("tsconfig.json"))?;
9    let directory = tempfile::tempdir()?;
10    let mut files = Vec::new();
11    for (index, declaration) in declarations.iter().enumerate() {
12        let path = directory.path().join(format!("module_{index}.d.ts"));
13        std::fs::write(&path, declaration)?;
14        files.push(path);
15    }
16    files.push(root.join("tests/public.ts"));
17    let config = directory.path().join("tsconfig.json");
18    std::fs::write(
19        &config,
20        serde_json::to_vec_pretty(&serde_json::json!({
21            "extends": root.join("../../lib/ext_build/tsconfig.harness.json"),
22            "files": files,
23        }))?,
24    )?;
25    check_types(config)
26}
27
28/// Check an extension's tsconfig with the native TypeScript backend.
29/// Returns early when AVRIL_TEST_TYPESCRIPT is unset, like the backend test suite.
30pub fn check_types(config: impl AsRef<Path>) -> Result<()> {
31    let Some(backend) = std::env::var_os("AVRIL_TEST_TYPESCRIPT").filter(|path| !path.is_empty())
32    else {
33        eprintln!("skipping: AVRIL_TEST_TYPESCRIPT is not set");
34        return Ok(());
35    };
36    let config = config.as_ref();
37    let output = Command::new(&backend)
38        .arg("--project")
39        .arg(config)
40        .args(["--noEmit", "--pretty", "false"])
41        .output()
42        .with_context(|| format!("Type-checking {} with {backend:?}", config.display()))?;
43    ensure!(
44        output.status.success(),
45        "{}:\n{}{}",
46        config.display(),
47        String::from_utf8_lossy(&output.stdout),
48        String::from_utf8_lossy(&output.stderr),
49    );
50    Ok(())
51}