pub mod common;
use anyhow::Result;
use garden::string;
#[test]
fn garden_root() -> Result<()> {
let app_context = common::garden_context()?;
let config = app_context.get_root_config();
let expect_src_dir = string!("/home/test/src");
assert_eq!("${root}", config.root.get_expr());
assert_eq!(
Some(expect_src_dir.clone()),
config.root.get_value().cloned()
);
assert_eq!(expect_src_dir, config.root_path.to_string_lossy());
Ok(())
}
#[test]
fn tree_variable() -> Result<()> {
let app_context = common::garden_context()?;
let config = app_context.get_root_config();
let tree_name = garden::model::TreeName::from("git");
let result =
garden::eval::tree_value(&app_context, config, None, "${prefix}", &tree_name, None);
assert_eq!(result, "/home/test/.local");
Ok(())
}
#[test]
fn config_variable() -> Result<()> {
let app_context = common::garden_context()?;
let config = app_context.get_root_config();
let tree_name = garden::model::TreeName::from("git");
let test = garden::eval::tree_value(&app_context, config, None, "${test}", &tree_name, None);
assert_eq!("TEST", test);
let local = garden::eval::tree_value(&app_context, config, None, "${local}", &tree_name, None);
assert_eq!("TEST/local", local);
Ok(())
}
#[test]
fn tree_name() -> Result<()> {
let app_context = common::garden_context()?;
let config = app_context.get_root_config();
let tree_name = garden::model::TreeName::from("git");
let expect = "git";
let actual =
garden::eval::tree_value(&app_context, config, None, "${TREE_NAME}", &tree_name, None);
assert_eq!(expect, actual);
Ok(())
}
#[test]
fn tree_path() -> Result<()> {
let app_context = common::garden_context()?;
let config = app_context.get_root_config();
let tree_name = garden::model::TreeName::from("git");
let expect = "/home/test/src/git";
let actual =
garden::eval::tree_value(&app_context, config, None, "${TREE_PATH}", &tree_name, None);
assert_eq!(expect, actual);
Ok(())
}
#[test]
fn garden_path() -> Result<()> {
let app_context = common::garden_context()?;
let config = app_context.get_root_config();
let expect = "/home/test/src";
let actual = garden::eval::value(&app_context, config, "${GARDEN_ROOT}");
assert_eq!(expect, actual);
Ok(())
}
#[test]
fn exec_expression() -> Result<()> {
let app_context = common::garden_context()?;
let config = app_context.get_root_config();
let value = garden::eval::value(&app_context, config, "$ echo test");
assert_eq!(value, "test");
let value = garden::eval::value(&app_context, config, "${echo_cmd_exec}");
assert_eq!(value, "cmd");
let context = garden::query::tree_context(&app_context, config, "tmp", None)?;
let value = garden::eval::tree_value(
&app_context,
config,
None,
"$ echo $PWD",
&context.tree,
None,
);
assert!(value == "/tmp" || value == "/private/tmp");
let value = garden::eval::tree_value(&app_context, config, None, "$ pwd", &context.tree, None);
assert!(value == "/tmp" || value == "/private/tmp");
Ok(())
}
#[test]
fn exec_expression_in_tree_context() -> Result<()> {
let app_context =
garden::model::ApplicationContext::from_path_string("tests/data/garden.yaml")?;
let config = app_context.get_root_config();
let context = garden::query::tree_context(&app_context, config, "trees/prebuilt", None)?;
let value = garden::eval::tree_value(&app_context, config, None, "$ pwd", &context.tree, None);
assert!(value.ends_with("/trees/prebuilt"));
Ok(())
}
#[test]
fn shell_variable_syntax() -> Result<()> {
let app_context = common::garden_context()?;
let config = app_context.get_root_config();
let value = garden::eval::value(&app_context, config, "$ value=$(echo test); echo $value");
assert_eq!(value, "test");
let value = garden::eval::value(&app_context, config, "$ echo '$${value[@]:0:1}'");
assert_eq!(value, "${value[@]:0:1}");
Ok(())
}
#[test]
fn exec_expression_with_dashed_variables() -> Result<()> {
let app_context =
garden::model::ApplicationContext::from_path_string("tests/data/variables.yaml")?;
let config = app_context.get_root_config();
let value = garden::eval::value(&app_context, config, "${var-dashed}");
assert_eq!(value, "ok");
Ok(())
}
#[test]
fn multi_variable_with_tree() -> Result<()> {
let app_context = common::garden_context()?;
let config = app_context.get_root_config();
assert!(config.trees.len() > 1);
assert!(config.trees[1].environment.len() > 1);
let mut var = config.trees[1].environment[1].clone();
assert_eq!("PATH", var.get_name());
let context = garden::model::TreeContext::new("cola", None, None, None);
let values = garden::eval::multi_variable(&app_context, config, None, &mut var, &context);
assert_eq!(
values,
[
"/home/test/src/git-cola/local/bin",
"/home/test/src/git-cola/bin",
]
);
Ok(())
}
#[test]
fn multi_variable_with_garden() -> Result<()> {
let app_context = common::garden_context()?;
let config = app_context.get_root_config();
assert!(config.trees.len() > 1);
assert!(config.trees[1].environment.len() > 1);
let mut var = config.trees[1].environment[1].clone();
assert_eq!("PATH", var.get_name());
let context = garden::model::TreeContext::new("cola", None, Some(string!("cola")), None);
let values = garden::eval::multi_variable(&app_context, config, None, &mut var, &context);
assert_eq!(
values,
[
"/home/test/apps/git-cola/current/bin",
"/home/test/src/git-cola/bin",
]
);
Ok(())
}
#[test]
fn garden_environment() -> Result<()> {
let app_context = common::garden_context()?;
let config = app_context.get_root_config();
let context = garden::model::TreeContext::new("cola", None, Some(string!("cola")), None);
let values = garden::eval::environment(&app_context, config, &context);
assert_eq!(values.len(), 9);
let mut idx = 0;
assert_eq!(values[idx].0, "EXAMPLE_VALUE"); assert_eq!(values[idx].1, "/home/test/src");
idx += 1;
assert_eq!(values[idx].0, "PATH"); assert_eq!(values[idx].1, "/home/test/bin:/usr/bin:/bin");
idx += 1;
assert_eq!(values[idx].0, "PYTHONPATH"); assert_eq!(values[idx].1, "/home/test/src/git-cola");
idx += 1;
assert_eq!(values[idx].0, "PATH"); assert_eq!(
values[idx].1,
"/home/test/apps/git-cola/current/bin:/home/test/bin:/usr/bin:/bin"
);
idx += 1;
assert_eq!(values[idx].0, "PATH"); assert_eq!(
values[idx].1,
format!(
"{}:{}:{}:/usr/bin:/bin",
"/home/test/src/git-cola/bin", "/home/test/apps/git-cola/current/bin", "/home/test/bin"
)
);
idx += 1;
assert_eq!(values[idx].0, "PYTHONPATH");
assert_eq!(
values[idx].1,
"/home/test/src/python/send2trash:/home/test/src/git-cola"
);
idx += 1;
assert_eq!(values[idx].0, "PYTHONPATH"); assert_eq!(
values[idx].1,
"/home/test/src/python/qtpy:/home/test/src/python/send2trash:/home/test/src/git-cola"
);
idx += 1;
assert_eq!(values[idx].0, "GIT_COLA_TRACE"); assert_eq!(values[idx].1, "full");
idx += 1;
assert_eq!(values[idx].0, "PATH"); assert_eq!(
values[idx].1,
format!(
"{}:{}:{}:/usr/bin:/bin:{}",
"/home/test/src/git-cola/bin",
"/home/test/apps/git-cola/current/bin",
"/home/test/bin",
"/home/test/apps/git-cola/current/bin"
)
);
idx += 1;
assert_eq!(values.len(), idx);
Ok(())
}
#[test]
fn group_environment() -> Result<()> {
let app_context = common::garden_context()?;
let config = app_context.get_root_config();
let context = garden::model::TreeContext::new("cola", None, None, Some(string!("cola")));
let values = garden::eval::environment(&app_context, config, &context);
assert_eq!(values.len(), 7);
let mut idx = 0;
assert_eq!(values[idx].0, "EXAMPLE_VALUE");
assert_eq!(values[idx].1, "/home/test/src");
idx += 1;
assert_eq!(values[idx].0, "PATH");
assert_eq!(values[idx].1, "/home/test/bin:/usr/bin:/bin");
idx += 1;
assert_eq!(values[idx].0, "PYTHONPATH");
assert_eq!(values[idx].1, "/home/test/src/git-cola");
idx += 1;
assert_eq!(values[idx].0, "PATH");
assert_eq!(
values[idx].1,
"/home/test/src/git-cola/local/bin:/home/test/bin:/usr/bin:/bin"
);
idx += 1;
assert_eq!(values[idx].0, "PATH");
assert_eq!(
values[idx].1,
"/home/test/src/git-cola/bin:/home/test/src/git-cola/local/bin:/home/test/bin:/usr/bin:/bin"
);
idx += 1;
assert_eq!(values[idx].0, "PYTHONPATH");
assert_eq!(
values[idx].1,
"/home/test/src/python/send2trash:/home/test/src/git-cola"
);
idx += 1;
assert_eq!(values[idx].0, "PYTHONPATH");
assert_eq!(
values[idx].1,
format!(
"{}:{}:{}",
"/home/test/src/python/qtpy",
"/home/test/src/python/send2trash",
"/home/test/src/git-cola"
)
);
idx += 1;
assert_eq!(values.len(), idx);
Ok(())
}
#[test]
fn environment_empty_value() -> Result<()> {
let app_context = common::garden_context()?;
let config = app_context.get_root_config();
let context = garden::query::tree_from_name(config, "tmp", None, None).unwrap();
let values = garden::eval::environment(&app_context, config, &context);
assert_eq!(values.len(), 5);
let mut idx = 0;
assert_eq!(values[idx].0, "EXAMPLE_VALUE");
assert_eq!(values[idx].1, "/home/test/src");
idx += 1;
assert_eq!(values[idx].0, "PATH");
assert_eq!(values[idx].1, "/home/test/bin:/usr/bin:/bin");
idx += 1;
assert_eq!(values[idx].0, "EMPTY"); assert_eq!(values[idx].1, "a");
idx += 1;
assert_eq!(values[idx].0, "EMPTY"); assert_eq!(values[idx].1, "b:a");
idx += 1;
assert_eq!(values[idx].0, "tmp_VALUE"); assert_eq!(values[idx].1, "/tmp");
idx += 1;
assert_eq!(values.len(), idx);
Ok(())
}
#[test]
fn command_garden_scope() -> Result<()> {
let app_context = common::garden_context()?;
let context = garden::model::TreeContext::new("cola", None, Some(string!("cola")), None);
let values = garden::eval::command(&app_context, &context, "build");
assert_eq!(values.len(), 1);
let cmd_vec = &values[0];
assert_eq!(cmd_vec.len(), 1);
let cmd = &cmd_vec[0];
assert_eq!(cmd, "make -j prefix=/home/test/apps/git-cola/current all");
Ok(())
}
#[test]
fn command_tree_scope() -> Result<()> {
let app_context = common::garden_context()?;
let context = garden::model::TreeContext::new("cola", None, None, None);
{
let values = garden::eval::command(&app_context, &context, "build");
assert_eq!(values.len(), 1);
assert_eq!(values[0].len(), 1);
let cmd = &values[0][0];
assert_eq!(cmd, "make -j prefix=/home/test/src/git-cola/local all");
}
{
let values = garden::eval::command(&app_context, &context, "test");
assert_eq!(values.len(), 1);
assert_eq!(values[0].len(), 2);
assert_eq!(values[0][0], "git status --short");
assert_eq!(values[0][1], "make tox");
}
Ok(())
}
#[test]
fn environment_variables() -> Result<()> {
let app_context = common::garden_context()?;
let config = app_context.get_root_config();
std::env::set_var("GARDEN_TEST_VALUE", "test");
let value = garden::eval::value(&app_context, config, "${GARDEN_TEST_VALUE}");
assert_eq!(value, "test");
let value = garden::eval::tree_value(
&app_context,
config,
None,
"${GARDEN_TEST_VALUE}",
"git",
None,
);
assert_eq!(value, "test");
Ok(())
}
#[test]
fn find_tree_in_graft() -> Result<()> {
let app = garden::model::ApplicationContext::from_path_string("tests/data/garden.yaml")?;
let id = app.get_root_id();
let ctx = garden::query::find_tree(&app, id, "graft::graft", None)?;
assert_eq!("graft", ctx.tree);
assert!(ctx.config.is_some());
let node_id: usize = ctx.config.unwrap().into();
assert_eq!(2usize, node_id);
Ok(())
}
#[test]
fn eval_graft_tree() -> Result<()> {
let app_context =
garden::model::ApplicationContext::from_path_string("tests/data/garden.yaml")?;
let id = app_context.get_root_id();
let ctx = garden::query::find_tree(&app_context, id, "graft::graft", None)?;
assert!(ctx.config.is_some());
let node_id: usize = ctx.config.unwrap().into();
assert_eq!(2usize, node_id);
let graft_config = Some(app_context.get_config(ctx.config.unwrap()));
let path = garden::eval::tree_value(
&app_context,
app_context.get_root_config(),
graft_config,
"${TREE_PATH}",
&ctx.tree,
ctx.garden.as_ref(),
);
assert!(path.ends_with("/graft"), "{path} does not end with /graft");
let actual = garden::eval::tree_value(
&app_context,
app_context.get_root_config(),
graft_config,
"${current_config}",
&ctx.tree,
ctx.garden.as_ref(),
);
assert_eq!("graft", actual);
let example_ctx = garden::query::find_tree(&app_context, id, "example/tree", None)?;
assert!(example_ctx.config.is_none());
let example_config = app_context.get_config(id);
let actual = garden::eval::tree_value(
&app_context,
example_config,
None,
"${current_config}",
&example_ctx.tree,
example_ctx.garden.as_ref(),
);
assert_eq!("main", actual);
let actual = garden::eval::tree_value(
&app_context,
app_context.get_root_config(),
graft_config,
"${undefined::variable}",
&ctx.tree,
ctx.garden.as_ref(),
);
assert_eq!("", actual);
let actual = garden::eval::tree_value(
&app_context,
app_context.get_root_config(),
graft_config,
"${graft::current_config}",
&ctx.tree,
ctx.garden.as_ref(),
);
assert_eq!("graft", actual);
Ok(())
}
#[test]
fn eval_graft_variables() -> Result<()> {
let app_context =
garden::model::ApplicationContext::from_path_string("tests/data/garden.yaml")?;
let config = app_context.get_root_config();
let actual = garden::eval::value(&app_context, config, "${graft::current_config}");
assert_eq!("graft", actual);
let actual = garden::eval::value(&app_context, config, "${graft::variable}");
assert_eq!("graft value", actual);
let actual = garden::eval::value(&app_context, config, "${graft::deps::current_config}");
assert_eq!("deps", actual);
let actual = garden::eval::value(&app_context, config, "${graft::deps::deps_graft_value}");
assert_eq!("deps-graft-value", actual);
Ok(())
}