Skip to content
Myr

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

4 files+152 added−18 removed

modified:src/app.rs-1 removed
88 unmodified lines
89 .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 lines
modified:src/internal.rs+117 added
5 unmodified lines
6// notice may not be copied, modified, or distributed except7// according to those terms.89use std::collections::HashSet;10use std::ffi::OsString;11use std::process;12use std::time;13use std::io::Write;14
94 unmodified lines
109pub const EXITCODE_ERROR: i32 = 1;110111/// Exit code representing that the process was killed by SIGINT112pub const EXITCODE_SIGINT: i32 = 130;113114/// Traverse args_os, looking for -exec and replacing it with --exec.115///116/// # Returns117///118/// * The args, with substitution if required119pub fn transform_args_with_exec<I>(original: I) -> Vec<OsString>120where121 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(";");128129 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 }137138 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}151152#[cfg(test)]153fn oss(v: &str) -> OsString {154 OsString::from(v)155}156157/// Ensure that -exec gets transformed into --exec158#[test]159fn 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")];162163 let actual = transform_args_with_exec(original.into_iter());164 assert_eq!(expected, actual);165}166167/// Ensure that --exec is not touched168#[test]169fn 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")];172173 let actual = transform_args_with_exec(original.into_iter());174 assert_eq!(expected, actual);175}176177#[test]178fn 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 ];225226 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 lines
39use atty::Stream;40use regex::RegexBuilder;4142use exec::CommandTemplate;43use internal::{error, pattern_has_uppercase_char, FdOptions};43use internal::{error, pattern_has_uppercase_char, transform_args_with_exec, FdOptions};44use lscolors::LsColors;45use walk::FileType;4647fn 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);5051 // Get the search pattern52 let pattern = matches.value_of("pattern").unwrap_or("");53
120 unmodified lines
modified:tests/tests.rs+32 added-15 removed
41 unmodified lines
4243 path44}4546#[cfg(test)]47fn 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}5253/// Simple tests54#[test]55fn test_simple() {56 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);
355 unmodified lines
412413/// Absolute paths (--absolute-path)414#[test]415fn 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);417411 let abs_path = get_absolute_root_path(&te);412418 te.assert_output(419 &["--absolute-path"],420 &format!(421 "{abs_path}/a.foo
112 unmodified lines
534535/// Symlinks misc536#[test]537fn 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);539535 let abs_path = get_absolute_root_path(&te);536540 // 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 lines
682683/// Shell script execution (--exec)684#[test]685fn test_exec() {683 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);686 assert_exec_output("--exec");687}688685 let abs_path = get_absolute_root_path(&te);689/// Shell script execution using -exec690#[test]691fn test_exec_substitution() {692 assert_exec_output("-exec");693}694695// Shell script execution using -x696#[test]697fn test_exec_short_arg() {698 assert_exec_output("-x");699}700701#[cfg(test)]702fn 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.Foo2
4 unmodified lines
716 ),717 );718719 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.foo
1 unmodified line
726 one/two/three/directory_foo",727 );728729 te.assert_output(713 &["foo", "--exec", "echo", "{.}"],730 &["foo", exec_style, "echo", "{.}"],731 "a732 one/b733 one/two/C734 one/two/c
1 unmodified line
736 one/two/three/directory_foo",737 );738739 te.assert_output(723 &["foo", "--exec", "echo", "{/}"],740 &["foo", exec_style, "echo", "{/}"],741 "a.foo742 b.foo743 C.Foo2744 c.foo
1 unmodified line
746 directory_foo",747 );748749 te.assert_output(733 &["foo", "--exec", "echo", "{/.}"],750 &["foo", exec_style, "echo", "{/.}"],751 "a752 b753 C754 c
1 unmodified line
756 directory_foo",757 );758759 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 );768752 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}