Merge pull request #131 from sagiegurari/0.6.7

0.6.7
This commit is contained in:
Sagie Gur-Ari 2020-08-27 02:44:59 -07:00 committed by GitHub
commit ffb3970efd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
37 changed files with 1105 additions and 28 deletions

View file

@ -1,5 +1,12 @@
## CHANGELOG
### v0.6.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)
* Bug fix in exec command with fail on error flag.

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)
@ -129,9 +129,14 @@
* [std::process::ProcessID (pid, process_id)](#std__process__ProcessID)
* [std::process::Spawn (spawn)](#std__process__Spawn)
* [std::process::Watchdog (watchdog)](#std__process__Watchdog)
* [std::random::Range (random_range, rand_range)](#std__random__Range)
* [std::random::Text (random_text, rand_text)](#std__random__Text)
* [std::scope::Clear (clear_scope)](#std__scope__Clear)
* [std::scope::PopStack (scope_pop_stack)](#std__scope__PopStack)
* [std::scope::PushStack (scope_push_stack)](#std__scope__PushStack)
* [std::semver::IsEqual (semver_is_equal)](#std__semver__IsEqual)
* [std::semver::IsNewer (semver_is_newer)](#std__semver__IsNewer)
* [std::semver::Parse (semver_parse)](#std__semver__Parse)
* [std::string::Base64 (base64)](#std__string__Base64)
* [std::string::Base64Decode (base64_decode)](#std__string__Base64Decode)
* [std::string::Base64Encode (base64_encode)](#std__string__Base64Encode)
@ -228,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
@ -4628,11 +4632,13 @@ output = exec command [args]*
stdout = set ${output.stdout}
stderr = set ${output.stderr}
exit_code = set ${output.code}
exit_code = exec --get-exit-code command [args]*
```
Executes the provided native command and arguments.<br>
If no output variable is set, the command output will be flushed to the main out/err stream of the parent process.<br>
In addition, in order to fail the command in case of the child process failed, add the --fail_on_error flag.<br>
In addition, in order to fail the command in case of the child process failed, add the --fail-on-error flag.<br>
If an output variable is set, it will be used as a base variable name from which the command stout, stderr and exit code information can be pulled from.<br>
The actual output variable name will not be modified, instead new variables will be created using that variable name as a baseline:
@ -4640,9 +4646,12 @@ The actual output variable name will not be modified, instead new variables will
* *output*.stderr - Will hold the stderr text content.
* *output*.code - Will hold the process exit code.
If an output variable is set and the --get-exit-code flag is provided, the output will only contain the exit code.
#### Parameters
* --fail-on-error - If no output variable is provided, it will cause an error in case the executed processed exists with an error exist code.
* --get-exit-code - If an output variable is provided, it will contain the exit code.
* The command to execute and its arguments.
#### Return Value
@ -4792,6 +4801,61 @@ assert_eq ${count} 4
#### Aliases:
watchdog
<a name="std__random__Range"></a>
## std::random::Range
```sh
output = random_range min max
```
Generate a random value in the range of min and max values provided, i.e. inclusive of min and exclusive of max.
#### Parameters
* min - The min range value (inclusive)
* max - The max range value (exclusive)
#### Return Value
The generated numeric value.
#### Examples
```sh
value = random_range -10 10
echo ${value}
```
#### Aliases:
random_range, rand_range
<a name="std__random__Text"></a>
## std::random::Text
```sh
output = random_text [length]
```
Generates random alphanumeric text with the requested length (length is 1 if not provided).
#### Parameters
Optional text length. Length is defaulted to 1 if not provided.
#### Return Value
The generated alphanumeric value.
#### Examples
```sh
value = random_text 50
echo ${value}
```
#### Aliases:
random_text, rand_text
<a name="std__scope__Clear"></a>
## std::scope::Clear
```sh
@ -4924,6 +4988,105 @@ echo ${defined}
#### Aliases:
scope_push_stack
<a name="std__semver__IsEqual"></a>
## std::semver::IsEqual
```sh
output = semver_is_equal value1 value2
```
Returns true if both semver values are valid and equal.
#### Parameters
Two semver values to compare.
#### Return Value
True if both semver values are valid and equal, else false.
#### Examples
```sh
equal = semver_is_equal 1.2.3 1.2.3
assert ${equal}
equal = semver_is_equal 1.2.3 2.2.3
assert_false ${equal}
```
#### Aliases:
semver_is_equal
<a name="std__semver__IsNewer"></a>
## std::semver::IsNewer
```sh
output = semver_is_newer newer older
```
Returns true if both semver values are valid and first value is newer.
#### Parameters
* The expected newer value
* The expected older value
#### Return Value
True if both semver values are valid and first value is newer, else false.
#### Examples
```sh
newer = semver_is_newer 3.2.3 2.2.3
assert ${newer}
newer = semver_is_newer 1.2.3 2.2.3
assert_false ${newer}
newer = semver_is_newer 1.2.3 1.2.3
assert_false ${newer}
```
#### Aliases:
semver_is_newer
<a name="std__semver__Parse"></a>
## std::semver::Parse
```sh
base = semver_parse value
```
Parses the provided value and sets the major, minor and patch variables.<br>
The variable names are based on the output variable name, for example if the output variable name is out:
* out.major - Holds the output major version
* out.minor - Holds the output minor version
* out.patch - Holds the output patch version
#### Parameters
The semver value.
#### Return Value
The major, minor and patch values.
#### Examples
```sh
version = semver_parse 1.2.3
echo ${version.major}
echo ${version.minor}
echo ${version.patch}
```
#### Aliases:
semver_parse
<a name="std__string__Base64"></a>
## std::string::Base64

View file

@ -36,6 +36,7 @@ java-properties = "^1"
meval = "^0.2"
num_cpus = "^1"
rand = "^0.7"
semver = "^0.10"
serde_json = "1"
walkdir = "^2"
which = { version = "^4", default-features = false }

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;
@ -15,9 +15,11 @@ mod noop;
mod not;
pub(crate) mod on_error;
mod process;
mod random;
mod read;
pub(crate) mod release;
pub(crate) mod scope;
mod semver;
pub(crate) mod string;
mod test;
mod thread;
@ -32,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))?;
@ -50,7 +52,9 @@ pub(crate) fn load(commands: &mut Commands) -> Result<(), ScriptError> {
net::load(commands, PACKAGE)?;
on_error::load(commands, PACKAGE)?;
process::load(commands, PACKAGE)?;
random::load(commands, PACKAGE)?;
scope::load(commands, PACKAGE)?;
semver::load(commands, PACKAGE)?;
string::load(commands, PACKAGE)?;
test::load(commands, PACKAGE)?;
thread::load(commands, PACKAGE)?;

View file

@ -5,11 +5,13 @@ output = exec command [args]*
stdout = set ${output.stdout}
stderr = set ${output.stderr}
exit_code = set ${output.code}
exit_code = exec --get-exit-code command [args]*
```
Executes the provided native command and arguments.<br>
If no output variable is set, the command output will be flushed to the main out/err stream of the parent process.<br>
In addition, in order to fail the command in case of the child process failed, add the --fail_on_error flag.<br>
In addition, in order to fail the command in case of the child process failed, add the --fail-on-error flag.<br>
If an output variable is set, it will be used as a base variable name from which the command stout, stderr and exit code information can be pulled from.<br>
The actual output variable name will not be modified, instead new variables will be created using that variable name as a baseline:
@ -17,9 +19,12 @@ The actual output variable name will not be modified, instead new variables will
* *output*.stderr - Will hold the stderr text content.
* *output*.code - Will hold the process exit code.
If an output variable is set and the --get-exit-code flag is provided, the output will only contain the exit code.
#### Parameters
* --fail-on-error - If no output variable is provided, it will cause an error in case the executed processed exists with an error exist code.
* --get-exit-code - If an output variable is provided, it will contain the exit code.
* The command to execute and its arguments.
#### Return Value

View file

@ -45,30 +45,40 @@ impl Command for CommandImpl {
_line: usize,
) -> CommandResult {
let allow_input = output_variable.is_some();
let print_output = !allow_input;
let (start_index, fail_on_error) =
let (print_output, start_index, fail_on_error, exit_code_output) =
if !arguments.is_empty() && arguments[0] == "--fail-on-error" {
(1, output_variable.is_none())
(
output_variable.is_none(),
1,
output_variable.is_none(),
false,
)
} else if !arguments.is_empty() && arguments[0] == "--get-exit-code" {
(true, 1, false, true)
} else {
(0, false)
(output_variable.is_none(), 0, false, false)
};
match exec::exec(&arguments, print_output, allow_input, start_index) {
Ok((stdout, stderr, exit_code)) => match output_variable {
Some(name) => {
let mut key = String::from(&name);
key.push_str(".stdout");
variables.insert(key.clone(), stdout);
if exit_code_output {
CommandResult::Continue(Some(exit_code.to_string()))
} else {
let mut key = String::from(&name);
key.push_str(".stdout");
variables.insert(key.clone(), stdout);
key = String::from(&name);
key.push_str(".stderr");
variables.insert(key.clone(), stderr);
key = String::from(&name);
key.push_str(".stderr");
variables.insert(key.clone(), stderr);
key = String::from(&name);
key.push_str(".code");
variables.insert(key.clone(), exit_code.to_string());
key = String::from(&name);
key.push_str(".code");
variables.insert(key.clone(), exit_code.to_string());
CommandResult::Continue(None)
CommandResult::Continue(None)
}
}
None => {
if fail_on_error && exit_code != 0 {

View file

@ -52,3 +52,23 @@ fn run_with_output() {
fn run_error_code_with_output() {
test::run_script_and_error(vec![create("")], "out = exec badcommand", "out");
}
#[test]
#[cfg(target_os = "linux")]
fn run_get_exit_code_valid() {
test::run_script_and_validate(
vec![create("")],
"out = exec --get-exit-code true",
CommandValidation::Match("out".to_string(), "0".to_string()),
);
}
#[test]
#[cfg(target_os = "linux")]
fn run_get_exit_code_error() {
test::run_script_and_validate(
vec![create("")],
"out = exec --get-exit-code false",
CommandValidation::Match("out".to_string(), "1".to_string()),
);
}

View file

@ -0,0 +1,17 @@
mod range;
mod text;
use crate::utils::pckg;
use duckscript::types::command::Commands;
use duckscript::types::error::ScriptError;
static PACKAGE: &str = "random";
pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptError> {
let package = pckg::concat(parent, PACKAGE);
commands.set(range::create(&package))?;
commands.set(text::create(&package))?;
Ok(())
}

View file

@ -0,0 +1,21 @@
```sh
output = random_range min max
```
Generate a random value in the range of min and max values provided, i.e. inclusive of min and exclusive of max.
#### Parameters
* min - The min range value (inclusive)
* max - The max range value (exclusive)
#### Return Value
The generated numeric value.
#### Examples
```sh
value = random_range -10 10
echo ${value}
```

View file

@ -0,0 +1,69 @@
use crate::utils::pckg;
use duckscript::types::command::{Command, CommandResult};
use rand::{thread_rng, Rng};
#[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, "Range")
}
fn aliases(&self) -> Vec<String> {
vec!["random_range".to_string(), "rand_range".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 run(&self, arguments: Vec<String>) -> CommandResult {
if arguments.len() < 2 {
CommandResult::Error("Missing random min/max values.".to_string())
} else {
match arguments[0].parse() {
Ok(min) => match arguments[1].parse() {
Ok(max) => {
if min > max {
CommandResult::Error(
format!("Min value: {} bigger than max value: {}", min, max)
.to_string(),
)
} else {
let mut rng = thread_rng();
let min_128: i128 = min;
let max_128: i128 = max;
let rand_value: i128 = rng.gen_range(min_128, max_128);
CommandResult::Continue(Some(rand_value.to_string()))
}
}
Err(_) => CommandResult::Error(
format!("Non numeric max value: {} provided.", &arguments[1]).to_string(),
),
},
Err(_) => CommandResult::Error(
format!("Non numeric min value: {} provided.", &arguments[0]).to_string(),
),
}
}
}
}
pub(crate) fn create(package: &str) -> Box<dyn Command> {
Box::new(CommandImpl {
package: package.to_string(),
})
}

View file

@ -0,0 +1,32 @@
use super::*;
use crate::test;
use crate::test::CommandValidation;
#[test]
fn common_functions() {
test::test_common_command_functions(create(""));
}
#[test]
fn run_no_args() {
test::run_script_and_error(vec![create("")], "out = rand_range", "out");
}
#[test]
fn run_single_arg() {
test::run_script_and_error(vec![create("")], "out = rand_range 1", "out");
}
#[test]
fn run_min_bigger_than_max() {
test::run_script_and_error(vec![create("")], "out = rand_range 10 5", "out");
}
#[test]
fn run_valid() {
test::run_script_and_validate(
vec![create("")],
"out = rand_range 10 15",
CommandValidation::PositiveNumber("out".to_string()),
);
}

View file

@ -0,0 +1,20 @@
```sh
output = random_text [length]
```
Generates random alphanumeric text with the requested length (length is 1 if not provided).
#### Parameters
Optional text length. Length is defaulted to 1 if not provided.
#### Return Value
The generated alphanumeric value.
#### Examples
```sh
value = random_text 50
echo ${value}
```

View file

@ -0,0 +1,64 @@
use crate::utils::pckg;
use duckscript::types::command::{Command, CommandResult};
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
use std::iter;
#[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, "Text")
}
fn aliases(&self) -> Vec<String> {
vec!["random_text".to_string(), "rand_text".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 run(&self, arguments: Vec<String>) -> CommandResult {
let length = if arguments.is_empty() {
1
} else {
match arguments[0].parse() {
Ok(value) => {
let value_usize: usize = value;
value_usize
}
Err(_) => {
return CommandResult::Error(
format!("Invalid length provided: {}", &arguments[0]).to_string(),
)
}
}
};
let mut rng = thread_rng();
let random_value: String = iter::repeat(())
.map(|()| rng.sample(Alphanumeric))
.take(length)
.collect();
CommandResult::Continue(Some(random_value))
}
}
pub(crate) fn create(package: &str) -> Box<dyn Command> {
Box::new(CommandImpl {
package: package.to_string(),
})
}

View file

@ -0,0 +1,31 @@
use super::*;
use crate::test;
use crate::test::CommandValidation;
#[test]
fn common_functions() {
test::test_common_command_functions(create(""));
}
#[test]
fn run_no_args() {
test::run_script_and_validate(
vec![create("")],
"out = rand_text",
CommandValidation::StringLength("out".to_string(), 1),
);
}
#[test]
fn run_invalid_size() {
test::run_script_and_error(vec![create("")], "out = rand_text -10", "out");
}
#[test]
fn run_valid_size() {
test::run_script_and_validate(
vec![create("")],
"out = rand_text 10",
CommandValidation::StringLength("out".to_string(), 10),
);
}

View file

@ -0,0 +1,23 @@
```sh
output = semver_is_equal value1 value2
```
Returns true if both semver values are valid and equal.
#### Parameters
Two semver values to compare.
#### Return Value
True if both semver values are valid and equal, else false.
#### Examples
```sh
equal = semver_is_equal 1.2.3 1.2.3
assert ${equal}
equal = semver_is_equal 1.2.3 2.2.3
assert_false ${equal}
```

View file

@ -0,0 +1,54 @@
use crate::utils::pckg;
use duckscript::types::command::{Command, CommandResult};
use semver::Version;
#[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, "IsEqual")
}
fn aliases(&self) -> Vec<String> {
vec!["semver_is_equal".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 run(&self, arguments: Vec<String>) -> CommandResult {
if arguments.len() < 2 {
CommandResult::Error("Missing semver values to compare.".to_string())
} else {
match Version::parse(&arguments[0]) {
Ok(version1) => match Version::parse(&arguments[1]) {
Ok(version2) => {
let result = if version1 == version2 { true } else { false };
CommandResult::Continue(Some(result.to_string()))
}
Err(error) => CommandResult::Error(error.to_string()),
},
Err(error) => CommandResult::Error(error.to_string()),
}
}
}
}
pub(crate) fn create(package: &str) -> Box<dyn Command> {
Box::new(CommandImpl {
package: package.to_string(),
})
}

View file

@ -0,0 +1,26 @@
use super::*;
use crate::test;
#[test]
fn common_functions() {
test::test_common_command_functions(create(""));
}
#[test]
fn run_no_args() {
test::run_script_and_error(vec![create("")], "out = semver_is_equal", "out");
}
#[test]
fn run_single_arg() {
test::run_script_and_error(vec![create("")], "out = semver_is_equal 1.2.3", "out");
}
#[test]
fn run_invalid_args() {
test::run_script_and_error(
vec![create("")],
"out = semver_is_equal abc_123 123_test",
"out",
);
}

View file

@ -0,0 +1,27 @@
```sh
output = semver_is_newer newer older
```
Returns true if both semver values are valid and first value is newer.
#### Parameters
* The expected newer value
* The expected older value
#### Return Value
True if both semver values are valid and first value is newer, else false.
#### Examples
```sh
newer = semver_is_newer 3.2.3 2.2.3
assert ${newer}
newer = semver_is_newer 1.2.3 2.2.3
assert_false ${newer}
newer = semver_is_newer 1.2.3 1.2.3
assert_false ${newer}
```

View file

@ -0,0 +1,58 @@
use crate::utils::pckg;
use duckscript::types::command::{Command, CommandResult};
use semver::Version;
#[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, "IsNewer")
}
fn aliases(&self) -> Vec<String> {
vec!["semver_is_newer".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 run(&self, arguments: Vec<String>) -> CommandResult {
if arguments.len() < 2 {
CommandResult::Error("Missing semver values to compare.".to_string())
} else {
match Version::parse(&arguments[0]) {
Ok(newer_version) => match Version::parse(&arguments[1]) {
Ok(older_version) => {
let result = if newer_version > older_version {
true
} else {
false
};
CommandResult::Continue(Some(result.to_string()))
}
Err(error) => CommandResult::Error(error.to_string()),
},
Err(error) => CommandResult::Error(error.to_string()),
}
}
}
}
pub(crate) fn create(package: &str) -> Box<dyn Command> {
Box::new(CommandImpl {
package: package.to_string(),
})
}

View file

@ -0,0 +1,26 @@
use super::*;
use crate::test;
#[test]
fn common_functions() {
test::test_common_command_functions(create(""));
}
#[test]
fn run_no_args() {
test::run_script_and_error(vec![create("")], "out = semver_is_newer", "out");
}
#[test]
fn run_single_arg() {
test::run_script_and_error(vec![create("")], "out = semver_is_newer 1.2.3", "out");
}
#[test]
fn run_invalid_args() {
test::run_script_and_error(
vec![create("")],
"out = semver_is_newer abc_123 123_test",
"out",
);
}

View file

@ -0,0 +1,19 @@
mod is_equal;
mod is_newer;
mod parse;
use crate::utils::pckg;
use duckscript::types::command::Commands;
use duckscript::types::error::ScriptError;
static PACKAGE: &str = "semver";
pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptError> {
let package = pckg::concat(parent, PACKAGE);
commands.set(is_equal::create(&package))?;
commands.set(is_newer::create(&package))?;
commands.set(parse::create(&package))?;
Ok(())
}

View file

@ -0,0 +1,28 @@
```sh
base = semver_parse value
```
Parses the provided value and sets the major, minor and patch variables.<br>
The variable names are based on the output variable name, for example if the output variable name is out:
* out.major - Holds the output major version
* out.minor - Holds the output minor version
* out.patch - Holds the output patch version
#### Parameters
The semver value.
#### Return Value
The major, minor and patch values.
#### Examples
```sh
version = semver_parse 1.2.3
echo ${version.major}
echo ${version.minor}
echo ${version.patch}
```

View file

@ -0,0 +1,81 @@
use crate::utils::pckg;
use duckscript::types::command::{Command, CommandResult, Commands};
use duckscript::types::instruction::Instruction;
use duckscript::types::runtime::StateValue;
use semver::Version;
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, "Parse")
}
fn aliases(&self) -> Vec<String> {
vec!["semver_parse".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("No semver value provided.".to_string())
} else {
match Version::parse(&arguments[0]) {
Ok(version) => match output_variable {
Some(name) => {
variables.insert(
format!("{}.major", &name).to_string(),
version.major.to_string(),
);
variables.insert(
format!("{}.minor", &name).to_string(),
version.minor.to_string(),
);
variables.insert(
format!("{}.patch", &name).to_string(),
version.patch.to_string(),
);
CommandResult::Continue(None)
}
None => CommandResult::Continue(None),
},
Err(error) => CommandResult::Error(error.to_string()),
}
}
}
}
pub(crate) fn create(package: &str) -> Box<dyn Command> {
Box::new(CommandImpl {
package: package.to_string(),
})
}

View file

@ -0,0 +1,45 @@
use super::*;
use crate::test;
use crate::test::CommandValidation;
#[test]
fn common_functions() {
test::test_common_command_functions(create(""));
}
#[test]
fn run_no_args() {
test::run_script_and_error(vec![create("")], "out = semver_parse", "out");
}
#[test]
fn run_invalid() {
test::run_script_and_error(vec![create("")], "out = semver_parse aaa_bbb", "out");
}
#[test]
fn run_major() {
test::run_script_and_validate(
vec![create("")],
"out = semver_parse 1.2.3",
CommandValidation::Match("out.major".to_string(), "1".to_string()),
);
}
#[test]
fn run_minor() {
test::run_script_and_validate(
vec![create("")],
"out = semver_parse 1.2.3",
CommandValidation::Match("out.minor".to_string(), "2".to_string()),
);
}
#[test]
fn run_patch() {
test::run_script_and_validate(
vec![create("")],
"out = semver_parse 1.2.3",
CommandValidation::Match("out.patch".to_string(), "3".to_string()),
);
}

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

@ -186,6 +186,7 @@ impl Command for OnErrorCommand {
pub(crate) enum CommandValidation {
None,
PositiveNumber(String),
StringLength(String, usize),
Match(String, String),
Contains(String, String),
Any(String, Vec<String>),
@ -295,6 +296,12 @@ pub(crate) fn run_script_and_validate(
let numeric_value: u128 = var_value.parse().unwrap();
assert!(numeric_value > 0)
}
CommandValidation::StringLength(key, length) => {
assert!(!context.variables.is_empty());
let var_value = context.variables.get(&key).unwrap();
assert!(var_value.len() == length)
}
CommandValidation::Ignore => {
assert!(!context.variables.is_empty());
}

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

View file

@ -0,0 +1,11 @@
fn test_valid
value = random_range -10 10
in_min = greater_than ${value} -11
in_max = greater_than 10 ${value}
assert ${in_min}
assert ${in_max}
end

View file

@ -0,0 +1,17 @@
fn test_no_length
value = random_text
len = strlen ${value}
assert_eq ${len} 1
end
fn test_with_length
value = random_text 50
len = strlen ${value}
assert_eq ${len} 50
end

View file

@ -0,0 +1,42 @@
fn test_equal
equal = semver_is_equal 1.2.3 1.2.3
assert ${equal}
newer = semver_is_newer 1.2.3 1.2.3
assert_false ${newer}
end
fn test_not_equal_major
equal = semver_is_equal 1.2.3 2.2.3
assert_false ${equal}
newer = semver_is_newer 1.2.3 2.2.3
assert_false ${newer}
newer = semver_is_newer 3.2.3 2.2.3
assert ${newer}
end
fn test_not_equal_minor
equal = semver_is_equal 1.2.3 1.3.3
assert_false ${equal}
newer = semver_is_newer 1.2.3 1.3.3
assert_false ${newer}
newer = semver_is_newer 1.3.3 1.2.3
assert ${newer}
end
fn test_not_equal_patch
equal = semver_is_equal 1.2.3 1.2.4
assert_false ${equal}
newer = semver_is_newer 1.2.3 1.2.4
assert_false ${newer}
newer = semver_is_newer 1.2.4 1.2.3
assert ${newer}
end

View file

@ -0,0 +1,9 @@
fn test_valid
version = semver_parse 1.2.3
assert_eq ${version.major} 1
assert_eq ${version.minor} 2
assert_eq ${version.patch} 3
end