Allow -exec to work as --exec (#226)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Replace usage of `-exec` by `--exec`, except when it is used as a value that is given to `--exec/-exec/-x`. * Limit usage of --exec option to single instance.
authored by Steve Pentland · committed by David Peter · parent 6d83eea · Browse files · 4 files·+152 added−18 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+152 added−18 removed
- tests:+32 added−15 removed
- other:+120 added−3 removed
Provenance
- Commit
b4d3927- Tree
5bc7fe8- Author
Steve Pentland <stevepentland@users.noreply.github.com>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
6d83eea
4 files changed+152 added−18 removed
modified:src/app.rs-1 removed
88 unmodified lines89 .arg(90 arg("exec")91 .long("exec")92 .short("x")93- .multiple(true)93 .min_values(1)94 .allow_hyphen_values(true)95 .value_terminator(";")96 .value_name("cmd"),130 unmodified linesmodified:src/internal.rs+117 added
5 unmodified lines6 // notice may not be copied, modified, or distributed except7 // according to those terms.8 9 use std::collections::HashSet;10+use std::ffi::OsString;11 use std::process;12 use std::time;13 use std::io::Write;14 94 unmodified lines109 pub const EXITCODE_ERROR: i32 = 1;110 111 /// Exit code representing that the process was killed by SIGINT112 pub const EXITCODE_SIGINT: i32 = 130;113+114+/// Traverse args_os, looking for -exec and replacing it with --exec.115+///116+/// # Returns117+///118+/// * The args, with substitution if required119+pub fn transform_args_with_exec<I>(original: I) -> Vec<OsString>120+where121+ I: Iterator<Item = OsString>,122+{123+ let mut in_exec_opt = false;124+ let target = OsString::from("-exec");125+ let long_start = OsString::from("--exec");126+ let short_start = OsString::from("-x");127+ let exec_end = OsString::from(";");128+129+ original.fold(vec![], |mut args, curr| {130+ if in_exec_opt {131+ if curr == exec_end {132+ in_exec_opt = false;133+ }134+ args.push(curr);135+ return args;136+ }137+138+ if curr == target || curr == long_start || curr == short_start {139+ args.push(if curr == target {140+ OsString::from("--exec")141+ } else {142+ curr143+ });144+ in_exec_opt = true;145+ } else {146+ args.push(curr);147+ }148+ args149+ })150+}151+152+#[cfg(test)]153+fn oss(v: &str) -> OsString {154+ OsString::from(v)155+}156+157+/// Ensure that -exec gets transformed into --exec158+#[test]159+fn normal_exec_substitution() {160+ let original = vec![oss("fd"), oss("foo"), oss("-exec"), oss("cmd")];161+ let expected = vec![oss("fd"), oss("foo"), oss("--exec"), oss("cmd")];162+163+ let actual = transform_args_with_exec(original.into_iter());164+ assert_eq!(expected, actual);165+}166+167+/// Ensure that --exec is not touched168+#[test]169+fn passthru_of_original_exec() {170+ let original = vec![oss("fd"), oss("foo"), oss("--exec"), oss("cmd")];171+ let expected = vec![oss("fd"), oss("foo"), oss("--exec"), oss("cmd")];172+173+ let actual = transform_args_with_exec(original.into_iter());174+ assert_eq!(expected, actual);175+}176+177+#[test]178+fn temp_check_that_exec_context_observed() {179+ let original = vec![180+ oss("fd"),181+ oss("foo"),182+ oss("-exec"),183+ oss("cmd"),184+ oss("-exec"),185+ oss("ls"),186+ oss(";"),187+ oss("-exec"),188+ oss("rm"),189+ oss(";"),190+ oss("--exec"),191+ oss("find"),192+ oss("-exec"),193+ oss("rm"),194+ oss(";"),195+ oss("-x"),196+ oss("foo"),197+ oss("-exec"),198+ oss("something"),199+ oss(";"),200+ oss("-exec"),201+ ];202+ let expected = vec![203+ oss("fd"),204+ oss("foo"),205+ oss("--exec"),206+ oss("cmd"),207+ oss("-exec"),208+ oss("ls"),209+ oss(";"),210+ oss("--exec"),211+ oss("rm"),212+ oss(";"),213+ oss("--exec"),214+ oss("find"),215+ oss("-exec"),216+ oss("rm"),217+ oss(";"),218+ oss("-x"),219+ oss("foo"),220+ oss("-exec"),221+ oss("something"),222+ oss(";"),223+ oss("--exec"),224+ ];225+226+ let actual = transform_args_with_exec(original.into_iter());227+ assert_eq!(expected, actual);228+}modified:src/main.rs+3 added-2 removed
38 unmodified lines39 use atty::Stream;40 use regex::RegexBuilder;41 42 use exec::CommandTemplate;43-use internal::{error, pattern_has_uppercase_char, FdOptions};43+use internal::{error, pattern_has_uppercase_char, transform_args_with_exec, FdOptions};44 use lscolors::LsColors;45 use walk::FileType;46 47 fn main() {48+ let checked_args = transform_args_with_exec(env::args_os());48- let matches = app::build_app().get_matches();49+ let matches = app::build_app().get_matches_from(checked_args);50 51 // Get the search pattern52 let pattern = matches.value_of("pattern").unwrap_or("");53 120 unmodified linesmodified:tests/tests.rs+32 added-15 removed
41 unmodified lines42 43 path44 }45 46+#[cfg(test)]47+fn get_test_env_with_abs_path(dirs: &[&'static str], files: &[&'static str]) -> (TestEnv, String) {48+ let env = TestEnv::new(dirs, files);49+ let root_path = get_absolute_root_path(&env);50+ (env, root_path)51+}52+53 /// Simple tests54 #[test]55 fn test_simple() {56 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);355 unmodified lines412 413 /// Absolute paths (--absolute-path)414 #[test]415 fn test_absolute_path() {409- let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);416+ let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);417 411- let abs_path = get_absolute_root_path(&te);412-418 te.assert_output(419 &["--absolute-path"],420 &format!(421 "{abs_path}/a.foo112 unmodified lines534 535 /// Symlinks misc536 #[test]537 fn test_symlink() {533- let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);538+ let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);539 535- let abs_path = get_absolute_root_path(&te);536-540 // From: http://pubs.opengroup.org/onlinepubs/9699919799/functions/getcwd.html541 // The getcwd() function shall place an absolute pathname of the current working directory in542 // the array pointed to by buf, and return buf. The pathname shall contain no components that543 // are dot or dot-dot, or are symbolic links.138 unmodified lines682 683 /// Shell script execution (--exec)684 #[test]685 fn test_exec() {683- let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);686+ assert_exec_output("--exec");687+}688 685- let abs_path = get_absolute_root_path(&te);689+/// Shell script execution using -exec690+#[test]691+fn test_exec_substitution() {692+ assert_exec_output("-exec");693+}694 695+// Shell script execution using -x696+#[test]697+fn test_exec_short_arg() {698+ assert_exec_output("-x");699+}700+701+#[cfg(test)]702+fn assert_exec_output(exec_style: &str) {703+ let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);704 // TODO Windows tests: D:file.txt \file.txt \\server\share\file.txt ...705 if !cfg!(windows) {706 te.assert_output(690- &["--absolute-path", "foo", "--exec", "echo"],707+ &["--absolute-path", "foo", exec_style, "echo"],708 &format!(709 "{abs_path}/a.foo710 {abs_path}/one/b.foo711 {abs_path}/one/two/C.Foo24 unmodified lines716 ),717 );718 719 te.assert_output(703- &["foo", "--exec", "echo", "{}"],720+ &["foo", exec_style, "echo", "{}"],721 "a.foo722 one/b.foo723 one/two/C.Foo2724 one/two/c.foo1 unmodified line726 one/two/three/directory_foo",727 );728 729 te.assert_output(713- &["foo", "--exec", "echo", "{.}"],730+ &["foo", exec_style, "echo", "{.}"],731 "a732 one/b733 one/two/C734 one/two/c1 unmodified line736 one/two/three/directory_foo",737 );738 739 te.assert_output(723- &["foo", "--exec", "echo", "{/}"],740+ &["foo", exec_style, "echo", "{/}"],741 "a.foo742 b.foo743 C.Foo2744 c.foo1 unmodified line746 directory_foo",747 );748 749 te.assert_output(733- &["foo", "--exec", "echo", "{/.}"],750+ &["foo", exec_style, "echo", "{/.}"],751 "a752 b753 C754 c1 unmodified line756 directory_foo",757 );758 759 te.assert_output(743- &["foo", "--exec", "echo", "{//}"],760+ &["foo", exec_style, "echo", "{//}"],761 ".762 one763 one/two764 one/two765 one/two/three766 one/two/three",767 );768 752- te.assert_output(&["e1", "--exec", "printf", "%s.%s\n"], "e1 e2.");769+ te.assert_output(&["e1", exec_style, "printf", "%s.%s\n"], "e1 e2.");770 }771 }