starmap-cli 0.4.4

The starmap command, which checks, explains, compares and runs an application's declared environment
Documentation
//! Tests for the masking of secret references in an excerpt.

use super::masked;

#[test]
fn a_masked_span_keeps_its_line_breaks_and_its_length() {
    let text = "a = \"\"\"\nsecret\r\n\"\"\"\nb = 1\n";
    let masked = masked(text.to_owned(), std::slice::from_ref(&(4..19))).unwrap();

    assert_eq!(masked, "a = ***\n******\r\n***\nb = 1\n");
}

#[test]
fn a_span_off_a_character_boundary_drops_the_excerpt() {
    assert_eq!(
        masked("x = \"é\"".to_owned(), std::slice::from_ref(&(0..6))),
        None
    );
    assert_eq!(
        masked("x = 1".to_owned(), std::slice::from_ref(&(3..9))),
        None
    );
}

#[test]
fn a_span_on_character_boundaries_masks_each_byte() {
    assert_eq!(
        masked("x = \"é\"".to_owned(), std::slice::from_ref(&(4..8))).as_deref(),
        Some("x = ****")
    );
}

#[test]
fn a_reference_with_no_span_is_masked_to_the_end_of_its_line() {
    let text =
        "bind = \"op://Ops' vault/item/field\" # note\r\nnext = 'op://a/b/c'\nlast op://x/y/z\n";

    let masked = masked(text.to_owned(), &[]).unwrap();

    assert_eq!(
        masked,
        format!(
            "bind = \"{}\r\nnext = '{}\nlast {}\n",
            "*".repeat(34),
            "*".repeat(11),
            "*".repeat(10)
        )
    );
}

#[test]
fn a_multi_line_string_that_still_holds_op_drops_the_excerpt() {
    let text = "bind = { one-password = \"\"\"op:\\\n    //acme-vault/harness-client/password\"\"\"\n[next]\n";

    assert_eq!(masked(text.to_owned(), &[]), None);
}

#[test]
fn a_multi_line_string_whose_reference_has_a_span_keeps_the_excerpt() {
    let text = "bind = { one-password = '''op://a/b/c''' }\n";
    let start = text.find("'''").unwrap();
    let end = text.rfind("'''").unwrap() + 3;

    let masked = masked(text.to_owned(), std::slice::from_ref(&(start..end))).unwrap();

    assert_eq!(
        masked,
        format!("bind = {{ one-password = {} }}\n", "*".repeat(end - start))
    );
}

#[test]
fn a_closed_multi_line_string_without_op_keeps_the_excerpt() {
    let text = "purpose = \"\"\"The port.\"\"\"\nbind = 'op://a/b/c'\n";

    assert_eq!(
        masked(text.to_owned(), &[]).as_deref(),
        Some(
            format!(
                "purpose = \"\"\"The port.\"\"\"\nbind = '{}\n",
                "*".repeat(11)
            )
            .as_str()
        )
    );
}

#[test]
fn text_without_a_reference_is_left_as_it_is() {
    let text = "purpose = \"op is not a reference: op:/x\"\n";

    assert_eq!(masked(text.to_owned(), &[]).as_deref(), Some(text));
}