New is_path_exists command

This commit is contained in:
sagie gur ari 2020-03-06 11:08:51 +00:00
parent 9d971b65c1
commit c197b042f4
7 changed files with 156 additions and 2 deletions

View file

@ -2,8 +2,9 @@
### v0.2.2
* New is_directory command
* New is_file command
* New is_path_exists command.
* New is_directory command.
* New is_file command.
### v0.2.1 (2020-02-21)

View file

@ -57,6 +57,7 @@
* [std::fs::CreateEmptyFile (touch)](#std__fs__CreateEmptyFile)
* [std::fs::DeleteEmptyDirectory (rmdir)](#std__fs__DeleteEmptyDirectory)
* [std::fs::DeletePath (rm)](#std__fs__DeletePath)
* [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::GetParentDirectory (dirname)](#std__fs__GetParentDirectory)
@ -2231,6 +2232,33 @@ deleted = rm -r ./target
#### Aliases:
rm
<a name="std__fs__Exists"></a>
## std::fs::Exists
```sh
var = is_path_exists path
```
This command will return true/false based if the provided path points to an existing file system entry.
#### Parameters
The path to check.
#### Return Value
True if the path points to an existing file system entry.
#### Examples
```sh
existing = is_path_exists ./dir
existing = is_path_exists ./dir/somefile.txt
```
#### Aliases:
is_path_exists
<a name="std__fs__GetCanonicalPath"></a>
## std::fs::GetCanonicalPath
```sh

View file

@ -0,0 +1,20 @@
```sh
var = is_path_exists path
```
This command will return true/false based if the provided path points to an existing file system entry.
#### Parameters
The path to check.
#### Return Value
True if the path points to an existing file system entry.
#### Examples
```sh
existing = is_path_exists ./dir
existing = is_path_exists ./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, "Exists")
}
fn aliases(&self) -> Vec<String> {
vec!["is_path_exists".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.exists().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_path_exists", "out");
}
#[test]
fn run_file() {
test::run_script_and_validate(
vec![create("")],
"out = is_path_exists ./Cargo.toml",
CommandValidation::Match("out".to_string(), "true".to_string()),
);
}
#[test]
fn run_directory() {
test::run_script_and_validate(
vec![create("")],
"out = is_path_exists ./src",
CommandValidation::Match("out".to_string(), "true".to_string()),
);
}
#[test]
fn run_not_found() {
test::run_script_and_validate(
vec![create("")],
"out = is_path_exists ./badpath",
CommandValidation::Match("out".to_string(), "false".to_string()),
);
}

View file

@ -3,6 +3,7 @@ mod basename;
mod canonical;
mod cp;
mod dirname;
mod exists;
mod is_directory;
mod is_file;
mod list;
@ -32,6 +33,7 @@ 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(exists::create(&package))?;
commands.set(is_directory::create(&package))?;
commands.set(is_file::create(&package))?;
commands.set(list::create(&package))?;

View file

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