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
Provenance
- Commit
342d12d- Tree
7b0cff4- Author
sharkdp <davidpeter@web.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
c79d163
3 files changed+20 added−161 removed
modified:src/internal/mod.rs+1 added-116 removed
1 use std::borrow::Cow;2-use std::ffi::{OsStr, OsString};2+use std::ffi::OsStr;3 4 #[cfg(any(unix, target_os = "redox"))]5 pub fn osstr_to_bytes(input: &OsStr) -> Cow<[u8]> {6 use std::os::unix::ffi::OsStrExt;8 unmodified lines15 Cow::Owned(string) => Cow::Owned(string.into_bytes()),16 Cow::Borrowed(string) => Cow::Borrowed(string.as_bytes()),17 }18 }19-20-/// Traverse args_os, looking for -exec and replacing it with --exec.21-///22-/// # Returns23-///24-/// * The args, with substitution if required25-pub fn transform_args_with_exec<I>(original: I) -> Vec<OsString>26-where27- 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(";");34-35- 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- }43-44- 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-}57-58-#[cfg(test)]59-mod tests {60- use super::*;61-62- fn oss_vec(strs: &[&str]) -> Vec<OsString> {63- strs.into_iter().map(OsString::from).collect()64- }65-66- /// 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 lines26 27 use crate::exec::CommandTemplate;28 use crate::filetypes::FileTypes;29 use crate::filter::{SizeFilter, TimeFilter};30-use crate::internal::transform_args_with_exec;30 use crate::options::Options;31 use crate::regex_helper::pattern_has_uppercase_char;32 33 // We use jemalloc for performance reasons, see https://github.com/sharkdp/fd/pull/4811 unmodified line35 #[global_allocator]36 static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;37 38 fn 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());40 41 // 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 linesmodified:tests/tests.rs+18 added-42 removed
1062 unmodified lines1063 1064 /// Shell script execution (--exec)1065 #[test]1066 fn test_exec() {1067- assert_exec_output("--exec");1068-}1069-1070-/// Shell script execution using -exec1071-#[test]1072-fn test_exec_substitution() {1073- assert_exec_output("-exec");1074-}1075-1076-// Shell script execution using -x1077-#[test]1078-fn test_exec_short_arg() {1079- assert_exec_output("-x");1080-}1081-1082-#[cfg(test)]1083-fn 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.Foo24 unmodified lines1080 ),1081 );1082 1083 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.foo1 unmodified line1090 one/two/three/directory_foo",1091 );1092 1093 te.assert_output(1111- &["foo", exec_style, "echo", "{.}"],1094+ &["foo", "--exec", "echo", "{.}"],1095 "a1096 one/b1097 one/two/C1098 one/two/c1 unmodified line1100 one/two/three/directory_foo",1101 );1102 1103 te.assert_output(1121- &["foo", exec_style, "echo", "{/}"],1104+ &["foo", "--exec", "echo", "{/}"],1105 "a.foo1106 b.foo1107 C.Foo21108 c.foo1 unmodified line1110 directory_foo",1111 );1112 1113 te.assert_output(1131- &["foo", exec_style, "echo", "{/.}"],1114+ &["foo", "--exec", "echo", "{/.}"],1115 "a1116 b1117 C1118 c1 unmodified line1120 directory_foo",1121 );1122 1123 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 );1132 1150- 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 }1136 1137 #[test]1138 fn test_exec_batch() {1156- assert_exec_batch_output("--exec-batch");1157-}1158-1159-#[test]1160-fn test_exec_batch_short_arg() {1161- assert_exec_batch_output("-X");1162-}1163-1164-#[cfg(test)]1165-fn 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);1141 1142 // 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 );1151 1152 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 );1156 1157 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 );1161 1189- te.assert_output(&["no_match", exec_style, "echo", "Matched: ", "{/}"], "");1162+ te.assert_output(1163+ &["no_match", "--exec-batch", "echo", "Matched: ", "{/}"],1164+ "",1165+ );1166 1167 te.assert_error(1192- &["foo", exec_style, "echo", "{}", "{}"],1168+ &["foo", "--exec-batch", "echo", "{}", "{}"],1169 "[fd error]: Only one placeholder allowed for batch commands",1170 );1171 1172 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 );1176 1177 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 );1181 1182 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