create-grafana-plugin 0.2.6

CLI tool to scaffold production-ready Grafana plugin projects
Documentation
//! Template rendering: substitution, conditionals, derived fields (9.5).

use create_grafana_plugin::config::{PluginType, ProjectConfig};
use create_grafana_plugin::template::{TemplateContext, render_string};

fn sample_config() -> ProjectConfig {
    ProjectConfig {
        name: "my-plugin".to_string(),
        description: "Test plugin".to_string(),
        author: "Jane Doe".to_string(),
        org: "acme".to_string(),
        plugin_type: PluginType::Panel,
        has_wasm: true,
        has_docker: false,
        has_mock: false,
        port_offset: 0,
    }
}

#[test]
fn render_string_substitutes_variables() {
    let cfg = sample_config();
    let ctx = TemplateContext::from_config(&cfg);
    let out = render_string(
        "id={{ plugin_id }} name={{ plugin_name }} org={{ org }}",
        &ctx,
    )
    .expect("render");
    assert_eq!(out, "id=acme-my-plugin name=my-plugin org=acme");
}

#[test]
fn render_string_conditional_includes_when_true() {
    let cfg = sample_config();
    let ctx = TemplateContext::from_config(&cfg);
    let tpl = "{% if has_wasm %}WASM{% else %}NO{% endif %}";
    assert_eq!(render_string(tpl, &ctx).expect("render"), "WASM");
}

#[test]
fn render_string_conditional_excludes_when_false() {
    let mut cfg = sample_config();
    cfg.has_wasm = false;
    let ctx = TemplateContext::from_config(&cfg);
    let tpl = "{% if has_wasm %}WASM{% else %}NO{% endif %}";
    assert_eq!(render_string(tpl, &ctx).expect("render"), "NO");
}

#[test]
fn template_context_derived_fields() {
    let cfg = ProjectConfig {
        name: "foo-bar-baz".to_string(),
        description: "d".to_string(),
        author: "a".to_string(),
        org: "orgx".to_string(),
        plugin_type: PluginType::Datasource,
        has_wasm: true,
        has_docker: true,
        has_mock: true,
        port_offset: 0,
    };
    let ctx = TemplateContext::from_config(&cfg);
    assert_eq!(ctx.plugin_id, "orgx-foo-bar-baz");
    assert_eq!(ctx.crate_name, "foo_bar_baz");
    assert_eq!(ctx.pascal_case_name, "FooBarBaz");
    assert_eq!(ctx.plugin_type, "datasource");
    assert!(!ctx.current_year.is_empty());
    assert!(!ctx.today.is_empty());
}