Skip to content

Commit

Permalink
Support TMPDIR=dir_a cargo run mktemp -t -p dir_b foo.XXXX
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvestre committed May 17, 2023
1 parent b2c84e1 commit 4c99951
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/uu/mktemp/src/mktemp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,15 @@ impl Options {
(tmpdir, template.to_string())
}
Some(template) => {
let tmpdir = if matches.contains_id(OPT_TMPDIR) {
let tmpdir = if let Ok(tmpdir_env) = env::var("TMPDIR") {
// Prioritize TMPDIR environment variable over -p flag
// Ignore TMPDIR when it's set to "."
if tmpdir_env != "." {
Some(tmpdir_env)
} else {
matches.get_one::<String>(OPT_TMPDIR).map(String::from)
}
} else if matches.contains_id(OPT_TMPDIR) {
matches.get_one::<String>(OPT_TMPDIR).map(String::from)
} else if matches.get_flag(OPT_T) {
// mktemp -t foo.xxx should export in TMPDIR
Expand Down
16 changes: 16 additions & 0 deletions tests/by-util/test_mktemp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -888,3 +888,19 @@ fn test_default_issue_4821_t_tmpdir_p() {
println!("stdout = {stdout}");
assert!(stdout.contains(&pathname));
}

#[test]
fn test_default_issue_4821_t_tmpdir_p_with_var() {
let (at, mut ucmd) = at_and_ucmd!();
at.mkdir("dir_a");
let result = ucmd
.env("TMPDIR", "dir_a")
.arg("-t")
.arg("-p")
.arg("dir_b")
.arg("foo.XXXX")
.succeeds();
let stdout = result.stdout_str();
println!("stdout = {stdout}");
assert!(stdout.contains("dir_a"));
}

0 comments on commit 4c99951

Please sign in to comment.