From 50ed0f8625178c711f4dc449a08b2ede063031bf Mon Sep 17 00:00:00 2001 From: sagie gur ari Date: Fri, 3 Jan 2020 10:30:03 +0000 Subject: [PATCH] commands should not error on runtime errors, only on invalid user input --- docs/sdk.md | 16 ++++++++-------- duckscript_sdk/src/sdk/std/alias/help.md | 8 ++++---- duckscript_sdk/src/sdk/std/alias/mod.rs | 4 ++-- duckscript_sdk/src/sdk/std/env/cd/help.md | 4 ++-- duckscript_sdk/src/sdk/std/env/cd/mod.rs | 4 ++-- duckscript_sdk/src/sdk/std/env/cd/mod_test.rs | 6 +++++- duckscript_sdk/src/sdk/std/env/pwd/mod.rs | 2 +- duckscript_sdk/src/sdk/std/fs/mkdir/mod.rs | 2 +- duckscript_sdk/src/sdk/std/fs/mkdir/mod_test.rs | 3 ++- duckscript_sdk/src/sdk/std/fs/mv/mod.rs | 10 ++++------ duckscript_sdk/src/sdk/std/fs/mv/mod_test.rs | 5 +++-- duckscript_sdk/src/sdk/std/fs/print/help.md | 2 +- duckscript_sdk/src/sdk/std/fs/print/mod.rs | 2 +- duckscript_sdk/src/sdk/std/fs/print/mod_test.rs | 6 +++++- duckscript_sdk/src/sdk/std/fs/read/help.md | 2 +- duckscript_sdk/src/sdk/std/fs/read/mod.rs | 2 +- duckscript_sdk/src/sdk/std/fs/read/mod_test.rs | 6 +++++- duckscript_sdk/src/sdk/std/fs/rm/mod.rs | 2 +- duckscript_sdk/src/sdk/std/fs/rm/mod_test.rs | 6 +++++- duckscript_sdk/src/sdk/std/fs/rmdir/mod.rs | 2 +- duckscript_sdk/src/sdk/std/fs/rmdir/mod_test.rs | 10 ++++++---- duckscript_sdk/src/sdk/std/man/mod.rs | 3 ++- duckscript_sdk/src/sdk/std/man/mod_test.rs | 6 +++++- duckscript_sdk/src/sdk/std/math/calc/mod.rs | 2 +- duckscript_sdk/src/sdk/std/process/exec/mod.rs | 2 +- 25 files changed, 70 insertions(+), 47 deletions(-) diff --git a/docs/sdk.md b/docs/sdk.md index 199d42e..fa40351 100644 --- a/docs/sdk.md +++ b/docs/sdk.md @@ -41,7 +41,7 @@ ## sdk::Alias ```sh -alias command arguments +var = alias command arguments ``` This command enables to define new commands with default arguments.
@@ -53,16 +53,16 @@ Any number of arguments which will be added to the already defined arguments set #### Return Value -None +**true** if the alias was created, else **false**. #### Examples ```sh # This example creates a new **my_echo** alias that will print the prefix before the requested arguments. -alias my_echo echo [ECHO] +created = alias my_echo echo [ECHO] # This will print "[ECHO] hello world " -my_echo hello world +created = my_echo hello world ``` @@ -668,7 +668,7 @@ cd path Sets the current directory based on the input path.
If no path is provided, it will default to the user home directory.
-If the path does not exist, it will return an error. +If the path does not exist, it will return none. #### Parameters @@ -676,7 +676,7 @@ The new current directory. #### Return Value -The new current directory. +The new current directory or none in case of any error such as target directory not found. #### Examples @@ -1027,7 +1027,7 @@ A single parameter holding the file path. #### Return Value -The file content. +The file content or none if the file does not exist. #### Examples @@ -1053,7 +1053,7 @@ A single parameter holding the file path. #### Return Value -The file content. +The file content or none in case file does not exist. #### Examples diff --git a/duckscript_sdk/src/sdk/std/alias/help.md b/duckscript_sdk/src/sdk/std/alias/help.md index 25d42a6..d8583fd 100644 --- a/duckscript_sdk/src/sdk/std/alias/help.md +++ b/duckscript_sdk/src/sdk/std/alias/help.md @@ -1,5 +1,5 @@ ```sh -alias command arguments +var = alias command arguments ``` This command enables to define new commands with default arguments.
@@ -11,14 +11,14 @@ Any number of arguments which will be added to the already defined arguments set #### Return Value -None +**true** if the alias was created, else **false**. #### Examples ```sh # This example creates a new **my_echo** alias that will print the prefix before the requested arguments. -alias my_echo echo [ECHO] +created = alias my_echo echo [ECHO] # This will print "[ECHO] hello world " -my_echo hello world +created = my_echo hello world ``` diff --git a/duckscript_sdk/src/sdk/std/alias/mod.rs b/duckscript_sdk/src/sdk/std/alias/mod.rs index 94dd691..09a1b67 100755 --- a/duckscript_sdk/src/sdk/std/alias/mod.rs +++ b/duckscript_sdk/src/sdk/std/alias/mod.rs @@ -106,8 +106,8 @@ impl Command for CommandImpl { let sub_state = get_sub_state(ALIAS_STATE_KEY.to_string(), state); match create_alias_command(name, arguments[1..].to_vec(), commands, sub_state) { - Ok(_) => CommandResult::Continue(None), - Err(error) => CommandResult::Error(error), + Ok(_) => CommandResult::Continue(Some("true".to_string())), + Err(_) => CommandResult::Continue(Some("true".to_string())), } } } diff --git a/duckscript_sdk/src/sdk/std/env/cd/help.md b/duckscript_sdk/src/sdk/std/env/cd/help.md index 031b114..2991938 100644 --- a/duckscript_sdk/src/sdk/std/env/cd/help.md +++ b/duckscript_sdk/src/sdk/std/env/cd/help.md @@ -4,7 +4,7 @@ cd path Sets the current directory based on the input path.
If no path is provided, it will default to the user home directory.
-If the path does not exist, it will return an error. +If the path does not exist, it will return none. #### Parameters @@ -12,7 +12,7 @@ The new current directory. #### Return Value -The new current directory. +The new current directory or none in case of any error such as target directory not found. #### Examples diff --git a/duckscript_sdk/src/sdk/std/env/cd/mod.rs b/duckscript_sdk/src/sdk/std/env/cd/mod.rs index 8bfa60c..6c3afea 100755 --- a/duckscript_sdk/src/sdk/std/env/cd/mod.rs +++ b/duckscript_sdk/src/sdk/std/env/cd/mod.rs @@ -39,9 +39,9 @@ impl Command for CommandImpl { let directory_str = directory.to_string_lossy().into_owned(); CommandResult::Continue(Some(directory_str)) } - Err(error) => CommandResult::Error(error.to_string()), + Err(_) => CommandResult::Continue(None), }, - None => CommandResult::Error("Unable to get target directory path.".to_string()), + None => CommandResult::Continue(None), } } } diff --git a/duckscript_sdk/src/sdk/std/env/cd/mod_test.rs b/duckscript_sdk/src/sdk/std/env/cd/mod_test.rs index fa3a020..91bac3b 100644 --- a/duckscript_sdk/src/sdk/std/env/cd/mod_test.rs +++ b/duckscript_sdk/src/sdk/std/env/cd/mod_test.rs @@ -10,7 +10,11 @@ fn common_functions() { #[test] #[ignore] fn run_to_directory_does_not_exist() { - test::run_script_and_fail(vec![create("")], "out = cd test123_bad"); + test::run_script_and_validate( + vec![create("")], + "out = cd test123_bad", + CommandValidation::None, + ); } #[test] diff --git a/duckscript_sdk/src/sdk/std/env/pwd/mod.rs b/duckscript_sdk/src/sdk/std/env/pwd/mod.rs index 4058a93..37ba075 100755 --- a/duckscript_sdk/src/sdk/std/env/pwd/mod.rs +++ b/duckscript_sdk/src/sdk/std/env/pwd/mod.rs @@ -30,7 +30,7 @@ impl Command for CommandImpl { println!("{}", &directory); CommandResult::Continue(Some(directory.to_string())) } - Err(error) => CommandResult::Error(error.to_string()), + Err(_) => CommandResult::Continue(None), } } } diff --git a/duckscript_sdk/src/sdk/std/fs/mkdir/mod.rs b/duckscript_sdk/src/sdk/std/fs/mkdir/mod.rs index 12d31fe..12c31f6 100755 --- a/duckscript_sdk/src/sdk/std/fs/mkdir/mod.rs +++ b/duckscript_sdk/src/sdk/std/fs/mkdir/mod.rs @@ -28,7 +28,7 @@ impl Command for CommandImpl { } else { match io::create_directory(&arguments[0]) { Ok(_) => CommandResult::Continue(Some("true".to_string())), - Err(error) => CommandResult::Error(error), + Err(_) => CommandResult::Continue(Some("false".to_string())), } } } diff --git a/duckscript_sdk/src/sdk/std/fs/mkdir/mod_test.rs b/duckscript_sdk/src/sdk/std/fs/mkdir/mod_test.rs index 20c8790..dbeba4e 100644 --- a/duckscript_sdk/src/sdk/std/fs/mkdir/mod_test.rs +++ b/duckscript_sdk/src/sdk/std/fs/mkdir/mod_test.rs @@ -41,8 +41,9 @@ fn run_directory_exists_as_file() { ); assert!(result.is_ok()); - test::run_script_and_fail( + test::run_script_and_validate( vec![create("")], "out = mkdir ./target/_duckscript/mkdir/run_directory_exists_as_file/test.txt", + CommandValidation::Match("out".to_string(), "false".to_string()), ); } diff --git a/duckscript_sdk/src/sdk/std/fs/mv/mod.rs b/duckscript_sdk/src/sdk/std/fs/mv/mod.rs index 4a4d066..c51d39e 100755 --- a/duckscript_sdk/src/sdk/std/fs/mv/mod.rs +++ b/duckscript_sdk/src/sdk/std/fs/mv/mod.rs @@ -32,9 +32,7 @@ impl Command for CommandImpl { let source_path = Path::new(&arguments[0]); if !source_path.exists() { - CommandResult::Error( - format!("Source path: {} not found.", &arguments[0]).to_string(), - ) + CommandResult::Continue(Some("false".to_string())) } else { let target_path = Path::new(&arguments[1]); let source_file = source_path.is_file(); @@ -43,7 +41,7 @@ impl Command for CommandImpl { if source_file && target_file { match fs::rename(&arguments[0], &arguments[1]) { Ok(_) => CommandResult::Continue(Some("true".to_string())), - Err(error) => CommandResult::Error(error.to_string()), + Err(_) => CommandResult::Continue(Some("false".to_string())), } } else { match io::create_directory(&arguments[1]) { @@ -52,10 +50,10 @@ impl Command for CommandImpl { let from_paths = vec![&arguments[0]]; match move_items(&from_paths, &arguments[1], &options) { Ok(_) => CommandResult::Continue(Some("true".to_string())), - Err(error) => CommandResult::Error(error.to_string()), + Err(_) => CommandResult::Continue(Some("false".to_string())), } } - Err(error) => CommandResult::Error(error), + Err(_) => CommandResult::Continue(Some("false".to_string())), } } } diff --git a/duckscript_sdk/src/sdk/std/fs/mv/mod_test.rs b/duckscript_sdk/src/sdk/std/fs/mv/mod_test.rs index eb20420..447d665 100644 --- a/duckscript_sdk/src/sdk/std/fs/mv/mod_test.rs +++ b/duckscript_sdk/src/sdk/std/fs/mv/mod_test.rs @@ -19,9 +19,10 @@ fn run_single_path_provided() { #[test] fn run_input_path_not_exists() { - test::run_script_and_fail( + test::run_script_and_validate( vec![create("")], - "mv ./target/_duckscript/mv/not_exists.txt ./target/_duckscript/mv/not_exists/", + "out = mv ./target/_duckscript/mv/not_exists.txt ./target/_duckscript/mv/not_exists/", + CommandValidation::Match("out".to_string(), "false".to_string()), ); } diff --git a/duckscript_sdk/src/sdk/std/fs/print/help.md b/duckscript_sdk/src/sdk/std/fs/print/help.md index 54efaa0..35c335a 100644 --- a/duckscript_sdk/src/sdk/std/fs/print/help.md +++ b/duckscript_sdk/src/sdk/std/fs/print/help.md @@ -11,7 +11,7 @@ A single parameter holding the file path. #### Return Value -The file content. +The file content or none if the file does not exist. #### Examples diff --git a/duckscript_sdk/src/sdk/std/fs/print/mod.rs b/duckscript_sdk/src/sdk/std/fs/print/mod.rs index db1a705..19d62a4 100755 --- a/duckscript_sdk/src/sdk/std/fs/print/mod.rs +++ b/duckscript_sdk/src/sdk/std/fs/print/mod.rs @@ -35,7 +35,7 @@ impl Command for CommandImpl { CommandResult::Continue(Some(text)) } - Err(error) => CommandResult::Error(error.to_string()), + Err(_) => CommandResult::Continue(None), } } } diff --git a/duckscript_sdk/src/sdk/std/fs/print/mod_test.rs b/duckscript_sdk/src/sdk/std/fs/print/mod_test.rs index cda2da1..9ab9a3c 100644 --- a/duckscript_sdk/src/sdk/std/fs/print/mod_test.rs +++ b/duckscript_sdk/src/sdk/std/fs/print/mod_test.rs @@ -9,7 +9,11 @@ fn common_functions() { #[test] fn run_no_file_provided() { - test::run_script_and_fail(vec![create("")], "cat ./Cargo2.toml"); + test::run_script_and_validate( + vec![create("")], + "out = cat ./Cargo2.toml", + CommandValidation::None, + ); } #[test] diff --git a/duckscript_sdk/src/sdk/std/fs/read/help.md b/duckscript_sdk/src/sdk/std/fs/read/help.md index db582d6..4dfb1b9 100644 --- a/duckscript_sdk/src/sdk/std/fs/read/help.md +++ b/duckscript_sdk/src/sdk/std/fs/read/help.md @@ -10,7 +10,7 @@ A single parameter holding the file path. #### Return Value -The file content. +The file content or none in case file does not exist. #### Examples diff --git a/duckscript_sdk/src/sdk/std/fs/read/mod.rs b/duckscript_sdk/src/sdk/std/fs/read/mod.rs index ffb8d7e..247c38b 100755 --- a/duckscript_sdk/src/sdk/std/fs/read/mod.rs +++ b/duckscript_sdk/src/sdk/std/fs/read/mod.rs @@ -30,7 +30,7 @@ impl Command for CommandImpl { match result { Ok(text) => CommandResult::Continue(Some(text)), - Err(error) => CommandResult::Error(error.to_string()), + Err(_) => CommandResult::Continue(None), } } } diff --git a/duckscript_sdk/src/sdk/std/fs/read/mod_test.rs b/duckscript_sdk/src/sdk/std/fs/read/mod_test.rs index ce96729..39cdf50 100644 --- a/duckscript_sdk/src/sdk/std/fs/read/mod_test.rs +++ b/duckscript_sdk/src/sdk/std/fs/read/mod_test.rs @@ -14,7 +14,11 @@ fn run_no_file_provided() { #[test] fn run_file_not_exists() { - test::run_script_and_fail(vec![create("")], "readfile ./Cargo2.toml"); + test::run_script_and_validate( + vec![create("")], + "readfile ./Cargo2.toml", + CommandValidation::None, + ); } #[test] diff --git a/duckscript_sdk/src/sdk/std/fs/rm/mod.rs b/duckscript_sdk/src/sdk/std/fs/rm/mod.rs index b1f55a9..15c20c2 100755 --- a/duckscript_sdk/src/sdk/std/fs/rm/mod.rs +++ b/duckscript_sdk/src/sdk/std/fs/rm/mod.rs @@ -53,7 +53,7 @@ impl Command for CommandImpl { match result { Ok(_) => CommandResult::Continue(Some("true".to_string())), - Err(error) => CommandResult::Error(error.to_string()), + Err(_) => CommandResult::Continue(Some("false".to_string())), } } } diff --git a/duckscript_sdk/src/sdk/std/fs/rm/mod_test.rs b/duckscript_sdk/src/sdk/std/fs/rm/mod_test.rs index 5cc8c02..f5b7755 100644 --- a/duckscript_sdk/src/sdk/std/fs/rm/mod_test.rs +++ b/duckscript_sdk/src/sdk/std/fs/rm/mod_test.rs @@ -32,7 +32,11 @@ fn run_path_not_empty_not_recursive() { let result = io::create_directory("./target/_duckscript/rm/not_empty/dir1"); assert!(result.is_ok()); - test::run_script_and_fail(vec![create("")], "rm ./target/_duckscript/rm/not_empty"); + test::run_script_and_validate( + vec![create("")], + "out = rm ./target/_duckscript/rm/not_empty", + CommandValidation::Match("out".to_string(), "false".to_string()), + ); } #[test] diff --git a/duckscript_sdk/src/sdk/std/fs/rmdir/mod.rs b/duckscript_sdk/src/sdk/std/fs/rmdir/mod.rs index 47852d3..efb7515 100755 --- a/duckscript_sdk/src/sdk/std/fs/rmdir/mod.rs +++ b/duckscript_sdk/src/sdk/std/fs/rmdir/mod.rs @@ -36,7 +36,7 @@ impl Command for CommandImpl { match result { Ok(_) => CommandResult::Continue(Some("true".to_string())), - Err(error) => CommandResult::Error(error.to_string()), + Err(_) => CommandResult::Continue(Some("false".to_string())), } } } diff --git a/duckscript_sdk/src/sdk/std/fs/rmdir/mod_test.rs b/duckscript_sdk/src/sdk/std/fs/rmdir/mod_test.rs index c5ddfa8..f321780 100644 --- a/duckscript_sdk/src/sdk/std/fs/rmdir/mod_test.rs +++ b/duckscript_sdk/src/sdk/std/fs/rmdir/mod_test.rs @@ -27,9 +27,10 @@ fn run_path_not_empty() { let result = io::create_directory("./target/_duckscript/rmdir/not_empty/dir1"); assert!(result.is_ok()); - test::run_script_and_fail( + test::run_script_and_validate( vec![create("")], - "rmdir ./target/_duckscript/rmdir/not_empty", + "out = rmdir ./target/_duckscript/rmdir/not_empty", + CommandValidation::Match("out".to_string(), "false".to_string()), ); } @@ -38,9 +39,10 @@ fn run_path_is_file() { let result = io::create_empty_file("./target/_duckscript/rmdir/file.txt"); assert!(result.is_ok()); - test::run_script_and_fail( + test::run_script_and_validate( vec![create("")], - "rmdir ./target/_duckscript/rmdir/file.txt", + "out = rmdir ./target/_duckscript/rmdir/file.txt", + CommandValidation::Match("out".to_string(), "false".to_string()), ); } diff --git a/duckscript_sdk/src/sdk/std/man/mod.rs b/duckscript_sdk/src/sdk/std/man/mod.rs index 8131332..cdb6ce9 100755 --- a/duckscript_sdk/src/sdk/std/man/mod.rs +++ b/duckscript_sdk/src/sdk/std/man/mod.rs @@ -63,7 +63,8 @@ impl Command for CommandImpl { if name == &self.name() || self.aliases().contains(name) { print_help(self.help(), &self.name()) } else { - CommandResult::Error(format!("Command: {} not found.", name).to_string()) + println!("Command: {} not found.", name); + CommandResult::Continue(None) } } } diff --git a/duckscript_sdk/src/sdk/std/man/mod_test.rs b/duckscript_sdk/src/sdk/std/man/mod_test.rs index 794d2cb..14d3f3e 100644 --- a/duckscript_sdk/src/sdk/std/man/mod_test.rs +++ b/duckscript_sdk/src/sdk/std/man/mod_test.rs @@ -28,7 +28,11 @@ fn run_self_command_found() { #[test] fn run_command_not_found() { - test::run_script_and_fail(vec![create("")], "out = man badcommand"); + test::run_script_and_validate( + vec![create("")], + "out = man badcommand", + CommandValidation::None, + ); } #[test] diff --git a/duckscript_sdk/src/sdk/std/math/calc/mod.rs b/duckscript_sdk/src/sdk/std/math/calc/mod.rs index e94dc03..edd997b 100755 --- a/duckscript_sdk/src/sdk/std/math/calc/mod.rs +++ b/duckscript_sdk/src/sdk/std/math/calc/mod.rs @@ -24,7 +24,7 @@ impl Command for CommandImpl { fn run(&self, arguments: Vec) -> CommandResult { if arguments.is_empty() { - CommandResult::Error("Assert failed, empty value.".to_string()) + CommandResult::Error("Missing input.".to_string()) } else { let operation = arguments.join(" "); diff --git a/duckscript_sdk/src/sdk/std/process/exec/mod.rs b/duckscript_sdk/src/sdk/std/process/exec/mod.rs index 4d17df4..3a0a759 100755 --- a/duckscript_sdk/src/sdk/std/process/exec/mod.rs +++ b/duckscript_sdk/src/sdk/std/process/exec/mod.rs @@ -42,7 +42,7 @@ impl Command for CommandImpl { _line: usize, ) -> CommandResult { if arguments.is_empty() { - CommandResult::Error("Command provided.".to_string()) + CommandResult::Error("Command not provided.".to_string()) } else { let mut command = ProcessCommand::new(&arguments[0]);