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

nordisk/gunnar

crates/gunnar-common/tests/build_ref.rs

//! The build-reference rule `build.rs` stamps `GUNNAR_BUILD_REF` by.
//!
//! A Build Thing container build of a tree copied WITHOUT its `.git` had
//! nothing for `git describe` to read, `BUILD_REF` fell to `unknown`, and the
//! console showed the crate version (`1.4.0`) for a box far past the tag. The
//! builder knows the commit and passes it — `NORDISK_BUILD_REF` from the Build
//! Thing, `GUNNAR_BUILD_REF` from a person — and the passed value wins.

#[path = "../build_ref.rs"]
mod build_ref;

use build_ref::{choose, vcs_info_ref, ENV_REFS};

fn s(v: &str) -> Option<String> {
    Some(v.to_owned())
}

#[test]
fn the_builders_variables_are_read_in_order() {
    assert_eq!(ENV_REFS, ["GUNNAR_BUILD_REF", "NORDISK_BUILD_REF"]);
}

/// ★ RED-FIRST — no `.git`, the Build Thing's ref: the build names its commit.
#[test]
fn a_build_without_git_takes_the_build_things_ref() {
    assert_eq!(
        choose(&[None, s("v1.5.0-3-gabc1234")], None).as_deref(),
        Some("v1.5.0-3-gabc1234")
    );
}

#[test]
fn a_build_without_git_takes_a_persons_ref_first() {
    assert_eq!(
        choose(&[s("v1.5.0"), s("v1.4.0-1-gdeadbee")], None).as_deref(),
        Some("v1.5.0")
    );
}

/// ★ RED-FIRST — the builder's value is preferred over git: the one who handed
/// over the tree knows which commit it is; a `git describe` that answers may be
/// describing some OTHER checkout the tree was copied into.
#[test]
fn a_passed_ref_is_preferred_over_git() {
    assert_eq!(
        choose(&[None, s("v1.5.0-3-gabc1234")], Some("v0.0-1-g0000000")).as_deref(),
        Some("v1.5.0-3-gabc1234")
    );
}

#[test]
fn git_answers_when_nobody_passed_a_ref_and_blanks_are_not_refs() {
    assert_eq!(
        choose(&[None, None], Some("v1.4.0-9-gabcdef0\n")).as_deref(),
        Some("v1.4.0-9-gabcdef0")
    );
    assert_eq!(
        choose(&[s("  "), s("")], Some("v1.4.0")).as_deref(),
        Some("v1.4.0")
    );
    assert_eq!(
        choose(&[s(" "), None], None),
        None,
        "nothing to say ⇒ unstamped, never invented"
    );
}

/// ★ RED-FIRST (GUN-VERSION-STAMP) — a published package names its commit: the
/// `.cargo_vcs_info.json` `cargo package` writes, as cargo writes it.
#[test]
fn a_package_names_the_commit_it_was_made_from() {
    let clean = "{\n  \"git\": {\n    \"sha1\": \"ad3106bc76d80c02d3ee0e2138ba916cef5bc8e2\"\n  },\n  \"path_in_vcs\": \"crates/gunnar-common\"\n}";
    assert_eq!(vcs_info_ref(clean).as_deref(), Some("ad3106b"));
    let dirty = "{\n  \"git\": {\n    \"sha1\": \"ad3106bc76d80c02d3ee0e2138ba916cef5bc8e2\",\n    \"dirty\": true\n  },\n  \"path_in_vcs\": \"\"\n}";
    assert_eq!(vcs_info_ref(dirty).as_deref(), Some("ad3106b-modified"));
}

#[test]
fn a_vcs_info_without_a_commit_names_none() {
    assert_eq!(vcs_info_ref("{}"), None);
    assert_eq!(vcs_info_ref("{\"git\":{\"sha1\":\"not-a-sha\"}}"), None);
    assert_eq!(vcs_info_ref("{\"git\":{\"sha1\":\"ad3106b\"}}"), None);
}