Skip to content

Commit

Permalink
eval: execute "$ exec expressions" in the directory of the current tree
Browse files Browse the repository at this point in the history
Teach from_path() to use the parent directory of the specified
path as the config root. This allows us to write tests to ensure
that "$ <command>" exec expressions are executed with the
current directory set to the directory of the tree.

Signed-off-by: David Aguilar <davvid@gmail.com>
  • Loading branch information
davvid committed Jul 3, 2023
1 parent 59114ec commit f80e2a5
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 3 deletions.
1 change: 0 additions & 1 deletion src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ pub fn tree_value(
})
.to_string();

// TODO exec_expression_with_path() to use the tree path.
// NOTE: an environment must not be calculated here otherwise any
// exec expression will implicitly depend on the entire environment,
// and potentially many variables (including itself). Exec expressions
Expand Down
6 changes: 5 additions & 1 deletion src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1250,7 +1250,11 @@ impl ApplicationContext {

/// Construct an ApplicationContext from a path using default MainOptions.
pub fn from_path(pathbuf: std::path::PathBuf) -> Result<Self, errors::GardenError> {
Self::from_path_and_root(pathbuf, None)
if let Some(root_dir) = pathbuf.parent().map(std::path::Path::to_owned) {
Self::from_path_and_root(pathbuf, Some(&root_dir))
} else {
Self::from_path_and_root(pathbuf, None)
}
}

/// Construct an ApplicationContext from a path using default MainOptions.
Expand Down
1 change: 0 additions & 1 deletion src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,6 @@ pub fn tree_context(
garden: Option<&str>,
) -> Result<model::TreeContext, errors::GardenError> {
let mut ctx = model::TreeContext::new("", config.get_id(), None, None);
// TODO: grafted trees
if let Some(context) = tree_from_name(config, tree, None, None) {
ctx.tree = context.tree;
} else {
Expand Down
2 changes: 2 additions & 0 deletions tests/data/garden.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ trees:
url: https://example.com/replacement/tree
replace: true

trees/prebuilt: "file://${repos}/example.git"

templates:
echo-template-extended:
extend: echo-template
Expand Down
4 changes: 4 additions & 0 deletions tests/data/trees/prebuilt/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Test tree data

This directory is used by the tests to simulate the existence of the
`example/tree`, which is referenced by `tests/data/graden.yaml`.
12 changes: 12 additions & 0 deletions tests/eval_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@ fn exec_expression() -> Result<()> {
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(&config, "trees/prebuilt", None)?;
let value = garden::eval::tree_value(&app_context, &config, "$ pwd", &context.tree, None);
assert!(value.ends_with("/trees/prebuilt"));

Ok(())
}

/// Ensure that shell $variables can be used.
#[test]
fn shell_variable_syntax() -> Result<()> {
Expand Down

0 comments on commit f80e2a5

Please sign in to comment.