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: respect POSIXLY_CORRECT env var when parsing args #3604

Merged
merged 1 commit into from
Jun 9, 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
25 changes: 23 additions & 2 deletions src/uu/mktemp/src/mktemp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,14 @@ enum MkTempError {
/// The template suffix contains a path separator (e.g. `"XXXa/b"`).
SuffixContainsDirSeparator(String),
InvalidTemplate(String),
TooManyTemplates,
}

impl UError for MkTempError {}
impl UError for MkTempError {
fn usage(&self) -> bool {
matches!(self, Self::TooManyTemplates)
}
}

impl Error for MkTempError {}

Expand Down Expand Up @@ -85,6 +90,9 @@ impl Display for MkTempError {
"invalid template, {}; with --tmpdir, it may not be absolute",
s.quote()
),
TooManyTemplates => {
write!(f, "too many templates")
}
}
}
}
Expand Down Expand Up @@ -308,11 +316,24 @@ impl Params {

#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = uu_app().try_get_matches_from(args)?;
let args = args.collect_str_lossy().accept_any();

let matches = uu_app().try_get_matches_from(&args)?;

// Parse command-line options into a format suitable for the
// application logic.
let options = Options::from(&matches);

if env::var("POSIXLY_CORRECT").is_ok() {
// If POSIXLY_CORRECT was set, template MUST be the last argument.
if is_tmpdir_argument_actually_the_template(&matches) || matches.is_present(ARG_TEMPLATE) {
// Template argument was provided, check if was the last one.
if args.last().unwrap() != &options.template {
return Err(Box::new(MkTempError::TooManyTemplates));
}
}
}

let dry_run = options.dry_run;
let suppress_file_err = options.quiet;
let make_dir = options.directory;
Expand Down
22 changes: 22 additions & 0 deletions tests/by-util/test_mktemp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,3 +641,25 @@ fn test_suffix_empty_template() {
.fails()
.stderr_is("mktemp: with --suffix, template '' must end in X\n");
}

#[test]
fn test_mktemp_with_posixly_correct() {
let scene = TestScenario::new(util_name!());

scene
.ucmd()
.env("POSIXLY_CORRECT", "1")
.args(&["aXXXX", "--suffix=b"])
.fails()
.stderr_is(&format!(
"mktemp: too many templates\nTry '{} {} --help' for more information.\n",
scene.bin_path.to_string_lossy(),
scene.util_name
));

scene
.ucmd()
.env("POSIXLY_CORRECT", "1")
.args(&["--suffix=b", "aXXXX"])
.succeeds();
}