Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/uu/split/src/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,9 @@ struct OutFile {
filename: OsString,
maybe_writer: Option<Writer>,
is_new: bool,
/// Whether `--verbose` already reported this file, so that retries and
/// re-opens of the writer stay silent.
announced: bool,
}

/// A set of output files
Expand Down Expand Up @@ -853,6 +856,9 @@ impl ManageOutFiles for OutFiles {
let maybe_writer = if is_writer_optional {
None
} else {
if settings.verbose {
print_creating_file(&filename)?;
}
let instantiated = settings.instantiate_current_writer(&filename, true);
// If there was an error instantiating the writer for a file,
// it could be due to hitting the system limit of open files,
Expand All @@ -873,12 +879,17 @@ impl ManageOutFiles for OutFiles {
filename,
maybe_writer,
is_new: true,
announced: !is_writer_optional,
});
}
Ok(out_files)
}

fn instantiate_writer(&mut self, idx: usize, settings: &Settings) -> UResult<&mut Writer> {
if settings.verbose && !self[idx].announced {
print_creating_file(&self[idx].filename)?;
self[idx].announced = true;
}
let mut count = 0;
// Use-case for doing multiple tries of closing fds:
// E.g. split running in parallel to other processes (e.g. another split) doing similar stuff,
Expand Down
57 changes: 57 additions & 0 deletions tests/by-util/test_split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2353,3 +2353,60 @@ fn test_obsolete_lines_not_read_after_double_dash() {
.fails()
.stderr_contains("cannot open '-1' for reading");
}

#[test]
fn test_verbose_number_of_chunks() {
use std::fmt::Write as _;

for (args, input, created) in [
(&["-n", "2"][..], "a\nb\n", &["xaa", "xab"][..]),
(&["-n", "l/2"], "a\nb\n", &["xaa", "xab"]),
(&["-n", "r/2"], "a\nb\n", &["xaa", "xab"]),
(&["-n", "3"], "a\nb\n", &["xaa", "xab", "xac"]),
(&["-n", "l/3"], "a\nb\n", &["xaa", "xab", "xac"]),
(&["-n", "r/3"], "a\nb\n", &["xaa", "xab", "xac"]),
// Only the files that get created are announced with `-e`.
(&["-e", "-n", "5"], "a\nb\n", &["xaa", "xab", "xac", "xad"]),
(&["-e", "-n", "l/3"], "a\nb\n", &["xaa", "xab"]),
(&["-e", "-n", "r/3"], "a\nb\n", &["xaa", "xab"]),
// A file written to several times is still announced once.
(&["-n", "2"], "1\n2\n3\n4\n5\n", &["xaa", "xab"]),
(&["-n", "l/2"], "1\n2\n3\n4\n5\n", &["xaa", "xab"]),
(&["-n", "r/2"], "1\n2\n3\n4\n5\n", &["xaa", "xab"]),
(&["-e", "-n", "l/2"], "1\n2\n3\n4\n5\n", &["xaa", "xab"]),
(&["-e", "-n", "r/2"], "1\n2\n3\n4\n5\n", &["xaa", "xab"]),
] {
let (at, mut ucmd) = at_and_ucmd!();
let expected = created.iter().fold(String::new(), |mut out, name| {
writeln!(out, "creating file '{name}'").unwrap();
out
});

ucmd.args(args)
.arg("--verbose")
.pipe_in(input)
.succeeds()
.stdout_only(expected);
for name in created {
assert!(at.file_exists(name), "{args:?}: {name} is missing");
}
}
}

#[test]
fn test_verbose_kth_chunk_creates_no_file() {
for (args, chunk) in [
(&["-n", "1/2"][..], "abc"),
(&["-n", "l/1/2"], "abcd\n"),
(&["-n", "r/1/2"], "abcd\n"),
] {
let (at, mut ucmd) = at_and_ucmd!();

ucmd.args(args)
.arg("--verbose")
.pipe_in("abcd\n")
.succeeds()
.stdout_only(chunk);
assert!(!at.file_exists("xaa"), "{args:?}: unexpected output file");
}
}
Loading