helm-rs 0.1.0

Typed Rust API for Helm 4: swappable backends, native chart, values and release reading
Documentation
//! Tests of the comment remover.

use super::super::{GenerateOptions, generate};
use super::remove_comments;

/// Inputs with the text the remover returns. helm-schema 0.24.0 keeps the line that ends a
/// block of commented YAML back, so the lines after it overtake it and the list below ends up
/// before its key (its schema then types `items` as `null` and glues `- one - two` onto the
/// default of `optional`); here the line stays in place.
const CASES: [(&str, &str); 2] = [
    (
        "# optional: x\nitems:\n  - one\n  - two\n",
        "optional: x\n\nitems:\n  - one\n  - two\n",
    ),
    ("plain: 1\n# not yaml\n", "plain: 1\n# not yaml\n"),
];

#[test]
fn lines_after_a_commented_block_keep_their_order() {
    for (input, expected) in CASES {
        assert_eq!(remove_comments(input), expected, "{input}");
    }
}

/// Commented YAML with the properties helm-schema 0.24.0 generates for it with `--uncomment`:
/// only the first line of a block is uncommented, the rest stays a comment (the remover strips
/// the marker with the indentation of the first line only), so nested and consecutive commented
/// settings are never values and the schema accepts the values file as it is.
const KEPT_AS_COMMENTS: [(&str, &[&str]); 4] = [
    ("# optional:\n#   enabled: true\n", &["optional"]),
    ("# a: 1\n# b: 2\n# c: 3\n", &["a"]),
    (
        "# optional:\n#   enabled: true\n#   name: x\n# other:\n#   x: 1\nx: 1\n",
        &["optional", "x"],
    ),
    ("# foo:\n#   - a\n#   - b\nx: 1\n", &["foo", "x"]),
];

#[test]
fn only_the_first_line_of_a_commented_block_becomes_yaml() {
    let options = GenerateOptions {
        uncomment: true,
        ..GenerateOptions::default()
    };

    for (input, expected) in KEPT_AS_COMMENTS {
        let schema = generate(input, &options).expect("generates");
        let mut names: Vec<&str> = schema["properties"]
            .as_object()
            .expect("properties")
            .keys()
            .map(String::as_str)
            .filter(|name| *name != "global")
            .collect();

        names.sort_unstable();

        assert_eq!(names, expected, "{input}");
    }
}