helm-rs 0.1.1

Typed Rust API for Helm 4: swappable backends, native chart, values and release reading
Documentation
//! Tests of the alias expansion limit against what helm-schema 0.24.0 (`yaml.v3`) does.

use std::fmt::Write as _;

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

const EXCESSIVE: &str = "yaml: document contains excessive aliasing";

/// A chain of `levels` anchored collections, each holding `fan` aliases to the previous one, every
/// line starting with `prefix`: sequences, or mappings when `mapping` is set.
fn bomb(prefix: &str, levels: usize, fan: usize, mapping: bool) -> String {
    let (open, close) = if mapping { ('{', '}') } else { ('[', ']') };
    let mut text = format!("{prefix}a0: &a0 {open}k0: x{close}\n");

    for level in 1..=levels {
        let items: Vec<String> = (0..fan)
            .map(|i| {
                let key = if mapping {
                    format!("k{i}: ")
                } else {
                    String::new()
                };

                format!("{key}*a{}", level - 1)
            })
            .collect();

        writeln!(
            text,
            "{prefix}a{level}: &a{level} {open}{}{close}",
            items.join(", ")
        )
        .expect("writes");
    }

    text
}

/// The same bomb inside the `default` of an annotation block, on key `y`.
fn annotated(levels: usize, fan: usize) -> String {
    let body = bomb("#   ", levels, fan, false);

    format!("# @schema\n# default:\n{body}# @schema\ny: 1\n")
}

#[test]
fn a_small_alias_bomb_is_refused_without_expanding_it() {
    let options = GenerateOptions::default();
    let generated = generate(&bomb("", 12, 9, true), &options).expect_err("generate fails");
    let items = generate(&bomb("", 12, 9, false), &options).expect_err("sequences fail");
    let read = annotations(&bomb("", 12, 9, true), false).expect_err("annotations fail");
    let comment = generate(&annotated(8, 9), &options).expect_err("annotation default fails");

    assert_eq!(generated.to_string(), EXCESSIVE);
    assert_eq!(items.to_string(), EXCESSIVE);
    assert_eq!(read.to_string(), EXCESSIVE);
    assert!(comment.to_string().ends_with(EXCESSIVE), "{comment}");
}

#[test]
fn modest_aliasing_is_accepted() {
    let options = GenerateOptions::default();

    generate(&bomb("", 4, 3, true), &options).expect("values generate");
    generate(&bomb("", 4, 3, false), &options).expect("sequences generate");
    generate(&annotated(3, 3), &options).expect("annotation generates");
    annotations(&bomb("", 4, 3, true), false).expect("annotations read");
}