Skip to content

Commit

Permalink
split: --number stdin handling
Browse files Browse the repository at this point in the history
  • Loading branch information
zhitkoff committed Oct 17, 2023
1 parent af372b5 commit b889e52
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/uu/split/src/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
})?;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down
18 changes: 18 additions & 0 deletions tests/by-util/test_split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit b889e52

Please sign in to comment.