Skip to content

Commit

Permalink
add support for renaming directories via mv command #374
Browse files Browse the repository at this point in the history
  • Loading branch information
sagiegurari committed Nov 18, 2023
1 parent 2395868 commit d8ccbc0
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## CHANGELOG

### v0.9.2

* Enhancement: \[Breaking Change\] add support for renaming directories via mv command #374

### v0.9.1 (2023-09-17)

* Fix: panic on empty environment variable name provided for set_env command
Expand Down
21 changes: 17 additions & 4 deletions duckscript_sdk/src/sdk/std/fs/mv/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::utils::io::ends_with_separator;
use crate::utils::pckg;
use duckscript::types::command::{Command, CommandResult};
use fs_extra::{dir, move_items};
use fsio;
use fsio::directory::create_parent;
use std::fs::rename;
use std::path::Path;

#[cfg(test)]
Expand Down Expand Up @@ -41,13 +43,14 @@ impl Command for CommandImpl {
CommandResult::Error("Path does not exist.".to_string())
} else {
let target_path = Path::new(&arguments[1]);
let source_ends_with_separator = ends_with_separator(&arguments[0]);
let source_file = source_path.is_file();
let target_file = if target_path.exists() {
let target_exists = target_path.exists();
let target_ends_with_separator = ends_with_separator(&arguments[1]);
let target_file = if target_exists {
target_path.is_file()
} else {
!target_path.ends_with("/")
&& !target_path.ends_with("\\")
&& target_path.extension().is_some()
!target_ends_with_separator && target_path.extension().is_some()
};

if source_file && target_file {
Expand All @@ -61,6 +64,16 @@ impl Command for CommandImpl {
}
Err(error) => CommandResult::Error(error.to_string()),
}
} else if !source_file
&& !target_file
&& !source_ends_with_separator
&& !target_ends_with_separator
{
// rename directory
match rename(source_path, target_path) {
Ok(_) => CommandResult::Continue(Some("true".to_string())),
Err(error) => CommandResult::Error(error.to_string()),

Check warning on line 75 in duckscript_sdk/src/sdk/std/fs/mv/mod.rs

View check run for this annotation

Codecov / codecov/patch

duckscript_sdk/src/sdk/std/fs/mv/mod.rs#L75

Added line #L75 was not covered by tests
}
} else {
match fsio::directory::create(&arguments[1]) {
Ok(_) => {
Expand Down
34 changes: 29 additions & 5 deletions duckscript_sdk/src/sdk/std/fs/mv/mod_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,45 @@ fn run_file_to_directory() {
}

#[test]
fn run_directory_to_directory() {
let mut path = Path::new("./target/_duckscript/mv/run_directory_to_directory/1/1/file1.txt");
let result = ensure_exists("./target/_duckscript/mv/run_directory_to_directory/1/1/file1.txt");
fn run_directory_to_directory_rename() {
let mut path =
Path::new("./target/_duckscript/mv/run_directory_to_directory_rename/1/1/file1.txt");
let result =
ensure_exists("./target/_duckscript/mv/run_directory_to_directory_rename/1/1/file1.txt");
assert!(result.is_ok());
assert!(path.exists());

test::run_script_and_validate(
vec![create("")],
r#"
out = mv ./target/_duckscript/mv/run_directory_to_directory/1 ./target/_duckscript/mv/run_directory_to_directory/2
out = mv ./target/_duckscript/mv/run_directory_to_directory_rename/1 ./target/_duckscript/mv/run_directory_to_directory_rename/2
"#,
CommandValidation::Match("out".to_string(), "true".to_string()),
);

assert!(!path.exists());
path = Path::new("./target/_duckscript/mv/run_directory_to_directory/2/1/1/file1.txt");
path = Path::new("./target/_duckscript/mv/run_directory_to_directory_rename/2/1/file1.txt");
assert!(path.exists());
}

#[test]
fn run_directory_to_directory_move() {
let mut path =
Path::new("./target/_duckscript/mv/run_directory_to_directory_move/1/1/file1.txt");
let result =
ensure_exists("./target/_duckscript/mv/run_directory_to_directory_move/1/1/file1.txt");
assert!(result.is_ok());
assert!(path.exists());

test::run_script_and_validate(
vec![create("")],
r#"
out = mv ./target/_duckscript/mv/run_directory_to_directory_move/1 ./target/_duckscript/mv/run_directory_to_directory_move/2/
"#,
CommandValidation::Match("out".to_string(), "true".to_string()),
);

assert!(!path.exists());
path = Path::new("./target/_duckscript/mv/run_directory_to_directory_move/2/1/1/file1.txt");
assert!(path.exists());
}
4 changes: 4 additions & 0 deletions duckscript_sdk/src/utils/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,7 @@ pub(crate) fn get_file_size(path: &str) -> Result<u64, String> {
Err(_error) => Err("Unable to extract metadata for path.".to_string()),
}
}

pub(crate) fn ends_with_separator(path_str: &str) -> bool {
path_str.ends_with("/") || path_str.ends_with("\\")
}

0 comments on commit d8ccbc0

Please sign in to comment.