From 01e5ba01f6a4790d327300084763800e1e9d2bc4 Mon Sep 17 00:00:00 2001 From: Thomas Coratger <60488569+tcoratger@users.noreply.github.com> Date: Wed, 5 Apr 2023 19:22:56 +0200 Subject: [PATCH] Correction bug multiple dots mkdir and touch (#8486) --- crates/nu-command/tests/commands/mkdir.rs | 42 +++++++++++++++++++++++ crates/nu-command/tests/commands/touch.rs | 39 +++++++++++++++++++++ crates/nu-path/src/dots.rs | 5 +-- 3 files changed, 84 insertions(+), 2 deletions(-) diff --git a/crates/nu-command/tests/commands/mkdir.rs b/crates/nu-command/tests/commands/mkdir.rs index f35a18e49..250e7bdbc 100644 --- a/crates/nu-command/tests/commands/mkdir.rs +++ b/crates/nu-command/tests/commands/mkdir.rs @@ -84,3 +84,45 @@ fn print_created_paths() { 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/touch.rs b/crates/nu-command/tests/commands/touch.rs index ea19ce8eb..3b30ab250 100644 --- a/crates/nu-command/tests/commands/touch.rs +++ b/crates/nu-command/tests/commands/touch.rs @@ -122,3 +122,42 @@ fn not_create_file_if_it_not_exists() { assert!(!path.exists()); }) } + +#[test] +fn creates_file_three_dots() { + Playground::setup("create_test_1", |dirs, _sandbox| { + nu!( + cwd: dirs.test(), + "touch file..." + ); + + let path = dirs.test().join("file..."); + assert!(path.exists()); + }) +} + +#[test] +fn creates_file_four_dots() { + Playground::setup("create_test_1", |dirs, _sandbox| { + nu!( + cwd: dirs.test(), + "touch file...." + ); + + let path = dirs.test().join("file...."); + assert!(path.exists()); + }) +} + +#[test] +fn creates_file_four_dots_quotation_marks() { + Playground::setup("create_test_1", |dirs, _sandbox| { + nu!( + cwd: dirs.test(), + "touch 'file....'" + ); + + let path = dirs.test().join("file...."); + assert!(path.exists()); + }) +} diff --git a/crates/nu-path/src/dots.rs b/crates/nu-path/src/dots.rs index 27c73351d..776a4caf8 100644 --- a/crates/nu-path/src/dots.rs +++ b/crates/nu-path/src/dots.rs @@ -33,6 +33,7 @@ pub fn expand_ndots(path: impl AsRef) -> PathBuf { // find if we need to expand any >2 dot paths and early exit if not let mut dots_count = 0u8; + let mut not_separator_before_dot = false; let ndots_present = { for chr in path_str.chars() { if chr == '.' { @@ -42,7 +43,7 @@ pub fn expand_ndots(path: impl AsRef) -> PathBuf { // this path component had >2 dots break; } - + not_separator_before_dot = !(is_separator(chr) || chr.is_whitespace()); dots_count = 0; } } @@ -50,7 +51,7 @@ pub fn expand_ndots(path: impl AsRef) -> PathBuf { dots_count > 2 }; - if !ndots_present { + if !ndots_present || not_separator_before_dot { return path.as_ref().into(); }