-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b78304c
commit a18ae71
Showing
12 changed files
with
304 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
```sh | ||
var = rm [-r] path | ||
``` | ||
|
||
This command delete the requested file, empty directory or recursively deletes a directory | ||
and all its content (files and sub directories) if the **-r** flag is provided. | ||
|
||
#### Parameters | ||
|
||
* Optional flags (currently only -r is supported which indicates recursive deletion) | ||
* The path to delete | ||
|
||
#### Return Value | ||
|
||
**true** if the path was deleted. | ||
|
||
#### Examples | ||
|
||
```sh | ||
# delete a file or empty directory | ||
deleted = rm ./target | ||
|
||
# deletes a directory and all its content | ||
deleted = rm -r ./target | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use crate::utils::{flags, pckg}; | ||
use duckscript::types::command::{Command, CommandResult}; | ||
use std::fs; | ||
use std::path::Path; | ||
|
||
#[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, "DeletePath") | ||
} | ||
|
||
fn aliases(&self) -> Vec<String> { | ||
vec!["rm".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("Path not provided.".to_string()) | ||
} else { | ||
let (path_str, recursive) = if arguments.len() == 1 { | ||
(&arguments[0], false) | ||
} else if flags::is_unix_flags_argument(&arguments[0]) { | ||
let recursive = flags::is_unix_flag_exists('r', &arguments[0]); | ||
(&arguments[1], recursive) | ||
} else { | ||
(&arguments[0], false) | ||
}; | ||
|
||
let path = Path::new(path_str); | ||
|
||
let result = if !path.exists() { | ||
Ok(()) | ||
} else if path.is_file() { | ||
fs::remove_file(&arguments[0]) | ||
} else if recursive { | ||
fs::remove_dir_all(&path) | ||
} else { | ||
fs::remove_dir(&path) | ||
}; | ||
|
||
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(), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
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("")], "rm"); | ||
} | ||
|
||
#[test] | ||
fn run_path_not_exists() { | ||
test::run_script_and_validate( | ||
vec![create("")], | ||
"out = rm ./target/_duckscript/rm/newdir", | ||
CommandValidation::Match("out".to_string(), "true".to_string()), | ||
); | ||
} | ||
|
||
#[test] | ||
fn run_path_not_empty_not_recursive() { | ||
let result = io::create_directory("./target/_duckscript/rm/not_empty/dir1"); | ||
assert!(result.is_ok()); | ||
|
||
test::run_script_and_fail(vec![create("")], "rm ./target/_duckscript/rm/not_empty"); | ||
} | ||
|
||
#[test] | ||
fn run_path_is_file() { | ||
let path = Path::new("./target/_duckscript/rm/file.txt"); | ||
let result = io::create_empty_file("./target/_duckscript/rm/file.txt"); | ||
assert!(result.is_ok()); | ||
assert!(path.exists()); | ||
|
||
test::run_script_and_validate( | ||
vec![create("")], | ||
"out = rm ./target/_duckscript/rm/file.txt", | ||
CommandValidation::Match("out".to_string(), "true".to_string()), | ||
); | ||
|
||
assert!(!path.exists()); | ||
} | ||
|
||
#[test] | ||
fn run_path_recursive() { | ||
let path = Path::new("./target/_duckscript/rm/recursive/file.txt"); | ||
let result = io::create_empty_file("./target/_duckscript/rm/recursive/file.txt"); | ||
assert!(result.is_ok()); | ||
assert!(path.exists()); | ||
|
||
test::run_script_and_validate( | ||
vec![create("")], | ||
"out = rm -r ./target/_duckscript/rm/recursive", | ||
CommandValidation::Match("out".to_string(), "true".to_string()), | ||
); | ||
|
||
assert!(!path.exists()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#[cfg(test)] | ||
#[path = "./flags_test.rs"] | ||
mod flags_test; | ||
|
||
pub(crate) fn is_unix_flags_argument(argument: &str) -> bool { | ||
if argument.is_empty() { | ||
false | ||
} else if argument.starts_with("-") && argument.len() > 1 { | ||
let argument_string = argument.to_string(); | ||
let chars = argument_string[1..].chars(); | ||
|
||
for letter in chars { | ||
if !letter.is_ascii_alphabetic() { | ||
return false; | ||
} | ||
} | ||
|
||
true | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
pub(crate) fn is_unix_flag_exists(flag: char, flags: &str) -> bool { | ||
if !is_unix_flags_argument(flags) { | ||
false | ||
} else { | ||
let lowercase_flags = flags[1..].to_lowercase(); | ||
let lowercase_flag = flag.to_string().to_lowercase(); | ||
|
||
lowercase_flags.contains(&lowercase_flag) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use super::*; | ||
|
||
#[test] | ||
fn is_unix_flags_argument_empty() { | ||
let output = is_unix_flags_argument(""); | ||
|
||
assert!(!output); | ||
} | ||
|
||
#[test] | ||
fn is_unix_flags_argument_no_flag_prefix() { | ||
let output = is_unix_flags_argument("abc"); | ||
|
||
assert!(!output); | ||
} | ||
|
||
#[test] | ||
fn is_unix_flags_argument_double_flag_prefix() { | ||
let output = is_unix_flags_argument("--abc"); | ||
|
||
assert!(!output); | ||
} | ||
|
||
#[test] | ||
fn is_unix_flags_argument_valid() { | ||
let output = is_unix_flags_argument("-abc"); | ||
|
||
assert!(output); | ||
} | ||
|
||
#[test] | ||
fn is_unix_flag_exists_empty() { | ||
let output = is_unix_flag_exists('a', ""); | ||
|
||
assert!(!output); | ||
} | ||
|
||
#[test] | ||
fn is_unix_flag_exists_not_flags() { | ||
let output = is_unix_flag_exists('a', "abc"); | ||
|
||
assert!(!output); | ||
} | ||
|
||
#[test] | ||
fn is_unix_flag_exists_not_found() { | ||
let output = is_unix_flag_exists('r', "abc"); | ||
|
||
assert!(!output); | ||
} | ||
|
||
#[test] | ||
fn is_unix_flag_exists_found() { | ||
let output = is_unix_flag_exists('r', "arbc"); | ||
|
||
assert!(!output); | ||
} | ||
|
||
#[test] | ||
fn is_unix_flag_exists_found_different_case() { | ||
let output = is_unix_flag_exists('R', "arbc"); | ||
|
||
assert!(!output); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters