1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

Merge pull request #1677 from sylvestre/mktemp-t

feature(mktemp): implement -t
This commit is contained in:
Sylvestre Ledru 2021-01-02 21:10:59 +01:00 committed by GitHub
commit 90722c1f3c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 73 additions and 7 deletions

View file

@ -32,6 +32,7 @@ static OPT_DRY_RUN: &str = "dry-run";
static OPT_QUIET: &str = "quiet"; static OPT_QUIET: &str = "quiet";
static OPT_SUFFIX: &str = "suffix"; static OPT_SUFFIX: &str = "suffix";
static OPT_TMPDIR: &str = "tmpdir"; static OPT_TMPDIR: &str = "tmpdir";
static OPT_T: &str = "t";
static ARG_TEMPLATE: &str = "template"; static ARG_TEMPLATE: &str = "template";
@ -79,12 +80,16 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
.long(OPT_TMPDIR) .long(OPT_TMPDIR)
.help( .help(
"interpret TEMPLATE relative to DIR; if DIR is not specified, use \ "interpret TEMPLATE relative to DIR; if DIR is not specified, use \
$TMPDIR if set, else /tmp. With this option, TEMPLATE must not \ $TMPDIR if set, else /tmp. With this option, TEMPLATE must not \
be an absolute name; unlike with -t, TEMPLATE may contain \ be an absolute name; unlike with -t, TEMPLATE may contain \
slashes, but mktemp creates only the final component", slashes, but mktemp creates only the final component",
) )
.value_name("DIR"), .value_name("DIR"),
) )
.arg(Arg::with_name(OPT_T).short(OPT_T).help(
"Generate a template (using the supplied prefix and TMPDIR if set) \
to create a filename template [deprecated]",
))
.arg( .arg(
Arg::with_name(ARG_TEMPLATE) Arg::with_name(ARG_TEMPLATE)
.multiple(false) .multiple(false)
@ -93,10 +98,6 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
.default_value(DEFAULT_TEMPLATE), .default_value(DEFAULT_TEMPLATE),
) )
.get_matches_from(args); .get_matches_from(args);
// deprecated option of GNU coreutils
// .arg(
// Arg::with_name(("t", "", "Generate a template (using the supplied prefix and TMPDIR if set) \
// to create a filename template");
let template = matches.value_of(ARG_TEMPLATE).unwrap(); let template = matches.value_of(ARG_TEMPLATE).unwrap();
@ -129,7 +130,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
crash!(1, "suffix cannot contain any path separators"); crash!(1, "suffix cannot contain any path separators");
} }
let tmpdir = match matches.value_of(OPT_TMPDIR) { let mut tmpdir = match matches.value_of(OPT_TMPDIR) {
Some(s) => { Some(s) => {
if PathBuf::from(prefix).is_absolute() { if PathBuf::from(prefix).is_absolute() {
show_info!( show_info!(
@ -143,6 +144,10 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
None => env::temp_dir(), None => env::temp_dir(),
}; };
if matches.is_present(OPT_T) {
tmpdir = env::temp_dir()
};
if dry_run { if dry_run {
dry_exec(tmpdir, prefix, rand, &suffix) dry_exec(tmpdir, prefix, rand, &suffix)
} else { } else {

View file

@ -65,6 +65,67 @@ fn test_mktemp_mktemp() {
.fails(); .fails();
} }
#[test]
fn test_mktemp_mktemp_t() {
let scene = TestScenario::new(util_name!());
let pathname = scene.fixtures.as_string();
scene
.ucmd()
.env(TMPDIR, &pathname)
.arg("-t")
.arg(TEST_TEMPLATE1)
.succeeds();
scene
.ucmd()
.env(TMPDIR, &pathname)
.arg("-t")
.arg(TEST_TEMPLATE2)
.fails();
scene
.ucmd()
.env(TMPDIR, &pathname)
.arg("-t")
.arg(TEST_TEMPLATE3)
.fails();
scene
.ucmd()
.env(TMPDIR, &pathname)
.arg("-t")
.arg(TEST_TEMPLATE4)
.fails();
scene
.ucmd()
.env(TMPDIR, &pathname)
.arg("-t")
.arg(TEST_TEMPLATE5)
.succeeds();
scene
.ucmd()
.env(TMPDIR, &pathname)
.arg("-t")
.arg(TEST_TEMPLATE6)
.succeeds();
scene
.ucmd()
.env(TMPDIR, &pathname)
.arg("-t")
.arg(TEST_TEMPLATE7)
.succeeds();
let result = scene
.ucmd()
.env(TMPDIR, &pathname)
.arg("-t")
.arg(TEST_TEMPLATE8)
.fails();
println!("stdout {}", result.stdout);
println!("stderr {}", result.stderr);
assert!(result
.stderr
.contains("error: suffix cannot contain any path separators"));
}
#[test] #[test]
fn test_mktemp_make_temp_dir() { fn test_mktemp_make_temp_dir() {
let scene = TestScenario::new(util_name!()); let scene = TestScenario::new(util_name!());