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

mktemp: prioritize TMPDIR over -p when using -t #4878

Merged
merged 1 commit into from
May 22, 2023
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
9 changes: 8 additions & 1 deletion src/uu/mktemp/src/mktemp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ static OPT_T: &str = "t";

static ARG_TEMPLATE: &str = "template";

#[cfg(not(windows))]
const TMPDIR_ENV_VAR: &str = "TMPDIR";
#[cfg(windows)]
const TMPDIR_ENV_VAR: &str = "TMP";

#[derive(Debug)]
enum MkTempError {
PersistError(PathBuf),
Expand Down Expand Up @@ -191,7 +196,9 @@ impl Options {
(tmpdir, template.to_string())
}
Some(template) => {
let tmpdir = if matches.contains_id(OPT_TMPDIR) {
let tmpdir = if env::var(TMPDIR_ENV_VAR).is_ok() && matches.get_flag(OPT_T) {
env::var(TMPDIR_ENV_VAR).ok()
} 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
17 changes: 17 additions & 0 deletions tests/by-util/test_mktemp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -884,3 +884,20 @@ fn test_default_issue_4821_t_tmpdir_p() {
println!("stdout = {stdout}");
assert!(stdout.contains(&pathname));
}

#[test]
fn test_t_ensure_tmpdir_has_higher_priority_than_p() {
let scene = TestScenario::new(util_name!());
let pathname = scene.fixtures.as_string();
let result = scene
.ucmd()
.env(TMPDIR, &pathname)
.arg("-t")
.arg("-p")
.arg("should_not_attempt_to_write_in_this_nonexisting_dir")
Copy link
Contributor

Choose a reason for hiding this comment

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

i love this trick ;)

.arg("foo.XXXX")
.succeeds();
let stdout = result.stdout_str();
println!("stdout = {stdout}");
assert!(stdout.contains(&pathname));
}