Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mkdir: fail if no arguments are provided #6791

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/uu/mkdir/src/mkdir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,29 @@ pub fn uu_app() -> Command {
* Create the list of new directories
*/
fn exec(dirs: ValuesRef<OsString>, recursive: bool, mode: u32, verbose: bool) -> UResult<()> {
let mut dirs_is_empty = true;

for dir in dirs {
dirs_is_empty = false;

let path_buf = PathBuf::from(dir);
let path = path_buf.as_path();

show_if_err!(mkdir(path, recursive, mode, verbose));
}

if dirs_is_empty {
return Err(USimpleError::new(
1,
format!(
"\
missing operand
Try '{} --help' for more information.",
uucore::execution_phrase()
),
));
}

Ok(())
}

Expand All @@ -159,6 +176,13 @@ fn exec(dirs: ValuesRef<OsString>, recursive: bool, mode: u32, verbose: bool) ->
/// To match the GNU behavior, a path with the last directory being a single dot
/// (like `some/path/to/.`) is created (with the dot stripped).
pub fn mkdir(path: &Path, recursive: bool, mode: u32, verbose: bool) -> UResult<()> {
if path.as_os_str().is_empty() {
return Err(USimpleError::new(
1,
"cannot create directory '': No such file or directory".to_owned(),
));
}

// Special case to match GNU's behavior:
// mkdir -p foo/. should work and just create foo/
// std::fs::create_dir("foo/."); fails in pure Rust
Expand Down
18 changes: 18 additions & 0 deletions tests/by-util/test_mkdir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

// spell-checker:ignore bindgen

#![allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]

use crate::common::util::TestScenario;
Expand Down Expand Up @@ -324,3 +327,18 @@ fn test_umask_compliance() {
test_single_case(i as mode_t);
}
}

#[test]
fn test_no_arguments() {
new_ucmd!().fails().no_stdout().stderr_str_check(|st| {
st.starts_with("mkdir: missing operand\n") && st.ends_with("for more information.\n")
});
}

#[test]
fn test_empty_argument() {
new_ucmd!()
.arg("")
.fails()
.stderr_only("mkdir: cannot create directory '': No such file or directory\n");
}
Loading