Skip to content

Commit

Permalink
shuf: Do not read input when -n0 is given
Browse files Browse the repository at this point in the history
This is explicitly tested by some suites, including the GNU test suite.
  • Loading branch information
BenWiederhake committed Feb 21, 2024
1 parent 4dba45d commit acd13f4
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/uu/shuf/src/shuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
},
};

if options.head_count == 0 {
// Do not attempt to read the random source or the input file.
// However, we must touch the output file, if given:
if let Some(s) = options.output {
File::create(&s[..])
.map_err_context(|| format!("failed to open {} for writing", s.quote()))?;
}
return Ok(());
}

match mode {
Mode::Echo(args) => {
let mut evec = args.iter().map(String::as_bytes).collect::<Vec<_>>();
Expand Down
75 changes: 75 additions & 0 deletions tests/by-util/test_shuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,81 @@ fn test_head_count() {
);
}

#[test]
fn test_zero_head_count_pipe() {
let result = new_ucmd!().arg("-n0").pipe_in(vec![]).succeeds();
// Output must be completely empty, not even a newline!
result.no_output();
}

#[test]
fn test_zero_head_count_pipe_explicit() {
let result = new_ucmd!().arg("-n0").arg("-").pipe_in(vec![]).succeeds();
result.no_stderr();

let result_seq: &str = result.stdout_str();
assert!(

Check warning on line 163 in tests/by-util/test_shuf.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_shuf.rs#L163

Added line #L163 was not covered by tests
result_seq.is_empty(),
"Output must be completely empty, not even newline"
);
}

#[test]
fn test_zero_head_count_file_unreadable() {
let result = new_ucmd!()
.arg("-n0")
.arg("/invalid/unreadable")
.pipe_in(vec![])
.succeeds();
result.no_stderr();

let result_seq: &str = result.stdout_str();
assert!(

Check warning on line 179 in tests/by-util/test_shuf.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_shuf.rs#L179

Added line #L179 was not covered by tests
result_seq.is_empty(),
"Output must be completely empty, not even newline"
);
}

#[test]
fn test_zero_head_count_file_touch_output() {
new_ucmd!()
.arg("-n0")
.arg("-o")
.arg("/invalid/unwritable")
.pipe_in(vec![])
.fails()
.stderr_contains("failed to open '/invalid/unwritable' for writing:");
}

#[test]
fn test_zero_head_count_echo() {
let result = new_ucmd!()
.arg("-n0")
.arg("-e")
.arg("hello")
.pipe_in(vec![])
.succeeds();
result.no_stderr();

let result_seq: &str = result.stdout_str();
assert!(

Check warning on line 207 in tests/by-util/test_shuf.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_shuf.rs#L207

Added line #L207 was not covered by tests
result_seq.is_empty(),
"Output must be completely empty, not even newline"
);
}

#[test]
fn test_zero_head_count_range() {
let result = new_ucmd!().arg("-n0").arg("-i4-8").succeeds();
result.no_stderr();

let result_seq: &str = result.stdout_str();
assert!(

Check warning on line 219 in tests/by-util/test_shuf.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_shuf.rs#L219

Added line #L219 was not covered by tests
result_seq.is_empty(),
"Output must be completely empty, not even newline"
);
}

#[test]
fn test_head_count_multi_big_then_small() {
let repeat_limit = 5;
Expand Down

0 comments on commit acd13f4

Please sign in to comment.