Skip to content

Commit ec5e847

Browse files
committed
feat: optimize dd parsing, bugfix
* Simplify param parsing in `dd` by using iterator instead of a slice * Fix two unit tests that appear to have a minor bug (?) - adding `iflag=<...>` instead of `oflag=<...>` during testing
1 parent da61ebf commit ec5e847

File tree

6 files changed

+45
-74
lines changed

6 files changed

+45
-74
lines changed

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ unicode-segmentation = "1.11.0"
348348
unicode-width = "0.2.0"
349349
utf-8 = "0.7.6"
350350
utmp-classic = "0.1.6"
351-
uutils_term_grid = "0.7"
351+
uutils_term_grid = "0.6"
352352
walkdir = "2.5"
353353
winapi-util = "0.1.8"
354354
windows-sys = { version = "0.59.0", default-features = false }

src/uu/dd/src/dd.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,11 +1397,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
13971397
let matches = uu_app().try_get_matches_from(args)?;
13981398

13991399
let settings: Settings = Parser::new().parse(
1400-
&matches
1400+
matches
14011401
.get_many::<String>(options::OPERANDS)
1402-
.unwrap_or_default()
1403-
.map(|s| s.as_ref())
1404-
.collect::<Vec<_>>()[..],
1402+
.unwrap_or_default(),
14051403
)?;
14061404

14071405
let i = match settings.infile {

src/uu/dd/src/parseargs.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,19 @@ impl Parser {
126126
Self::default()
127127
}
128128

129-
pub(crate) fn parse(self, operands: &[&str]) -> Result<Settings, ParseError> {
129+
pub(crate) fn parse(
130+
self,
131+
operands: impl IntoIterator<Item: AsRef<str>>,
132+
) -> Result<Settings, ParseError> {
130133
self.read(operands)?.validate()
131134
}
132135

133-
pub(crate) fn read(mut self, operands: &[&str]) -> Result<Self, ParseError> {
136+
pub(crate) fn read(
137+
mut self,
138+
operands: impl IntoIterator<Item: AsRef<str>>,
139+
) -> Result<Self, ParseError> {
134140
for operand in operands {
135-
self.parse_operand(operand)?;
141+
self.parse_operand(operand.as_ref())?;
136142
}
137143

138144
Ok(self)

src/uu/dd/src/parseargs/unit_tests.rs

Lines changed: 30 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,14 @@ fn unimplemented_flags_should_error_non_linux() {
2929
"noctty",
3030
"nofollow",
3131
] {
32-
let args = vec![format!("iflag={flag}")];
33-
34-
if Parser::new()
35-
.parse(&args.iter().map(AsRef::as_ref).collect::<Vec<_>>()[..])
36-
.is_ok()
37-
{
38-
succeeded.push(format!("iflag={flag}"));
32+
let arg = format!("iflag={flag}");
33+
if Parser::new().parse([&arg]).is_ok() {
34+
succeeded.push(arg);
3935
}
4036

41-
let args = vec![format!("oflag={flag}")];
42-
43-
if Parser::new()
44-
.parse(&args.iter().map(AsRef::as_ref).collect::<Vec<_>>()[..])
45-
.is_ok()
46-
{
47-
succeeded.push(format!("iflag={flag}"));
37+
let arg = format!("oflag={flag}");
38+
if Parser::new().parse([&arg]).is_ok() {
39+
succeeded.push(arg);
4840
}
4941
}
5042

@@ -61,22 +53,14 @@ fn unimplemented_flags_should_error() {
6153

6254
// The following flags are not implemented
6355
for flag in ["cio", "nolinks", "text", "binary"] {
64-
let args = vec![format!("iflag={flag}")];
65-
66-
if Parser::new()
67-
.parse(&args.iter().map(AsRef::as_ref).collect::<Vec<_>>()[..])
68-
.is_ok()
69-
{
70-
succeeded.push(format!("iflag={flag}"));
56+
let arg = format!("iflag={flag}");
57+
if Parser::new().parse([&arg]).is_ok() {
58+
succeeded.push(arg);
7159
}
7260

73-
let args = vec![format!("oflag={flag}")];
74-
75-
if Parser::new()
76-
.parse(&args.iter().map(AsRef::as_ref).collect::<Vec<_>>()[..])
77-
.is_ok()
78-
{
79-
succeeded.push(format!("iflag={flag}"));
61+
let arg = format!("oflag={flag}");
62+
if Parser::new().parse([&arg]).is_ok() {
63+
succeeded.push(arg);
8064
}
8165
}
8266

@@ -88,14 +72,14 @@ fn unimplemented_flags_should_error() {
8872

8973
#[test]
9074
fn test_status_level_absent() {
91-
let args = &["if=foo.file", "of=bar.file"];
75+
let args = ["if=foo.file", "of=bar.file"];
9276

9377
assert_eq!(Parser::new().parse(args).unwrap().status, None);
9478
}
9579

9680
#[test]
9781
fn test_status_level_none() {
98-
let args = &["status=none", "if=foo.file", "of=bar.file"];
82+
let args = ["status=none", "if=foo.file", "of=bar.file"];
9983

10084
assert_eq!(
10185
Parser::new().parse(args).unwrap().status,
@@ -106,7 +90,7 @@ fn test_status_level_none() {
10690
#[test]
10791
#[allow(clippy::cognitive_complexity)]
10892
fn test_all_top_level_args_no_leading_dashes() {
109-
let args = &[
93+
let args = [
11094
"if=foo.file",
11195
"of=bar.file",
11296
"ibs=10",
@@ -181,7 +165,7 @@ fn test_all_top_level_args_no_leading_dashes() {
181165

182166
#[test]
183167
fn test_status_level_progress() {
184-
let args = &["if=foo.file", "of=bar.file", "status=progress"];
168+
let args = ["if=foo.file", "of=bar.file", "status=progress"];
185169

186170
let settings = Parser::new().parse(args).unwrap();
187171

@@ -190,7 +174,7 @@ fn test_status_level_progress() {
190174

191175
#[test]
192176
fn test_status_level_noxfer() {
193-
let args = &["if=foo.file", "status=noxfer", "of=bar.file"];
177+
let args = ["if=foo.file", "status=noxfer", "of=bar.file"];
194178

195179
let settings = Parser::new().parse(args).unwrap();
196180

@@ -199,7 +183,7 @@ fn test_status_level_noxfer() {
199183

200184
#[test]
201185
fn test_multiple_flags_options() {
202-
let args = &[
186+
let args = [
203187
"iflag=fullblock,count_bytes",
204188
"iflag=skip_bytes",
205189
"oflag=append",
@@ -246,7 +230,7 @@ fn test_multiple_flags_options() {
246230

247231
#[test]
248232
fn test_override_multiple_options() {
249-
let args = &[
233+
let args = [
250234
"if=foo.file",
251235
"if=correct.file",
252236
"of=bar.file",
@@ -288,31 +272,31 @@ fn test_override_multiple_options() {
288272

289273
#[test]
290274
fn icf_ctable_error() {
291-
let args = &["conv=ascii,ebcdic,ibm"];
275+
let args = ["conv=ascii,ebcdic,ibm"];
292276
assert!(Parser::new().parse(args).is_err());
293277
}
294278

295279
#[test]
296280
fn icf_case_error() {
297-
let args = &["conv=ucase,lcase"];
281+
let args = ["conv=ucase,lcase"];
298282
assert!(Parser::new().parse(args).is_err());
299283
}
300284

301285
#[test]
302286
fn icf_block_error() {
303-
let args = &["conv=block,unblock"];
287+
let args = ["conv=block,unblock"];
304288
assert!(Parser::new().parse(args).is_err());
305289
}
306290

307291
#[test]
308292
fn icf_creat_error() {
309-
let args = &["conv=excl,nocreat"];
293+
let args = ["conv=excl,nocreat"];
310294
assert!(Parser::new().parse(args).is_err());
311295
}
312296

313297
#[test]
314298
fn parse_icf_token_ibm() {
315-
let args = &["conv=ibm"];
299+
let args = ["conv=ibm"];
316300
let settings = Parser::new().parse(args).unwrap();
317301

318302
assert_eq!(
@@ -326,7 +310,7 @@ fn parse_icf_token_ibm() {
326310

327311
#[test]
328312
fn parse_icf_tokens_elu() {
329-
let args = &["conv=ebcdic,lcase"];
313+
let args = ["conv=ebcdic,lcase"];
330314
let settings = Parser::new().parse(args).unwrap();
331315

332316
assert_eq!(
@@ -340,7 +324,7 @@ fn parse_icf_tokens_elu() {
340324

341325
#[test]
342326
fn parse_icf_tokens_remaining() {
343-
let args = &[
327+
let args = [
344328
"conv=ascii,ucase,block,sparse,swab,sync,noerror,excl,nocreat,notrunc,noerror,fdatasync,fsync",
345329
];
346330
assert_eq!(
@@ -369,7 +353,7 @@ fn parse_icf_tokens_remaining() {
369353

370354
#[test]
371355
fn parse_iflag_tokens() {
372-
let args = &["iflag=fullblock,count_bytes,skip_bytes"];
356+
let args = ["iflag=fullblock,count_bytes,skip_bytes"];
373357
assert_eq!(
374358
Parser::new().read(args),
375359
Ok(Parser {
@@ -386,7 +370,7 @@ fn parse_iflag_tokens() {
386370

387371
#[test]
388372
fn parse_oflag_tokens() {
389-
let args = &["oflag=append,seek_bytes"];
373+
let args = ["oflag=append,seek_bytes"];
390374
assert_eq!(
391375
Parser::new().read(args),
392376
Ok(Parser {
@@ -403,7 +387,7 @@ fn parse_oflag_tokens() {
403387
#[cfg(any(target_os = "linux", target_os = "android"))]
404388
#[test]
405389
fn parse_iflag_tokens_linux() {
406-
let args = &["iflag=direct,directory,dsync,sync,nonblock,noatime,noctty,nofollow"];
390+
let args = ["iflag=direct,directory,dsync,sync,nonblock,noatime,noctty,nofollow"];
407391
assert_eq!(
408392
Parser::new().read(args),
409393
Ok(Parser {
@@ -426,7 +410,7 @@ fn parse_iflag_tokens_linux() {
426410
#[cfg(any(target_os = "linux", target_os = "android"))]
427411
#[test]
428412
fn parse_oflag_tokens_linux() {
429-
let args = &["oflag=direct,directory,dsync,sync,nonblock,noatime,noctty,nofollow"];
413+
let args = ["oflag=direct,directory,dsync,sync,nonblock,noatime,noctty,nofollow"];
430414
assert_eq!(
431415
Parser::new().read(args),
432416
Ok(Parser {

tests/by-util/test_cp.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6167,20 +6167,3 @@ fn test_cp_update_older_interactive_prompt_no() {
61676167
.fails()
61686168
.stdout_is("cp: overwrite 'old'? ");
61696169
}
6170-
6171-
#[test]
6172-
fn test_cp_update_none_interactive_prompt_no() {
6173-
let (at, mut ucmd) = at_and_ucmd!();
6174-
let old_file = "old";
6175-
let new_file = "new";
6176-
6177-
at.write(old_file, "old content");
6178-
at.write(new_file, "new content");
6179-
6180-
ucmd.args(&["-i", "--update=none", new_file, old_file])
6181-
.succeeds()
6182-
.no_output();
6183-
6184-
assert_eq!(at.read(old_file), "old content");
6185-
assert_eq!(at.read(new_file), "new content");
6186-
}

0 commit comments

Comments
 (0)