New is_command_defined command #130

This commit is contained in:
sagie gur ari 2020-08-27 09:10:11 +00:00
parent b267106753
commit 675ed4793e
11 changed files with 152 additions and 12 deletions

View file

@ -5,6 +5,7 @@
* New --get-exit-code flag for exec command #127
* New random_range and random_text commands #128
* New semver_parse, semver_is_equal and semver_is_newer commands #129
* New is_command_defined command #130
### v0.6.6 (2020-08-14)

View file

@ -1,7 +1,7 @@
# Table of Contents
* [std::Echo (echo)](#std__Echo)
* [std::Eval (eval)](#std__Eval)
* [std::IsDefined (is_defined)](#std__IsDefined)
* [std::IsCommandDefined (is_command_defined)](#std__IsCommandDefined)
* [std::Noop (noop)](#std__Noop)
* [std::Not (not)](#std__Not)
* [std::ReadUserInput (read)](#std__ReadUserInput)
@ -233,32 +233,31 @@ eval ${command} hello world
#### Aliases:
eval
<a name="std__IsDefined"></a>
## std::IsDefined
<a name="std__IsCommandDefined"></a>
## std::IsCommandDefined
```sh
var = is_defined key
var = is_command_defined key
```
Returns true if the provided variable name (not value) exists.
Returns true if the provided command name exists.
#### Parameters
The variable name.
The command name.
#### Return Value
True if the variable is defined.
True if the command exists.
#### Examples
```sh
key = set "hello world"
exists = is_defined key
exists = is_command_defined exec
```
#### Aliases:
is_defined
is_command_defined
<a name="std__Noop"></a>
## std::Noop

View file

@ -0,0 +1,19 @@
```sh
var = is_command_defined key
```
Returns true if the provided command name exists.
#### Parameters
The command name.
#### Return Value
True if the command exists.
#### Examples
```sh
exists = is_command_defined exec
```

View file

@ -0,0 +1,61 @@
use crate::utils::pckg;
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;
#[derive(Clone)]
pub(crate) struct CommandImpl {
package: String,
}
impl Command for CommandImpl {
fn name(&self) -> String {
pckg::concat(&self.package, "IsCommandDefined")
}
fn aliases(&self) -> Vec<String> {
vec!["is_command_defined".to_string()]
}
fn help(&self) -> String {
include_str!("help.md").to_string()
}
fn clone_and_box(&self) -> Box<dyn Command> {
Box::new((*self).clone())
}
fn requires_context(&self) -> bool {
true
}
fn run_with_context(
&self,
arguments: Vec<String>,
_state: &mut HashMap<String, StateValue>,
_variables: &mut HashMap<String, String>,
_output_variable: Option<String>,
_instructions: &Vec<Instruction>,
commands: &mut Commands,
_line: usize,
) -> CommandResult {
if arguments.is_empty() {
CommandResult::Error("Command name not provided.".to_string())
} else {
let result = commands.exists(&arguments[0]);
CommandResult::Continue(Some(result.to_string()))
}
}
}
pub(crate) fn create(package: &str) -> Box<dyn Command> {
Box::new(CommandImpl {
package: package.to_string(),
})
}

View file

@ -0,0 +1,40 @@
use super::*;
use crate::test;
use crate::test::{CommandValidation, SetCommand};
#[test]
fn common_functions() {
test::test_common_command_functions(create(""));
}
#[test]
fn run_no_args() {
test::run_script_and_error(vec![create("")], "out = is_command_defined", "out");
}
#[test]
fn run_with_empty_string() {
test::run_script_and_validate(
vec![create("")],
r#"out = is_command_defined """#,
CommandValidation::Match("out".to_string(), "false".to_string()),
);
}
#[test]
fn run_not_defined() {
test::run_script_and_validate(
vec![create("")],
r#"out = is_command_defined badcommand"#,
CommandValidation::Match("out".to_string(), "false".to_string()),
);
}
#[test]
fn run_defined() {
test::run_script_and_validate(
vec![create(""), Box::new(SetCommand {})],
r#"out = is_command_defined test_set"#,
CommandValidation::Match("out".to_string(), "true".to_string()),
);
}

View file

@ -5,7 +5,7 @@ mod env;
mod eval;
mod flowcontrol;
mod fs;
mod is_defined;
mod is_command_defined;
mod json;
mod lib;
mod man;
@ -34,7 +34,7 @@ static PACKAGE: &str = "std";
pub(crate) fn load(commands: &mut Commands) -> Result<(), ScriptError> {
commands.set(echo::create(PACKAGE))?;
commands.set(eval::create(PACKAGE))?;
commands.set(is_defined::create(PACKAGE))?;
commands.set(is_command_defined::create(PACKAGE))?;
commands.set(man::create(PACKAGE))?;
commands.set(noop::create(PACKAGE))?;
commands.set(not::create(PACKAGE))?;

View file

@ -1,5 +1,6 @@
mod get_all_var_names;
mod get_by_name;
mod is_defined;
pub(crate) mod set;
mod set_by_name;
mod unset;
@ -16,6 +17,7 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr
commands.set(get_all_var_names::create(&package))?;
commands.set(get_by_name::create(&package))?;
commands.set(is_defined::create(PACKAGE))?;
commands.set(set::create(&package))?;
commands.set(set_by_name::create(&package))?;
commands.set(unset::create(&package)?)?;

View file

@ -0,0 +1,18 @@
fn test_not_defined
defined = is_command_defined badcommand
assert_false ${defined}
end
fn test_defined
defined = is_command_defined is_command_defined
assert ${defined}
defined = is_command_defined assert
assert ${defined}
defined = is_command_defined test_defined
assert ${defined}
end