new rmdir command #14

This commit is contained in:
sagie gur ari 2020-01-02 17:03:26 +00:00
parent ee78556b71
commit b78304c323
6 changed files with 149 additions and 0 deletions

View file

@ -2,6 +2,8 @@
### v0.1.2
* New **rmdir** command #14
* New **assert_eq** command #22
* New **assert_fail** command #3
* New **assert** command #2
* New **touch** command #4

View file

@ -17,6 +17,7 @@
* [sdk::env::SetCurrentDirectory](#sdk__env__SetCurrentDirectory)
* [sdk::fs::CreateDirectory](#sdk__fs__CreateDirectory)
* [sdk::fs::CreateEmptyFile](#sdk__fs__CreateEmptyFile)
* [sdk::fs::DeleteEmptyDirectory](#sdk__fs__DeleteEmptyDirectory)
* [sdk::fs::GetCanonicalPath](#sdk__fs__GetCanonicalPath)
* [sdk::fs::GetFileName](#sdk__fs__GetFileName)
* [sdk::fs::GetParentDirectory](#sdk__fs__GetParentDirectory)
@ -798,6 +799,33 @@ exists = touch ./dir/file.txt
#### Aliases:
touch
<a name="sdk__fs__DeleteEmptyDirectory"></a>
## sdk::fs::DeleteEmptyDirectory
```sh
var = rmdir path
```
This command delete the requested empty directory and returns true if successful.<br>
If the path leads to a file or the directory is not empty, this command will fail.
#### Parameters
A single parameter holding the directory path.
#### Return Value
**true** if the directory was deleted.
#### Examples
```sh
deleted = rmdir ./mydir
```
#### Aliases:
rmdir
<a name="sdk__fs__GetCanonicalPath"></a>
## sdk::fs::GetCanonicalPath
```sh

View file

@ -4,6 +4,7 @@ mod dirname;
mod mkdir;
mod print;
mod read;
mod rmdir;
mod touch;
mod write;
@ -22,6 +23,7 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr
commands.set(mkdir::create(&package))?;
commands.set(print::create(&package))?;
commands.set(read::create(&package))?;
commands.set(rmdir::create(&package))?;
commands.set(touch::create(&package))?;
commands.set(write::create(&package))?;

View file

@ -0,0 +1,20 @@
```sh
var = rmdir path
```
This command delete the requested empty directory and returns true if successful.<br>
If the path leads to a file or the directory is not empty, this command will fail.
#### Parameters
A single parameter holding the directory path.
#### Return Value
**true** if the directory was deleted.
#### Examples
```sh
deleted = rmdir ./mydir
```

View file

@ -0,0 +1,44 @@
use crate::utils::pckg;
use duckscript::types::command::{Command, CommandResult};
use std::fs;
#[cfg(test)]
#[path = "./mod_test.rs"]
mod mod_test;
struct CommandImpl {
package: String,
}
impl Command for CommandImpl {
fn name(&self) -> String {
pckg::concat(&self.package, "DeleteEmptyDirectory")
}
fn aliases(&self) -> Vec<String> {
vec!["rmdir".to_string()]
}
fn help(&self) -> String {
include_str!("help.md").to_string()
}
fn run(&self, arguments: Vec<String>) -> CommandResult {
if arguments.is_empty() {
CommandResult::Error("Directory path not provided.".to_string())
} else {
let result = fs::remove_dir(&arguments[0]);
match result {
Ok(_) => CommandResult::Continue(Some("true".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,53 @@
use super::*;
use crate::test;
use crate::test::CommandValidation;
use crate::utils::io;
#[test]
fn common_functions() {
test::test_common_command_functions(create(""));
}
#[test]
fn run_no_path_provided() {
test::run_script_and_fail(vec![create("")], "rmdir");
}
#[test]
fn run_path_not_exists() {
test::run_script_and_fail(vec![create("")], "rmdir ./target/_duckscript/rmdir/newdir");
}
#[test]
fn run_path_not_empty() {
let result = io::create_directory("./target/_duckscript/rmdir/not_empty/dir1");
assert!(result.is_ok());
test::run_script_and_fail(
vec![create("")],
"rmdir ./target/_duckscript/rmdir/not_empty",
);
}
#[test]
fn run_path_is_file() {
let result = io::create_empty_file("./target/_duckscript/rmdir/file.txt");
assert!(result.is_ok());
test::run_script_and_fail(
vec![create("")],
"rmdir ./target/_duckscript/rmdir/file.txt",
);
}
#[test]
fn run_valid() {
let result = io::create_directory("./target/_duckscript/rmdir/existing_dir");
assert!(result.is_ok());
test::run_script_and_validate(
vec![create("")],
"out = rmdir ./target/_duckscript/rmdir/existing_dir",
CommandValidation::Match("out".to_string(), "true".to_string()),
);
}