From 7b95e37bbebdf0b52795ca9f2d4cff446d6f0b6a Mon Sep 17 00:00:00 2001 From: Justin Ma Date: Wed, 28 Feb 2024 20:27:10 +0800 Subject: [PATCH] Making coreutils umkdir as the default mkdir (#12007) # Description `umkdir` was added in #10785, I think it's time to replace the default one. # After Submitting Remove the old `mkdir` command and making coreutils' `umkdir` as the default --- crates/nu-command/src/default_context.rs | 1 - crates/nu-command/src/filesystem/mkdir.rs | 108 ------------------ crates/nu-command/src/filesystem/mod.rs | 2 - crates/nu-command/src/filesystem/umkdir.rs | 8 +- crates/nu-command/tests/commands/mkdir.rs | 125 --------------------- crates/nu-command/tests/commands/mod.rs | 1 - crates/nu-command/tests/commands/umkdir.rs | 32 +++--- 7 files changed, 20 insertions(+), 257 deletions(-) delete mode 100644 crates/nu-command/src/filesystem/mkdir.rs delete mode 100644 crates/nu-command/tests/commands/mkdir.rs diff --git a/crates/nu-command/src/default_context.rs b/crates/nu-command/src/default_context.rs index 913362f7d3..6f74bcc0dc 100644 --- a/crates/nu-command/src/default_context.rs +++ b/crates/nu-command/src/default_context.rs @@ -203,7 +203,6 @@ pub fn add_shell_command_context(mut engine_state: EngineState) -> EngineState { bind_command! { Cd, Ls, - Mkdir, UMkdir, Mktemp, Mv, diff --git a/crates/nu-command/src/filesystem/mkdir.rs b/crates/nu-command/src/filesystem/mkdir.rs deleted file mode 100644 index ea21794ce6..0000000000 --- a/crates/nu-command/src/filesystem/mkdir.rs +++ /dev/null @@ -1,108 +0,0 @@ -use std::collections::VecDeque; - -use nu_engine::env::current_dir; -use nu_engine::CallExt; -use nu_protocol::ast::Call; -use nu_protocol::engine::{Command, EngineState, Stack}; -use nu_protocol::{ - Category, Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature, - SyntaxShape, Type, Value, -}; - -#[derive(Clone)] -pub struct Mkdir; - -impl Command for Mkdir { - fn name(&self) -> &str { - "mkdir" - } - - fn signature(&self) -> Signature { - Signature::build("mkdir") - .input_output_types(vec![(Type::Nothing, Type::Nothing)]) - .rest( - "rest", - SyntaxShape::Directory, - "The name(s) of the path(s) to create.", - ) - .switch("verbose", "print created path(s).", Some('v')) - .category(Category::FileSystem) - } - - fn usage(&self) -> &str { - "Make directories, creates intermediary directories as required." - } - - fn search_terms(&self) -> Vec<&str> { - vec!["directory", "folder", "create", "make_dirs"] - } - - fn run( - &self, - engine_state: &EngineState, - stack: &mut Stack, - call: &Call, - _input: PipelineData, - ) -> Result { - let path = current_dir(engine_state, stack)?; - let mut directories = call - .rest::(engine_state, stack, 0)? - .into_iter() - .map(|dir| path.join(dir)) - .peekable(); - - let is_verbose = call.has_flag(engine_state, stack, "verbose")?; - let mut stream: VecDeque = VecDeque::new(); - - if directories.peek().is_none() { - return Err(ShellError::MissingParameter { - param_name: "requires directory paths".to_string(), - span: call.head, - }); - } - - for (i, dir) in directories.enumerate() { - let span = call - .positional_nth(i) - .expect("already checked through directories") - .span; - let dir_res = std::fs::create_dir_all(&dir); - - if let Err(reason) = dir_res { - return Err(ShellError::CreateNotPossible { - msg: format!("failed to create directory: {reason}"), - span: call - .positional_nth(i) - .expect("already checked through directories") - .span, - }); - } - - if is_verbose { - let val = format!("{:}", dir.to_string_lossy()); - stream.push_back(Value::string(val, span)); - } - } - - stream - .into_iter() - .into_pipeline_data(engine_state.ctrlc.clone()) - .print_not_formatted(engine_state, false, true)?; - Ok(PipelineData::empty()) - } - - fn examples(&self) -> Vec { - vec![ - Example { - description: "Make a directory named foo", - example: "mkdir foo", - result: None, - }, - Example { - description: "Make multiple directories and show the paths created", - example: "mkdir -v foo/bar foo2", - result: None, - }, - ] - } -} diff --git a/crates/nu-command/src/filesystem/mod.rs b/crates/nu-command/src/filesystem/mod.rs index d6774c68f1..bc62a39637 100644 --- a/crates/nu-command/src/filesystem/mod.rs +++ b/crates/nu-command/src/filesystem/mod.rs @@ -2,7 +2,6 @@ mod cd; mod du; mod glob; mod ls; -mod mkdir; mod mktemp; mod mv; mod open; @@ -21,7 +20,6 @@ pub use cd::Cd; pub use du::Du; pub use glob::Glob; pub use ls::Ls; -pub use mkdir::Mkdir; pub use mktemp::Mktemp; pub use mv::Mv; pub use rm::Rm; diff --git a/crates/nu-command/src/filesystem/umkdir.rs b/crates/nu-command/src/filesystem/umkdir.rs index 58e1a579b6..7f9267bf03 100644 --- a/crates/nu-command/src/filesystem/umkdir.rs +++ b/crates/nu-command/src/filesystem/umkdir.rs @@ -16,7 +16,7 @@ const DEFAULT_MODE: u32 = 0o777; impl Command for UMkdir { fn name(&self) -> &str { - "umkdir" + "mkdir" } fn usage(&self) -> &str { @@ -28,7 +28,7 @@ impl Command for UMkdir { } fn signature(&self) -> Signature { - Signature::build("umkdir") + Signature::build("mkdir") .input_output_types(vec![(Type::Nothing, Type::Nothing)]) .rest( "rest", @@ -85,12 +85,12 @@ impl Command for UMkdir { vec![ Example { description: "Make a directory named foo", - example: "umkdir foo", + example: "mkdir foo", result: None, }, Example { description: "Make multiple directories and show the paths created", - example: "umkdir -v foo/bar foo2", + example: "mkdir -v foo/bar foo2", result: None, }, ] diff --git a/crates/nu-command/tests/commands/mkdir.rs b/crates/nu-command/tests/commands/mkdir.rs deleted file mode 100644 index 7851fc006d..0000000000 --- a/crates/nu-command/tests/commands/mkdir.rs +++ /dev/null @@ -1,125 +0,0 @@ -use nu_test_support::fs::files_exist_at; -use nu_test_support::playground::Playground; -use nu_test_support::{nu, pipeline}; -use std::path::Path; - -#[test] -fn creates_directory() { - Playground::setup("mkdir_test_1", |dirs, _| { - nu!( - cwd: dirs.test(), - "mkdir my_new_directory" - ); - - let expected = dirs.test().join("my_new_directory"); - - assert!(expected.exists()); - }) -} - -#[test] -fn accepts_and_creates_directories() { - Playground::setup("mkdir_test_2", |dirs, _| { - nu!( - cwd: dirs.test(), - "mkdir dir_1 dir_2 dir_3" - ); - - assert!(files_exist_at( - vec![Path::new("dir_1"), Path::new("dir_2"), Path::new("dir_3")], - dirs.test() - )); - }) -} - -#[test] -fn creates_intermediary_directories() { - Playground::setup("mkdir_test_3", |dirs, _| { - nu!( - cwd: dirs.test(), - "mkdir some_folder/another/deeper_one" - ); - - let expected = dirs.test().join("some_folder/another/deeper_one"); - - assert!(expected.exists()); - }) -} - -#[test] -fn create_directory_two_parents_up_using_multiple_dots() { - Playground::setup("mkdir_test_4", |dirs, sandbox| { - sandbox.within("foo").mkdir("bar"); - - nu!( - cwd: dirs.test().join("foo/bar"), - "mkdir .../boo" - ); - - let expected = dirs.test().join("boo"); - - assert!(expected.exists()); - }) -} - -#[test] -fn print_created_paths() { - Playground::setup("mkdir_test_2", |dirs, _| { - let actual = nu!( - cwd: dirs.test(), - pipeline( - "mkdir -v dir_1 dir_2 dir_3" - )); - - assert!(files_exist_at( - vec![Path::new("dir_1"), Path::new("dir_2"), Path::new("dir_3")], - dirs.test() - )); - - assert!(actual.err.contains("dir_1")); - assert!(actual.err.contains("dir_2")); - assert!(actual.err.contains("dir_3")); - }) -} - -#[test] -fn creates_directory_three_dots() { - Playground::setup("mkdir_test_1", |dirs, _| { - nu!( - cwd: dirs.test(), - "mkdir test..." - ); - - let expected = dirs.test().join("test..."); - - assert!(expected.exists()); - }) -} - -#[test] -fn creates_directory_four_dots() { - Playground::setup("mkdir_test_1", |dirs, _| { - nu!( - cwd: dirs.test(), - "mkdir test...." - ); - - let expected = dirs.test().join("test...."); - - assert!(expected.exists()); - }) -} - -#[test] -fn creates_directory_three_dots_quotation_marks() { - Playground::setup("mkdir_test_1", |dirs, _| { - nu!( - cwd: dirs.test(), - "mkdir 'test...'" - ); - - let expected = dirs.test().join("test..."); - - assert!(expected.exists()); - }) -} diff --git a/crates/nu-command/tests/commands/mod.rs b/crates/nu-command/tests/commands/mod.rs index e329c0f9b1..ffbfba3a5a 100644 --- a/crates/nu-command/tests/commands/mod.rs +++ b/crates/nu-command/tests/commands/mod.rs @@ -60,7 +60,6 @@ mod ls; mod match_; mod math; mod merge; -mod mkdir; mod mktemp; mod move_; mod mut_; diff --git a/crates/nu-command/tests/commands/umkdir.rs b/crates/nu-command/tests/commands/umkdir.rs index 8d4cf78c79..e6a74ab51c 100644 --- a/crates/nu-command/tests/commands/umkdir.rs +++ b/crates/nu-command/tests/commands/umkdir.rs @@ -5,10 +5,10 @@ use std::path::Path; #[test] fn creates_directory() { - Playground::setup("umkdir_test_1", |dirs, _| { + Playground::setup("mkdir_test_1", |dirs, _| { nu!( cwd: dirs.test(), - "umkdir my_new_directory" + "mkdir my_new_directory" ); let expected = dirs.test().join("my_new_directory"); @@ -19,10 +19,10 @@ fn creates_directory() { #[test] fn accepts_and_creates_directories() { - Playground::setup("umkdir_test_2", |dirs, _| { + Playground::setup("mkdir_test_2", |dirs, _| { nu!( cwd: dirs.test(), - "umkdir dir_1 dir_2 dir_3" + "mkdir dir_1 dir_2 dir_3" ); assert!(files_exist_at( @@ -34,10 +34,10 @@ fn accepts_and_creates_directories() { #[test] fn creates_intermediary_directories() { - Playground::setup("umkdir_test_3", |dirs, _| { + Playground::setup("mkdir_test_3", |dirs, _| { nu!( cwd: dirs.test(), - "umkdir some_folder/another/deeper_one" + "mkdir some_folder/another/deeper_one" ); let expected = dirs.test().join("some_folder/another/deeper_one"); @@ -48,12 +48,12 @@ fn creates_intermediary_directories() { #[test] fn create_directory_two_parents_up_using_multiple_dots() { - Playground::setup("umkdir_test_4", |dirs, sandbox| { + Playground::setup("mkdir_test_4", |dirs, sandbox| { sandbox.within("foo").mkdir("bar"); nu!( cwd: dirs.test().join("foo/bar"), - "umkdir .../boo" + "mkdir .../boo" ); let expected = dirs.test().join("boo"); @@ -64,10 +64,10 @@ fn create_directory_two_parents_up_using_multiple_dots() { #[test] fn print_created_paths() { - Playground::setup("umkdir_test_2", |dirs, _| { + Playground::setup("mkdir_test_2", |dirs, _| { let actual = nu!( cwd: dirs.test(), - pipeline("umkdir -v dir_1 dir_2 dir_3") + pipeline("mkdir -v dir_1 dir_2 dir_3") ); assert!(files_exist_at( @@ -83,10 +83,10 @@ fn print_created_paths() { #[test] fn creates_directory_three_dots() { - Playground::setup("umkdir_test_1", |dirs, _| { + Playground::setup("mkdir_test_1", |dirs, _| { nu!( cwd: dirs.test(), - "umkdir test..." + "mkdir test..." ); let expected = dirs.test().join("test..."); @@ -97,10 +97,10 @@ fn creates_directory_three_dots() { #[test] fn creates_directory_four_dots() { - Playground::setup("umkdir_test_1", |dirs, _| { + Playground::setup("mkdir_test_1", |dirs, _| { nu!( cwd: dirs.test(), - "umkdir test...." + "mkdir test...." ); let expected = dirs.test().join("test...."); @@ -111,10 +111,10 @@ fn creates_directory_four_dots() { #[test] fn creates_directory_three_dots_quotation_marks() { - Playground::setup("umkdir_test_1", |dirs, _| { + Playground::setup("mkdir_test_1", |dirs, _| { nu!( cwd: dirs.test(), - "umkdir 'test...'" + "mkdir 'test...'" ); let expected = dirs.test().join("test...");