Skip to content

Commit

Permalink
Enhancement: Support script runners and shebang in condition scripts #…
Browse files Browse the repository at this point in the history
  • Loading branch information
sagiegurari committed Jan 19, 2024
1 parent f600d65 commit 0030096
Show file tree
Hide file tree
Showing 17 changed files with 116 additions and 58 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## CHANGELOG

### v0.37.8

* Enhancement: Support script runners and shebang in condition scripts #987

### v0.37.7 (2024-01-14)

* Fix: Detect crate installation fix for new cargo list format
Expand Down
14 changes: 14 additions & 0 deletions examples/condition.toml
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,17 @@ condition = { files_modified = { input = [
] } }
command = "cargo"
args = ["build"]

[tasks.test-duckscript-condition]
condition_script = ['''
#!@duckscript
var = set "hello from duckscript"
echo ${var}
echo %{var}
exit 0
''']
script = '''
echo "duckscript condition was met"
'''
20 changes: 10 additions & 10 deletions src/lib/condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
#[path = "condition_test.rs"]
mod condition_test;

use crate::command;
use crate::environment;
use crate::profile;
use crate::scriptengine;
use crate::types;
use crate::types::{FlowInfo, RustVersionCondition, Step, TaskCondition};
use crate::types::{FlowInfo, RustVersionCondition, ScriptValue, Step, TaskCondition};
use crate::version::{is_newer, is_same};
use envmnt;
use fsio;
Expand Down Expand Up @@ -438,14 +438,14 @@ fn validate_script(condition_script: &Option<Vec<String>>, script_runner: Option
Some(ref script) => {
debug!("Checking task condition script.");

let exit_code =
command::run_script_get_exit_code(&script, script_runner, &vec![], false);

if exit_code == 0 {
true
} else {
false
}
return scriptengine::invoke_script_pre_flow(
&ScriptValue::Text(script.to_vec()),
script_runner,
None,
None,
false,
&vec![],
);
}
None => true,
}
Expand Down
14 changes: 14 additions & 0 deletions src/lib/condition_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,20 @@ fn validate_script_invalid() {
assert!(!enabled);
}

#[test]
fn validate_script_valid_with_duckscript_shebang() {
let enabled = validate_script(&Some(vec!["#!@duckscript\nexit 0".to_string()]), None);

assert!(enabled);
}

#[test]
fn validate_script_invalid_with_duckscript_shebang() {
let enabled = validate_script(&Some(vec!["#!@duckscript\nexit 1".to_string()]), None);

assert!(!enabled);
}

#[test]
#[ignore]
fn validate_profile_valid() {
Expand Down
10 changes: 8 additions & 2 deletions src/lib/scriptengine/duck_script/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub(crate) fn execute(
flow_info: Option<&FlowInfo>,
flow_state: Option<Rc<RefCell<FlowState>>>,
validate: bool,
) {
) -> bool {
let mut array_command = "@ = array".to_string();
let mut index = 0;
for _ in cli_arguments {
Expand Down Expand Up @@ -59,20 +59,26 @@ pub(crate) fn execute(
if validate {
error!("Error while running duckscript: {}", error);
}

return false;
}
};

// revert to originl working directory
if !directory.is_empty() {
environment::setup_cwd(Some(&directory));
}

true
}
Err(error) => {
if validate {
error!("Unable to load duckscript SDK: {}", error);
}

false

Check warning on line 79 in src/lib/scriptengine/duck_script/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/lib/scriptengine/duck_script/mod.rs#L79

Added line #L79 was not covered by tests
}
};
}
}

pub(crate) fn create_common_context(cli_arguments: &Vec<String>) -> Context {
Expand Down
9 changes: 6 additions & 3 deletions src/lib/scriptengine/duck_script/mod_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,26 @@ use envmnt;

#[test]
fn execute_duckscript() {
execute(
let valid = execute(
&vec!["echo test".to_string()],
&vec![],
Some(&test::create_empty_flow_info()),
Some(Rc::new(RefCell::new(FlowState::new()))),
true,
);
assert!(valid);
}

#[test]
fn execute_duckscript_error_no_validate() {
execute(
let valid = execute(
&vec!["badcommand".to_string()],
&vec![],
Some(&test::create_empty_flow_info()),
Some(Rc::new(RefCell::new(FlowState::new()))),
false,
);
assert!(!valid);
}

#[test]
Expand All @@ -39,13 +41,14 @@ fn execute_duckscript_error_with_validate() {

#[test]
fn execute_duckscript_cli_arguments() {
execute(
let valid = execute(
&vec!["assert ${1}".to_string()],
&vec!["true".to_string()],
Some(&test::create_empty_flow_info()),
Some(Rc::new(RefCell::new(FlowState::new()))),
true,
);
assert!(valid);
}

#[test]
Expand Down
4 changes: 3 additions & 1 deletion src/lib/scriptengine/generic_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub(crate) fn execute(
arguments: Option<Vec<String>>,
cli_arguments: &Vec<String>,
validate: bool,
) {
) -> bool {
let file = create_script_file(script_text, &extension);

let valid = run_file(&file, &runner, arguments, &mut cli_arguments.clone());
Expand All @@ -49,4 +49,6 @@ pub(crate) fn execute(
if validate && !valid {
error!("Unable to execute script.");
}

return valid;
}
12 changes: 8 additions & 4 deletions src/lib/scriptengine/generic_script_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ use crate::test;

#[test]
fn execute_shell() {
execute(
let valid = execute(
&vec!["exit 0".to_string()],
test::get_os_runner(),
test::get_os_extension(),
None,
&vec![],
true,
);
assert!(valid);
}

#[test]
Expand All @@ -28,39 +29,42 @@ fn execute_shell_error() {

#[test]
fn execute_shell_error_no_validate() {
execute(
let valid = execute(
&vec!["exit 1".to_string()],
test::get_os_runner(),
test::get_os_extension(),
None,
&vec![],
false,
);
assert!(!valid);
}

#[test]
fn execute_shell_empty_arguments() {
execute(
let valid = execute(
&vec!["exit 0".to_string()],
test::get_os_runner(),
test::get_os_extension(),
Some(vec![]),
&vec![],
true,
);
assert!(valid);
}

#[test]
#[cfg(target_os = "linux")]
fn execute_shell_cli_arguments() {
execute(
let valid = execute(
&vec!["exit $1".to_string()],
test::get_os_runner(),
test::get_os_extension(),
Some(vec![]),
&vec!["0".to_string()],
true,
);
assert!(valid);
}

#[test]
Expand Down
24 changes: 6 additions & 18 deletions src/lib/scriptengine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,27 +261,19 @@ fn invoke_script(
match engine_type {
EngineType::OS => {
let script_text = get_script_text(script);
os_script::execute(&script_text, script_runner, cli_arguments, validate);

true
os_script::execute(&script_text, script_runner, cli_arguments, validate)
}
EngineType::Duckscript => {
let script_text = get_script_text(script);
duck_script::execute(&script_text, cli_arguments, flow_info, flow_state, validate);

true
duck_script::execute(&script_text, cli_arguments, flow_info, flow_state, validate)
}
EngineType::Rust => {
let script_text = get_script_text(script);
rsscript::execute(&script_text, cli_arguments, validate);

true
rsscript::execute(&script_text, cli_arguments, validate)
}
EngineType::Shell2Batch => {
let script_text = get_script_text(script);
shell_to_batch::execute(&script_text, cli_arguments, validate);

true
shell_to_batch::execute(&script_text, cli_arguments, validate)
}
EngineType::Generic => {
let script_text = get_script_text(script);
Expand All @@ -293,16 +285,12 @@ fn invoke_script(
script_runner_args.clone(),
cli_arguments,
validate,
);

true
)
}
EngineType::Shebang => {
let script_text = get_script_text(script);
let extension = script_extension.clone();
shebang_script::execute(&script_text, &extension, cli_arguments, validate);

true
shebang_script::execute(&script_text, &extension, cli_arguments, validate)

Check warning on line 293 in src/lib/scriptengine/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/lib/scriptengine/mod.rs#L293

Added line #L293 was not covered by tests
}
EngineType::Unsupported => false,
}
Expand Down
6 changes: 4 additions & 2 deletions src/lib/scriptengine/os_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub(crate) fn execute(
runner: Option<String>,
cli_arguments: &Vec<String>,
validate: bool,
) {
command::run_script_get_exit_code(&script_text, runner, &cli_arguments, validate);
) -> bool {
let exit_code =
command::run_script_get_exit_code(&script_text, runner, &cli_arguments, validate);
exit_code == 0
}
6 changes: 4 additions & 2 deletions src/lib/scriptengine/os_script_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ fn execute_shell() {

#[test]
fn execute_shell_with_runner() {
execute(
let valid = execute(
&vec!["exit 0".to_string()],
Some(test::get_os_runner()),
&vec![],
true,
);
assert!(valid);
}

#[test]
Expand All @@ -24,5 +25,6 @@ fn execute_shell_error() {

#[test]
fn execute_shell_error_no_validate() {
execute(&vec!["exit 1".to_string()], None, &vec![], false);
let valid = execute(&vec!["exit 1".to_string()], None, &vec![], false);
assert!(!valid);
}
8 changes: 7 additions & 1 deletion src/lib/scriptengine/rsscript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ fn run_file(file: &str, cli_arguments: &Vec<String>, provider: &ScriptRunner) ->
exit_code == 0
}

pub(crate) fn execute(rust_script: &Vec<String>, cli_arguments: &Vec<String>, validate: bool) {
pub(crate) fn execute(
rust_script: &Vec<String>,
cli_arguments: &Vec<String>,
validate: bool,
) -> bool {
let provider = get_script_runner();

install_crate(&provider);
Expand All @@ -113,4 +117,6 @@ pub(crate) fn execute(rust_script: &Vec<String>, cli_arguments: &Vec<String>, va
if validate && !valid {
error!("Unable to execute rust code.");
}

return valid;
}
Loading

0 comments on commit 0030096

Please sign in to comment.