diff --git a/src/uu/mktemp/src/mktemp.rs b/src/uu/mktemp/src/mktemp.rs index dd1f57b8b44..7787af596f5 100644 --- a/src/uu/mktemp/src/mktemp.rs +++ b/src/uu/mktemp/src/mktemp.rs @@ -162,12 +162,21 @@ impl Options { let template = matches.value_of(OPT_TMPDIR).unwrap().to_string(); (tmpdir, template) } else { - let tmpdir = matches.value_of(OPT_TMPDIR).map(String::from); - let template = matches - .value_of(ARG_TEMPLATE) - .unwrap_or(DEFAULT_TEMPLATE) - .to_string(); - (tmpdir, template) + // If no template argument is given, `--tmpdir` is implied. + match matches.value_of(ARG_TEMPLATE) { + None => { + let tmpdir = match matches.value_of(OPT_TMPDIR) { + None => Some(env::temp_dir().display().to_string()), + Some(tmpdir) => Some(tmpdir.to_string()), + }; + let template = DEFAULT_TEMPLATE; + (tmpdir, template.to_string()) + } + Some(template) => { + let tmpdir = matches.value_of(OPT_TMPDIR).map(String::from); + (tmpdir, template.to_string()) + } + } }; Self { directory: matches.is_present(OPT_DIRECTORY), diff --git a/tests/by-util/test_mktemp.rs b/tests/by-util/test_mktemp.rs index 55ca021c105..744e02a92c1 100644 --- a/tests/by-util/test_mktemp.rs +++ b/tests/by-util/test_mktemp.rs @@ -4,7 +4,7 @@ use crate::common::util::*; use uucore::display::Quotable; -use std::path::PathBuf; +use std::path::{PathBuf, MAIN_SEPARATOR}; use tempfile::tempdir; #[cfg(unix)] @@ -572,3 +572,42 @@ fn test_too_few_xs_suffix_directory() { fn test_too_many_arguments() { new_ucmd!().args(&["-q", "a", "b"]).fails().code_is(1); } + +/// Test that files are created relative to `TMPDIR` environment variable. +#[test] +fn test_tmpdir_env_var() { + // `TMPDIR=. mktemp` + let (at, mut ucmd) = at_and_ucmd!(); + let result = ucmd.env(TMPDIR, ".").succeeds(); + let filename = result.no_stderr().stdout_str().trim_end(); + let template = format!(".{}tmp.XXXXXXXXXX", MAIN_SEPARATOR); + assert_matches_template!(&template, filename); + assert!(at.file_exists(filename)); + + // FIXME This is not working because --tmpdir is configured to + // require a value. + // + // // `TMPDIR=. mktemp --tmpdir` + // let (at, mut ucmd) = at_and_ucmd!(); + // let result = ucmd.env(TMPDIR, ".").arg("--tmpdir").succeeds(); + // let filename = result.no_stderr().stdout_str().trim_end(); + // let template = format!(".{}tmp.XXXXXXXXXX", MAIN_SEPARATOR); + // assert_matches_template!(&template, filename); + // assert!(at.file_exists(filename)); + + // `TMPDIR=. mktemp --tmpdir XXX` + let (at, mut ucmd) = at_and_ucmd!(); + let result = ucmd.env(TMPDIR, ".").args(&["--tmpdir", "XXX"]).succeeds(); + let filename = result.no_stderr().stdout_str().trim_end(); + let template = format!(".{}XXX", MAIN_SEPARATOR); + assert_matches_template!(&template, filename); + assert!(at.file_exists(filename)); + + // `TMPDIR=. mktemp XXX` - in this case `TMPDIR` is ignored. + let (at, mut ucmd) = at_and_ucmd!(); + let result = ucmd.env(TMPDIR, ".").arg("XXX").succeeds(); + let filename = result.no_stderr().stdout_str().trim_end(); + let template = "XXX"; + assert_matches_template!(&template, filename); + assert!(at.file_exists(filename)); +}