mirror of
https://github.com/sagiegurari/duckscript
synced 2024-10-06 16:09:39 +00:00
Break with an error if exit code with non zero value #144
This commit is contained in:
parent
e4a1eea783
commit
745a4facb2
|
@ -1,5 +1,9 @@
|
|||
## CHANGELOG
|
||||
|
||||
### v0.7.0
|
||||
|
||||
* Runtime - \[Breaking Change\] Break with an error if exit code with non zero value #144
|
||||
|
||||
### v0.6.9 (2020-10-16)
|
||||
|
||||
* New while loop command #138
|
||||
|
|
|
@ -151,9 +151,30 @@ fn run_instructions(
|
|||
|
||||
match command_result {
|
||||
CommandResult::Exit(output) => {
|
||||
update_output(&mut runtime.context.variables, output_variable, output);
|
||||
update_output(
|
||||
&mut runtime.context.variables,
|
||||
output_variable,
|
||||
output.clone(),
|
||||
);
|
||||
end_reason = EndReason::ExitCalled;
|
||||
|
||||
if repl_mode {
|
||||
return Ok((runtime.context, end_reason));
|
||||
}
|
||||
|
||||
if let Some(exit_code_str) = output {
|
||||
if let Ok(exit_code) = exit_code_str.parse::<i32>() {
|
||||
if exit_code != 0 {
|
||||
return Err(ScriptError {
|
||||
info: ErrorInfo::Runtime(
|
||||
format!("Exit with error code: {}", exit_code).to_string(),
|
||||
Some(meta_info.clone()),
|
||||
),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
CommandResult::Error(error) => {
|
||||
|
|
|
@ -250,7 +250,7 @@ fn run_instructions_exit_result_no_output() {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn run_instructions_exit_result_with_output() {
|
||||
fn run_instructions_exit_result_with_string_output() {
|
||||
let mut instructions = vec![];
|
||||
|
||||
let mut script_instruction = ScriptInstruction::new();
|
||||
|
@ -285,6 +285,69 @@ fn run_instructions_exit_result_with_output() {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_instructions_exit_result_with_0_output() {
|
||||
let mut instructions = vec![];
|
||||
|
||||
let mut script_instruction = ScriptInstruction::new();
|
||||
script_instruction.command = Some("exit".to_string());
|
||||
script_instruction.output = Some("out".to_string());
|
||||
script_instruction.arguments = Some(vec!["0".to_string()]);
|
||||
instructions.push(Instruction {
|
||||
meta_info: InstructionMetaInfo::new(),
|
||||
instruction_type: InstructionType::Script(script_instruction),
|
||||
});
|
||||
script_instruction = ScriptInstruction::new();
|
||||
script_instruction.command = Some("bad".to_string());
|
||||
instructions.push(Instruction {
|
||||
meta_info: InstructionMetaInfo::new(),
|
||||
instruction_type: InstructionType::Script(script_instruction),
|
||||
});
|
||||
|
||||
let mut context = Context::new();
|
||||
let result = context.commands.set(Box::new(ExitCommand {}));
|
||||
assert!(result.is_ok());
|
||||
|
||||
let runtime = create_runtime(instructions, context);
|
||||
|
||||
let context_result = run_instructions(runtime, 0, false);
|
||||
|
||||
assert!(context_result.is_ok());
|
||||
let (updated_context, end_reason) = context_result.unwrap();
|
||||
assert_end_reason_exit_called(end_reason);
|
||||
assert_eq!(updated_context.variables.get("out"), Some(&"0".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_instructions_exit_result_with_error_code_output() {
|
||||
let mut instructions = vec![];
|
||||
|
||||
let mut script_instruction = ScriptInstruction::new();
|
||||
script_instruction.command = Some("exit".to_string());
|
||||
script_instruction.output = Some("out".to_string());
|
||||
script_instruction.arguments = Some(vec!["1".to_string()]);
|
||||
instructions.push(Instruction {
|
||||
meta_info: InstructionMetaInfo::new(),
|
||||
instruction_type: InstructionType::Script(script_instruction),
|
||||
});
|
||||
script_instruction = ScriptInstruction::new();
|
||||
script_instruction.command = Some("bad".to_string());
|
||||
instructions.push(Instruction {
|
||||
meta_info: InstructionMetaInfo::new(),
|
||||
instruction_type: InstructionType::Script(script_instruction),
|
||||
});
|
||||
|
||||
let mut context = Context::new();
|
||||
let result = context.commands.set(Box::new(ExitCommand {}));
|
||||
assert!(result.is_ok());
|
||||
|
||||
let runtime = create_runtime(instructions, context);
|
||||
|
||||
let context_result = run_instructions(runtime, 0, false);
|
||||
|
||||
assert!(context_result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_instructions_error_result() {
|
||||
let mut instructions = vec![];
|
||||
|
|
|
@ -6,7 +6,7 @@ Exits the script with the given code stored in the output variable.
|
|||
|
||||
#### Parameters
|
||||
|
||||
A positive number as exit code or none for 0.
|
||||
A number as exit code or none for 0.
|
||||
|
||||
#### Return Value
|
||||
|
||||
|
|
|
@ -31,7 +31,7 @@ impl Command for CommandImpl {
|
|||
if arguments.is_empty() {
|
||||
CommandResult::Exit(Some("0".to_string()))
|
||||
} else {
|
||||
match arguments[0].parse::<u32>() {
|
||||
match arguments[0].parse::<i32>() {
|
||||
Ok(_) => CommandResult::Exit(Some(arguments[0].clone())),
|
||||
Err(_) => {
|
||||
CommandResult::Error(format!("Invalid exit code: {}", arguments[0]).to_string())
|
||||
|
|
|
@ -26,17 +26,8 @@ fn run_0() {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn run_positive_number() {
|
||||
test::run_script_and_validate(
|
||||
vec![create("")],
|
||||
"out = exit 10",
|
||||
CommandValidation::Match("out".to_string(), "10".to_string()),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_negative_number() {
|
||||
test::run_script_and_error(vec![create("")], "out = exit -10", "out");
|
||||
fn run_number() {
|
||||
test::run_script_and_crash(vec![create("")], "out = exit 1");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
Loading…
Reference in a new issue