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

Merge pull request #1363 from rethab/reject-null-with-program

env: reject program with --null, error stderr
This commit is contained in:
Alex Lyon 2019-04-28 04:04:23 -07:00 committed by GitHub
commit bcc821caa8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 8 deletions

24
src/env/env.rs vendored
View file

@ -47,7 +47,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
.optflag( .optflag(
"0", "0",
"null", "null",
"end each output line with a 0 byte rather than newline", "end each output line with a 0 byte rather than newline (only valid when printing the environment)",
) )
.optopt("u", "unset", "remove variable from the environment", "NAME"); .optopt("u", "unset", "remove variable from the environment", "NAME");
@ -103,14 +103,14 @@ pub fn uumain(args: Vec<String>) -> i32 {
let var = iter.next(); let var = iter.next();
match var { match var {
None => println!("{}: this option requires an argument: {}", NAME, opt), None => eprintln!("{}: this option requires an argument: {}", NAME, opt),
Some(s) => opts.unsets.push(s.to_owned()), Some(s) => opts.unsets.push(s.to_owned()),
} }
} }
_ => { _ => {
println!("{}: invalid option \"{}\"", NAME, *opt); eprintln!("{}: invalid option \"{}\"", NAME, *opt);
println!("Type \"{} --help\" for detailed informations", NAME); eprintln!("Type \"{} --help\" for detailed information", NAME);
return 1; return 1;
} }
} }
@ -134,13 +134,13 @@ pub fn uumain(args: Vec<String>) -> i32 {
let var = iter.next(); let var = iter.next();
match var { match var {
None => println!("{}: this option requires an argument: {}", NAME, opt), None => eprintln!("{}: this option requires an argument: {}", NAME, opt),
Some(s) => opts.unsets.push(s.to_owned()), Some(s) => opts.unsets.push(s.to_owned()),
} }
} }
_ => { _ => {
println!("{}: illegal option -- {}", NAME, c); eprintln!("{}: illegal option -- {}", NAME, c);
println!("Type \"{} --help\" for detailed informations", NAME); eprintln!("Type \"{} --help\" for detailed information", NAME);
return 1; return 1;
} }
} }
@ -159,6 +159,11 @@ pub fn uumain(args: Vec<String>) -> i32 {
} }
// no, its a program-like opt // no, its a program-like opt
_ => { _ => {
if opts.null {
eprintln!("{}: cannot specify --null (-0) with command", NAME);
eprintln!("Type \"{} --help\" for detailed information", NAME);
return 1;
}
opts.program.push(opt.clone()); opts.program.push(opt.clone());
break; break;
} }
@ -170,6 +175,11 @@ pub fn uumain(args: Vec<String>) -> i32 {
// read program arguments // read program arguments
for opt in iter { for opt in iter {
if opts.null {
eprintln!("{}: cannot specify --null (-0) with command", NAME);
eprintln!("Type \"{} --help\" for detailed information", NAME);
return 1;
}
opts.program.push(opt.clone()) opts.program.push(opt.clone())
} }

View file

@ -606,7 +606,7 @@ impl UCommand {
} }
/// Spawns the command, feeds the stdin if any, waits for the result, /// Spawns the command, feeds the stdin if any, waits for the result,
/// asserts success, and returns a command result. /// asserts failure, and returns a command result.
pub fn fails(&mut self) -> CmdResult { pub fn fails(&mut self) -> CmdResult {
let cmd_result = self.run(); let cmd_result = self.run();
cmd_result.failure(); cmd_result.failure();

View file

@ -81,3 +81,9 @@ fn test_unset_variable() {
assert_eq!(out.lines().any(|line| line.starts_with("HOME=")), false); assert_eq!(out.lines().any(|line| line.starts_with("HOME=")), false);
} }
#[test]
fn test_fail_null_with_program() {
let out = new_ucmd!().arg("--null").arg("cd").fails().stderr;
assert!(out.contains("cannot specify --null (-0) with command"));
}