new is_file and is_dir commands

This commit is contained in:
sagie gur ari 2020-03-06 11:02:52 +00:00
parent 3b47706990
commit 9d971b65c1
11 changed files with 307 additions and 0 deletions

View file

@ -1,5 +1,10 @@
## CHANGELOG
### v0.2.2
* New is_directory command
* New is_file command
### v0.2.1 (2020-02-21)
* New temp_file command #85

View file

@ -60,6 +60,8 @@
* [std::fs::GetCanonicalPath (canonicalize)](#std__fs__GetCanonicalPath)
* [std::fs::GetFileName (basename)](#std__fs__GetFileName)
* [std::fs::GetParentDirectory (dirname)](#std__fs__GetParentDirectory)
* [std::fs::IsDirectory (is_directory, is_dir)](#std__fs__IsDirectory)
* [std::fs::IsFile (is_file)](#std__fs__IsFile)
* [std::fs::List (ls)](#std__fs__List)
* [std::fs::MovePath (mv)](#std__fs__MovePath)
* [std::fs::Print (cat)](#std__fs__Print)
@ -2310,6 +2312,58 @@ directory = dirname ./dir/file.txt
#### Aliases:
dirname
<a name="std__fs__IsDirectory"></a>
## std::fs::IsDirectory
```sh
var = is_dir path
```
This command will return true/false based if the provided path points to an existing directory.
#### Parameters
The path to check.
#### Return Value
True if the path points to an existing directory.
#### Examples
```sh
existing_dir = is_dir ./dir
```
#### Aliases:
is_directory, is_dir
<a name="std__fs__IsFile"></a>
## std::fs::IsFile
```sh
var = is_file path
```
This command will return true/false based if the provided path points to an existing file.
#### Parameters
The path to check.
#### Return Value
True if the path points to an existing file.
#### Examples
```sh
existing_file = is_file ./dir/somefile.txt
```
#### Aliases:
is_file
<a name="std__fs__List"></a>
## std::fs::List
```sh

View file

@ -0,0 +1,19 @@
```sh
var = is_dir path
```
This command will return true/false based if the provided path points to an existing directory.
#### Parameters
The path to check.
#### Return Value
True if the path points to an existing directory.
#### Examples
```sh
existing_dir = is_dir ./dir
```

View file

@ -0,0 +1,45 @@
use crate::utils::pckg;
use duckscript::types::command::{Command, CommandResult};
use fsio::path::as_path::AsPath;
#[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, "IsDirectory")
}
fn aliases(&self) -> Vec<String> {
vec!["is_directory".to_string(), "is_dir".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 {
let path = &arguments[0].as_path();
CommandResult::Continue(Some(path.is_dir().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;
#[test]
fn common_functions() {
test::test_common_command_functions(create(""));
}
#[test]
fn run_no_path_provided() {
test::run_script_and_error(vec![create("")], "out = is_dir", "out");
}
#[test]
fn run_directory() {
test::run_script_and_validate(
vec![create("")],
"out = is_dir ./src",
CommandValidation::Match("out".to_string(), "true".to_string()),
);
}
#[test]
fn run_not_directory() {
test::run_script_and_validate(
vec![create("")],
"out = is_dir ./Cargo.toml",
CommandValidation::Match("out".to_string(), "false".to_string()),
);
}
#[test]
fn run_not_found() {
test::run_script_and_validate(
vec![create("")],
"out = is_dir ./badpath",
CommandValidation::Match("out".to_string(), "false".to_string()),
);
}

View file

@ -0,0 +1,19 @@
```sh
var = is_file path
```
This command will return true/false based if the provided path points to an existing file.
#### Parameters
The path to check.
#### Return Value
True if the path points to an existing file.
#### Examples
```sh
existing_file = is_file ./dir/somefile.txt
```

View file

@ -0,0 +1,45 @@
use crate::utils::pckg;
use duckscript::types::command::{Command, CommandResult};
use fsio::path::as_path::AsPath;
#[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, "IsFile")
}
fn aliases(&self) -> Vec<String> {
vec!["is_file".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 {
let path = &arguments[0].as_path();
CommandResult::Continue(Some(path.is_file().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;
#[test]
fn common_functions() {
test::test_common_command_functions(create(""));
}
#[test]
fn run_no_path_provided() {
test::run_script_and_error(vec![create("")], "out = is_file", "out");
}
#[test]
fn run_file() {
test::run_script_and_validate(
vec![create("")],
"out = is_file ./Cargo.toml",
CommandValidation::Match("out".to_string(), "true".to_string()),
);
}
#[test]
fn run_not_file() {
test::run_script_and_validate(
vec![create("")],
"out = is_file ./src",
CommandValidation::Match("out".to_string(), "false".to_string()),
);
}
#[test]
fn run_not_found() {
test::run_script_and_validate(
vec![create("")],
"out = is_file ./badpath",
CommandValidation::Match("out".to_string(), "false".to_string()),
);
}

View file

@ -3,6 +3,8 @@ mod basename;
mod canonical;
mod cp;
mod dirname;
mod is_directory;
mod is_file;
mod list;
mod mkdir;
mod mv;
@ -30,6 +32,8 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr
commands.set(canonical::create(&package))?;
commands.set(cp::create(&package))?;
commands.set(dirname::create(&package))?;
commands.set(is_directory::create(&package))?;
commands.set(is_file::create(&package))?;
commands.set(list::create(&package))?;
commands.set(mkdir::create(&package))?;
commands.set(mv::create(&package))?;

View file

@ -0,0 +1,18 @@
fn test_directory
value = is_dir ./target
assert ${value}
end
fn test_not_directory
value = is_dir ./Cargo.toml
assert_false ${value}
end
fn test_not_found
value = is_dir ./badpath
assert_false ${value}
end

View file

@ -0,0 +1,18 @@
fn test_file
value = is_file ./Cargo.toml
assert ${value}
end
fn test_not_file
value = is_file ./test
assert_false ${value}
end
fn test_not_found
value = is_file ./badpath
assert_false ${value}
end