mirror of
https://github.com/sagiegurari/duckscript
synced 2024-10-04 15:09:08 +00:00
new rmdir command #14
This commit is contained in:
parent
ee78556b71
commit
b78304c323
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
### v0.1.2
|
### v0.1.2
|
||||||
|
|
||||||
|
* New **rmdir** command #14
|
||||||
|
* New **assert_eq** command #22
|
||||||
* New **assert_fail** command #3
|
* New **assert_fail** command #3
|
||||||
* New **assert** command #2
|
* New **assert** command #2
|
||||||
* New **touch** command #4
|
* New **touch** command #4
|
||||||
|
|
28
docs/sdk.md
28
docs/sdk.md
|
@ -17,6 +17,7 @@
|
||||||
* [sdk::env::SetCurrentDirectory](#sdk__env__SetCurrentDirectory)
|
* [sdk::env::SetCurrentDirectory](#sdk__env__SetCurrentDirectory)
|
||||||
* [sdk::fs::CreateDirectory](#sdk__fs__CreateDirectory)
|
* [sdk::fs::CreateDirectory](#sdk__fs__CreateDirectory)
|
||||||
* [sdk::fs::CreateEmptyFile](#sdk__fs__CreateEmptyFile)
|
* [sdk::fs::CreateEmptyFile](#sdk__fs__CreateEmptyFile)
|
||||||
|
* [sdk::fs::DeleteEmptyDirectory](#sdk__fs__DeleteEmptyDirectory)
|
||||||
* [sdk::fs::GetCanonicalPath](#sdk__fs__GetCanonicalPath)
|
* [sdk::fs::GetCanonicalPath](#sdk__fs__GetCanonicalPath)
|
||||||
* [sdk::fs::GetFileName](#sdk__fs__GetFileName)
|
* [sdk::fs::GetFileName](#sdk__fs__GetFileName)
|
||||||
* [sdk::fs::GetParentDirectory](#sdk__fs__GetParentDirectory)
|
* [sdk::fs::GetParentDirectory](#sdk__fs__GetParentDirectory)
|
||||||
|
@ -798,6 +799,33 @@ exists = touch ./dir/file.txt
|
||||||
#### Aliases:
|
#### Aliases:
|
||||||
touch
|
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>
|
<a name="sdk__fs__GetCanonicalPath"></a>
|
||||||
## sdk::fs::GetCanonicalPath
|
## sdk::fs::GetCanonicalPath
|
||||||
```sh
|
```sh
|
||||||
|
|
|
@ -4,6 +4,7 @@ mod dirname;
|
||||||
mod mkdir;
|
mod mkdir;
|
||||||
mod print;
|
mod print;
|
||||||
mod read;
|
mod read;
|
||||||
|
mod rmdir;
|
||||||
mod touch;
|
mod touch;
|
||||||
mod write;
|
mod write;
|
||||||
|
|
||||||
|
@ -22,6 +23,7 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr
|
||||||
commands.set(mkdir::create(&package))?;
|
commands.set(mkdir::create(&package))?;
|
||||||
commands.set(print::create(&package))?;
|
commands.set(print::create(&package))?;
|
||||||
commands.set(read::create(&package))?;
|
commands.set(read::create(&package))?;
|
||||||
|
commands.set(rmdir::create(&package))?;
|
||||||
commands.set(touch::create(&package))?;
|
commands.set(touch::create(&package))?;
|
||||||
commands.set(write::create(&package))?;
|
commands.set(write::create(&package))?;
|
||||||
|
|
||||||
|
|
20
duckscript_sdk/src/sdk/std/fs/rmdir/help.md
Normal file
20
duckscript_sdk/src/sdk/std/fs/rmdir/help.md
Normal 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
|
||||||
|
```
|
44
duckscript_sdk/src/sdk/std/fs/rmdir/mod.rs
Executable file
44
duckscript_sdk/src/sdk/std/fs/rmdir/mod.rs
Executable 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(),
|
||||||
|
})
|
||||||
|
}
|
53
duckscript_sdk/src/sdk/std/fs/rmdir/mod_test.rs
Normal file
53
duckscript_sdk/src/sdk/std/fs/rmdir/mod_test.rs
Normal 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()),
|
||||||
|
);
|
||||||
|
}
|
Loading…
Reference in a new issue