commands should not error on runtime errors, only on invalid user input

This commit is contained in:
sagie gur ari 2020-01-03 10:30:03 +00:00
parent 9d8c986a22
commit 50ed0f8625
25 changed files with 70 additions and 47 deletions

View file

@ -41,7 +41,7 @@
<a name="sdk__Alias"></a>
## sdk::Alias
```sh
alias command arguments
var = alias command arguments
```
This command enables to define new commands with default arguments.<br>
@ -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.<br>
If no path is provided, it will default to the user home directory.<br>
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

View file

@ -1,5 +1,5 @@
```sh
alias command arguments
var = alias command arguments
```
This command enables to define new commands with default arguments.<br>
@ -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
```

View file

@ -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())),
}
}
}

View file

@ -4,7 +4,7 @@ cd path
Sets the current directory based on the input path.<br>
If no path is provided, it will default to the user home directory.<br>
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

View file

@ -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),
}
}
}

View file

@ -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]

View file

@ -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),
}
}
}

View file

@ -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())),
}
}
}

View file

@ -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()),
);
}

View file

@ -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())),
}
}
}

View file

@ -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()),
);
}

View file

@ -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

View file

@ -35,7 +35,7 @@ impl Command for CommandImpl {
CommandResult::Continue(Some(text))
}
Err(error) => CommandResult::Error(error.to_string()),
Err(_) => CommandResult::Continue(None),
}
}
}

View file

@ -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]

View file

@ -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

View file

@ -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),
}
}
}

View file

@ -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]

View file

@ -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())),
}
}
}

View file

@ -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]

View file

@ -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())),
}
}
}

View file

@ -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()),
);
}

View file

@ -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)
}
}
}

View file

@ -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]

View file

@ -24,7 +24,7 @@ impl Command for CommandImpl {
fn run(&self, arguments: Vec<String>) -> CommandResult {
if arguments.is_empty() {
CommandResult::Error("Assert failed, empty value.".to_string())
CommandResult::Error("Missing input.".to_string())
} else {
let operation = arguments.join(" ");

View file

@ -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]);