diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c843c21..fb043891 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/duckscript_sdk/src/sdk/std/fs/mv/mod.rs b/duckscript_sdk/src/sdk/std/fs/mv/mod.rs index c86f7291..b1b40115 100755 --- a/duckscript_sdk/src/sdk/std/fs/mv/mod.rs +++ b/duckscript_sdk/src/sdk/std/fs/mv/mod.rs @@ -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)] @@ -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 { @@ -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()), + } } else { match fsio::directory::create(&arguments[1]) { Ok(_) => { diff --git a/duckscript_sdk/src/sdk/std/fs/mv/mod_test.rs b/duckscript_sdk/src/sdk/std/fs/mv/mod_test.rs index 98d55713..a978b305 100644 --- a/duckscript_sdk/src/sdk/std/fs/mv/mod_test.rs +++ b/duckscript_sdk/src/sdk/std/fs/mv/mod_test.rs @@ -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()); } diff --git a/duckscript_sdk/src/utils/io.rs b/duckscript_sdk/src/utils/io.rs index b6531362..18f784bc 100644 --- a/duckscript_sdk/src/utils/io.rs +++ b/duckscript_sdk/src/utils/io.rs @@ -67,3 +67,7 @@ pub(crate) fn get_file_size(path: &str) -> Result { 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("\\") +}