New Enable to error an exec command via new --fail-on-error flag #108

This commit is contained in:
sagie gur ari 2020-06-04 16:34:14 +00:00
parent 3fe3243032
commit cb7cd678a5
7 changed files with 85 additions and 31 deletions

View file

@ -1,5 +1,9 @@
## CHANGELOG
### v0.4.1
* New Enable to error an exec command via new --fail-on-error flag #108
### v0.4.0 (2020-05-07)
* New set_from_array command.

View file

@ -4144,7 +4144,7 @@ wget
<a name="std__process__Execute"></a>
## std::process::Execute
```sh
exec command [args]*
exec [--fail-on-error] command [args]*
output = exec command [args]*
stdout = set ${output.stdout}
@ -4154,6 +4154,7 @@ exit_code = set ${output.code}
Executes the provided native command and arguments.<br>
If no output variable is set, the command output will be flushed to the main out/err stream of the parent process.<br>
In addition, in order to fail the command in case of the child process failed, add the --fail_on_error flag.<br>
If an output variable is set, it will be used as a base variable name from which the command stout, stderr and exit code information can be pulled from.<br>
The actual output variable name will not be modified, instead new variables will be created using that variable name as a baseline:
@ -4163,7 +4164,8 @@ The actual output variable name will not be modified, instead new variables will
#### Parameters
The command to execute and its arguments.
* --fail-on-error - If no output variable is provided, it will cause an error in case the executed processed exists with an error exist code.
* The command to execute and its arguments.
#### Return Value

View file

@ -1,5 +1,5 @@
```sh
exec command [args]*
exec [--fail-on-error] command [args]*
output = exec command [args]*
stdout = set ${output.stdout}
@ -9,6 +9,7 @@ exit_code = set ${output.code}
Executes the provided native command and arguments.<br>
If no output variable is set, the command output will be flushed to the main out/err stream of the parent process.<br>
In addition, in order to fail the command in case of the child process failed, add the --fail_on_error flag.<br>
If an output variable is set, it will be used as a base variable name from which the command stout, stderr and exit code information can be pulled from.<br>
The actual output variable name will not be modified, instead new variables will be created using that variable name as a baseline:
@ -18,7 +19,8 @@ The actual output variable name will not be modified, instead new variables will
#### Parameters
The command to execute and its arguments.
* --fail-on-error - If no output variable is provided, it will cause an error in case the executed processed exists with an error exist code.
* The command to execute and its arguments.
#### Return Value

View file

@ -46,28 +46,48 @@ impl Command for CommandImpl {
) -> CommandResult {
let allow_input = output_variable.is_some();
let print_output = !allow_input;
match exec::exec(&arguments, print_output, allow_input, 0) {
Ok((stdout, stderr, exit_code)) => {
match output_variable {
Some(name) => {
let mut key = String::from(&name);
key.push_str(".stdout");
variables.insert(key.clone(), stdout);
key = String::from(&name);
key.push_str(".stderr");
variables.insert(key.clone(), stderr);
key = String::from(&name);
key.push_str(".code");
variables.insert(key.clone(), exit_code.to_string());
}
None => (),
};
CommandResult::Continue(None)
let (start_index, fail_on_error) = if !arguments.is_empty() {
if arguments[0] == "--fail-on-error" {
if output_variable.is_some() {
(1, true)
} else {
(1, false)
}
} else {
(0, false)
}
} else {
(0, false)
};
match exec::exec(&arguments, print_output, allow_input, start_index) {
Ok((stdout, stderr, exit_code)) => match output_variable {
Some(name) => {
let mut key = String::from(&name);
key.push_str(".stdout");
variables.insert(key.clone(), stdout);
key = String::from(&name);
key.push_str(".stderr");
variables.insert(key.clone(), stderr);
key = String::from(&name);
key.push_str(".code");
variables.insert(key.clone(), exit_code.to_string());
CommandResult::Continue(None)
}
None => {
if fail_on_error && exit_code != 0 {
CommandResult::Error(
format!("Error while executing command, exit code: {}", exit_code)
.to_string(),
)
} else {
CommandResult::Continue(None)
}
}
},
Err(error) => CommandResult::Error(error),
}
}

View file

@ -17,6 +17,20 @@ fn run_no_output() {
test::run_script_and_validate(vec![create("")], "exec echo test", CommandValidation::None);
}
#[test]
fn run_no_output_with_fail_on_error_valid() {
test::run_script_and_validate(
vec![create("")],
"exec --fail-on-error echo test",
CommandValidation::None,
);
}
#[test]
fn run_no_output_with_fail_on_error_invalid() {
test::run_script_and_error(vec![create("")], "exec --fail-on-error badcommand", "");
}
#[test]
fn run_with_output() {
let context = test::run_script_and_validate(

View file

@ -226,10 +226,13 @@ pub(crate) fn run_script_and_error(
let result = run_command(commands, script);
match result {
Ok(context) => {
assert_eq!(
context.variables.get(&output_variable.to_string()).unwrap(),
"false"
);
if !output_variable.is_empty() {
assert_eq!(
context.variables.get(&output_variable.to_string()).unwrap(),
"false"
);
}
assert_eq!(
context
.variables

View file

@ -1,12 +1,21 @@
fn test_echo
fn test_echo_with_output
output = exec echo hello world
stdout = trim ${output.stdout}
stderr = trim ${output.stderr}
exit_code = set ${output.code}
assert_eq ${stdout} "hello world"
assert_eq ${stderr} ""
assert_eq ${exit_code} 0
end
fn test_echo_without_output
exec echo hello world
end
fn test_echo_with_fail_on_error_flag
exec --fail-on-error echo hello world
end