From b889e52029e978058b14bf44c98705eb863b4603 Mon Sep 17 00:00:00 2001 From: zhitkoff Date: Tue, 17 Oct 2023 19:12:43 -0400 Subject: [PATCH] split: `--number` stdin handling --- src/uu/split/src/split.rs | 12 ++++++------ tests/by-util/test_split.rs | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/uu/split/src/split.rs b/src/uu/split/src/split.rs index 53e47105da..dba425c670 100644 --- a/src/uu/split/src/split.rs +++ b/src/uu/split/src/split.rs @@ -1408,7 +1408,6 @@ where USimpleError::new(1, format!("{}: cannot determine file size", settings.input)) })?; - // TODO - cannot determine file size for stdin input let num_bytes = metadata.len(); let will_have_empty_files = settings.elide_empty_files && num_chunks > num_bytes; let (num_chunks, chunk_size) = if will_have_empty_files { @@ -1502,7 +1501,6 @@ where // NOTE: the `elide_empty_files` parameter is ignored here // as we do not generate any files // and instead writing to stdout - // TODO - cannot get metadata or determine file size for stdin input let metadata = metadata(&settings.input).map_err(|_| { USimpleError::new(1, format!("{}: cannot determine file size", settings.input)) })?; @@ -1589,8 +1587,9 @@ where { // Get the size of the input file in bytes and compute the number // of bytes per chunk. - // TODO - cannot get metadata or determine file size for stdin input - let metadata = metadata(&settings.input).unwrap(); + let metadata = metadata(&settings.input).map_err(|_| { + USimpleError::new(1, format!("{}: cannot determine file size", settings.input)) + })?; let num_bytes = metadata.len(); let chunk_size = (num_bytes / num_chunks) as usize; @@ -1664,8 +1663,9 @@ where { // Get the size of the input file in bytes and compute the number // of bytes per chunk. - // TODO - cannot get metadata or determine file size for stdin input - let metadata = metadata(&settings.input).unwrap(); + let metadata = metadata(&settings.input).map_err(|_| { + USimpleError::new(1, format!("{}: cannot determine file size", settings.input)) + })?; let num_bytes = metadata.len(); let chunk_size = (num_bytes / num_chunks) as usize; diff --git a/tests/by-util/test_split.rs b/tests/by-util/test_split.rs index e7452bae64..85ce5f8919 100644 --- a/tests/by-util/test_split.rs +++ b/tests/by-util/test_split.rs @@ -729,6 +729,24 @@ fn test_split_stdin_num_kth_chunk() { .stderr_only("split: -: cannot determine file size\n"); } +#[test] +fn test_split_stdin_num_line_chunks() { + new_ucmd!() + .args(&["--number=l/2"]) + .fails() + .code_is(1) + .stderr_only("split: -: cannot determine file size\n"); +} + +#[test] +fn test_split_stdin_num_kth_line_chunk() { + new_ucmd!() + .args(&["--number=l/2/5"]) + .fails() + .code_is(1) + .stderr_only("split: -: cannot determine file size\n"); +} + fn file_read(at: &AtPath, filename: &str) -> String { let mut s = String::new(); at.open(filename).read_to_string(&mut s).unwrap();