Merge pull request #239 from sagiegurari/0.8.11

0.8.11
This commit is contained in:
Sagie Gur-Ari 2022-04-20 10:30:29 +03:00 committed by GitHub
commit 0bc03410b2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 257 additions and 30 deletions

View file

@ -1,5 +1,11 @@
## CHANGELOG
### v0.8.11
* Fix: Runtime - fix control characters '\' parsing and expansion #237
* Enhancement: New get_file_size/filesize command #222
* Enhancement: Add include hidden files option for gitignore_path_array command #236 (thanks @Blightbuster)
### v0.8.10 (2021-12-10)
* New SDK and cli tls feature to enable usage without tls support (by default enabled)

36
Cargo.lock generated
View file

@ -179,22 +179,22 @@ dependencies = [
[[package]]
name = "crypto-common"
version = "0.1.1"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "683d6b536309245c849479fba3da410962a43ed8e51c26b729208ec0ac2798d0"
checksum = "57952ca27b5e3606ff4dd79b0020231aaf9d6aa76dc05fd30137538c50bd3ce8"
dependencies = [
"generic-array",
"typenum",
]
[[package]]
name = "digest"
version = "0.10.1"
version = "0.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b697d66081d42af4fba142d56918a3cb21dc8eb63372c6b85d14f44fb9c5979b"
checksum = "f2fb860ca6fafa5552fb6d0e816a69c8e49f0908bf524e30a90d97c85892d506"
dependencies = [
"block-buffer",
"crypto-common",
"generic-array",
]
[[package]]
@ -748,14 +748,13 @@ dependencies = [
[[package]]
name = "rand"
version = "0.8.4"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
"rand_hc",
]
[[package]]
@ -777,15 +776,6 @@ dependencies = [
"getrandom",
]
[[package]]
name = "rand_hc"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d51e9f596de227fda2ea6c84607f5558e196eeaf43c986b724ba4fb8fdf497e7"
dependencies = [
"rand_core",
]
[[package]]
name = "redox_syscall"
version = "0.2.10"
@ -890,9 +880,9 @@ dependencies = [
[[package]]
name = "semver"
version = "1.0.5"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0486718e92ec9a68fbed73bb5ef687d71103b142595b406835649bebd33f72c7"
checksum = "a4a3381e03edd24287172047536f20cabde766e2cd3e65e6b00fb3af51c4f38d"
[[package]]
name = "serde"
@ -902,9 +892,9 @@ checksum = "97565067517b60e2d1ea8b268e59ce036de907ac523ad83a0475da04e818989a"
[[package]]
name = "serde_json"
version = "1.0.78"
version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d23c1ba4cf0efd44be32017709280b32d1cea5c3f1275c3b6d9e8bc54f758085"
checksum = "8e8d9fa5c3b304765ce1fd9c4c8a3de2c8db365a5b91be52f186efc675681d95"
dependencies = [
"itoa",
"ryu",
@ -913,9 +903,9 @@ dependencies = [
[[package]]
name = "sha2"
version = "0.10.1"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99c3bd8169c58782adad9290a9af5939994036b76187f7b4f0e6de91dbbfc0ec"
checksum = "55deaec60f81eefe3cce0dc50bda92d6d8e88f2a27df7c5033b42afeb1ed2676"
dependencies = [
"cfg-if",
"cpufeatures",

View file

@ -92,6 +92,7 @@
* [`std::fs::Exists` (is_path_exists)](#std__fs__Exists)
* [`std::fs::GetCanonicalPath` (canonicalize)](#std__fs__GetCanonicalPath)
* [`std::fs::GetFileName` (basename)](#std__fs__GetFileName)
* [`std::fs::GetFileSize` (get_file_size, filesize)](#std__fs__GetFileSize)
* [`std::fs::GetLastModifiedTime` (get_last_modified_time)](#std__fs__GetLastModifiedTime)
* [`std::fs::GetParentDirectory` (dirname)](#std__fs__GetParentDirectory)
* [`std::fs::GitIgnorePathArray` (gitignore_path_array)](#std__fs__GitIgnorePathArray)
@ -3690,6 +3691,32 @@ file = basename ./dir/file.txt
### Aliases:
basename
<a name="std__fs__GetFileSize"></a>
## `std::fs::GetFileSize`
```sh
var = get_file_size path
```
This command will return the size of the file in bytes.
### Parameters
The path to check.
### Return Value
The size of the file in bytes or false in case path is a directory or does not exist.
### Examples
```sh
size = get_file_size ./dir/somefile.txt
```
### Aliases:
get_file_size, filesize
<a name="std__fs__GetLastModifiedTime"></a>
## `std::fs::GetLastModifiedTime`
```sh

View file

@ -47,14 +47,14 @@ pub(crate) fn expand_by_wrapper(
let mut single_type = true;
for next_char in value.chars() {
if !found_prefix {
if next_char == '\\' && prefix_index == 0 {
force_push = true
} else if force_push {
if force_push {
if next_char != '$' && next_char != '%' {
value_string.push('\\');
}
value_string.push(next_char);
force_push = false;
} else if next_char == '\\' && prefix_index == 0 {
force_push = true
} else if prefix_index == 0 && (next_char == '$' || next_char == '%') {
prefix_index = 1;

View file

@ -38,6 +38,18 @@ fn expand_by_wrapper_only_control_chars() {
assert_eq!(r#"\\"#, value);
}
#[test]
fn expand_by_wrapper_only_control_chars2() {
let mut variables = HashMap::new();
variables.insert("FOUND".to_string(), r#"\\\\"#.to_string());
let output = expand_by_wrapper("${FOUND}", &InstructionMetaInfo::new(), &mut variables);
let value = get_single_value(output);
assert_eq!(r#"\\\\"#, value);
}
#[test]
fn expand_by_wrapper_found_fully() {
let mut variables = HashMap::new();

View file

@ -825,7 +825,7 @@ fn parse_lines_all() {
let text = r#"
!print test pre process line
:label_test variable_test = command_test arg1 arg2 " arg3 arg3 arg3 " #some comment
:label_test variable_test = command_test arg1 arg2 " arg3 arg3 arg3 " \\\\ #some comment
#comment
!print test
@ -865,7 +865,7 @@ fn parse_lines_all() {
assert_eq!(script_instruction.command.unwrap(), "command_test");
assert_eq!(
script_instruction.arguments.unwrap(),
vec!["arg1", "arg2", " arg3 arg3 arg3 "]
vec!["arg1", "arg2", " arg3 arg3 arg3 ", "\\\\"]
);
}

View file

@ -1130,6 +1130,38 @@ fn run_instruction_script_instruction_error_result() {
assert!(test::validate_error_result(&command_result));
}
#[test]
fn run_instruction_control_characters() {
let mut script_instruction = ScriptInstruction::new();
script_instruction.command = Some("set".to_string());
script_instruction.output = Some("out".to_string());
script_instruction.arguments = Some(vec!["\\\\".to_string()]);
let instruction = Instruction {
meta_info: InstructionMetaInfo::new(),
instruction_type: InstructionType::Script(script_instruction),
};
let mut context = Context::new();
let result = context.commands.set(Box::new(SetCommand {}));
assert!(result.is_ok());
let (command_result, output_variable) = run_instruction(
&mut context.commands,
&mut context.variables,
&mut HashMap::new(),
&vec![],
instruction,
0,
);
assert!(output_variable.is_some());
assert!(test::validate_continue_result(
&command_result,
Some("\\\\".to_string())
));
}
#[test]
fn run_on_error_instruction_no_command() {
let mut commands = Commands::new();

View file

@ -0,0 +1,19 @@
```sh
var = get_file_size path
```
This command will return the size of the file in bytes.
### Parameters
The path to check.
### Return Value
The size of the file in bytes or false in case path is a directory or does not exist.
### Examples
```sh
size = get_file_size ./dir/somefile.txt
```

View file

@ -0,0 +1,46 @@
use crate::utils::{io, pckg};
use duckscript::types::command::{Command, CommandResult};
#[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, "GetFileSize")
}
fn aliases(&self) -> Vec<String> {
vec!["get_file_size".to_string(), "filesize".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.is_empty() {
CommandResult::Error("Path not provided.".to_string())
} else {
match io::get_file_size(&arguments[0]) {
Ok(size) => CommandResult::Continue(Some(size.to_string())),
Err(error) => CommandResult::Error(error),
}
}
}
}
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_path_provided() {
test::run_script_and_error(vec![create("")], "out = get_file_size", "out");
}
#[test]
fn run_file() {
test::run_script_and_validate(
vec![create("")],
"out = get_file_size ./Cargo.toml",
CommandValidation::PositiveNumber("out".to_string()),
);
}
#[test]
fn run_directory() {
test::run_script_and_error(vec![create("")], "out = get_file_size ./src", "out");
}
#[test]
fn run_not_found() {
test::run_script_and_error(vec![create("")], "out = get_file_size ./badpath", "out");
}

View file

@ -52,8 +52,15 @@ impl Command for CommandImpl {
} else {
let mut array = vec![];
for entry in WalkBuilder::new(&arguments[0])
.hidden(true)
let (path_index, include_hidden) =
if arguments.len() > 1 && arguments[0] == "--include-hidden" {
(1, true)
} else {
(0, false)
};
for entry in WalkBuilder::new(&arguments[path_index])
.hidden(!include_hidden)
.parents(true)
.git_ignore(true)
.git_exclude(true)

View file

@ -5,6 +5,7 @@ mod cp;
mod cp_glob;
mod dirname;
mod exists;
mod get_file_size;
mod get_last_modified_time;
mod gitignore_path_array;
mod glob_array;
@ -45,6 +46,7 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr
commands.set(cp_glob::create(&package)?)?;
commands.set(dirname::create(&package))?;
commands.set(exists::create(&package))?;
commands.set(get_file_size::create(&package))?;
commands.set(get_last_modified_time::create(&package))?;
commands.set(gitignore_path_array::create(&package))?;
commands.set(glob_array::create(&package))?;

View file

@ -54,3 +54,16 @@ pub(crate) fn get_last_modified_time(path: &str) -> Result<u128, String> {
Err(error) => Err(error.to_string()),
}
}
pub(crate) fn get_file_size(path: &str) -> Result<u64, String> {
match std::fs::metadata(path) {
Ok(metadata) => {
if metadata.is_file() {
Ok(metadata.len())
} else {
Err("The provided path is not a file.".to_string())
}
}
Err(_error) => Err("Unable to extract metadata for path.".to_string()),
}
}

View file

@ -99,3 +99,25 @@ fn create_empty_file_exists() {
let result = create_empty_file(path);
assert!(result.is_ok());
}
#[test]
fn get_file_size_exists() {
let size = get_file_size("Cargo.toml");
assert!(size.is_ok());
assert!(size.unwrap() > 0);
}
#[test]
fn get_file_size_not_exists() {
let size = get_file_size("Cargo.toml2");
assert!(size.is_err());
}
#[test]
fn get_dir_size() {
let size = get_file_size("src");
assert!(size.is_err());
}

View file

@ -0,0 +1,19 @@
fn test_file
size = get_file_size ./Cargo.toml
positive = greater_than ${size} 0
assert ${positive}
end
fn test_directory
size = get_file_size ./src
assert_false ${size}
end
fn test_not_found
size = get_file_size ./badfile
assert_false ${size}
end