mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 03:27:44 +00:00
split: implementing separator option (#5331)
* split: implementing separator option * split: separator option - handle multiple update * split: style * split: separator tests * split: separator tests - stdin in ci/cd * split: tests - ci/cd stdin errors * split: refactor based on feedback * split: improve test coverage * split: fix broken pipe error in tests with stdin * split: fix for handle_multiple_separator_options * split: comments * split: refactor separator code * split: changes based on feedback * split: changes based on feedback
This commit is contained in:
parent
9f6a720582
commit
c5a0aa92f8
5 changed files with 348 additions and 38 deletions
|
@ -1483,3 +1483,242 @@ fn test_split_non_utf8_argument_windows() {
|
|||
.fails()
|
||||
.stderr_contains("error: invalid UTF-8 was detected in one or more arguments");
|
||||
}
|
||||
|
||||
// Test '--separator' / '-t' option following GNU tests example
|
||||
// test separators: '\n' , '\0' , ';'
|
||||
// test with '--lines=2' , '--line-bytes=4' , '--number=l/3' , '--number=r/3' , '--number=l/1/3' , '--number=r/1/3'
|
||||
#[test]
|
||||
fn test_split_separator_nl_lines() {
|
||||
let (at, mut ucmd) = at_and_ucmd!();
|
||||
ucmd.args(&["--lines=2", "-t", "\n"])
|
||||
.pipe_in("1\n2\n3\n4\n5\n")
|
||||
.succeeds();
|
||||
|
||||
assert_eq!(file_read(&at, "xaa"), "1\n2\n");
|
||||
assert_eq!(file_read(&at, "xab"), "3\n4\n");
|
||||
assert_eq!(file_read(&at, "xac"), "5\n");
|
||||
assert!(!at.plus("xad").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_split_separator_nl_line_bytes() {
|
||||
let (at, mut ucmd) = at_and_ucmd!();
|
||||
ucmd.args(&["--line-bytes=4", "-t", "\n"])
|
||||
.pipe_in("1\n2\n3\n4\n5\n")
|
||||
.succeeds();
|
||||
|
||||
assert_eq!(file_read(&at, "xaa"), "1\n2\n");
|
||||
assert_eq!(file_read(&at, "xab"), "3\n4\n");
|
||||
assert_eq!(file_read(&at, "xac"), "5\n");
|
||||
assert!(!at.plus("xad").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_split_separator_nl_number_l() {
|
||||
let (at, mut ucmd) = at_and_ucmd!();
|
||||
ucmd.args(&["--number=l/3", "--separator=\n", "fivelines.txt"])
|
||||
.succeeds();
|
||||
|
||||
assert_eq!(file_read(&at, "xaa"), "1\n2\n");
|
||||
assert_eq!(file_read(&at, "xab"), "3\n4\n");
|
||||
assert_eq!(file_read(&at, "xac"), "5\n");
|
||||
assert!(!at.plus("xad").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_split_separator_nl_number_r() {
|
||||
let (at, mut ucmd) = at_and_ucmd!();
|
||||
ucmd.args(&["--number=r/3", "--separator", "\n", "fivelines.txt"])
|
||||
.succeeds();
|
||||
|
||||
assert_eq!(file_read(&at, "xaa"), "1\n4\n");
|
||||
assert_eq!(file_read(&at, "xab"), "2\n5\n");
|
||||
assert_eq!(file_read(&at, "xac"), "3\n");
|
||||
assert!(!at.plus("xad").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_split_separator_nul_lines() {
|
||||
let (at, mut ucmd) = at_and_ucmd!();
|
||||
ucmd.args(&["--lines=2", "-t", "\\0", "separator_nul.txt"])
|
||||
.succeeds();
|
||||
|
||||
assert_eq!(file_read(&at, "xaa"), "1\02\0");
|
||||
assert_eq!(file_read(&at, "xab"), "3\04\0");
|
||||
assert_eq!(file_read(&at, "xac"), "5\0");
|
||||
assert!(!at.plus("xad").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_split_separator_nul_line_bytes() {
|
||||
let (at, mut ucmd) = at_and_ucmd!();
|
||||
ucmd.args(&["--line-bytes=4", "-t", "\\0", "separator_nul.txt"])
|
||||
.succeeds();
|
||||
|
||||
assert_eq!(file_read(&at, "xaa"), "1\02\0");
|
||||
assert_eq!(file_read(&at, "xab"), "3\04\0");
|
||||
assert_eq!(file_read(&at, "xac"), "5\0");
|
||||
assert!(!at.plus("xad").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_split_separator_nul_number_l() {
|
||||
let (at, mut ucmd) = at_and_ucmd!();
|
||||
ucmd.args(&["--number=l/3", "--separator=\\0", "separator_nul.txt"])
|
||||
.succeeds();
|
||||
|
||||
assert_eq!(file_read(&at, "xaa"), "1\02\0");
|
||||
assert_eq!(file_read(&at, "xab"), "3\04\0");
|
||||
assert_eq!(file_read(&at, "xac"), "5\0");
|
||||
assert!(!at.plus("xad").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_split_separator_nul_number_r() {
|
||||
let (at, mut ucmd) = at_and_ucmd!();
|
||||
ucmd.args(&["--number=r/3", "--separator=\\0", "separator_nul.txt"])
|
||||
.succeeds();
|
||||
|
||||
assert_eq!(file_read(&at, "xaa"), "1\04\0");
|
||||
assert_eq!(file_read(&at, "xab"), "2\05\0");
|
||||
assert_eq!(file_read(&at, "xac"), "3\0");
|
||||
assert!(!at.plus("xad").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_split_separator_semicolon_lines() {
|
||||
let (at, mut ucmd) = at_and_ucmd!();
|
||||
ucmd.args(&["--lines=2", "-t", ";", "separator_semicolon.txt"])
|
||||
.succeeds();
|
||||
|
||||
assert_eq!(file_read(&at, "xaa"), "1;2;");
|
||||
assert_eq!(file_read(&at, "xab"), "3;4;");
|
||||
assert_eq!(file_read(&at, "xac"), "5;");
|
||||
assert!(!at.plus("xad").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_split_separator_semicolon_line_bytes() {
|
||||
let (at, mut ucmd) = at_and_ucmd!();
|
||||
ucmd.args(&["--line-bytes=4", "-t", ";", "separator_semicolon.txt"])
|
||||
.succeeds();
|
||||
|
||||
assert_eq!(file_read(&at, "xaa"), "1;2;");
|
||||
assert_eq!(file_read(&at, "xab"), "3;4;");
|
||||
assert_eq!(file_read(&at, "xac"), "5;");
|
||||
assert!(!at.plus("xad").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_split_separator_semicolon_number_l() {
|
||||
let (at, mut ucmd) = at_and_ucmd!();
|
||||
ucmd.args(&["--number=l/3", "--separator=;", "separator_semicolon.txt"])
|
||||
.succeeds();
|
||||
|
||||
assert_eq!(file_read(&at, "xaa"), "1;2;");
|
||||
assert_eq!(file_read(&at, "xab"), "3;4;");
|
||||
assert_eq!(file_read(&at, "xac"), "5;");
|
||||
assert!(!at.plus("xad").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_split_separator_semicolon_number_r() {
|
||||
let (at, mut ucmd) = at_and_ucmd!();
|
||||
ucmd.args(&["--number=r/3", "--separator=;", "separator_semicolon.txt"])
|
||||
.succeeds();
|
||||
|
||||
assert_eq!(file_read(&at, "xaa"), "1;4;");
|
||||
assert_eq!(file_read(&at, "xab"), "2;5;");
|
||||
assert_eq!(file_read(&at, "xac"), "3;");
|
||||
assert!(!at.plus("xad").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_split_separator_semicolon_number_kth_l() {
|
||||
new_ucmd!()
|
||||
.args(&[
|
||||
"--number=l/1/3",
|
||||
"--separator",
|
||||
";",
|
||||
"separator_semicolon.txt",
|
||||
])
|
||||
.succeeds()
|
||||
.stdout_only("1;2;");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_split_separator_semicolon_number_kth_r() {
|
||||
new_ucmd!()
|
||||
.args(&[
|
||||
"--number=r/1/3",
|
||||
"--separator",
|
||||
";",
|
||||
"separator_semicolon.txt",
|
||||
])
|
||||
.succeeds()
|
||||
.stdout_only("1;4;");
|
||||
}
|
||||
|
||||
// Test error edge cases for separator option
|
||||
#[test]
|
||||
fn test_split_separator_no_value() {
|
||||
new_ucmd!()
|
||||
.args(&["-t"])
|
||||
.ignore_stdin_write_error()
|
||||
.pipe_in("a\n")
|
||||
.fails()
|
||||
.stderr_contains(
|
||||
"error: a value is required for '--separator <SEP>' but none was supplied",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_split_separator_invalid_usage() {
|
||||
let scene = TestScenario::new(util_name!());
|
||||
scene
|
||||
.ucmd()
|
||||
.args(&["--separator=xx"])
|
||||
.ignore_stdin_write_error()
|
||||
.pipe_in("a\n")
|
||||
.fails()
|
||||
.no_stdout()
|
||||
.stderr_contains("split: multi-character separator 'xx'");
|
||||
scene
|
||||
.ucmd()
|
||||
.args(&["-ta", "-tb"])
|
||||
.ignore_stdin_write_error()
|
||||
.pipe_in("a\n")
|
||||
.fails()
|
||||
.no_stdout()
|
||||
.stderr_contains("split: multiple separator characters specified");
|
||||
scene
|
||||
.ucmd()
|
||||
.args(&["-t'\n'", "-tb"])
|
||||
.ignore_stdin_write_error()
|
||||
.pipe_in("a\n")
|
||||
.fails()
|
||||
.no_stdout()
|
||||
.stderr_contains("split: multiple separator characters specified");
|
||||
}
|
||||
|
||||
// Test using same separator multiple times
|
||||
#[test]
|
||||
fn test_split_separator_same_multiple() {
|
||||
let scene = TestScenario::new(util_name!());
|
||||
scene
|
||||
.ucmd()
|
||||
.args(&["--separator=:", "--separator=:", "fivelines.txt"])
|
||||
.succeeds();
|
||||
scene
|
||||
.ucmd()
|
||||
.args(&["-t:", "--separator=:", "fivelines.txt"])
|
||||
.succeeds();
|
||||
scene
|
||||
.ucmd()
|
||||
.args(&["-t", ":", "-t", ":", "fivelines.txt"])
|
||||
.succeeds();
|
||||
scene
|
||||
.ucmd()
|
||||
.args(&["-t:", "-t:", "-t,", "fivelines.txt"])
|
||||
.fails();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue