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

nordisk/facett

facett-ops/tests/index_config_follows_the_checkout.rs

//! **The pip index must serve THIS checkout's archive, not a path baked into a file.**
//!
//! `pkg-facet-wrapped/holger-facett-pip.ron` used to name an absolute path in one
//! person's home directory, and specifically the SHARED checkout. Both halves are a
//! problem (the template now carries the variable `${FACETT_CHECKOUT}` instead):
//!
//! * a lane working from a worktree packs its archive under its own `dist/` and then
//!   serves the shared tree's stale one, with nothing saying so — the install would
//!   "succeed" against a wheel from hours ago;
//! * the alternative is writing into a checkout another lane is building from, which
//!   is the failure that cost three lanes time on 2026-09-05.
//!
//! `render_index_config` makes the checked-in file a TEMPLATE and renders it against
//! the root the xtask is actually running in.

use std::path::PathBuf;

fn scratch(tag: &str) -> PathBuf {
    let base = std::env::var("CARGO_TARGET_TMPDIR")
        .map(PathBuf::from)
        .unwrap_or_else(|_| std::env::temp_dir());
    let d = base.join(format!("idxcfg-{tag}"));
    let _ = std::fs::remove_dir_all(&d);
    std::fs::create_dir_all(d.join("pkg-facet-wrapped")).unwrap();
    d
}

/// A checkout-shaped scratch tree holding a copy of the real template.
fn fake_checkout(tag: &str) -> PathBuf {
    let d = scratch(tag);
    let real = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
        .parent()
        .unwrap()
        .join("pkg-facet-wrapped/holger-facett-pip.ron");
    std::fs::copy(&real, d.join("pkg-facet-wrapped/holger-facett-pip.ron")).unwrap();
    d
}

#[test]
fn the_rendered_config_points_at_the_checkout_it_was_rendered_for() {
    let root = fake_checkout("mine");
    let out = facett_ops::holger::render_index_config(&root).expect("renders");
    let text = std::fs::read_to_string(&out).unwrap();

    assert!(
        text.contains(&root.join("dist/holger-pip").display().to_string()),
        "the rendered config does not name this checkout's dist/: {text}"
    );
    assert!(
        !text.contains("${FACETT_CHECKOUT}/dist/holger-pip"),
        "the rendered config still points at the SHARED checkout — a worktree would \
         publish its wheel and then serve somebody else's"
    );
}

#[test]
fn both_the_store_and_the_archive_path_move_together() {
    // The template names the directory once as a storage endpoint and once as the
    // archive file. Rewriting only one produces a config that starts and serves
    // nothing, which reads as an empty index rather than as a misconfiguration.
    let root = fake_checkout("both");
    let out = facett_ops::holger::render_index_config(&root).unwrap();
    let text = std::fs::read_to_string(&out).unwrap();
    let mine = root.join("dist/holger-pip").display().to_string();
    assert_eq!(
        text.matches(&mine).count(),
        2,
        "expected both the store path and the archive path to be rewritten: {text}"
    );
}

#[test]
fn the_rendered_file_lands_under_target_and_not_in_the_source_tree() {
    // Writing the rendered config beside the template would dirty the checkout on
    // every run — and `git status` lying is how a shared tree becomes unusable.
    let root = fake_checkout("target");
    let out = facett_ops::holger::render_index_config(&root).unwrap();
    assert!(
        out.starts_with(root.join("target")),
        "the rendered config must live under target/, got {}",
        out.display()
    );
    assert!(
        std::fs::read_to_string(root.join("pkg-facet-wrapped/holger-facett-pip.ron"))
            .unwrap()
            .contains("${FACETT_CHECKOUT}/dist/holger-pip"),
        "the TEMPLATE must be left untouched"
    );
}

#[test]
fn a_template_whose_baked_path_moved_is_refused_rather_than_silently_unrewritten() {
    // If someone edits the .ron and the substring no longer matches, the render would
    // quietly return the template unchanged — and the worktree would serve the shared
    // archive again, which is the exact bug this exists to prevent. It must fail loudly.
    let root = scratch("moved");
    std::fs::write(
        root.join("pkg-facet-wrapped/holger-facett-pip.ron"),
        "( storage_endpoints: [ ( ron_path: \"/somewhere/else/dist/holger-pip/\" ) ] )\n",
    )
    .unwrap();
    let err = facett_ops::holger::render_index_config(&root);
    // The baked token is absent, so nothing is rewritten — and because the root is not
    // the shared checkout, that is refused.
    assert!(
        err.is_err(),
        "a template that no longer carries the baked path must be refused, not rendered \
         unchanged"
    );
}