flow-toolkit 0.1.0-rc.5

Ordinary typed Rust functions and exact native data for Flow Tools, served from a Rust library's one launcher.
Documentation
//! A library's launcher reads the one command grammar `flow-toolkit-rust`
//! reads (`flow_toolkit::cli`): it runs `serve` and `extract`, refuses the
//! toolkit's other commands and the forms no command takes, and writes the
//! depfile a host's `extract` asks for.
use flow_start::Spawn as _;
use std::path::{Path, PathBuf};
use std::process::{Command, Output};

fn launcher(arguments: &[&std::ffi::OsStr]) -> Output {
    Command::new(env!("CARGO_BIN_EXE_flow-toolkit-functions-sample"))
        .args(arguments)
        .locked_output()
        .expect("the launcher starts")
}

fn refused(output: &Output) -> String {
    let stderr = String::from_utf8_lossy(&output.stderr).into_owned();
    assert_eq!(output.status.code(), Some(2), "{stderr}");
    assert!(
        !stderr.contains("panicked"),
        "a refusal, not a panic: {stderr}"
    );
    stderr
}

#[test]
fn the_launcher_refuses_the_toolkit_s_other_commands_and_forms_no_command_takes() {
    fn words(words: &[&'static str]) -> Vec<&'static std::ffi::OsStr> {
        words
            .iter()
            .map(|word| std::ffi::OsStr::new(*word))
            .collect()
    }
    for (arguments, said) in [
        (
            words(&["export", "--library", "l", "--out", "o"]),
            "export is flow-toolkit-rust's",
        ),
        (words(&["new", "ledger"]), "new is flow-toolkit-rust's"),
        (
            words(&["--contract", "/tmp/contract"]),
            "--contract is no command",
        ),
        (
            words(&["extract", "/tmp/library", "/tmp/out"]),
            "does not take /tmp/library",
        ),
        (words(&[]), "no command"),
    ] {
        let stderr = refused(&launcher(&arguments));
        assert!(stderr.contains(said), "{arguments:?}: {stderr}");
    }
}

#[test]
fn extract_writes_the_contract_and_the_depfile_a_host_asks_for() {
    let files = tempfile::TempDir::with_prefix("flow-launcher-extract").unwrap();
    let out = files.path().join("out");
    let depfile = files.path().join("depfile");
    let library = Path::new(env!("CARGO_MANIFEST_DIR"));
    let output = launcher(&[
        "extract".as_ref(),
        "--library".as_ref(),
        library.as_os_str(),
        "--out".as_ref(),
        out.as_os_str(),
        "--depfile".as_ref(),
        depfile.as_os_str(),
    ]);
    assert!(
        output.status.success(),
        "{}",
        String::from_utf8_lossy(&output.stderr)
    );
    let written = flow_abi::contract::Contract::decode(&std::fs::read(&out).unwrap()).unwrap();
    assert!(written.modules.iter().any(|module| module.path.is_empty()));
    let listed: Vec<PathBuf> = std::fs::read_to_string(&depfile)
        .unwrap()
        .lines()
        .map(PathBuf::from)
        .collect();
    assert!(listed.iter().all(|path| path.is_absolute()), "{listed:?}");
    let executable =
        std::fs::canonicalize(env!("CARGO_BIN_EXE_flow-toolkit-functions-sample")).unwrap();
    assert!(listed.contains(&executable), "its own code: {listed:?}");
    assert!(
        listed.contains(&library.join("Cargo.toml")),
        "the manifest it read: {listed:?}"
    );
}

#[cfg(unix)]
#[test]
fn the_launcher_refuses_a_path_argument_that_is_not_utf8_naming_the_argument() {
    use std::os::unix::ffi::OsStrExt as _;
    let not_utf8 = std::ffi::OsStr::from_bytes(b"/tmp/caf\xe9");
    for (arguments, named) in [
        (
            vec![
                "extract".as_ref(),
                "--library".as_ref(),
                not_utf8,
                "--out".as_ref(),
                "/tmp/out".as_ref(),
            ],
            "--library",
        ),
        (
            vec![
                "extract".as_ref(),
                "--library".as_ref(),
                "/tmp/library".as_ref(),
                "--out".as_ref(),
                not_utf8,
            ],
            "--out",
        ),
        (
            vec!["serve".as_ref(), "--contract".as_ref(), not_utf8],
            "--contract",
        ),
    ] {
        let stderr = refused(&launcher(&arguments));
        assert!(
            stderr.contains(named) && stderr.contains("UTF-8"),
            "{stderr}"
        );
    }
}