From e1f399b0c78832c2d907687c24ec97bf5dc39619 Mon Sep 17 00:00:00 2001 From: sagie gur ari Date: Wed, 22 Jan 2020 20:28:55 +0000 Subject: [PATCH] New set_error command #68 --- CHANGELOG.md | 1 + docs/sdk.md | 32 ++++++++++ duckscript_sdk/src/sdk/std/on_error/mod.rs | 2 + .../src/sdk/std/on_error/set_error/help.md | 24 +++++++ .../src/sdk/std/on_error/set_error/mod.rs | 63 +++++++++++++++++++ .../sdk/std/on_error/set_error/mod_test.rs | 38 +++++++++++ test/std/on_error/set_error_test.ds | 8 +++ 7 files changed, 168 insertions(+) create mode 100644 duckscript_sdk/src/sdk/std/on_error/set_error/help.md create mode 100755 duckscript_sdk/src/sdk/std/on_error/set_error/mod.rs create mode 100644 duckscript_sdk/src/sdk/std/on_error/set_error/mod_test.rs create mode 100644 test/std/on_error/set_error_test.ds diff --git a/CHANGELOG.md b/CHANGELOG.md index f15c01a..72c8207 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### v0.1.8 +* New set_error command #68 * New is_array command #70 * Test errors in duckscript based tests will break build flow. * New --version cli option. diff --git a/docs/sdk.md b/docs/sdk.md index 9764e14..c101cf1 100644 --- a/docs/sdk.md +++ b/docs/sdk.md @@ -34,6 +34,7 @@ * [std::error::GetLastError (get_last_error)](#std__error__GetLastError) * [std::error::GetLastErrorLine (get_last_error_line)](#std__error__GetLastErrorLine) * [std::error::GetLastErrorSource (get_last_error_source)](#std__error__GetLastErrorSource) +* [std::error::SetError (set_error)](#std__error__SetError) * [std::error::SetExitOnError (exit_on_error, set_exit_on_error)](#std__error__SetExitOnError) * [std::fs::CopyPath (cp)](#std__fs__CopyPath) * [std::fs::CreateDirectory (mkdir)](#std__fs__CreateDirectory) @@ -1290,6 +1291,37 @@ echo Error Source File: ${source} #### Aliases: get_last_error_source + +## std::error::SetError +```sh +set_error message +``` + +Sets the last error which is accessible via get_last_error.
+This command will not trigger the on_error command flow. + +#### Parameters + +The error message. + +#### Return Value + +None + +#### Examples + +```sh +set_error "my error message" + +error = get_last_error + +assert_eq ${error} "my error message" +``` + + +#### Aliases: +set_error + ## std::error::SetExitOnError ```sh diff --git a/duckscript_sdk/src/sdk/std/on_error/mod.rs b/duckscript_sdk/src/sdk/std/on_error/mod.rs index c52ac23..7dcad54 100755 --- a/duckscript_sdk/src/sdk/std/on_error/mod.rs +++ b/duckscript_sdk/src/sdk/std/on_error/mod.rs @@ -3,6 +3,7 @@ mod get_last_error; mod get_last_error_line; mod get_last_error_source; mod on_error; +mod set_error; use crate::utils::pckg; use crate::utils::state::get_core_sub_state_for_command; @@ -36,6 +37,7 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr commands.set(get_last_error_line::create(&package))?; commands.set(get_last_error_source::create(&package))?; commands.set(on_error::create(&package))?; + commands.set(set_error::create(&package))?; Ok(()) } diff --git a/duckscript_sdk/src/sdk/std/on_error/set_error/help.md b/duckscript_sdk/src/sdk/std/on_error/set_error/help.md new file mode 100644 index 0000000..fe00b94 --- /dev/null +++ b/duckscript_sdk/src/sdk/std/on_error/set_error/help.md @@ -0,0 +1,24 @@ +```sh +set_error message +``` + +Sets the last error which is accessible via get_last_error.
+This command will not trigger the on_error command flow. + +#### Parameters + +The error message. + +#### Return Value + +None + +#### Examples + +```sh +set_error "my error message" + +error = get_last_error + +assert_eq ${error} "my error message" +``` diff --git a/duckscript_sdk/src/sdk/std/on_error/set_error/mod.rs b/duckscript_sdk/src/sdk/std/on_error/set_error/mod.rs new file mode 100755 index 0000000..a189bff --- /dev/null +++ b/duckscript_sdk/src/sdk/std/on_error/set_error/mod.rs @@ -0,0 +1,63 @@ +use crate::sdk::std::on_error::STATE_KEY; +use crate::utils::pckg; +use crate::utils::state::get_core_sub_state_for_command; +use duckscript::types::command::{Command, CommandResult, Commands}; +use duckscript::types::instruction::Instruction; +use duckscript::types::runtime::StateValue; +use std::collections::HashMap; + +#[cfg(test)] +#[path = "./mod_test.rs"] +mod mod_test; + +struct CommandImpl { + package: String, +} + +impl Command for CommandImpl { + fn name(&self) -> String { + pckg::concat(&self.package, "SetError") + } + + fn aliases(&self) -> Vec { + vec!["set_error".to_string()] + } + + fn help(&self) -> String { + include_str!("help.md").to_string() + } + + fn requires_context(&self) -> bool { + true + } + + fn run_with_context( + &self, + arguments: Vec, + state: &mut HashMap, + _variables: &mut HashMap, + _output_variable: Option, + _instructions: &Vec, + _commands: &mut Commands, + line: usize, + ) -> CommandResult { + if !arguments.is_empty() { + let error = arguments[0].clone(); + + let sub_state = get_core_sub_state_for_command(state, STATE_KEY.to_string()); + sub_state.insert("error".to_string(), StateValue::String(error)); + sub_state.insert("line".to_string(), StateValue::String(line.to_string())); + sub_state.remove("source"); + + CommandResult::Continue(None) + } else { + CommandResult::Error("Invalid input provided.".to_string()) + } + } +} + +pub(crate) fn create(package: &str) -> Box { + Box::new(CommandImpl { + package: package.to_string(), + }) +} diff --git a/duckscript_sdk/src/sdk/std/on_error/set_error/mod_test.rs b/duckscript_sdk/src/sdk/std/on_error/set_error/mod_test.rs new file mode 100644 index 0000000..730dd1c --- /dev/null +++ b/duckscript_sdk/src/sdk/std/on_error/set_error/mod_test.rs @@ -0,0 +1,38 @@ +use super::*; +use crate::test; +use crate::test::CommandValidation; + +#[test] +fn common_functions() { + test::test_common_command_functions(create("")); +} + +#[test] +fn run_invalid_args() { + test::run_script_and_error(vec![create("")], "out = set_error", "out"); +} + +#[test] +fn run_valid() { + let context = test::run_script_and_validate( + vec![create("")], + "out = set_error text", + CommandValidation::None, + ); + + let sub_state_value = context.state.get("duckscriptsdk::on_error").unwrap(); + match sub_state_value { + StateValue::SubState(sub_state) => { + match sub_state.get("error").unwrap() { + StateValue::String(value) => assert_eq!(value, "text"), + _ => panic!("Invalid value type."), + }; + match sub_state.get("line").unwrap() { + StateValue::String(value) => assert_eq!(value, "0"), + _ => panic!("Invalid value type."), + }; + assert!(sub_state.get("source").is_none()); + } + _ => panic!("Invalid sub state type."), + } +} diff --git a/test/std/on_error/set_error_test.ds b/test/std/on_error/set_error_test.ds new file mode 100644 index 0000000..4a733cb --- /dev/null +++ b/test/std/on_error/set_error_test.ds @@ -0,0 +1,8 @@ + +function test_set_error + set_error "my error message" + + error = get_last_error + + assert_eq ${error} "my error message" +end