Skip to content
Myr

Remove possibility to use '--exec <cmd>' with a single dash

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by sharkdp · committed by David Peter · parent c79d163 · Browse files · 3 files+20 added−161 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+20 added−161 removed

  • tests:+18 added−42 removed
  • other:+2 added−119 removed

3 files+20 added−161 removed

modified:src/internal/mod.rs+1 added-116 removed
1use std::borrow::Cow;2use std::ffi::{OsStr, OsString};2use std::ffi::OsStr;34#[cfg(any(unix, target_os = "redox"))]5pub fn osstr_to_bytes(input: &OsStr) -> Cow<[u8]> {6 use std::os::unix::ffi::OsStrExt;
8 unmodified lines
15 Cow::Owned(string) => Cow::Owned(string.into_bytes()),16 Cow::Borrowed(string) => Cow::Borrowed(string.as_bytes()),17 }18}1920/// Traverse args_os, looking for -exec and replacing it with --exec.21///22/// # Returns23///24/// * The args, with substitution if required25pub fn transform_args_with_exec<I>(original: I) -> Vec<OsString>26where27 I: Iterator<Item = OsString>,28{29 let mut in_exec_opt = false;30 let target = OsString::from("-exec");31 let long_start = OsString::from("--exec");32 let short_start = OsString::from("-x");33 let exec_end = OsString::from(";");3435 original.fold(vec![], |mut args, curr| {36 if in_exec_opt {37 if curr == exec_end {38 in_exec_opt = false;39 }40 args.push(curr);41 return args;42 }4344 if curr == target || curr == long_start || curr == short_start {45 args.push(if curr == target {46 OsString::from("--exec")47 } else {48 curr49 });50 in_exec_opt = true;51 } else {52 args.push(curr);53 }54 args55 })56}5758#[cfg(test)]59mod tests {60 use super::*;6162 fn oss_vec(strs: &[&str]) -> Vec<OsString> {63 strs.into_iter().map(OsString::from).collect()64 }6566 /// Ensure that -exec gets transformed into --exec67 #[test]68 fn normal_exec_substitution() {69 let original = oss_vec(&["fd", "foo", "-exec", "cmd"]);70 let expected = oss_vec(&["fd", "foo", "--exec", "cmd"]);71 let actual = transform_args_with_exec(original.into_iter());72 assert_eq!(expected, actual);73 }74 /// Ensure that --exec is not touched75 #[test]76 fn passthru_of_original_exec() {77 let original = oss_vec(&["fd", "foo", "--exec", "cmd"]);78 let expected = oss_vec(&["fd", "foo", "--exec", "cmd"]);79 let actual = transform_args_with_exec(original.into_iter());80 assert_eq!(expected, actual);81 }82 #[test]83 fn temp_check_that_exec_context_observed() {84 let original = oss_vec(&[85 "fd",86 "foo",87 "-exec",88 "cmd",89 "-exec",90 "ls",91 ";",92 "-exec",93 "rm",94 ";",95 "--exec",96 "find",97 "-exec",98 "rm",99 ";",100 "-x",101 "foo",102 "-exec",103 "something",104 ";",105 "-exec",106 ]);107 let expected = oss_vec(&[108 "fd",109 "foo",110 "--exec",111 "cmd",112 "-exec",113 "ls",114 ";",115 "--exec",116 "rm",117 ";",118 "--exec",119 "find",120 "-exec",121 "rm",122 ";",123 "-x",124 "foo",125 "-exec",126 "something",127 ";",128 "--exec",129 ]);130 let actual = transform_args_with_exec(original.into_iter());131 assert_eq!(expected, actual);132 }133}
modified:src/main.rs+1 added-3 removed
25 unmodified lines
2627use crate::exec::CommandTemplate;28use crate::filetypes::FileTypes;29use crate::filter::{SizeFilter, TimeFilter};30use crate::internal::transform_args_with_exec;30use crate::options::Options;31use crate::regex_helper::pattern_has_uppercase_char;3233// We use jemalloc for performance reasons, see https://github.com/sharkdp/fd/pull/481
1 unmodified line
35#[global_allocator]36static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;3738fn main() {40 let checked_args = transform_args_with_exec(env::args_os());41 let matches = app::build_app().get_matches_from(checked_args);39 let matches = app::build_app().get_matches_from(env::args_os());4041 // Set the current working directory of the process42 if let Some(base_directory) = matches.value_of("base-directory") {43 let basedir = Path::new(base_directory);
276 unmodified lines
modified:tests/tests.rs+18 added-42 removed
1062 unmodified lines
10631064/// Shell script execution (--exec)1065#[test]1066fn test_exec() {1067 assert_exec_output("--exec");1068}10691070/// Shell script execution using -exec1071#[test]1072fn test_exec_substitution() {1073 assert_exec_output("-exec");1074}10751076// Shell script execution using -x1077#[test]1078fn test_exec_short_arg() {1079 assert_exec_output("-x");1080}10811082#[cfg(test)]1083fn assert_exec_output(exec_style: &str) {1067 let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);1068 // TODO Windows tests: D:file.txt \file.txt \\server\share\file.txt ...1069 if !cfg!(windows) {1070 te.assert_output(1088 &["--absolute-path", "foo", exec_style, "echo"],1071 &["--absolute-path", "foo", "--exec", "echo"],1072 &format!(1073 "{abs_path}/a.foo1074 {abs_path}/one/b.foo1075 {abs_path}/one/two/C.Foo2
4 unmodified lines
1080 ),1081 );10821083 te.assert_output(1101 &["foo", exec_style, "echo", "{}"],1084 &["foo", "--exec", "echo", "{}"],1085 "a.foo1086 one/b.foo1087 one/two/C.Foo21088 one/two/c.foo
1 unmodified line
1090 one/two/three/directory_foo",1091 );10921093 te.assert_output(1111 &["foo", exec_style, "echo", "{.}"],1094 &["foo", "--exec", "echo", "{.}"],1095 "a1096 one/b1097 one/two/C1098 one/two/c
1 unmodified line
1100 one/two/three/directory_foo",1101 );11021103 te.assert_output(1121 &["foo", exec_style, "echo", "{/}"],1104 &["foo", "--exec", "echo", "{/}"],1105 "a.foo1106 b.foo1107 C.Foo21108 c.foo
1 unmodified line
1110 directory_foo",1111 );11121113 te.assert_output(1131 &["foo", exec_style, "echo", "{/.}"],1114 &["foo", "--exec", "echo", "{/.}"],1115 "a1116 b1117 C1118 c
1 unmodified line
1120 directory_foo",1121 );11221123 te.assert_output(1141 &["foo", exec_style, "echo", "{//}"],1124 &["foo", "--exec", "echo", "{//}"],1125 ".1126 one1127 one/two1128 one/two1129 one/two/three1130 one/two/three",1131 );11321150 te.assert_output(&["e1", exec_style, "printf", "%s.%s\n"], "e1 e2.");1133 te.assert_output(&["e1", "--exec", "printf", "%s.%s\n"], "e1 e2.");1134 }1135}11361137#[test]1138fn test_exec_batch() {1156 assert_exec_batch_output("--exec-batch");1157}11581159#[test]1160fn test_exec_batch_short_arg() {1161 assert_exec_batch_output("-X");1162}11631164#[cfg(test)]1165fn assert_exec_batch_output(exec_style: &str) {1139 let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);1140 let te = te.normalize_line(true);11411142 // TODO Test for windows1143 if !cfg!(windows) {1144 te.assert_output(1172 &["--absolute-path", "foo", exec_style, "echo"],1145 &["--absolute-path", "foo", "--exec-batch", "echo"],1146 &format!(1147 "{abs_path}/a.foo {abs_path}/one/b.foo {abs_path}/one/two/C.Foo2 {abs_path}/one/two/c.foo {abs_path}/one/two/three/d.foo {abs_path}/one/two/three/directory_foo",1148 abs_path = &abs_path1149 ),1150 );11511152 te.assert_output(1180 &["foo", exec_style, "echo", "{}"],1153 &["foo", "--exec-batch", "echo", "{}"],1154 "a.foo one/b.foo one/two/C.Foo2 one/two/c.foo one/two/three/d.foo one/two/three/directory_foo",1155 );11561157 te.assert_output(1185 &["foo", exec_style, "echo", "{/}"],1158 &["foo", "--exec-batch", "echo", "{/}"],1159 "a.foo b.foo C.Foo2 c.foo d.foo directory_foo",1160 );11611189 te.assert_output(&["no_match", exec_style, "echo", "Matched: ", "{/}"], "");1162 te.assert_output(1163 &["no_match", "--exec-batch", "echo", "Matched: ", "{/}"],1164 "",1165 );11661167 te.assert_error(1192 &["foo", exec_style, "echo", "{}", "{}"],1168 &["foo", "--exec-batch", "echo", "{}", "{}"],1169 "[fd error]: Only one placeholder allowed for batch commands",1170 );11711172 te.assert_error(1197 &["foo", exec_style, "echo", "{/}", ";", "-x", "echo"],1173 &["foo", "--exec-batch", "echo", "{/}", ";", "-x", "echo"],1174 "error: The argument '--exec <cmd>' cannot be used with '--exec-batch <cmd>'",1175 );11761177 te.assert_error(1202 &["foo", exec_style],1178 &["foo", "--exec-batch"],1179 "error: The argument '--exec-batch <cmd>' requires a value but none was supplied",1180 );11811182 te.assert_error(1207 &["foo", exec_style, "echo {}"],1183 &["foo", "--exec-batch", "echo {}"],1184 "[fd error]: First argument of exec-batch is expected to be a fixed executable",1185 );1186 }1187}
279 unmodified lines