8000 mv: allow a single source with --target-directory by ilkecan · Pull Request #3529 · uutils/coreutils · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

mv: allow a single source with --target-directory #3529

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

Merged
merged 1 commit into from
May 21, 2022
Merged
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
31 changes: 22 additions & 9 deletions src/uu/mv/src/mv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod error;
#[macro_use]
extern crate uucore;

use clap::{crate_version, Arg, ArgMatches, Command};
use clap::{crate_version, Arg, ArgMatches, Command, ErrorKind};
use std::env;
use std::ffi::OsString;
use std::fs;
Expand Down Expand Up @@ -70,13 +70,26 @@ static ARG_FILES: &str = "files";

#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = uu_app()
.after_help(&*format!(
"{}\n{}",
LONG_HELP,
backup_control::BACKUP_CONTROL_LONG_HELP
))
.get_matches_from(args);
let help = format!(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems like an unrelated change, no ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That variable is created to avoid "temporary value dropped while borrowed" error:

  --> src/uu/mv/src/mv.rs:79:41
   |
79 |       let mut app = uu_app().after_help(&*format!(
   |  _________________________________________^
80 | |         "{}\n{}",
81 | |         LONG_HELP,
82 | |         backup_control::BACKUP_CONTROL_LONG_HELP
83 | |     ));
   | |     ^ - temporary value is freed at the end of this statement
   | |_____|
   |       creates a temporary which is freed while still in use
84 |       let matches = app
   |                     --- borrow later used here
   |
   = note: consider using a `let` binding to create a longer lived value
   = note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0716`.
error: could not compile `uu_mv` due to previous error

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, right, thanks

"{}\n{}",
LONG_HELP,
backup_control::BACKUP_CONTROL_LONG_HELP
);
let mut app = uu_app().after_help(&*help);
let matches = app
.try_get_matches_from_mut(args)
.unwrap_or_else(|e| e.exit());

if !matches.is_present(OPT_TARGET_DIRECTORY) && matches.occurrences_of(ARG_FILES) == 1 {
app.error(
ErrorKind::TooFewValues,
format!(
"The argument '<{}>...' requires at least 2 values, but only 1 was provided",
ARG_FILES
),
)
.exit();
}

let files: Vec<OsString> = matches
.values_of_os(ARG_FILES)
Expand Down Expand Up @@ -180,7 +193,7 @@ pub fn uu_app<'a>() -> Command<'a> {
Arg::new(ARG_FILES)
.multiple_occurrences(true)
.takes_value(true)
.min_values(2)
.min_values(1)
.required(true)
.allow_invalid_utf8(true)
)
Expand Down
14 changes: 14 additions & 0 deletions tests/by-util/test_mv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,20 @@ fn test_mv_target_dir() {
assert!(at.file_exists(&format!("{}/{}", dir, file_b)));
}

#[test]
fn test_mv_target_dir_single_source() {
let (at, mut ucmd) = at_and_ucmd!();
let dir = "test_mv_target_dir_single_source_dir";
let file = "test_mv_target_dir_single_source_file";

at.touch(file);
at.mkdir(dir);
ucmd.arg("-t").arg(dir).arg(file).succeeds().no_stderr();

assert!(!at.file_exists(file));
assert!(at.file_exists(&format!("{}/{}", dir, file)));
}

#[test]
fn test_mv_overwrite_dir() {
let (at, mut ucmd) = at_and_ucmd!();
Expand Down
0