Skip to content

Commit

Permalink
fix: re-enable rendering in test script section (#1091)
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfv authored Oct 10, 2024
1 parent b4d32b1 commit 49d5588
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
32 changes: 31 additions & 1 deletion src/package_test/serialize_test.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
use std::path::{Path, PathBuf};

use fs_err as fs;
use minijinja::Value;

use crate::{
metadata::Output,
packaging::PackagingError,
recipe::parser::{CommandsTest, TestType},
recipe::{
parser::{CommandsTest, ScriptContent, TestType},
Jinja,
},
script::ResolvedScriptContents,
};

impl CommandsTest {
Expand Down Expand Up @@ -50,6 +55,18 @@ impl CommandsTest {
}
}

fn default_jinja_context(output: &Output) -> Jinja {
let selector_config = output.build_configuration.selector_config();
let mut jinja = Jinja::new(selector_config);
for (k, v) in output.recipe.context.iter() {
jinja
.context_mut()
.insert(k.clone(), Value::from_safe_string(v.clone()));
}

jinja
}

/// Write out the test files for the final package
pub(crate) fn write_test_files(
output: &Output,
Expand All @@ -76,6 +93,19 @@ pub(crate) fn write_test_files(
// store the cwd in the rendered test
command_test.script.cwd = Some(cwd);
}

// Try to render the script contents here
// Note: we want to improve this with better rendering in the future
let contents = command_test.script.resolve_content(
&output.build_configuration.directories.recipe_dir,
Some(default_jinja_context(output)),
&["sh", "bat"],
);

// Replace with rendered contents
if let Ok(ResolvedScriptContents::Inline(contents)) = contents {
command_test.script.content = ScriptContent::Command(contents)
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ impl Script {
}
}

fn get_contents(
pub(crate) fn resolve_content(
&self,
recipe_dir: &Path,
jinja_context: Option<Jinja>,
Expand Down Expand Up @@ -622,7 +622,7 @@ impl Script {
});
}

let contents = self.get_contents(recipe_dir, jinja_config, &valid_script_extensions)?;
let contents = self.resolve_content(recipe_dir, jinja_config, &valid_script_extensions)?;

// Select a different interpreter if the script is a nushell script.
if contents
Expand Down
11 changes: 8 additions & 3 deletions test-data/recipes/test-execution/recipe-test-succeed.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
context:
version: "1.2.3"

package:
name: test-execution
version: 0.1.0
version: ${{ version }}

build:
script:
Expand All @@ -17,6 +20,8 @@ tests:
- test -f $PREFIX/test-execution.txt
- test -f ./testfile.txt
- test -f ./testfolder/data.txt
# check that the version is rendered properly from context into the script
- if [[ "${{ version }}" == "1.2.3" ]]; then echo "Version matches 1.2.3"; else exit 123; fi
else:
- if not exist %PREFIX%\test-execution.txt (exit 1)
- if not exist .\testfile.txt (exit 1)
Expand All @@ -28,12 +33,12 @@ tests:
- script:
- if: unix
then:
- test $PKG_VERSION = "0.1.0"
- test $PKG_VERSION = "1.2.3"
- test $PKG_NAME = "test-execution"
- test $PKG_BUILDNUM = "0"
- test -n $PKG_BUILD_STRING
else:
- if not "%PKG_VERSION%" == "0.1.0" (exit 1)
- if not "%PKG_VERSION%" == "1.2.3" (exit 1)
- if not "%PKG_NAME%" == "test-execution" (exit 1)
- if not "%PKG_BUILDNUM%" == "0" (exit 1)
# make sure that the build string is not empty
Expand Down
5 changes: 3 additions & 2 deletions test/end-to-end/__snapshots__/test_simple.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
- flask
- flask.json
- script:
- flask --help
- pip check
content: |-
flask --help
pip check
requirements:
run:
- pip
Expand Down

0 comments on commit 49d5588

Please sign in to comment.