use anyhow::Result;
#[test]
fn read_includes() -> Result<()> {
let app = garden::build::context_from_path("tests/data/garden.yaml")?;
let config = app.get_root_config();
let actual = garden::eval::value(config, "${var_0}");
assert_eq!(actual, "zero");
let actual = garden::eval::value(config, "${var_1}");
assert_eq!(actual, "ONE");
let actual = garden::eval::value(config, "${var_2}");
assert_eq!(actual, "two");
assert!(config.trees.len() >= 2);
assert_eq!(config.trees[0].get_name(), "tree-zero");
assert_eq!(config.trees[3].get_name(), "example/tree");
let actual = garden::eval::value(config, "${var_included}");
assert_eq!(actual, "relative to config");
Ok(())
}
#[test]
fn template_includes() -> Result<()> {
let app = garden::build::context_from_path("tests/data/garden.yaml")?;
let config_id = app.get_root_id();
let context = garden::query::find_tree(&app, config_id, "tree-echo", None)?;
let config = app.get_root_config();
let tree = &config.trees[context.tree];
let result = garden::eval::tree_value(config, "${template-variable}", context.tree, None);
assert_eq!(result, "template");
let constant = garden::eval::tree_value(config, "${template-constant}", context.tree, None);
assert_eq!(constant, "constant");
assert_eq!(tree.commands.len(), 1);
assert_eq!(tree.commands[0].get_name(), "echo");
assert_eq!(
tree.commands[0].get(0).get_expr(),
"echo Hello, ${TREE_NAME}"
);
let context = garden::query::find_tree(&app, config_id, "tree-echo-extended", None)?;
let tree = &config.trees[context.tree];
let result = garden::eval::tree_value(config, "${template-variable}", context.tree, None);
let constant = garden::eval::tree_value(config, "${template-constant}", context.tree, None);
assert_eq!(result, "extended");
assert_eq!(constant, "constant");
assert_eq!(tree.commands.len(), 2);
assert_eq!(tree.commands[0].get_name(), "echo");
assert_eq!(
tree.commands[0].get(0).get_expr(),
"echo Hello, ${TREE_NAME}"
);
assert_eq!(tree.commands[1].get_name(), "echo");
assert_eq!(tree.commands[1].get(0).get_expr(), "echo extended");
let context = garden::query::find_tree(&app, config_id, "tree-echo-nested", None)?;
let tree = &config.trees[context.tree];
let result = garden::eval::tree_value(config, "${template-variable}", context.tree, None);
let constant = garden::eval::tree_value(config, "${template-constant}", context.tree, None);
assert_eq!(constant, "constant");
assert_eq!(result, "nested");
assert_eq!(tree.commands.len(), 1);
assert_eq!(tree.commands[0].get_name(), "echo");
assert_eq!(
tree.commands[0].get(0).get_expr(),
"echo Hello, ${TREE_NAME}"
);
let context = garden::query::find_tree(&app, config_id, "tree-echo-extended-tree", None)?;
let result = garden::eval::tree_value(config, "${template-variable}", context.tree, None);
assert_eq!(result, "extended-tree");
let result = garden::eval::tree_value(config, "${tree-variable}", context.tree, None);
assert_eq!(result, "nested");
let result = garden::eval::tree_value(config, "${tree-override}", context.tree, None);
assert_eq!(result, "extended-tree");
Ok(())
}