Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/gunnar

xtask/tests/iso_installer_run.rs

//! `cargo xtask iso-installer` (`xtask/src/isoinstaller.rs`): everything it refuses
//! BEFORE it compiles anything. A malformed argument, a per-box `hub_conf` on a medium
//! for the whole site, a linked worktree, and no dwarves checkout to compose with.
//!
//! Its own test binary: one test sets and clears `$DWARVES_ROOT`.

use std::path::PathBuf;

use xtask::isoinstaller::{dwarves_root, run};

struct Scratch(PathBuf);

impl Drop for Scratch {
    fn drop(&mut self) {
        let _ = std::fs::remove_dir_all(&self.0);
    }
}

fn args(a: &[&str]) -> Vec<String> {
    a.iter().map(|s| s.to_string()).collect()
}

fn refused(root: &std::path::Path, a: &[&str], want: &str) {
    match run(root, &args(a)) {
        Ok(v) => panic!("{a:?} ran ({v})"),
        Err(e) => assert!(e.contains(want), "{a:?}: {e}"),
    }
}

#[test]
fn nothing_is_compiled_for_a_request_the_verb_refuses() {
    let base = std::env::temp_dir().join(format!("gunnar-iso-run-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&base);
    let root = base.join("gunnar");
    std::fs::create_dir_all(&root).unwrap();
    let _s = Scratch(base.clone());
    std::env::remove_var("DWARVES_ROOT");

    refused(&root, &["--frob"], "unknown argument \"--frob\"");
    for flag in ["--variant", "--opt", "--param", "--build", "--unenrolled"] {
        refused(&root, &[flag], &format!("{flag} needs a value"));
    }

    // a hub_conf that carries a box's own address, inline or from a file
    refused(
        &root,
        &["--param", "hub_conf=[Interface]\\nAddress = 10.13.8.2/24"],
        "REFUSED [hub-conf]",
    );
    let conf = base.join("hub.conf");
    std::fs::write(&conf, "[Peer]\nPublicKey = x\nAddress = 10.13.8.2\n").unwrap();
    refused(
        &root,
        &["--param", &format!("site.hub_conf=@{}", conf.display())],
        "declares `Address = 10.13.8.2`",
    );
    refused(
        &root,
        &[
            "--param",
            &format!("hub_conf=@{}", base.join("absent.conf").display()),
        ],
        "REFUSED [hub-conf] --param hub_conf=@",
    );

    // the site's own conf passes the hub check and the next refusal is the checkout
    std::fs::write(&conf, "[Peer]\nPublicKey = x\nEndpoint = hub:51820\n").unwrap();
    let wt = base.join("wt");
    std::fs::create_dir_all(&wt).unwrap();
    std::fs::write(wt.join(".git"), "gitdir: /elsewhere/.git/worktrees/wt\n").unwrap();
    refused(
        &wt,
        &["--param", &format!("hub_conf=@{}", conf.display())],
        "REFUSED [gunnar-worktree]",
    );

    // no dwarves beside the checkout, and a $DWARVES_ROOT that is not one
    refused(&root, &[], "no dwarves checkout at");
    let not_dwarves = base.join("not-dwarves");
    std::fs::create_dir_all(&not_dwarves).unwrap();
    std::env::set_var("DWARVES_ROOT", &not_dwarves);
    refused(&root, &[], "has no Cargo.toml");
    std::fs::write(not_dwarves.join("Cargo.toml"), "[workspace]\n").unwrap();
    assert_eq!(dwarves_root(&root), Ok(not_dwarves.clone()));
    std::env::remove_var("DWARVES_ROOT");

    let sibling = base.join("dwarves");
    std::fs::create_dir_all(&sibling).unwrap();
    std::fs::write(sibling.join("Cargo.toml"), "[workspace]\n").unwrap();
    assert_eq!(dwarves_root(&root), Ok(sibling));
}