| 1 |
// This file is part of the uutils coreutils package. |
| 2 |
// |
| 3 |
// For the full copyright and license information, please view the LICENSE |
| 4 |
// file that was distributed with this source code. |
| 5 |
|
| 6 |
// spell-checker:ignore xzaaa sixhundredfiftyonebytes ninetyonebytes threebytes asciilowercase ghijkl mnopq rstuv wxyz fivelines twohundredfortyonebytes onehundredlines nbbbb dxen ncccc rlimit NOFILE |
| 7 |
|
| 8 |
use rand::{RngExt as _, SeedableRng, rng}; |
| 9 |
use regex::Regex; |
| 10 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 11 |
use rlimit::Resource; |
| 12 |
#[cfg(not(windows))] |
| 13 |
use std::env; |
| 14 |
#[cfg(target_os = "linux")] |
| 15 |
use std::os::unix::ffi::OsStringExt; |
| 16 |
use std::path::Path; |
| 17 |
use std::{ |
| 18 |
fs::{self, File}, |
| 19 |
io::{BufWriter, Read, Write}, |
| 20 |
}; |
| 21 |
use uutests::util::{AtPath, TestScenario}; |
| 22 |
|
| 23 |
use uutests::{at_and_ucmd, new_ucmd, util_name}; |
| 24 |
|
| 25 |
fn random_chars(n: usize) -> String { |
| 26 |
rng() |
| 27 |
.sample_iter(&rand::distr::Alphanumeric) |
| 28 |
.map(char::from) |
| 29 |
.take(n) |
| 30 |
.collect::<String>() |
| 31 |
} |
| 32 |
|
| 33 |
struct Glob { |
| 34 |
directory: AtPath, |
| 35 |
regex: Regex, |
| 36 |
} |
| 37 |
|
| 38 |
impl Glob { |
| 39 |
fn new(at: &AtPath, directory: &str, regex: &str) -> Self { |
| 40 |
Self { |
| 41 |
directory: AtPath::new(Path::new(&at.plus_as_string(directory))), |
| 42 |
regex: Regex::new(regex).unwrap(), |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
fn count(&self) -> usize { |
| 47 |
self.collect().len() |
| 48 |
} |
| 49 |
|
| 50 |
/// Get all files in `self.directory` that match `self.regex` |
| 51 |
fn collect(&self) -> Vec<String> { |
| 52 |
fs::read_dir(Path::new(&self.directory.subdir)) |
| 53 |
.unwrap() |
| 54 |
.filter_map(|entry| { |
| 55 |
let path = entry.unwrap().path(); |
| 56 |
let name = self |
| 57 |
.directory |
| 58 |
.minus_as_string(path.as_path().to_str().unwrap_or("")); |
| 59 |
if self.regex.is_match(&name) { |
| 60 |
Some(name) |
| 61 |
} else { |
| 62 |
None |
| 63 |
} |
| 64 |
}) |
| 65 |
.collect() |
| 66 |
} |
| 67 |
|
| 68 |
/// Accumulate bytes of all files in `self.collect()` |
| 69 |
fn collate(&self) -> Vec<u8> { |
| 70 |
let mut files = self.collect(); |
| 71 |
files.sort(); |
| 72 |
let mut data: Vec<u8> = vec![]; |
| 73 |
for name in &files { |
| 74 |
data.extend(self.directory.read_bytes(name)); |
| 75 |
} |
| 76 |
data |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
/// File handle that user can add random bytes (line-formatted or not) to |
| 81 |
struct RandomFile { |
| 82 |
inner: File, |
| 83 |
} |
| 84 |
|
| 85 |
impl RandomFile { |
| 86 |
/// Size of each line that's being generated |
| 87 |
const LINESIZE: usize = 32; |
| 88 |
|
| 89 |
/// `create()` file handle located at `at` / `name` |
| 90 |
fn new(at: &AtPath, name: &str) -> Self { |
| 91 |
Self { |
| 92 |
inner: File::create(at.plus(name)).unwrap(), |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
fn add_bytes(&mut self, bytes: usize) { |
| 97 |
// Note that just writing random characters isn't enough to cover all |
| 98 |
// cases. We need truly random bytes. |
| 99 |
let mut writer = BufWriter::new(&self.inner); |
| 100 |
|
| 101 |
// Seed the rng so as to avoid spurious test failures. |
| 102 |
let mut rng = rand::rngs::StdRng::seed_from_u64(123); |
| 103 |
let mut buffer = [0; 1024]; |
| 104 |
let mut remaining_size = bytes; |
| 105 |
|
| 106 |
while remaining_size > 0 { |
| 107 |
let to_write = std::cmp::min(remaining_size, buffer.len()); |
| 108 |
let buf = &mut buffer[..to_write]; |
| 109 |
rng.fill(buf); |
| 110 |
writer.write_all(buf).unwrap(); |
| 111 |
|
| 112 |
remaining_size -= to_write; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
/// Add n lines each of size `RandomFile::LINESIZE` |
| 117 |
fn add_lines(&mut self, lines: usize) { |
| 118 |
self.add_lines_with_line_size(lines, Self::LINESIZE); |
| 119 |
} |
| 120 |
|
| 121 |
/// Add n lines each of the given size. |
| 122 |
fn add_lines_with_line_size(&mut self, lines: usize, line_size: usize) { |
| 123 |
let mut n = lines; |
| 124 |
while n > 0 { |
| 125 |
writeln!(self.inner, "{}", random_chars(line_size)).unwrap(); |
| 126 |
n -= 1; |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
#[test] |
| 132 |
fn test_invalid_arg() { |
| 133 |
new_ucmd!().arg("--definitely-invalid").fails_with_code(1); |
| 134 |
} |
| 135 |
|
| 136 |
#[test] |
| 137 |
#[cfg(all(unix, not(target_os = "android")))] |
| 138 |
fn test_split_to_non_seekable() { |
| 139 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 140 |
at.symlink_file("/dev/stdout", "xaa"); |
| 141 |
|
| 142 |
ucmd.args(&["-"]) |
| 143 |
.pipe_in("string") |
| 144 |
.succeeds() |
| 145 |
.stdout_is("string"); |
| 146 |
} |
| 147 |
|
| 148 |
#[test] |
| 149 |
fn test_split_non_existing_file() { |
| 150 |
new_ucmd!() |
| 151 |
.arg("non-existing") |
| 152 |
.fails_with_code(1) |
| 153 |
.stderr_is("split: cannot open 'non-existing' for reading: No such file or directory\n"); |
| 154 |
} |
| 155 |
|
| 156 |
#[test] |
| 157 |
fn test_split_default() { |
| 158 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 159 |
let name = "split_default"; |
| 160 |
RandomFile::new(&at, name).add_lines(2000); |
| 161 |
ucmd.args(&[name]).succeeds(); |
| 162 |
|
| 163 |
let glob = Glob::new(&at, ".", r"x[[:alpha:]][[:alpha:]]$"); |
| 164 |
assert_eq!(glob.count(), 2); |
| 165 |
assert_eq!(glob.collate(), at.read_bytes(name)); |
| 166 |
} |
| 167 |
|
| 168 |
#[test] |
| 169 |
fn test_split_numeric_prefixed_chunks_by_bytes() { |
| 170 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 171 |
let name = "split_num_prefixed_chunks_by_bytes"; |
| 172 |
RandomFile::new(&at, name).add_bytes(10000); |
| 173 |
ucmd.args(&[ |
| 174 |
"-d", // --numeric-suffixes |
| 175 |
"-b", // --bytes |
| 176 |
"1000", name, "a", |
| 177 |
]) |
| 178 |
.succeeds(); |
| 179 |
|
| 180 |
let glob = Glob::new(&at, ".", r"a\d\d$"); |
| 181 |
assert_eq!(glob.count(), 10); |
| 182 |
for filename in glob.collect() { |
| 183 |
assert_eq!(glob.directory.metadata(&filename).len(), 1000); |
| 184 |
} |
| 185 |
assert_eq!(glob.collate(), at.read_bytes(name)); |
| 186 |
} |
| 187 |
|
| 188 |
#[test] |
| 189 |
fn test_split_str_prefixed_chunks_by_bytes() { |
| 190 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 191 |
let name = "split_str_prefixed_chunks_by_bytes"; |
| 192 |
RandomFile::new(&at, name).add_bytes(10000); |
| 193 |
// Important that this is less than 1024 since that's our internal buffer |
| 194 |
// size. Good to test that we don't overshoot. |
| 195 |
ucmd.args(&["-b", "1000", name, "b"]).succeeds(); |
| 196 |
|
| 197 |
let glob = Glob::new(&at, ".", r"b[[:alpha:]][[:alpha:]]$"); |
| 198 |
assert_eq!(glob.count(), 10); |
| 199 |
for filename in glob.collect() { |
| 200 |
assert_eq!(glob.directory.metadata(&filename).len(), 1000); |
| 201 |
} |
| 202 |
assert_eq!(glob.collate(), at.read_bytes(name)); |
| 203 |
} |
| 204 |
|
| 205 |
/// Test short bytes option concatenated with value |
| 206 |
#[test] |
| 207 |
fn test_split_by_bytes_short_concatenated_with_value() { |
| 208 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 209 |
let name = "split_by_bytes_short_concatenated_with_value"; |
| 210 |
RandomFile::new(&at, name).add_bytes(10000); |
| 211 |
ucmd.args(&["-b1000", name]).succeeds(); |
| 212 |
|
| 213 |
let glob = Glob::new(&at, ".", r"x[[:alpha:]][[:alpha:]]$"); |
| 214 |
assert_eq!(glob.count(), 10); |
| 215 |
for filename in glob.collect() { |
| 216 |
assert_eq!(glob.directory.metadata(&filename).len(), 1000); |
| 217 |
} |
| 218 |
assert_eq!(glob.collate(), at.read_bytes(name)); |
| 219 |
} |
| 220 |
|
| 221 |
// This is designed to test what happens when the desired part size is not a |
| 222 |
// multiple of the buffer size and we hopefully don't overshoot the desired part |
| 223 |
// size. |
| 224 |
#[test] |
| 225 |
fn test_split_bytes_prime_part_size() { |
| 226 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 227 |
let name = "test_split_bytes_prime_part_size"; |
| 228 |
RandomFile::new(&at, name).add_bytes(10000); |
| 229 |
// 1753 is prime and greater than the buffer size, 1024. |
| 230 |
ucmd.args(&["-b", "1753", name, "b"]).succeeds(); |
| 231 |
|
| 232 |
let glob = Glob::new(&at, ".", r"b[[:alpha:]][[:alpha:]]$"); |
| 233 |
assert_eq!(glob.count(), 6); |
| 234 |
let mut fns = glob.collect(); |
| 235 |
// glob.collect() is not guaranteed to return in sorted order, so we sort. |
| 236 |
fns.sort(); |
| 237 |
#[allow(clippy::needless_range_loop)] |
| 238 |
for i in 0..5 { |
| 239 |
assert_eq!(glob.directory.metadata(&fns[i]).len(), 1753); |
| 240 |
} |
| 241 |
assert_eq!(glob.directory.metadata(&fns[5]).len(), 1235); |
| 242 |
assert_eq!(glob.collate(), at.read_bytes(name)); |
| 243 |
} |
| 244 |
|
| 245 |
#[test] |
| 246 |
fn test_split_num_prefixed_chunks_by_lines() { |
| 247 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 248 |
let name = "split_num_prefixed_chunks_by_lines"; |
| 249 |
RandomFile::new(&at, name).add_lines(10000); |
| 250 |
ucmd.args(&["-d", "-l", "1000", name, "c"]).succeeds(); |
| 251 |
|
| 252 |
let glob = Glob::new(&at, ".", r"c\d\d$"); |
| 253 |
assert_eq!(glob.count(), 10); |
| 254 |
assert_eq!(glob.collate(), at.read_bytes(name)); |
| 255 |
} |
| 256 |
|
| 257 |
#[test] |
| 258 |
fn test_split_str_prefixed_chunks_by_lines() { |
| 259 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 260 |
let name = "split_str_prefixed_chunks_by_lines"; |
| 261 |
RandomFile::new(&at, name).add_lines(10000); |
| 262 |
ucmd.args(&["-l", "1000", name, "d"]).succeeds(); |
| 263 |
|
| 264 |
let glob = Glob::new(&at, ".", r"d[[:alpha:]][[:alpha:]]$"); |
| 265 |
assert_eq!(glob.count(), 10); |
| 266 |
assert_eq!(glob.collate(), at.read_bytes(name)); |
| 267 |
} |
| 268 |
|
| 269 |
#[test] |
| 270 |
fn test_split_additional_suffix() { |
| 271 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 272 |
let name = "split_additional_suffix"; |
| 273 |
RandomFile::new(&at, name).add_lines(2000); |
| 274 |
ucmd.args(&["--additional-suffix", ".txt", name]).succeeds(); |
| 275 |
|
| 276 |
let glob = Glob::new(&at, ".", r"x[[:alpha:]][[:alpha:]].txt$"); |
| 277 |
assert_eq!(glob.count(), 2); |
| 278 |
assert_eq!(glob.collate(), at.read_bytes(name)); |
| 279 |
} |
| 280 |
|
| 281 |
#[test] |
| 282 |
fn test_additional_suffix_dir_separator() { |
| 283 |
#[cfg(unix)] |
| 284 |
new_ucmd!() |
| 285 |
.args(&["--additional-suffix", "a/b"]) |
| 286 |
.fails() |
| 287 |
.usage_error("invalid suffix 'a/b', contains directory separator"); |
| 288 |
|
| 289 |
#[cfg(windows)] |
| 290 |
new_ucmd!() |
| 291 |
.args(&["--additional-suffix", "a\\b"]) |
| 292 |
.fails() |
| 293 |
.usage_error("invalid suffix 'a\\b', contains directory separator"); |
| 294 |
} |
| 295 |
|
| 296 |
#[test] |
| 297 |
fn test_split_additional_suffix_hyphen_value() { |
| 298 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 299 |
let name = "split_additional_suffix"; |
| 300 |
RandomFile::new(&at, name).add_lines(2000); |
| 301 |
ucmd.args(&["--additional-suffix", "-300", name]).succeeds(); |
| 302 |
|
| 303 |
let glob = Glob::new(&at, ".", r"x[[:alpha:]][[:alpha:]]-300$"); |
| 304 |
assert_eq!(glob.count(), 2); |
| 305 |
assert_eq!(glob.collate(), at.read_bytes(name)); |
| 306 |
} |
| 307 |
|
| 308 |
// note: the test_filter* tests below are unix-only |
| 309 |
// windows support has been waived for now because of the difficulty of getting |
| 310 |
// the `cmd` call right |
| 311 |
// see https://github.com/rust-lang/rust/issues/29494 |
| 312 |
|
| 313 |
#[test] |
| 314 |
#[cfg(unix)] |
| 315 |
fn test_filter() { |
| 316 |
// like `test_split_default()` but run a command before writing |
| 317 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 318 |
let name = "filtered"; |
| 319 |
let n_lines = 3; |
| 320 |
RandomFile::new(&at, name).add_lines(n_lines); |
| 321 |
|
| 322 |
// change all characters to 'i' |
| 323 |
ucmd.args(&["--filter=sed s/./i/g > $FILE", name]) |
| 324 |
.succeeds(); |
| 325 |
|
| 326 |
// assert all characters are 'i' / no character is not 'i' |
| 327 |
// (assert that command succeeded) |
| 328 |
let glob = Glob::new(&at, ".", r"x[[:alpha:]][[:alpha:]]$"); |
| 329 |
assert!(!glob.collate().iter().any(|&c| { |
| 330 |
// is not i |
| 331 |
c != (b'i') |
| 332 |
// is not newline |
| 333 |
&& c != (b'\n') |
| 334 |
})); |
| 335 |
} |
| 336 |
|
| 337 |
#[test] |
| 338 |
#[cfg(unix)] |
| 339 |
fn test_filter_with_env_var_set() { |
| 340 |
// This test will ensure that if $FILE env var was set before running --filter, it'll stay that |
| 341 |
// way |
| 342 |
// implemented like `test_split_default()` but run a command before writing |
| 343 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 344 |
let name = "filtered"; |
| 345 |
let n_lines = 3; |
| 346 |
RandomFile::new(&at, name).add_lines(n_lines); |
| 347 |
|
| 348 |
let env_var_value = "some-value"; |
| 349 |
unsafe { |
| 350 |
env::set_var("FILE", env_var_value); |
| 351 |
} |
| 352 |
ucmd.args(&[format!("--filter={}", "cat > $FILE").as_str(), name]) |
| 353 |
.succeeds(); |
| 354 |
|
| 355 |
let glob = Glob::new(&at, ".", r"x[[:alpha:]][[:alpha:]]$"); |
| 356 |
assert_eq!(glob.collate(), at.read_bytes(name)); |
| 357 |
assert_eq!( |
| 358 |
env::var("FILE").unwrap_or_else(|_| "var was unset".to_owned()), |
| 359 |
env_var_value |
| 360 |
); |
| 361 |
} |
| 362 |
|
| 363 |
#[test] |
| 364 |
#[cfg(unix)] |
| 365 |
fn test_filter_command_fails() { |
| 366 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 367 |
let name = "filter-will-fail"; |
| 368 |
RandomFile::new(&at, name).add_lines(4); |
| 369 |
|
| 370 |
ucmd.args(&["--filter=/a/path/that/totally/does/not/exist", name]) |
| 371 |
.fails(); |
| 372 |
} |
| 373 |
|
| 374 |
#[test] |
| 375 |
#[cfg(unix)] |
| 376 |
#[cfg(not(target_os = "openbsd"))] |
| 377 |
fn test_filter_broken_pipe() { |
| 378 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 379 |
let name = "filter-big-input"; |
| 380 |
|
| 381 |
RandomFile::new(&at, name).add_lines(1024 * 10); |
| 382 |
ucmd.args(&["--filter=head -c1 > /dev/null", "-n", "r/1", name]) |
| 383 |
.succeeds(); |
| 384 |
} |
| 385 |
|
| 386 |
#[test] |
| 387 |
#[cfg(unix)] |
| 388 |
fn test_filter_with_kth_chunk() { |
| 389 |
let scene = TestScenario::new(util_name!()); |
| 390 |
scene |
| 391 |
.ucmd() |
| 392 |
.args(&["--filter='some'", "--number=1/2"]) |
| 393 |
.ignore_stdin_write_error() |
| 394 |
.pipe_in("a\n") |
| 395 |
.fails() |
| 396 |
.no_stdout() |
| 397 |
.stderr_contains("--filter does not process a chunk extracted to stdout"); |
| 398 |
scene |
| 399 |
.ucmd() |
| 400 |
.args(&["--filter='some'", "--number=l/1/2"]) |
| 401 |
.ignore_stdin_write_error() |
| 402 |
.pipe_in("a\n") |
| 403 |
.fails() |
| 404 |
.no_stdout() |
| 405 |
.stderr_contains("--filter does not process a chunk extracted to stdout"); |
| 406 |
scene |
| 407 |
.ucmd() |
| 408 |
.args(&["--filter='some'", "--number=r/1/2"]) |
| 409 |
.ignore_stdin_write_error() |
| 410 |
.pipe_in("a\n") |
| 411 |
.fails() |
| 412 |
.no_stdout() |
| 413 |
.stderr_contains("--filter does not process a chunk extracted to stdout"); |
| 414 |
} |
| 415 |
|
| 416 |
#[test] |
| 417 |
fn test_split_lines_number() { |
| 418 |
// Test if stdout/stderr for '--lines' option is correct |
| 419 |
let scene = TestScenario::new(util_name!()); |
| 420 |
let at = &scene.fixtures; |
| 421 |
at.touch("file"); |
| 422 |
|
| 423 |
scene |
| 424 |
.ucmd() |
| 425 |
.args(&["--lines", "2", "file"]) |
| 426 |
.succeeds() |
| 427 |
.no_output(); |
| 428 |
scene |
| 429 |
.ucmd() |
| 430 |
.args(&["--lines", "0", "file"]) |
| 431 |
.fails_with_code(1) |
| 432 |
.stderr_only("split: invalid number of lines: 0\n"); |
| 433 |
scene |
| 434 |
.ucmd() |
| 435 |
.args(&["-0", "file"]) |
| 436 |
.fails_with_code(1) |
| 437 |
.stderr_only("split: invalid number of lines: 0\n"); |
| 438 |
scene |
| 439 |
.ucmd() |
| 440 |
.args(&["--lines", "2fb", "file"]) |
| 441 |
.fails_with_code(1) |
| 442 |
.stderr_only("split: invalid number of lines: '2fb'\n"); |
| 443 |
scene |
| 444 |
.ucmd() |
| 445 |
.args(&["--lines", "file"]) |
| 446 |
.fails_with_code(1) |
| 447 |
.stderr_only("split: invalid number of lines: 'file'\n"); |
| 448 |
} |
| 449 |
|
| 450 |
/// Test interference between split line size and IO buffer capacity. |
| 451 |
/// See issue #7869. |
| 452 |
#[test] |
| 453 |
fn test_split_lines_interfere_with_io_buf_capacity() { |
| 454 |
let buf_capacity = BufWriter::new(Vec::new()).capacity(); |
| 455 |
// We intentionally set the line size to be less than the IO write buffer |
| 456 |
// capacity. This is to trigger the condition where after the first split |
| 457 |
// file is written, there are still bytes left in the buffer. We then |
| 458 |
// test that those bytes are written to the next split file. |
| 459 |
let line_size = buf_capacity - 2; |
| 460 |
|
| 461 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 462 |
let name = "split_lines_interfere_with_io_buf_capacity"; |
| 463 |
RandomFile::new(&at, name).add_lines_with_line_size(2, line_size); |
| 464 |
ucmd.args(&["-l", "1", name]).succeeds(); |
| 465 |
|
| 466 |
// Note that `lines_size` doesn't take the trailing newline into account, |
| 467 |
// we add 1 for adjustment. |
| 468 |
assert_eq!(at.read("xaa").len(), line_size + 1); |
| 469 |
assert_eq!(at.read("xab").len(), line_size + 1); |
| 470 |
} |
| 471 |
|
| 472 |
/// Test short lines option with value concatenated |
| 473 |
#[test] |
| 474 |
fn test_split_lines_short_concatenated_with_value() { |
| 475 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 476 |
let name = "split_num_prefixed_chunks_by_lines"; |
| 477 |
RandomFile::new(&at, name).add_lines(10000); |
| 478 |
ucmd.args(&["-l1000", name]).succeeds(); |
| 479 |
|
| 480 |
let glob = Glob::new(&at, ".", r"x[[:alpha:]][[:alpha:]]$"); |
| 481 |
assert_eq!(glob.count(), 10); |
| 482 |
assert_eq!(glob.collate(), at.read_bytes(name)); |
| 483 |
} |
| 484 |
|
| 485 |
/// Test for obsolete lines option standalone |
| 486 |
#[test] |
| 487 |
fn test_split_obs_lines_standalone() { |
| 488 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 489 |
let name = "obs-lines-standalone"; |
| 490 |
RandomFile::new(&at, name).add_lines(4); |
| 491 |
ucmd.args(&["-2", name]).succeeds().no_output(); |
| 492 |
let glob = Glob::new(&at, ".", r"x[[:alpha:]][[:alpha:]]$"); |
| 493 |
assert_eq!(glob.count(), 2); |
| 494 |
assert_eq!(glob.collate(), at.read_bytes(name)); |
| 495 |
} |
| 496 |
|
| 497 |
/// Test for obsolete lines option standalone overflow |
| 498 |
#[test] |
| 499 |
fn test_split_obs_lines_standalone_overflow() { |
| 500 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 501 |
let name = "obs-lines-standalone"; |
| 502 |
RandomFile::new(&at, name).add_lines(4); |
| 503 |
ucmd.args(&["-99999999999999999991", name]) |
| 504 |
.succeeds() |
| 505 |
.no_output(); |
| 506 |
let glob = Glob::new(&at, ".", r"x[[:alpha:]][[:alpha:]]$"); |
| 507 |
assert_eq!(glob.count(), 1); |
| 508 |
assert_eq!(glob.collate(), at.read_bytes(name)); |
| 509 |
} |
| 510 |
|
| 511 |
/// Test for obsolete lines option as part of invalid combined short options |
| 512 |
#[test] |
| 513 |
fn test_split_obs_lines_within_invalid_combined_shorts() { |
| 514 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 515 |
|
| 516 |
at.touch("file"); |
| 517 |
|
| 518 |
ucmd.args(&["-2fb", "file"]) |
| 519 |
.fails_with_code(1) |
| 520 |
.stderr_contains("error: unexpected argument '-f' found\n"); |
| 521 |
} |
| 522 |
|
| 523 |
/// Test for obsolete lines option as part of combined short options |
| 524 |
#[test] |
| 525 |
fn test_split_obs_lines_within_combined_shorts() { |
| 526 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 527 |
|
| 528 |
let name = "obs-lines-within-shorts"; |
| 529 |
RandomFile::new(&at, name).add_lines(400); |
| 530 |
|
| 531 |
ucmd.args(&["-x200de", name]).succeeds().no_output(); |
| 532 |
let glob = Glob::new(&at, ".", r"x\d\d$"); |
| 533 |
assert_eq!(glob.count(), 2); |
| 534 |
assert_eq!(glob.collate(), at.read_bytes(name)); |
| 535 |
} |
| 536 |
|
| 537 |
/// Test for obsolete lines option as part of combined short options with tailing suffix length with value |
| 538 |
#[test] |
| 539 |
fn test_split_obs_lines_within_combined_shorts_tailing_suffix_length() { |
| 540 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 541 |
let name = "obs-lines-combined-shorts-tailing-suffix-length"; |
| 542 |
RandomFile::new(&at, name).add_lines(1000); |
| 543 |
|
| 544 |
ucmd.args(&["-d200a4", name]).succeeds(); |
| 545 |
|
| 546 |
let glob = Glob::new(&at, ".", r"x\d\d\d\d$"); |
| 547 |
assert_eq!(glob.count(), 5); |
| 548 |
assert_eq!(glob.collate(), at.read_bytes(name)); |
| 549 |
} |
| 550 |
|
| 551 |
/// Test for obsolete lines option starts as part of combined short options |
| 552 |
#[test] |
| 553 |
fn test_split_obs_lines_starts_combined_shorts() { |
| 554 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 555 |
|
| 556 |
let name = "obs-lines-starts-shorts"; |
| 557 |
RandomFile::new(&at, name).add_lines(400); |
| 558 |
|
| 559 |
ucmd.args(&["-200xd", name]).succeeds().no_output(); |
| 560 |
|
| 561 |
let glob = Glob::new(&at, ".", r"x\d\d$"); |
| 562 |
assert_eq!(glob.count(), 2); |
| 563 |
assert_eq!(glob.collate(), at.read_bytes(name)); |
| 564 |
} |
| 565 |
|
| 566 |
/// Test for using both obsolete lines (standalone) option and short/long lines option simultaneously |
| 567 |
#[test] |
| 568 |
fn test_split_both_lines_and_obs_lines_standalone() { |
| 569 |
// This test will ensure that: |
| 570 |
// if both lines option '-l' or '--lines' (with value) and obsolete lines option '-100' are used - it fails |
| 571 |
// if standalone lines option is used incorrectly and treated as a hyphen prefixed value of other option - it fails |
| 572 |
let scene = TestScenario::new(util_name!()); |
| 573 |
let at = &scene.fixtures; |
| 574 |
at.touch("file"); |
| 575 |
|
| 576 |
scene |
| 577 |
.ucmd() |
| 578 |
.args(&["-l", "2", "-2", "file"]) |
| 579 |
.fails_with_code(1) |
| 580 |
.stderr_contains("split: cannot split in more than one way\n"); |
| 581 |
scene |
| 582 |
.ucmd() |
| 583 |
.args(&["--lines", "2", "-2", "file"]) |
| 584 |
.fails_with_code(1) |
| 585 |
.stderr_contains("split: cannot split in more than one way\n"); |
| 586 |
} |
| 587 |
|
| 588 |
/// Test for using obsolete lines option incorrectly, so it is treated as a hyphen prefixed value of other option |
| 589 |
#[test] |
| 590 |
fn test_split_obs_lines_as_other_option_value() { |
| 591 |
// This test will ensure that: |
| 592 |
// if obsolete lines option is used incorrectly and treated as a hyphen prefixed value of other option - it fails |
| 593 |
let scene = TestScenario::new(util_name!()); |
| 594 |
let at = &scene.fixtures; |
| 595 |
at.touch("file"); |
| 596 |
|
| 597 |
scene |
| 598 |
.ucmd() |
| 599 |
.args(&["--lines", "-200", "file"]) |
| 600 |
.fails_with_code(1) |
| 601 |
.stderr_contains("split: invalid number of lines: '-200'\n"); |
| 602 |
scene |
| 603 |
.ucmd() |
| 604 |
.args(&["-l", "-200", "file"]) |
| 605 |
.fails_with_code(1) |
| 606 |
.stderr_contains("split: invalid number of lines: '-200'\n"); |
| 607 |
scene |
| 608 |
.ucmd() |
| 609 |
.args(&["-a", "-200", "file"]) |
| 610 |
.fails_with_code(1) |
| 611 |
.stderr_contains("split: invalid suffix length: '-200'\n"); |
| 612 |
scene |
| 613 |
.ucmd() |
| 614 |
.args(&["--suffix-length", "-d200e", "file"]) |
| 615 |
.fails_with_code(1) |
| 616 |
.stderr_contains("split: invalid suffix length: '-d200e'\n"); |
| 617 |
scene |
| 618 |
.ucmd() |
| 619 |
.args(&["-C", "-200", "file"]) |
| 620 |
.fails_with_code(1) |
| 621 |
.stderr_contains("split: invalid number of bytes: '-200'\n"); |
| 622 |
scene |
| 623 |
.ucmd() |
| 624 |
.args(&["--line-bytes", "-x200a4", "file"]) |
| 625 |
.fails_with_code(1) |
| 626 |
.stderr_contains("split: invalid number of bytes: '-x200a4'\n"); |
| 627 |
scene |
| 628 |
.ucmd() |
| 629 |
.args(&["-b", "-200", "file"]) |
| 630 |
.fails_with_code(1) |
| 631 |
.stderr_contains("split: invalid number of bytes: '-200'\n"); |
| 632 |
scene |
| 633 |
.ucmd() |
| 634 |
.args(&["--bytes", "-200xd", "file"]) |
| 635 |
.fails_with_code(1) |
| 636 |
.stderr_contains("split: invalid number of bytes: '-200xd'\n"); |
| 637 |
scene |
| 638 |
.ucmd() |
| 639 |
.args(&["-n", "-200", "file"]) |
| 640 |
.fails_with_code(1) |
| 641 |
.stderr_contains("split: invalid number of chunks: '-200'\n"); |
| 642 |
scene |
| 643 |
.ucmd() |
| 644 |
.args(&["--number", "-e200", "file"]) |
| 645 |
.fails_with_code(1) |
| 646 |
.stderr_contains("split: invalid number of chunks: '-e200'\n"); |
| 647 |
} |
| 648 |
|
| 649 |
#[test] |
| 650 |
fn test_split_chunks_by_line_or_round_robin_zero_chunks() { |
| 651 |
let scene = TestScenario::new(util_name!()); |
| 652 |
scene.fixtures.write("file", "a\nb\nc\nd\n"); |
| 653 |
scene |
| 654 |
.ucmd() |
| 655 |
.args(&["-n", "l/0", "file"]) |
| 656 |
.fails_with_code(1) |
| 657 |
.stderr_only("split: invalid number of chunks: '0'\n"); |
| 658 |
scene |
| 659 |
.ucmd() |
| 660 |
.args(&["-n", "r/0", "file"]) |
| 661 |
.fails_with_code(1) |
| 662 |
.stderr_only("split: invalid number of chunks: '0'\n"); |
| 663 |
} |
| 664 |
|
| 665 |
/// Test for using more than one obsolete lines option (standalone) |
| 666 |
/// last one wins |
| 667 |
#[test] |
| 668 |
fn test_split_multiple_obs_lines_standalone() { |
| 669 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 670 |
|
| 671 |
let name = "multiple-obs-lines"; |
| 672 |
RandomFile::new(&at, name).add_lines(400); |
| 673 |
|
| 674 |
ucmd.args(&["-3000", "-200", name]).succeeds().no_output(); |
| 675 |
|
| 676 |
let glob = Glob::new(&at, ".", r"x[[:alpha:]][[:alpha:]]$"); |
| 677 |
assert_eq!(glob.count(), 2); |
| 678 |
assert_eq!(glob.collate(), at.read_bytes(name)); |
| 679 |
} |
| 680 |
|
| 681 |
/// Test for using more than one obsolete lines option within combined shorts |
| 682 |
/// last one wins |
| 683 |
#[test] |
| 684 |
fn test_split_multiple_obs_lines_within_combined() { |
| 685 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 686 |
|
| 687 |
let name = "multiple-obs-lines"; |
| 688 |
RandomFile::new(&at, name).add_lines(400); |
| 689 |
|
| 690 |
ucmd.args(&["-d5000x", "-e200d", name]) |
| 691 |
.succeeds() |
| 692 |
.no_output(); |
| 693 |
|
| 694 |
let glob = Glob::new(&at, ".", r"x\d\d$"); |
| 695 |
assert_eq!(glob.count(), 2); |
| 696 |
assert_eq!(glob.collate(), at.read_bytes(name)); |
| 697 |
} |
| 698 |
|
| 699 |
/// Test for using both obsolete lines option within combined shorts with conflicting -n option simultaneously |
| 700 |
#[test] |
| 701 |
fn test_split_obs_lines_within_combined_with_number() { |
| 702 |
let scene = TestScenario::new(util_name!()); |
| 703 |
let at = &scene.fixtures; |
| 704 |
at.touch("file"); |
| 705 |
|
| 706 |
scene |
| 707 |
.ucmd() |
| 708 |
.args(&["-3dxen", "4", "file"]) |
| 709 |
.fails_with_code(1) |
| 710 |
.stderr_contains("split: cannot split in more than one way\n"); |
| 711 |
scene |
| 712 |
.ucmd() |
| 713 |
.args(&["-dxe30n", "4", "file"]) |
| 714 |
.fails_with_code(1) |
| 715 |
.stderr_contains("split: cannot split in more than one way\n"); |
| 716 |
} |
| 717 |
|
| 718 |
#[test] |
| 719 |
fn test_split_invalid_bytes_size() { |
| 720 |
new_ucmd!() |
| 721 |
.args(&["-b", "1024W"]) |
| 722 |
.fails_with_code(1) |
| 723 |
.stderr_only("split: invalid number of bytes: '1024W'\n"); |
| 724 |
#[cfg(target_pointer_width = "32")] |
| 725 |
{ |
| 726 |
let sizes = ["1000G", "10T"]; |
| 727 |
for size in &sizes { |
| 728 |
new_ucmd!().args(&["-b", size]).succeeds(); |
| 729 |
} |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
#[test] |
| 734 |
fn test_split_overflow_bytes_size() { |
| 735 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 736 |
|
| 737 |
let name = "test_split_overflow_bytes_size"; |
| 738 |
RandomFile::new(&at, name).add_bytes(1000); |
| 739 |
|
| 740 |
ucmd.args(&["-b", "1Y", name]).succeeds(); |
| 741 |
|
| 742 |
let glob = Glob::new(&at, ".", r"x[[:alpha:]][[:alpha:]]$"); |
| 743 |
assert_eq!(glob.count(), 1); |
| 744 |
assert_eq!(glob.collate(), at.read_bytes(name)); |
| 745 |
} |
| 746 |
|
| 747 |
#[test] |
| 748 |
fn test_split_stdin_num_chunks() { |
| 749 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 750 |
|
| 751 |
ucmd.args(&["--number=1"]).pipe_in("").succeeds(); |
| 752 |
|
| 753 |
assert_eq!(at.read("xaa"), ""); |
| 754 |
assert!(!at.plus("xab").exists()); |
| 755 |
} |
| 756 |
|
| 757 |
#[test] |
| 758 |
fn test_split_stdin_num_kth_chunk() { |
| 759 |
new_ucmd!() |
| 760 |
.args(&["--number=1/2"]) |
| 761 |
.pipe_in("1\n2\n3\n4\n5\n") |
| 762 |
.succeeds() |
| 763 |
.stdout_only("1\n2\n3"); |
| 764 |
} |
| 765 |
|
| 766 |
#[test] |
| 767 |
fn test_split_stdin_num_line_chunks() { |
| 768 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 769 |
ucmd.args(&["--number=l/2"]) |
| 770 |
.pipe_in("1\n2\n3\n4\n5\n") |
| 771 |
.succeeds(); |
| 772 |
assert_eq!(at.read("xaa"), "1\n2\n3\n"); |
| 773 |
assert_eq!(at.read("xab"), "4\n5\n"); |
| 774 |
assert!(!at.plus("xac").exists()); |
| 775 |
} |
| 776 |
|
| 777 |
#[test] |
| 778 |
fn test_split_stdin_num_kth_line_chunk() { |
| 779 |
new_ucmd!() |
| 780 |
.args(&["--number=l/2/5"]) |
| 781 |
.pipe_in("1\n2\n3\n4\n5\n") |
| 782 |
.succeeds() |
| 783 |
.stdout_only("2\n"); |
| 784 |
} |
| 785 |
|
| 786 |
/// Test for the default suffix length behavior: dynamically increasing size. |
| 787 |
#[test] |
| 788 |
fn test_alphabetic_dynamic_suffix_length() { |
| 789 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 790 |
// Split into chunks of one byte each. |
| 791 |
// |
| 792 |
// The input file has (26^2) - 26 + 1 = 651 bytes. This is just |
| 793 |
// enough to force `split` to dynamically increase the length of |
| 794 |
// the filename for the very last chunk. |
| 795 |
// |
| 796 |
// We expect the output files to be named |
| 797 |
// |
| 798 |
// xaa, xab, xac, ..., xyx, xyy, xyz, xzaaa |
| 799 |
// |
| 800 |
ucmd.args(&["-b", "1", "sixhundredfiftyonebytes.txt"]) |
| 801 |
.succeeds(); |
| 802 |
for i in b'a'..=b'y' { |
| 803 |
for j in b'a'..=b'z' { |
| 804 |
let filename = format!("x{}{}", i as char, j as char); |
| 805 |
let contents = at.read(&filename); |
| 806 |
assert_eq!(contents, "a"); |
| 807 |
} |
| 808 |
} |
| 809 |
assert_eq!(at.read("xzaaa"), "a"); |
| 810 |
} |
| 811 |
|
| 812 |
/// Test for the default suffix length behavior: dynamically increasing size. |
| 813 |
#[test] |
| 814 |
fn test_numeric_dynamic_suffix_length() { |
| 815 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 816 |
// Split into chunks of one byte each, use numbers instead of |
| 817 |
// letters as file suffixes. |
| 818 |
// |
| 819 |
// The input file has (10^2) - 10 + 1 = 91 bytes. This is just |
| 820 |
// enough to force `split` to dynamically increase the length of |
| 821 |
// the filename for the very last chunk. |
| 822 |
// |
| 823 |
// x00, x01, x02, ..., x87, x88, x89, x9000 |
| 824 |
// |
| 825 |
ucmd.args(&["-d", "-b", "1", "ninetyonebytes.txt"]) |
| 826 |
.succeeds(); |
| 827 |
for i in 0..90 { |
| 828 |
let filename = format!("x{i:02}"); |
| 829 |
let contents = at.read(&filename); |
| 830 |
assert_eq!(contents, "a"); |
| 831 |
} |
| 832 |
assert_eq!(at.read("x9000"), "a"); |
| 833 |
} |
| 834 |
|
| 835 |
#[test] |
| 836 |
fn test_hex_dynamic_suffix_length() { |
| 837 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 838 |
// Split into chunks of one byte each, use hexadecimal digits |
| 839 |
// instead of letters as file suffixes. |
| 840 |
// |
| 841 |
// The input file has (16^2) - 16 + 1 = 241 bytes. This is just |
| 842 |
// enough to force `split` to dynamically increase the length of |
| 843 |
// the filename for the very last chunk. |
| 844 |
// |
| 845 |
// x00, x01, x02, ..., xed, xee, xef, xf000 |
| 846 |
// |
| 847 |
ucmd.args(&["-x", "-b", "1", "twohundredfortyonebytes.txt"]) |
| 848 |
.succeeds(); |
| 849 |
for i in 0..240 { |
| 850 |
let filename = format!("x{i:02x}"); |
| 851 |
let contents = at.read(&filename); |
| 852 |
assert_eq!(contents, "a"); |
| 853 |
} |
| 854 |
assert_eq!(at.read("xf000"), "a"); |
| 855 |
} |
| 856 |
|
| 857 |
/// Test for dynamic suffix length (auto-widening) disabled when suffix start number is specified |
| 858 |
#[test] |
| 859 |
fn test_dynamic_suffix_length_off_with_suffix_start() { |
| 860 |
new_ucmd!() |
| 861 |
.args(&["-b", "1", "--numeric-suffixes=89", "ninetyonebytes.txt"]) |
| 862 |
.fails() |
| 863 |
.stderr_only("split: output file suffixes exhausted\n"); |
| 864 |
} |
| 865 |
|
| 866 |
/// Test for dynamic suffix length (auto-widening) enabled when suffix start number is NOT specified |
| 867 |
#[test] |
| 868 |
fn test_dynamic_suffix_length_on_with_suffix_start_no_value() { |
| 869 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 870 |
ucmd.args(&["-b", "1", "--numeric-suffixes", "ninetyonebytes.txt"]) |
| 871 |
.succeeds(); |
| 872 |
assert_eq!(at.read("x9000"), "a"); |
| 873 |
} |
| 874 |
|
| 875 |
/// Test for suffix auto-width with --number strategy and suffix start number |
| 876 |
#[test] |
| 877 |
fn test_suffix_auto_width_with_number() { |
| 878 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 879 |
ucmd.args(&["--numeric-suffixes=1", "--number=r/100", "fivelines.txt"]) |
| 880 |
.succeeds(); |
| 881 |
let glob = Glob::new(&at, ".", r"x\d\d\d$"); |
| 882 |
assert_eq!(glob.count(), 100); |
| 883 |
assert_eq!(glob.collate(), at.read_bytes("fivelines.txt")); |
| 884 |
assert_eq!(at.read("x001"), "1\n"); |
| 885 |
assert_eq!(at.read("x100"), ""); |
| 886 |
|
| 887 |
new_ucmd!() |
| 888 |
.args(&["--numeric-suffixes=100", "--number=r/100", "fivelines.txt"]) |
| 889 |
.fails(); |
| 890 |
} |
| 891 |
|
| 892 |
/// Test for edge case of specifying 0 for suffix length |
| 893 |
#[test] |
| 894 |
fn test_suffix_length_zero() { |
| 895 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 896 |
ucmd.args(&[ |
| 897 |
"--numeric-suffixes=1", |
| 898 |
"--number=r/100", |
| 899 |
"-a", |
| 900 |
"0", |
| 901 |
"fivelines.txt", |
| 902 |
]) |
| 903 |
.succeeds(); |
| 904 |
let glob = Glob::new(&at, ".", r"x\d\d\d$"); |
| 905 |
assert_eq!(glob.count(), 100); |
| 906 |
|
| 907 |
new_ucmd!() |
| 908 |
.args(&[ |
| 909 |
"--numeric-suffixes=100", |
| 910 |
"--number=r/100", |
| 911 |
"-a", |
| 912 |
"0", |
| 913 |
"fivelines.txt", |
| 914 |
]) |
| 915 |
.fails(); |
| 916 |
|
| 917 |
new_ucmd!() |
| 918 |
.args(&[ |
| 919 |
"-b", |
| 920 |
"1", |
| 921 |
"--numeric-suffixes=89", |
| 922 |
"-a", |
| 923 |
"0", |
| 924 |
"ninetyonebytes.txt", |
| 925 |
]) |
| 926 |
.fails() |
| 927 |
.stderr_only("split: output file suffixes exhausted\n"); |
| 928 |
} |
| 929 |
|
| 930 |
#[test] |
| 931 |
fn test_suffixes_exhausted() { |
| 932 |
new_ucmd!() |
| 933 |
.args(&["-b", "1", "-a", "1", "asciilowercase.txt"]) |
| 934 |
.fails() |
| 935 |
.stderr_only("split: output file suffixes exhausted\n"); |
| 936 |
} |
| 937 |
|
| 938 |
#[test] |
| 939 |
fn test_suffix_length_req() { |
| 940 |
new_ucmd!() |
| 941 |
.args(&["-n", "100", "-a", "1", "asciilowercase.txt"]) |
| 942 |
.fails() |
| 943 |
.stderr_only("split: the suffix length needs to be at least 2\n"); |
| 944 |
} |
| 945 |
|
| 946 |
#[test] |
| 947 |
fn test_large_suffix_length_is_rejected() { |
| 948 |
new_ucmd!() |
| 949 |
.args(&["-a", "66542562175252"]) |
| 950 |
.fails_with_code(1) |
| 951 |
.stderr_only("split: invalid suffix length: '66542562175252'\n"); |
| 952 |
} |
| 953 |
|
| 954 |
#[test] |
| 955 |
fn test_verbose() { |
| 956 |
new_ucmd!() |
| 957 |
.args(&["-b", "5", "--verbose", "asciilowercase.txt"]) |
| 958 |
.succeeds() |
| 959 |
.stdout_only( |
| 960 |
"creating file 'xaa' |
| 961 |
creating file 'xab' |
| 962 |
creating file 'xac' |
| 963 |
creating file 'xad' |
| 964 |
creating file 'xae' |
| 965 |
creating file 'xaf' |
| 966 |
", |
| 967 |
); |
| 968 |
} |
| 969 |
|
| 970 |
#[test] |
| 971 |
fn test_number_n() { |
| 972 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 973 |
ucmd.args(&["-n", "5", "asciilowercase.txt"]).succeeds(); |
| 974 |
assert_eq!(at.read("xaa"), "abcdef"); |
| 975 |
assert_eq!(at.read("xab"), "ghijkl"); |
| 976 |
assert_eq!(at.read("xac"), "mnopq"); |
| 977 |
assert_eq!(at.read("xad"), "rstuv"); |
| 978 |
assert_eq!(at.read("xae"), "wxyz\n"); |
| 979 |
#[cfg(unix)] |
| 980 |
new_ucmd!() |
| 981 |
.args(&["--number=100", "/dev/null"]) |
| 982 |
.succeeds() |
| 983 |
.stdout_only(""); |
| 984 |
} |
| 985 |
|
| 986 |
#[test] |
| 987 |
fn test_number_kth_of_n() { |
| 988 |
new_ucmd!() |
| 989 |
.args(&["--number=3/5", "asciilowercase.txt"]) |
| 990 |
.succeeds() |
| 991 |
.stdout_only("mnopq"); |
| 992 |
new_ucmd!() |
| 993 |
.args(&["--number=5/5", "asciilowercase.txt"]) |
| 994 |
.succeeds() |
| 995 |
.stdout_only("wxyz\n"); |
| 996 |
new_ucmd!() |
| 997 |
.args(&["-e", "--number=99/100", "asciilowercase.txt"]) |
| 998 |
.succeeds() |
| 999 |
.stdout_only(""); |
| 1000 |
#[cfg(unix)] |
| 1001 |
new_ucmd!() |
| 1002 |
.args(&["--number=3/10", "/dev/null"]) |
| 1003 |
.succeeds() |
| 1004 |
.stdout_only(""); |
| 1005 |
#[cfg(target_pointer_width = "64")] |
| 1006 |
new_ucmd!() |
| 1007 |
.args(&[ |
| 1008 |
"--number=r/9223372036854775807/18446744073709551615", |
| 1009 |
"asciilowercase.txt", |
| 1010 |
]) |
| 1011 |
.succeeds() |
| 1012 |
.stdout_only(""); |
| 1013 |
new_ucmd!() |
| 1014 |
.args(&["--number=0/5", "asciilowercase.txt"]) |
| 1015 |
.fails() |
| 1016 |
.stderr_contains("split: invalid chunk number: '0'"); |
| 1017 |
new_ucmd!() |
| 1018 |
.args(&["--number=10/5", "asciilowercase.txt"]) |
| 1019 |
.fails() |
| 1020 |
.stderr_contains("split: invalid chunk number: '10'"); |
| 1021 |
#[cfg(target_pointer_width = "64")] |
| 1022 |
new_ucmd!() |
| 1023 |
.args(&[ |
| 1024 |
"--number=9223372036854775807/18446744073709551616", |
| 1025 |
"asciilowercase.txt", |
| 1026 |
]) |
| 1027 |
.fails() |
| 1028 |
.stderr_contains("split: invalid number of chunks: '18446744073709551616'"); |
| 1029 |
} |
| 1030 |
|
| 1031 |
#[test] |
| 1032 |
fn test_number_kth_of_n_round_robin() { |
| 1033 |
new_ucmd!() |
| 1034 |
.args(&["--number", "r/2/3", "fivelines.txt"]) |
| 1035 |
.succeeds() |
| 1036 |
.stdout_only("2\n5\n"); |
| 1037 |
new_ucmd!() |
| 1038 |
.args(&["--number", "r/1/4", "fivelines.txt"]) |
| 1039 |
.succeeds() |
| 1040 |
.stdout_only("1\n5\n"); |
| 1041 |
new_ucmd!() |
| 1042 |
.args(&["-e", "--number", "r/7/7", "fivelines.txt"]) |
| 1043 |
.succeeds() |
| 1044 |
.stdout_only(""); |
| 1045 |
#[cfg(target_pointer_width = "64")] |
| 1046 |
new_ucmd!() |
| 1047 |
.args(&[ |
| 1048 |
"--number", |
| 1049 |
"r/9223372036854775807/18446744073709551615", |
| 1050 |
"fivelines.txt", |
| 1051 |
]) |
| 1052 |
.succeeds() |
| 1053 |
.stdout_only(""); |
| 1054 |
#[cfg(target_pointer_width = "64")] |
| 1055 |
new_ucmd!() |
| 1056 |
.args(&[ |
| 1057 |
"--number", |
| 1058 |
"r/9223372036854775807/18446744073709551616", |
| 1059 |
"fivelines.txt", |
| 1060 |
]) |
| 1061 |
.fails() |
| 1062 |
.stderr_contains("split: invalid number of chunks: '18446744073709551616'"); |
| 1063 |
new_ucmd!() |
| 1064 |
.args(&["--number", "r/0/3", "fivelines.txt"]) |
| 1065 |
.fails() |
| 1066 |
.stderr_contains("split: invalid chunk number: '0'"); |
| 1067 |
new_ucmd!() |
| 1068 |
.args(&["--number", "r/10/3", "fivelines.txt"]) |
| 1069 |
.fails() |
| 1070 |
.stderr_contains("split: invalid chunk number: '10'"); |
| 1071 |
} |
| 1072 |
|
| 1073 |
#[test] |
| 1074 |
fn test_split_number_with_io_blksize() { |
| 1075 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1076 |
ucmd.args(&["-n", "5", "asciilowercase.txt", "---io-blksize", "1024"]) |
| 1077 |
.succeeds(); |
| 1078 |
assert_eq!(at.read("xaa"), "abcdef"); |
| 1079 |
assert_eq!(at.read("xab"), "ghijkl"); |
| 1080 |
assert_eq!(at.read("xac"), "mnopq"); |
| 1081 |
assert_eq!(at.read("xad"), "rstuv"); |
| 1082 |
assert_eq!(at.read("xae"), "wxyz\n"); |
| 1083 |
} |
| 1084 |
|
| 1085 |
#[test] |
| 1086 |
fn test_split_default_with_io_blksize() { |
| 1087 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1088 |
let name = "split_default_with_io_blksize"; |
| 1089 |
RandomFile::new(&at, name).add_lines(2000); |
| 1090 |
ucmd.args(&[name, "---io-blksize", "2M"]).succeeds(); |
| 1091 |
|
| 1092 |
let glob = Glob::new(&at, ".", r"x[[:alpha:]][[:alpha:]]$"); |
| 1093 |
assert_eq!(glob.count(), 2); |
| 1094 |
assert_eq!(glob.collate(), at.read_bytes(name)); |
| 1095 |
} |
| 1096 |
|
| 1097 |
#[test] |
| 1098 |
fn test_split_invalid_io_blksize() { |
| 1099 |
new_ucmd!() |
| 1100 |
.args(&["---io-blksize=XYZ", "threebytes.txt"]) |
| 1101 |
.fails() |
| 1102 |
.stderr_only("split: invalid IO block size: 'XYZ'\n"); |
| 1103 |
new_ucmd!() |
| 1104 |
.args(&["---io-blksize=5000000000", "threebytes.txt"]) |
| 1105 |
.fails() |
| 1106 |
.stderr_only("split: invalid IO block size: '5000000000'\n"); |
| 1107 |
#[cfg(target_pointer_width = "32")] |
| 1108 |
new_ucmd!() |
| 1109 |
.args(&["---io-blksize=2146435072", "threebytes.txt"]) |
| 1110 |
.fails() |
| 1111 |
.stderr_only("split: invalid IO block size: '2146435072'\n"); |
| 1112 |
} |
| 1113 |
|
| 1114 |
#[test] |
| 1115 |
fn test_split_number_oversized_stdin() { |
| 1116 |
new_ucmd!() |
| 1117 |
.args(&["--number=3", "---io-blksize=600"]) |
| 1118 |
.pipe_in_fixture("sixhundredfiftyonebytes.txt") |
| 1119 |
.ignore_stdin_write_error() |
| 1120 |
.fails() |
| 1121 |
.stderr_only("split: -: cannot determine input size\n"); |
| 1122 |
} |
| 1123 |
|
| 1124 |
#[test] |
| 1125 |
fn test_invalid_suffix_length() { |
| 1126 |
new_ucmd!() |
| 1127 |
.args(&["-a", "xyz"]) |
| 1128 |
.fails() |
| 1129 |
.no_stdout() |
| 1130 |
.stderr_contains("invalid suffix length: 'xyz'"); |
| 1131 |
} |
| 1132 |
|
| 1133 |
/// Test short suffix length option with value concatenated |
| 1134 |
#[test] |
| 1135 |
fn test_split_suffix_length_short_concatenated_with_value() { |
| 1136 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1137 |
let name = "split_num_prefixed_chunks_by_lines"; |
| 1138 |
RandomFile::new(&at, name).add_lines(10000); |
| 1139 |
ucmd.args(&["-a4", name]).succeeds(); |
| 1140 |
|
| 1141 |
let glob = Glob::new(&at, ".", r"x[[:alpha:]][[:alpha:]][[:alpha:]][[:alpha:]]$"); |
| 1142 |
assert_eq!(glob.count(), 10); |
| 1143 |
assert_eq!(glob.collate(), at.read_bytes(name)); |
| 1144 |
} |
| 1145 |
|
| 1146 |
#[test] |
| 1147 |
fn test_include_newlines() { |
| 1148 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1149 |
ucmd.args(&["-l", "2", "fivelines.txt"]).succeeds(); |
| 1150 |
|
| 1151 |
let mut s = String::new(); |
| 1152 |
at.open("xaa").read_to_string(&mut s).unwrap(); |
| 1153 |
assert_eq!(s, "1\n2\n"); |
| 1154 |
|
| 1155 |
let mut s = String::new(); |
| 1156 |
at.open("xab").read_to_string(&mut s).unwrap(); |
| 1157 |
assert_eq!(s, "3\n4\n"); |
| 1158 |
|
| 1159 |
let mut s = String::new(); |
| 1160 |
at.open("xac").read_to_string(&mut s).unwrap(); |
| 1161 |
assert_eq!(s, "5\n"); |
| 1162 |
} |
| 1163 |
|
| 1164 |
/// Test short number of chunks option concatenated with value |
| 1165 |
#[test] |
| 1166 |
fn test_split_number_chunks_short_concatenated_with_value() { |
| 1167 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1168 |
ucmd.args(&["-n3", "threebytes.txt"]).succeeds().no_output(); |
| 1169 |
assert_eq!(at.read("xaa"), "a"); |
| 1170 |
assert_eq!(at.read("xab"), "b"); |
| 1171 |
assert_eq!(at.read("xac"), "c"); |
| 1172 |
} |
| 1173 |
|
| 1174 |
#[test] |
| 1175 |
fn test_allow_empty_files() { |
| 1176 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1177 |
ucmd.args(&["-n", "4", "threebytes.txt"]) |
| 1178 |
.succeeds() |
| 1179 |
.no_output(); |
| 1180 |
assert_eq!(at.read("xaa"), "a"); |
| 1181 |
assert_eq!(at.read("xab"), "b"); |
| 1182 |
assert_eq!(at.read("xac"), "c"); |
| 1183 |
assert_eq!(at.read("xad"), ""); |
| 1184 |
} |
| 1185 |
|
| 1186 |
#[test] |
| 1187 |
fn test_elide_empty_files_n_chunks() { |
| 1188 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1189 |
ucmd.args(&["-e", "-n", "4", "threebytes.txt"]) |
| 1190 |
.succeeds() |
| 1191 |
.no_output(); |
| 1192 |
assert_eq!(at.read("xaa"), "a"); |
| 1193 |
assert_eq!(at.read("xab"), "b"); |
| 1194 |
assert_eq!(at.read("xac"), "c"); |
| 1195 |
assert!(!at.plus("xad").exists()); |
| 1196 |
} |
| 1197 |
|
| 1198 |
#[test] |
| 1199 |
#[cfg(unix)] |
| 1200 |
fn test_elide_dev_null_n_chunks() { |
| 1201 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1202 |
ucmd.args(&["-e", "-n", "3", "/dev/null"]) |
| 1203 |
.succeeds() |
| 1204 |
.no_output(); |
| 1205 |
assert!(!at.plus("xaa").exists()); |
| 1206 |
assert!(!at.plus("xab").exists()); |
| 1207 |
assert!(!at.plus("xac").exists()); |
| 1208 |
} |
| 1209 |
|
| 1210 |
#[test] |
| 1211 |
#[cfg(unix)] |
| 1212 |
fn test_dev_zero() { |
| 1213 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1214 |
ucmd.args(&["-n", "3", "/dev/zero"]) |
| 1215 |
.fails() |
| 1216 |
.stderr_only("split: /dev/zero: cannot determine file size\n"); |
| 1217 |
assert!(!at.plus("xaa").exists()); |
| 1218 |
assert!(!at.plus("xab").exists()); |
| 1219 |
assert!(!at.plus("xac").exists()); |
| 1220 |
} |
| 1221 |
|
| 1222 |
#[test] |
| 1223 |
fn test_elide_empty_files_l_chunks() { |
| 1224 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1225 |
ucmd.args(&["-e", "-n", "l/7", "fivelines.txt"]) |
| 1226 |
.succeeds() |
| 1227 |
.no_output(); |
| 1228 |
assert_eq!(at.read("xaa"), "1\n"); |
| 1229 |
assert_eq!(at.read("xab"), "2\n"); |
| 1230 |
assert_eq!(at.read("xac"), "3\n"); |
| 1231 |
assert_eq!(at.read("xad"), "4\n"); |
| 1232 |
assert_eq!(at.read("xae"), "5\n"); |
| 1233 |
assert!(!at.plus("xaf").exists()); |
| 1234 |
assert!(!at.plus("xag").exists()); |
| 1235 |
} |
| 1236 |
|
| 1237 |
#[test] |
| 1238 |
#[cfg(unix)] |
| 1239 |
fn test_elide_dev_null_l_chunks() { |
| 1240 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1241 |
ucmd.args(&["-e", "-n", "l/3", "/dev/null"]) |
| 1242 |
.succeeds() |
| 1243 |
.no_output(); |
| 1244 |
assert!(!at.plus("xaa").exists()); |
| 1245 |
assert!(!at.plus("xab").exists()); |
| 1246 |
assert!(!at.plus("xac").exists()); |
| 1247 |
} |
| 1248 |
|
| 1249 |
#[test] |
| 1250 |
#[cfg(unix)] |
| 1251 |
fn test_number_by_bytes_dev_zero() { |
| 1252 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1253 |
ucmd.args(&["-n", "3", "/dev/zero"]) |
| 1254 |
.fails() |
| 1255 |
.stderr_only("split: /dev/zero: cannot determine file size\n"); |
| 1256 |
assert!(!at.plus("xaa").exists()); |
| 1257 |
assert!(!at.plus("xab").exists()); |
| 1258 |
assert!(!at.plus("xac").exists()); |
| 1259 |
} |
| 1260 |
|
| 1261 |
#[test] |
| 1262 |
fn test_number_by_lines() { |
| 1263 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1264 |
// Split into two files without splitting up lines. |
| 1265 |
ucmd.args(&["-n", "l/2", "fivelines.txt"]).succeeds(); |
| 1266 |
|
| 1267 |
assert_eq!(at.read("xaa"), "1\n2\n3\n"); |
| 1268 |
assert_eq!(at.read("xab"), "4\n5\n"); |
| 1269 |
} |
| 1270 |
|
| 1271 |
#[test] |
| 1272 |
fn test_number_by_lines_kth() { |
| 1273 |
new_ucmd!() |
| 1274 |
.args(&["-n", "l/3/10", "onehundredlines.txt"]) |
| 1275 |
.succeeds() |
| 1276 |
.stdout_only("20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n"); |
| 1277 |
} |
| 1278 |
|
| 1279 |
/// Chunks smaller than one byte must not loop forever. |
| 1280 |
/// |
| 1281 |
/// When the input is shorter than the requested number of chunks, the trailing |
| 1282 |
/// chunks are zero-sized. The chunk-advancing loop used to make no progress in |
| 1283 |
/// that case and spun forever at 100% CPU. |
| 1284 |
#[test] |
| 1285 |
fn test_number_by_lines_fewer_bytes_than_chunks_elide() { |
| 1286 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1287 |
at.write("in", "a"); |
| 1288 |
ucmd.args(&["-e", "-n", "l/3", "in"]).succeeds().no_output(); |
| 1289 |
assert_eq!(at.read("xaa"), "a"); |
| 1290 |
assert!(!at.plus("xab").exists()); |
| 1291 |
assert!(!at.plus("xac").exists()); |
| 1292 |
} |
| 1293 |
|
| 1294 |
/// A huge chunk count must not overflow the skipped-chunk counter. |
| 1295 |
/// |
| 1296 |
/// With far more chunks than input bytes, all of the trailing chunks are |
| 1297 |
/// zero-sized and get skipped in one go. Counting them used to overflow and |
| 1298 |
/// panic in a build with overflow checks enabled. |
| 1299 |
#[test] |
| 1300 |
fn test_number_by_lines_kth_huge_number_of_chunks() { |
| 1301 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1302 |
at.write("in", "a"); |
| 1303 |
ucmd.args(&["-n", "l/1/9999999999", "in"]) |
| 1304 |
.succeeds() |
| 1305 |
.stdout_only("a"); |
| 1306 |
} |
| 1307 |
|
| 1308 |
#[test] |
| 1309 |
#[cfg(unix)] |
| 1310 |
fn test_number_by_lines_kth_dev_null() { |
| 1311 |
new_ucmd!() |
| 1312 |
.args(&["-n", "l/3/10", "/dev/null"]) |
| 1313 |
.succeeds() |
| 1314 |
.stdout_only(""); |
| 1315 |
} |
| 1316 |
|
| 1317 |
#[test] |
| 1318 |
fn test_number_by_lines_kth_no_end_sep() { |
| 1319 |
new_ucmd!() |
| 1320 |
.args(&["-n", "l/3/10"]) |
| 1321 |
.pipe_in("1\n2222\n3\n4") |
| 1322 |
.succeeds() |
| 1323 |
.stdout_only("2222\n"); |
| 1324 |
new_ucmd!() |
| 1325 |
.args(&["-e", "-n", "l/2/2"]) |
| 1326 |
.pipe_in("1\n2222\n3\n4") |
| 1327 |
.succeeds() |
| 1328 |
.stdout_only("3\n4"); |
| 1329 |
} |
| 1330 |
|
| 1331 |
#[test] |
| 1332 |
fn test_number_by_lines_rr_kth_no_end_sep() { |
| 1333 |
new_ucmd!() |
| 1334 |
.args(&["-n", "r/2/3"]) |
| 1335 |
.pipe_in("1\n2\n3\n4\n5") |
| 1336 |
.succeeds() |
| 1337 |
.stdout_only("2\n5"); |
| 1338 |
} |
| 1339 |
|
| 1340 |
#[test] |
| 1341 |
fn test_line_bytes() { |
| 1342 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1343 |
ucmd.args(&["-C", "8", "letters.txt"]).succeeds(); |
| 1344 |
assert_eq!(at.read("xaa"), "aaaaaaaa"); |
| 1345 |
assert_eq!(at.read("xab"), "a\nbbbb\n"); |
| 1346 |
assert_eq!(at.read("xac"), "cccc\ndd\n"); |
| 1347 |
assert_eq!(at.read("xad"), "ee\n"); |
| 1348 |
} |
| 1349 |
|
| 1350 |
#[test] |
| 1351 |
#[cfg(target_pointer_width = "64")] |
| 1352 |
fn test_line_bytes_overflow() { |
| 1353 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1354 |
ucmd.args(&["-C", "18446744073709551616", "letters.txt"]) |
| 1355 |
.succeeds(); |
| 1356 |
assert_eq!(at.read("xaa"), "aaaaaaaaa\nbbbb\ncccc\ndd\nee\n"); |
| 1357 |
} |
| 1358 |
|
| 1359 |
#[test] |
| 1360 |
fn test_line_bytes_concatenated_with_value() { |
| 1361 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1362 |
ucmd.args(&["-C8", "letters.txt"]).succeeds(); |
| 1363 |
assert_eq!(at.read("xaa"), "aaaaaaaa"); |
| 1364 |
assert_eq!(at.read("xab"), "a\nbbbb\n"); |
| 1365 |
assert_eq!(at.read("xac"), "cccc\ndd\n"); |
| 1366 |
assert_eq!(at.read("xad"), "ee\n"); |
| 1367 |
} |
| 1368 |
|
| 1369 |
#[test] |
| 1370 |
fn test_line_bytes_no_final_newline() { |
| 1371 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1372 |
ucmd.args(&["-C", "2"]) |
| 1373 |
.pipe_in("1\n2222\n3\n4") |
| 1374 |
.succeeds() |
| 1375 |
.no_output(); |
| 1376 |
assert_eq!(at.read("xaa"), "1\n"); |
| 1377 |
assert_eq!(at.read("xab"), "22"); |
| 1378 |
assert_eq!(at.read("xac"), "22"); |
| 1379 |
assert_eq!(at.read("xad"), "\n"); |
| 1380 |
assert_eq!(at.read("xae"), "3\n"); |
| 1381 |
assert_eq!(at.read("xaf"), "4"); |
| 1382 |
} |
| 1383 |
|
| 1384 |
#[test] |
| 1385 |
fn test_line_bytes_no_empty_file() { |
| 1386 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1387 |
ucmd.args(&["-C", "1"]) |
| 1388 |
.pipe_in("1\n2222\n3\n4") |
| 1389 |
.succeeds() |
| 1390 |
.no_output(); |
| 1391 |
assert_eq!(at.read("xaa"), "1"); |
| 1392 |
assert_eq!(at.read("xab"), "\n"); |
| 1393 |
assert_eq!(at.read("xac"), "2"); |
| 1394 |
assert_eq!(at.read("xad"), "2"); |
| 1395 |
assert_eq!(at.read("xae"), "2"); |
| 1396 |
assert_eq!(at.read("xaf"), "2"); |
| 1397 |
assert_eq!(at.read("xag"), "\n"); |
| 1398 |
assert_eq!(at.read("xah"), "3"); |
| 1399 |
assert_eq!(at.read("xai"), "\n"); |
| 1400 |
assert_eq!(at.read("xaj"), "4"); |
| 1401 |
assert!(!at.plus("xak").exists()); |
| 1402 |
} |
| 1403 |
|
| 1404 |
#[test] |
| 1405 |
fn test_line_bytes_no_eof() { |
| 1406 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1407 |
ucmd.args(&["-C", "3"]) |
| 1408 |
.pipe_in("1\n2222\n3\n4") |
| 1409 |
.succeeds() |
| 1410 |
.no_output(); |
| 1411 |
assert_eq!(at.read("xaa"), "1\n"); |
| 1412 |
assert_eq!(at.read("xab"), "222"); |
| 1413 |
assert_eq!(at.read("xac"), "2\n"); |
| 1414 |
assert_eq!(at.read("xad"), "3\n"); |
| 1415 |
assert_eq!(at.read("xae"), "4"); |
| 1416 |
assert!(!at.plus("xaf").exists()); |
| 1417 |
} |
| 1418 |
|
| 1419 |
#[test] |
| 1420 |
fn test_guard_input() { |
| 1421 |
let scene = TestScenario::new(util_name!()); |
| 1422 |
let at = &scene.fixtures; |
| 1423 |
|
| 1424 |
scene |
| 1425 |
.ucmd() |
| 1426 |
.args(&["-C", "6"]) |
| 1427 |
.pipe_in("1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n") |
| 1428 |
.succeeds() |
| 1429 |
.no_output(); |
| 1430 |
assert_eq!(at.read("xaa"), "1\n2\n3\n"); |
| 1431 |
|
| 1432 |
scene |
| 1433 |
.ucmd() |
| 1434 |
.args(&["-C", "6"]) |
| 1435 |
.pipe_in("1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n") |
| 1436 |
.succeeds() |
| 1437 |
.no_output(); |
| 1438 |
assert_eq!(at.read("xaa"), "1\n2\n3\n"); |
| 1439 |
|
| 1440 |
scene |
| 1441 |
.ucmd() |
| 1442 |
.args(&["-C", "6", "xaa"]) |
| 1443 |
.fails() |
| 1444 |
.stderr_only("split: 'xaa' would overwrite input; aborting\n"); |
| 1445 |
assert_eq!(at.read("xaa"), "1\n2\n3\n"); |
| 1446 |
} |
| 1447 |
|
| 1448 |
#[test] |
| 1449 |
fn test_multiple_of_input_chunk() { |
| 1450 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1451 |
let name = "multiple_of_input_chunk"; |
| 1452 |
RandomFile::new(&at, name).add_bytes(16 * 1024); |
| 1453 |
ucmd.args(&["-b", "8K", name, "b"]).succeeds(); |
| 1454 |
|
| 1455 |
let glob = Glob::new(&at, ".", r"b[[:alpha:]][[:alpha:]]$"); |
| 1456 |
assert_eq!(glob.count(), 2); |
| 1457 |
for filename in glob.collect() { |
| 1458 |
assert_eq!(glob.directory.metadata(&filename).len(), 8 * 1024); |
| 1459 |
} |
| 1460 |
assert_eq!(glob.collate(), at.read_bytes(name)); |
| 1461 |
} |
| 1462 |
|
| 1463 |
#[test] |
| 1464 |
fn test_numeric_suffix() { |
| 1465 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1466 |
ucmd.args(&["-n", "4", "--numeric-suffixes=9", "threebytes.txt"]) |
| 1467 |
.succeeds() |
| 1468 |
.no_output(); |
| 1469 |
assert_eq!(at.read("x09"), "a"); |
| 1470 |
assert_eq!(at.read("x10"), "b"); |
| 1471 |
assert_eq!(at.read("x11"), "c"); |
| 1472 |
assert_eq!(at.read("x12"), ""); |
| 1473 |
} |
| 1474 |
|
| 1475 |
#[test] |
| 1476 |
fn test_numeric_suffix_inferred() { |
| 1477 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1478 |
ucmd.args(&["-n", "4", "--numeric=9", "threebytes.txt"]) |
| 1479 |
.succeeds() |
| 1480 |
.no_output(); |
| 1481 |
assert_eq!(at.read("x09"), "a"); |
| 1482 |
assert_eq!(at.read("x10"), "b"); |
| 1483 |
assert_eq!(at.read("x11"), "c"); |
| 1484 |
assert_eq!(at.read("x12"), ""); |
| 1485 |
} |
| 1486 |
|
| 1487 |
#[test] |
| 1488 |
fn test_hex_suffix() { |
| 1489 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1490 |
ucmd.args(&["-n", "4", "--hex-suffixes=9", "threebytes.txt"]) |
| 1491 |
.succeeds() |
| 1492 |
.no_output(); |
| 1493 |
assert_eq!(at.read("x09"), "a"); |
| 1494 |
assert_eq!(at.read("x0a"), "b"); |
| 1495 |
assert_eq!(at.read("x0b"), "c"); |
| 1496 |
assert_eq!(at.read("x0c"), ""); |
| 1497 |
} |
| 1498 |
|
| 1499 |
#[test] |
| 1500 |
fn test_hex_suffix_alias() { |
| 1501 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1502 |
ucmd.args(&["-n", "4", "--hex=9", "threebytes.txt"]) |
| 1503 |
.succeeds() |
| 1504 |
.no_output(); |
| 1505 |
assert_eq!(at.read("x09"), "a"); |
| 1506 |
assert_eq!(at.read("x0a"), "b"); |
| 1507 |
assert_eq!(at.read("x0b"), "c"); |
| 1508 |
assert_eq!(at.read("x0c"), ""); |
| 1509 |
} |
| 1510 |
|
| 1511 |
#[test] |
| 1512 |
fn test_numeric_suffix_no_equal() { |
| 1513 |
new_ucmd!() |
| 1514 |
.args(&["-n", "4", "--numeric-suffixes", "9", "threebytes.txt"]) |
| 1515 |
.fails() |
| 1516 |
.stderr_contains("split: cannot open '9' for reading: No such file or directory"); |
| 1517 |
} |
| 1518 |
|
| 1519 |
#[test] |
| 1520 |
fn test_hex_suffix_no_equal() { |
| 1521 |
new_ucmd!() |
| 1522 |
.args(&["-n", "4", "--hex-suffixes", "9", "threebytes.txt"]) |
| 1523 |
.fails() |
| 1524 |
.stderr_contains("split: cannot open '9' for reading: No such file or directory"); |
| 1525 |
} |
| 1526 |
|
| 1527 |
/// Test for short numeric suffix not having any value |
| 1528 |
#[test] |
| 1529 |
fn test_short_numeric_suffix_no_value() { |
| 1530 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1531 |
ucmd.args(&["-l", "9", "-d", "onehundredlines.txt"]) |
| 1532 |
.succeeds() |
| 1533 |
.no_output(); |
| 1534 |
assert_eq!(at.read("x00"), "00\n01\n02\n03\n04\n05\n06\n07\n08\n"); |
| 1535 |
assert_eq!(at.read("x01"), "09\n10\n11\n12\n13\n14\n15\n16\n17\n"); |
| 1536 |
assert_eq!(at.read("x02"), "18\n19\n20\n21\n22\n23\n24\n25\n26\n"); |
| 1537 |
assert_eq!(at.read("x03"), "27\n28\n29\n30\n31\n32\n33\n34\n35\n"); |
| 1538 |
assert_eq!(at.read("x04"), "36\n37\n38\n39\n40\n41\n42\n43\n44\n"); |
| 1539 |
assert_eq!(at.read("x05"), "45\n46\n47\n48\n49\n50\n51\n52\n53\n"); |
| 1540 |
assert_eq!(at.read("x06"), "54\n55\n56\n57\n58\n59\n60\n61\n62\n"); |
| 1541 |
assert_eq!(at.read("x07"), "63\n64\n65\n66\n67\n68\n69\n70\n71\n"); |
| 1542 |
assert_eq!(at.read("x08"), "72\n73\n74\n75\n76\n77\n78\n79\n80\n"); |
| 1543 |
assert_eq!(at.read("x09"), "81\n82\n83\n84\n85\n86\n87\n88\n89\n"); |
| 1544 |
assert_eq!(at.read("x10"), "90\n91\n92\n93\n94\n95\n96\n97\n98\n"); |
| 1545 |
assert_eq!(at.read("x11"), "99\n"); |
| 1546 |
} |
| 1547 |
|
| 1548 |
/// Test for long numeric suffix not having any value |
| 1549 |
#[test] |
| 1550 |
fn test_numeric_suffix_no_value() { |
| 1551 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1552 |
ucmd.args(&["-l", "9", "--numeric-suffixes", "onehundredlines.txt"]) |
| 1553 |
.succeeds() |
| 1554 |
.no_output(); |
| 1555 |
assert_eq!(at.read("x00"), "00\n01\n02\n03\n04\n05\n06\n07\n08\n"); |
| 1556 |
assert_eq!(at.read("x01"), "09\n10\n11\n12\n13\n14\n15\n16\n17\n"); |
| 1557 |
assert_eq!(at.read("x02"), "18\n19\n20\n21\n22\n23\n24\n25\n26\n"); |
| 1558 |
assert_eq!(at.read("x03"), "27\n28\n29\n30\n31\n32\n33\n34\n35\n"); |
| 1559 |
assert_eq!(at.read("x04"), "36\n37\n38\n39\n40\n41\n42\n43\n44\n"); |
| 1560 |
assert_eq!(at.read("x05"), "45\n46\n47\n48\n49\n50\n51\n52\n53\n"); |
| 1561 |
assert_eq!(at.read("x06"), "54\n55\n56\n57\n58\n59\n60\n61\n62\n"); |
| 1562 |
assert_eq!(at.read("x07"), "63\n64\n65\n66\n67\n68\n69\n70\n71\n"); |
| 1563 |
assert_eq!(at.read("x08"), "72\n73\n74\n75\n76\n77\n78\n79\n80\n"); |
| 1564 |
assert_eq!(at.read("x09"), "81\n82\n83\n84\n85\n86\n87\n88\n89\n"); |
| 1565 |
assert_eq!(at.read("x10"), "90\n91\n92\n93\n94\n95\n96\n97\n98\n"); |
| 1566 |
assert_eq!(at.read("x11"), "99\n"); |
| 1567 |
} |
| 1568 |
|
| 1569 |
/// Test for short hex suffix not having any value |
| 1570 |
#[test] |
| 1571 |
fn test_short_hex_suffix_no_value() { |
| 1572 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1573 |
ucmd.args(&["-l", "9", "-x", "onehundredlines.txt"]) |
| 1574 |
.succeeds() |
| 1575 |
.no_output(); |
| 1576 |
assert_eq!(at.read("x00"), "00\n01\n02\n03\n04\n05\n06\n07\n08\n"); |
| 1577 |
assert_eq!(at.read("x01"), "09\n10\n11\n12\n13\n14\n15\n16\n17\n"); |
| 1578 |
assert_eq!(at.read("x02"), "18\n19\n20\n21\n22\n23\n24\n25\n26\n"); |
| 1579 |
assert_eq!(at.read("x03"), "27\n28\n29\n30\n31\n32\n33\n34\n35\n"); |
| 1580 |
assert_eq!(at.read("x04"), "36\n37\n38\n39\n40\n41\n42\n43\n44\n"); |
| 1581 |
assert_eq!(at.read("x05"), "45\n46\n47\n48\n49\n50\n51\n52\n53\n"); |
| 1582 |
assert_eq!(at.read("x06"), "54\n55\n56\n57\n58\n59\n60\n61\n62\n"); |
| 1583 |
assert_eq!(at.read("x07"), "63\n64\n65\n66\n67\n68\n69\n70\n71\n"); |
| 1584 |
assert_eq!(at.read("x08"), "72\n73\n74\n75\n76\n77\n78\n79\n80\n"); |
| 1585 |
assert_eq!(at.read("x09"), "81\n82\n83\n84\n85\n86\n87\n88\n89\n"); |
| 1586 |
assert_eq!(at.read("x0a"), "90\n91\n92\n93\n94\n95\n96\n97\n98\n"); |
| 1587 |
assert_eq!(at.read("x0b"), "99\n"); |
| 1588 |
} |
| 1589 |
|
| 1590 |
/// Test for long hex suffix not having any value |
| 1591 |
#[test] |
| 1592 |
fn test_hex_suffix_no_value() { |
| 1593 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1594 |
ucmd.args(&["-l", "9", "--hex-suffixes", "onehundredlines.txt"]) |
| 1595 |
.succeeds() |
| 1596 |
.no_output(); |
| 1597 |
assert_eq!(at.read("x00"), "00\n01\n02\n03\n04\n05\n06\n07\n08\n"); |
| 1598 |
assert_eq!(at.read("x01"), "09\n10\n11\n12\n13\n14\n15\n16\n17\n"); |
| 1599 |
assert_eq!(at.read("x02"), "18\n19\n20\n21\n22\n23\n24\n25\n26\n"); |
| 1600 |
assert_eq!(at.read("x03"), "27\n28\n29\n30\n31\n32\n33\n34\n35\n"); |
| 1601 |
assert_eq!(at.read("x04"), "36\n37\n38\n39\n40\n41\n42\n43\n44\n"); |
| 1602 |
assert_eq!(at.read("x05"), "45\n46\n47\n48\n49\n50\n51\n52\n53\n"); |
| 1603 |
assert_eq!(at.read("x06"), "54\n55\n56\n57\n58\n59\n60\n61\n62\n"); |
| 1604 |
assert_eq!(at.read("x07"), "63\n64\n65\n66\n67\n68\n69\n70\n71\n"); |
| 1605 |
assert_eq!(at.read("x08"), "72\n73\n74\n75\n76\n77\n78\n79\n80\n"); |
| 1606 |
assert_eq!(at.read("x09"), "81\n82\n83\n84\n85\n86\n87\n88\n89\n"); |
| 1607 |
assert_eq!(at.read("x0a"), "90\n91\n92\n93\n94\n95\n96\n97\n98\n"); |
| 1608 |
assert_eq!(at.read("x0b"), "99\n"); |
| 1609 |
} |
| 1610 |
|
| 1611 |
/// Test for short numeric suffix having value provided after space - should fail |
| 1612 |
#[test] |
| 1613 |
fn test_short_numeric_suffix_with_value_spaced() { |
| 1614 |
new_ucmd!() |
| 1615 |
.args(&["-n", "4", "-d", "9", "threebytes.txt"]) |
| 1616 |
.fails() |
| 1617 |
.stderr_contains("split: cannot open '9' for reading: No such file or directory"); |
| 1618 |
} |
| 1619 |
|
| 1620 |
/// Test for short numeric suffix having value provided after space - should fail |
| 1621 |
#[test] |
| 1622 |
fn test_short_hex_suffix_with_value_spaced() { |
| 1623 |
new_ucmd!() |
| 1624 |
.args(&["-n", "4", "-x", "9", "threebytes.txt"]) |
| 1625 |
.fails() |
| 1626 |
.stderr_contains("split: cannot open '9' for reading: No such file or directory"); |
| 1627 |
} |
| 1628 |
|
| 1629 |
/// Test for some combined short options |
| 1630 |
#[test] |
| 1631 |
fn test_short_combination() { |
| 1632 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1633 |
ucmd.args(&["-dxen", "4", "threebytes.txt"]) |
| 1634 |
.succeeds() |
| 1635 |
.no_output(); |
| 1636 |
assert_eq!(at.read("x00"), "a"); |
| 1637 |
assert_eq!(at.read("x01"), "b"); |
| 1638 |
assert_eq!(at.read("x02"), "c"); |
| 1639 |
assert!(!at.file_exists("x03")); |
| 1640 |
} |
| 1641 |
|
| 1642 |
/// Test for the last effective suffix, ignoring all others - numeric long last |
| 1643 |
/// Any combination of short and long (as well as duplicates) should be allowed |
| 1644 |
#[test] |
| 1645 |
fn test_effective_suffix_numeric_last() { |
| 1646 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1647 |
ucmd.args(&[ |
| 1648 |
"-n", |
| 1649 |
"4", |
| 1650 |
"--numeric-suffixes=7", |
| 1651 |
"--hex-suffixes=4", |
| 1652 |
"-d", |
| 1653 |
"-x", |
| 1654 |
"--numeric-suffixes=9", |
| 1655 |
"threebytes.txt", |
| 1656 |
]) |
| 1657 |
.succeeds() |
| 1658 |
.no_output(); |
| 1659 |
assert_eq!(at.read("x09"), "a"); |
| 1660 |
assert_eq!(at.read("x10"), "b"); |
| 1661 |
assert_eq!(at.read("x11"), "c"); |
| 1662 |
assert_eq!(at.read("x12"), ""); |
| 1663 |
} |
| 1664 |
|
| 1665 |
/// Test for the last effective suffix, ignoring all others - hex long last |
| 1666 |
/// Any combination of short and long (as well as duplicates) should be allowed |
| 1667 |
#[test] |
| 1668 |
fn test_effective_suffix_hex_last() { |
| 1669 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1670 |
ucmd.args(&[ |
| 1671 |
"-n", |
| 1672 |
"4", |
| 1673 |
"--hex-suffixes=7", |
| 1674 |
"--numeric-suffixes=4", |
| 1675 |
"-x", |
| 1676 |
"-d", |
| 1677 |
"--hex-suffixes=9", |
| 1678 |
"threebytes.txt", |
| 1679 |
]) |
| 1680 |
.succeeds() |
| 1681 |
.no_output(); |
| 1682 |
assert_eq!(at.read("x09"), "a"); |
| 1683 |
assert_eq!(at.read("x0a"), "b"); |
| 1684 |
assert_eq!(at.read("x0b"), "c"); |
| 1685 |
assert_eq!(at.read("x0c"), ""); |
| 1686 |
} |
| 1687 |
|
| 1688 |
#[test] |
| 1689 |
fn test_round_robin() { |
| 1690 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1691 |
ucmd.args(&["-n", "r/2", "fivelines.txt"]).succeeds(); |
| 1692 |
|
| 1693 |
assert_eq!(at.read("xaa"), "1\n3\n5\n"); |
| 1694 |
assert_eq!(at.read("xab"), "2\n4\n"); |
| 1695 |
} |
| 1696 |
|
| 1697 |
#[test] |
| 1698 |
// TODO(#7542): Re-enable on Android once we figure out why rlimit is broken. |
| 1699 |
// #[cfg(any(target_os = "linux", target_os = "android"))] |
| 1700 |
#[cfg(target_os = "linux")] |
| 1701 |
fn test_round_robin_limited_file_descriptors() { |
| 1702 |
new_ucmd!() |
| 1703 |
.args(&["-n", "r/40", "onehundredlines.txt"]) |
| 1704 |
.limit(Resource::NOFILE, 9, 9) |
| 1705 |
.succeeds(); |
| 1706 |
} |
| 1707 |
|
| 1708 |
#[test] |
| 1709 |
fn test_split_invalid_input() { |
| 1710 |
// Test if stdout/stderr for '--lines' option is correct |
| 1711 |
let scene = TestScenario::new(util_name!()); |
| 1712 |
let at = &scene.fixtures; |
| 1713 |
at.touch("file"); |
| 1714 |
|
| 1715 |
scene |
| 1716 |
.ucmd() |
| 1717 |
.args(&["--lines", "0", "file"]) |
| 1718 |
.fails() |
| 1719 |
.no_stdout() |
| 1720 |
.stderr_contains("split: invalid number of lines: 0"); |
| 1721 |
scene |
| 1722 |
.ucmd() |
| 1723 |
.args(&["-C", "0", "file"]) |
| 1724 |
.fails() |
| 1725 |
.no_stdout() |
| 1726 |
.stderr_contains("split: invalid number of bytes: 0"); |
| 1727 |
scene |
| 1728 |
.ucmd() |
| 1729 |
.args(&["-b", "0", "file"]) |
| 1730 |
.fails() |
| 1731 |
.no_stdout() |
| 1732 |
.stderr_contains("split: invalid number of bytes: 0"); |
| 1733 |
scene |
| 1734 |
.ucmd() |
| 1735 |
.args(&["-n", "0", "file"]) |
| 1736 |
.fails() |
| 1737 |
.no_stdout() |
| 1738 |
.stderr_contains("split: invalid number of chunks: '0'"); |
| 1739 |
} |
| 1740 |
|
| 1741 |
/// Test if there are invalid (non UTF-8) in the arguments - unix |
| 1742 |
/// clap is expected to fail/panic |
| 1743 |
#[test] |
| 1744 |
#[cfg(target_os = "linux")] |
| 1745 |
fn test_split_non_utf8_argument_unix() { |
| 1746 |
use std::ffi::OsStr; |
| 1747 |
use std::os::unix::ffi::OsStrExt; |
| 1748 |
|
| 1749 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1750 |
let name = "test_split_non_utf8_argument"; |
| 1751 |
let opt = OsStr::from_bytes("--additional-suffix".as_bytes()); |
| 1752 |
RandomFile::new(&at, name).add_lines(2000); |
| 1753 |
// Here, the values 0x66 and 0x6f correspond to 'f' and 'o' |
| 1754 |
// respectively. The value 0x80 is a lone continuation byte, invalid |
| 1755 |
// in a UTF-8 sequence. |
| 1756 |
let opt_value = [0x66, 0x6f, 0x80, 0x6f]; |
| 1757 |
let opt_value = OsStr::from_bytes(&opt_value[..]); |
| 1758 |
let name = OsStr::from_bytes(name.as_bytes()); |
| 1759 |
ucmd.args(&[opt, opt_value, name]).succeeds(); |
| 1760 |
} |
| 1761 |
|
| 1762 |
/// Test if there are invalid (non UTF-8) in the arguments - windows |
| 1763 |
/// clap is expected to fail/panic |
| 1764 |
#[test] |
| 1765 |
#[cfg(windows)] |
| 1766 |
fn test_split_non_utf8_argument_windows() { |
| 1767 |
use std::ffi::OsString; |
| 1768 |
use std::os::windows::ffi::OsStringExt; |
| 1769 |
|
| 1770 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1771 |
let name = "test_split_non_utf8_argument"; |
| 1772 |
let opt = OsString::from("--additional-suffix"); |
| 1773 |
RandomFile::new(&at, name).add_lines(2000); |
| 1774 |
// Here the values 0x0066 and 0x006f correspond to 'f' and 'o' |
| 1775 |
// respectively. The value 0xD800 is a lone surrogate half, invalid |
| 1776 |
// in a UTF-16 sequence. |
| 1777 |
let opt_value = [0x0066, 0x006f, 0xD800, 0x006f]; |
| 1778 |
let opt_value = OsString::from_wide(&opt_value[..]); |
| 1779 |
let name = OsString::from(name); |
| 1780 |
ucmd.args(&[opt, opt_value, name]).succeeds(); |
| 1781 |
} |
| 1782 |
|
| 1783 |
// Test '--separator' / '-t' option |
| 1784 |
// test separators: '\n' , '\0' , ';' |
| 1785 |
// test with '--lines=2' , '--line-bytes=4' , '--number=l/3' , '--number=r/3' , '--number=l/1/3' , '--number=r/1/3' |
| 1786 |
#[test] |
| 1787 |
fn test_split_separator_nl_lines() { |
| 1788 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1789 |
ucmd.args(&["--lines=2", "-t", "\n"]) |
| 1790 |
.pipe_in("1\n2\n3\n4\n5\n") |
| 1791 |
.succeeds(); |
| 1792 |
|
| 1793 |
assert_eq!(at.read("xaa"), "1\n2\n"); |
| 1794 |
assert_eq!(at.read("xab"), "3\n4\n"); |
| 1795 |
assert_eq!(at.read("xac"), "5\n"); |
| 1796 |
assert!(!at.plus("xad").exists()); |
| 1797 |
} |
| 1798 |
|
| 1799 |
#[test] |
| 1800 |
fn test_split_separator_nl_line_bytes() { |
| 1801 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1802 |
ucmd.args(&["--line-bytes=4", "-t", "\n"]) |
| 1803 |
.pipe_in("1\n2\n3\n4\n5\n") |
| 1804 |
.succeeds(); |
| 1805 |
|
| 1806 |
assert_eq!(at.read("xaa"), "1\n2\n"); |
| 1807 |
assert_eq!(at.read("xab"), "3\n4\n"); |
| 1808 |
assert_eq!(at.read("xac"), "5\n"); |
| 1809 |
assert!(!at.plus("xad").exists()); |
| 1810 |
} |
| 1811 |
|
| 1812 |
#[test] |
| 1813 |
fn test_split_separator_nl_number_l() { |
| 1814 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1815 |
ucmd.args(&["--number=l/3", "--separator=\n", "fivelines.txt"]) |
| 1816 |
.succeeds(); |
| 1817 |
|
| 1818 |
assert_eq!(at.read("xaa"), "1\n2\n"); |
| 1819 |
assert_eq!(at.read("xab"), "3\n4\n"); |
| 1820 |
assert_eq!(at.read("xac"), "5\n"); |
| 1821 |
assert!(!at.plus("xad").exists()); |
| 1822 |
} |
| 1823 |
|
| 1824 |
#[test] |
| 1825 |
fn test_split_separator_nl_number_r() { |
| 1826 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1827 |
ucmd.args(&["--number=r/3", "--separator", "\n", "fivelines.txt"]) |
| 1828 |
.succeeds(); |
| 1829 |
|
| 1830 |
assert_eq!(at.read("xaa"), "1\n4\n"); |
| 1831 |
assert_eq!(at.read("xab"), "2\n5\n"); |
| 1832 |
assert_eq!(at.read("xac"), "3\n"); |
| 1833 |
assert!(!at.plus("xad").exists()); |
| 1834 |
} |
| 1835 |
|
| 1836 |
#[test] |
| 1837 |
fn test_split_separator_nul_lines() { |
| 1838 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1839 |
ucmd.args(&["--lines=2", "-t", "\\0", "separator_nul.txt"]) |
| 1840 |
.succeeds(); |
| 1841 |
|
| 1842 |
assert_eq!(at.read("xaa"), "1\x002\0"); |
| 1843 |
assert_eq!(at.read("xab"), "3\x004\0"); |
| 1844 |
assert_eq!(at.read("xac"), "5\0"); |
| 1845 |
assert!(!at.plus("xad").exists()); |
| 1846 |
} |
| 1847 |
|
| 1848 |
#[test] |
| 1849 |
fn test_split_separator_nul_line_bytes() { |
| 1850 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1851 |
ucmd.args(&["--line-bytes=4", "-t", "\\0", "separator_nul.txt"]) |
| 1852 |
.succeeds(); |
| 1853 |
|
| 1854 |
assert_eq!(at.read("xaa"), "1\x002\0"); |
| 1855 |
assert_eq!(at.read("xab"), "3\x004\0"); |
| 1856 |
assert_eq!(at.read("xac"), "5\0"); |
| 1857 |
assert!(!at.plus("xad").exists()); |
| 1858 |
} |
| 1859 |
|
| 1860 |
#[test] |
| 1861 |
fn test_split_separator_nul_number_l() { |
| 1862 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1863 |
ucmd.args(&["--number=l/3", "--separator=\\0", "separator_nul.txt"]) |
| 1864 |
.succeeds(); |
| 1865 |
|
| 1866 |
assert_eq!(at.read("xaa"), "1\x002\0"); |
| 1867 |
assert_eq!(at.read("xab"), "3\x004\0"); |
| 1868 |
assert_eq!(at.read("xac"), "5\0"); |
| 1869 |
assert!(!at.plus("xad").exists()); |
| 1870 |
} |
| 1871 |
|
| 1872 |
#[test] |
| 1873 |
fn test_split_separator_nul_number_r() { |
| 1874 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1875 |
ucmd.args(&["--number=r/3", "--separator=\\0", "separator_nul.txt"]) |
| 1876 |
.succeeds(); |
| 1877 |
|
| 1878 |
assert_eq!(at.read("xaa"), "1\x004\0"); |
| 1879 |
assert_eq!(at.read("xab"), "2\x005\0"); |
| 1880 |
assert_eq!(at.read("xac"), "3\0"); |
| 1881 |
assert!(!at.plus("xad").exists()); |
| 1882 |
} |
| 1883 |
|
| 1884 |
#[test] |
| 1885 |
fn test_split_separator_semicolon_lines() { |
| 1886 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1887 |
ucmd.args(&["--lines=2", "-t", ";", "separator_semicolon.txt"]) |
| 1888 |
.succeeds(); |
| 1889 |
|
| 1890 |
assert_eq!(at.read("xaa"), "1;2;"); |
| 1891 |
assert_eq!(at.read("xab"), "3;4;"); |
| 1892 |
assert_eq!(at.read("xac"), "5;"); |
| 1893 |
assert!(!at.plus("xad").exists()); |
| 1894 |
} |
| 1895 |
|
| 1896 |
#[test] |
| 1897 |
fn test_split_separator_semicolon_line_bytes() { |
| 1898 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1899 |
ucmd.args(&["--line-bytes=4", "-t", ";", "separator_semicolon.txt"]) |
| 1900 |
.succeeds(); |
| 1901 |
|
| 1902 |
assert_eq!(at.read("xaa"), "1;2;"); |
| 1903 |
assert_eq!(at.read("xab"), "3;4;"); |
| 1904 |
assert_eq!(at.read("xac"), "5;"); |
| 1905 |
assert!(!at.plus("xad").exists()); |
| 1906 |
} |
| 1907 |
|
| 1908 |
#[test] |
| 1909 |
fn test_split_separator_semicolon_number_l() { |
| 1910 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1911 |
ucmd.args(&["--number=l/3", "--separator=;", "separator_semicolon.txt"]) |
| 1912 |
.succeeds(); |
| 1913 |
|
| 1914 |
assert_eq!(at.read("xaa"), "1;2;"); |
| 1915 |
assert_eq!(at.read("xab"), "3;4;"); |
| 1916 |
assert_eq!(at.read("xac"), "5;"); |
| 1917 |
assert!(!at.plus("xad").exists()); |
| 1918 |
} |
| 1919 |
|
| 1920 |
#[test] |
| 1921 |
fn test_split_separator_semicolon_number_r() { |
| 1922 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1923 |
ucmd.args(&["--number=r/3", "--separator=;", "separator_semicolon.txt"]) |
| 1924 |
.succeeds(); |
| 1925 |
|
| 1926 |
assert_eq!(at.read("xaa"), "1;4;"); |
| 1927 |
assert_eq!(at.read("xab"), "2;5;"); |
| 1928 |
assert_eq!(at.read("xac"), "3;"); |
| 1929 |
assert!(!at.plus("xad").exists()); |
| 1930 |
} |
| 1931 |
|
| 1932 |
#[test] |
| 1933 |
fn test_split_separator_semicolon_number_kth_l() { |
| 1934 |
new_ucmd!() |
| 1935 |
.args(&[ |
| 1936 |
"--number=l/1/3", |
| 1937 |
"--separator", |
| 1938 |
";", |
| 1939 |
"separator_semicolon.txt", |
| 1940 |
]) |
| 1941 |
.succeeds() |
| 1942 |
.stdout_only("1;2;"); |
| 1943 |
} |
| 1944 |
|
| 1945 |
#[test] |
| 1946 |
fn test_split_separator_semicolon_number_kth_r() { |
| 1947 |
new_ucmd!() |
| 1948 |
.args(&[ |
| 1949 |
"--number=r/1/3", |
| 1950 |
"--separator", |
| 1951 |
";", |
| 1952 |
"separator_semicolon.txt", |
| 1953 |
]) |
| 1954 |
.succeeds() |
| 1955 |
.stdout_only("1;4;"); |
| 1956 |
} |
| 1957 |
|
| 1958 |
// Test error edge cases for separator option |
| 1959 |
#[test] |
| 1960 |
fn test_split_separator_no_value() { |
| 1961 |
new_ucmd!() |
| 1962 |
.args(&["-t"]) |
| 1963 |
.ignore_stdin_write_error() |
| 1964 |
.pipe_in("a\n") |
| 1965 |
.fails() |
| 1966 |
.stderr_contains("error: a value is required for '--separator <SEP>' but none was supplied") |
| 1967 |
.stderr_contains("For more information, try '--help'."); |
| 1968 |
} |
| 1969 |
|
| 1970 |
#[test] |
| 1971 |
fn test_split_separator_invalid_usage() { |
| 1972 |
let scene = TestScenario::new(util_name!()); |
| 1973 |
scene |
| 1974 |
.ucmd() |
| 1975 |
.args(&["--separator=xx"]) |
| 1976 |
.ignore_stdin_write_error() |
| 1977 |
.pipe_in("a\n") |
| 1978 |
.fails() |
| 1979 |
.no_stdout() |
| 1980 |
.stderr_contains("split: multi-character separator 'xx'"); |
| 1981 |
scene |
| 1982 |
.ucmd() |
| 1983 |
.args(&["-ta", "-tb"]) |
| 1984 |
.ignore_stdin_write_error() |
| 1985 |
.pipe_in("a\n") |
| 1986 |
.fails() |
| 1987 |
.no_stdout() |
| 1988 |
.stderr_contains("split: multiple separator characters specified"); |
| 1989 |
scene |
| 1990 |
.ucmd() |
| 1991 |
.args(&["-t'\n'", "-tb"]) |
| 1992 |
.ignore_stdin_write_error() |
| 1993 |
.pipe_in("a\n") |
| 1994 |
.fails() |
| 1995 |
.no_stdout() |
| 1996 |
.stderr_contains("split: multiple separator characters specified"); |
| 1997 |
} |
| 1998 |
|
| 1999 |
// Test using same separator multiple times |
| 2000 |
#[test] |
| 2001 |
fn test_split_separator_same_multiple() { |
| 2002 |
let scene = TestScenario::new(util_name!()); |
| 2003 |
scene |
| 2004 |
.ucmd() |
| 2005 |
.args(&["--separator=:", "--separator=:", "fivelines.txt"]) |
| 2006 |
.succeeds(); |
| 2007 |
scene |
| 2008 |
.ucmd() |
| 2009 |
.args(&["-t:", "--separator=:", "fivelines.txt"]) |
| 2010 |
.succeeds(); |
| 2011 |
scene |
| 2012 |
.ucmd() |
| 2013 |
.args(&["-t", ":", "-t", ":", "fivelines.txt"]) |
| 2014 |
.succeeds(); |
| 2015 |
scene |
| 2016 |
.ucmd() |
| 2017 |
.args(&["-t:", "-t:", "-t,", "fivelines.txt"]) |
| 2018 |
.fails(); |
| 2019 |
} |
| 2020 |
|
| 2021 |
#[test] |
| 2022 |
fn test_long_lines() { |
| 2023 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 2024 |
let line1 = [" ".repeat(131_070), String::from("\n")].concat(); |
| 2025 |
let line2 = [" ", "\n"].concat(); |
| 2026 |
let line3 = [" ".repeat(131_071), String::from("\n")].concat(); |
| 2027 |
let infile = [line1, line2, line3].concat(); |
| 2028 |
ucmd.args(&["-C", "131072"]) |
| 2029 |
.pipe_in(infile) |
| 2030 |
.succeeds() |
| 2031 |
.no_output(); |
| 2032 |
assert_eq!(at.read("xaa").len(), 131_071); |
| 2033 |
assert_eq!(at.read("xab").len(), 2); |
| 2034 |
assert_eq!(at.read("xac").len(), 131_072); |
| 2035 |
assert!(!at.plus("xad").exists()); |
| 2036 |
} |
| 2037 |
|
| 2038 |
#[test] |
| 2039 |
#[cfg(target_os = "linux")] |
| 2040 |
fn test_split_non_utf8_paths() { |
| 2041 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 2042 |
|
| 2043 |
let filename = std::ffi::OsString::from_vec(vec![0xFF, 0xFE]); |
| 2044 |
fs::write(at.plus(&filename), b"line1\nline2\nline3\nline4\nline5\n").unwrap(); |
| 2045 |
|
| 2046 |
ucmd.arg(&filename).succeeds(); |
| 2047 |
|
| 2048 |
// Check that at least one split file was created |
| 2049 |
assert!(at.plus("xaa").exists()); |
| 2050 |
} |
| 2051 |
|
| 2052 |
#[test] |
| 2053 |
#[cfg(target_os = "linux")] |
| 2054 |
fn test_split_non_utf8_prefix_is_byte_preserving() { |
| 2055 |
use std::ffi::OsStr; |
| 2056 |
use std::os::unix::ffi::{OsStrExt, OsStringExt}; |
| 2057 |
|
| 2058 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 2059 |
at.write("input.txt", "AB"); |
| 2060 |
|
| 2061 |
let invalid_prefix_bytes = b"p\xFF"; |
| 2062 |
at.write("p�aa", "keep-aa"); |
| 2063 |
at.write("p�ab", "keep-ab"); |
| 2064 |
|
| 2065 |
ucmd.args(&["-b", "1", "input.txt"]) |
| 2066 |
.arg(OsStr::from_bytes(invalid_prefix_bytes)) |
| 2067 |
.succeeds(); |
| 2068 |
|
| 2069 |
let mut produced_split_files: Vec<Vec<u8>> = fs::read_dir(at.as_string()) |
| 2070 |
.expect("temporary split directory should be readable") |
| 2071 |
.filter_map(|entry| { |
| 2072 |
let entry = entry.ok()?; |
| 2073 |
let name = entry.file_name().into_vec(); |
| 2074 |
if name.starts_with(invalid_prefix_bytes) { |
| 2075 |
Some(name) |
| 2076 |
} else { |
| 2077 |
None |
| 2078 |
} |
| 2079 |
}) |
| 2080 |
.collect(); |
| 2081 |
produced_split_files.sort(); |
| 2082 |
|
| 2083 |
assert_eq!( |
| 2084 |
produced_split_files, |
| 2085 |
vec![b"p\xFFaa".to_vec(), b"p\xFFab".to_vec()] |
| 2086 |
); |
| 2087 |
assert_eq!(at.read("p�aa"), "keep-aa"); |
| 2088 |
assert_eq!(at.read("p�ab"), "keep-ab"); |
| 2089 |
} |
| 2090 |
|
| 2091 |
#[test] |
| 2092 |
#[cfg(target_os = "linux")] |
| 2093 |
fn test_split_non_utf8_additional_suffix_is_byte_preserving() { |
| 2094 |
use std::ffi::OsStr; |
| 2095 |
use std::os::unix::ffi::{OsStrExt, OsStringExt}; |
| 2096 |
|
| 2097 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 2098 |
at.write("input.txt", "AB"); |
| 2099 |
|
| 2100 |
let suffix_bytes = b"\xFF\xFE"; |
| 2101 |
ucmd.args(&["-b", "1", "input.txt", "--additional-suffix"]) |
| 2102 |
.arg(OsStr::from_bytes(suffix_bytes)) |
| 2103 |
.succeeds(); |
| 2104 |
|
| 2105 |
let mut produced_with_suffix: Vec<Vec<u8>> = fs::read_dir(at.as_string()) |
| 2106 |
.expect("temporary split directory should be readable") |
| 2107 |
.filter_map(|entry| { |
| 2108 |
let entry = entry.ok()?; |
| 2109 |
let name = entry.file_name().into_vec(); |
| 2110 |
if name.starts_with(b"xa") && name.ends_with(suffix_bytes) { |
| 2111 |
Some(name) |
| 2112 |
} else { |
| 2113 |
None |
| 2114 |
} |
| 2115 |
}) |
| 2116 |
.collect(); |
| 2117 |
produced_with_suffix.sort(); |
| 2118 |
|
| 2119 |
assert_eq!( |
| 2120 |
produced_with_suffix, |
| 2121 |
vec![b"xaa\xFF\xFE".to_vec(), b"xab\xFF\xFE".to_vec()] |
| 2122 |
); |
| 2123 |
} |
| 2124 |
|
| 2125 |
#[test] |
| 2126 |
#[cfg(unix)] // To re-enable on Windows once I work out what goes wrong with it. |
| 2127 |
fn test_split_directory_already_exists() { |
| 2128 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 2129 |
|
| 2130 |
at.mkdir("xaa"); // For collision with. |
| 2131 |
at.touch("file"); |
| 2132 |
ucmd.args(&["file"]) |
| 2133 |
.fails_with_code(1) |
| 2134 |
.no_stdout() |
| 2135 |
.stderr_is("split: 'xaa': Is a directory\n"); |
| 2136 |
} |
| 2137 |
|
| 2138 |
#[test] |
| 2139 |
#[cfg(all(target_os = "linux", target_env = "gnu"))] |
| 2140 |
fn test_io_error() { |
| 2141 |
// /proc/self/mem causes EIO |
| 2142 |
new_ucmd!() |
| 2143 |
.arg("/proc/self/mem") |
| 2144 |
.fails_with_code(1) |
| 2145 |
//todo: add file path with proper distinction of input/output |
| 2146 |
.stderr_contains("Input/output error\n"); |
| 2147 |
} |
| 2148 |
|
| 2149 |
/// Writing a chunk to a full device must be reported and must stop the split. |
| 2150 |
#[test] |
| 2151 |
#[cfg(target_os = "linux")] |
| 2152 |
fn test_write_error_on_full_device() { |
| 2153 |
if !Path::new("/dev/full").exists() { |
| 2154 |
return; |
| 2155 |
} |
| 2156 |
|
| 2157 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 2158 |
|
| 2159 |
// The first chunk lands on /dev/full, so its write can never succeed. |
| 2160 |
at.symlink_file("/dev/full", "xaa"); |
| 2161 |
at.write("input", "uv"); |
| 2162 |
|
| 2163 |
ucmd.args(&["-b", "1", "input"]) |
| 2164 |
.fails_with_code(1) |
| 2165 |
.no_stdout() |
| 2166 |
.stderr_contains("split: xaa: No space left on device"); |
| 2167 |
|
| 2168 |
// split must not have moved on to the next chunk. |
| 2169 |
assert!(!at.file_exists("xab")); |
| 2170 |
} |
| 2171 |
|
| 2172 |
#[cfg(all(feature = "feat_diagnostics", not(wasi_runner)))] |
| 2173 |
mod diagnostics { |
| 2174 |
use super::*; |
| 2175 |
|
| 2176 |
#[cfg(unix)] |
| 2177 |
#[test] |
| 2178 |
fn test_snippet_points_at_the_unknown_unit() { |
| 2179 |
let result = new_ucmd!() |
| 2180 |
.terminal_sim_stderr() |
| 2181 |
.args(&["-b", "7zq", "/dev/null"]) |
| 2182 |
.fails_with_code(1); |
| 2183 |
|
| 2184 |
// The number parsed; only the unit did not. |
| 2185 |
assert_eq!( |
| 2186 |
result.stderr_as_displayed(), |
| 2187 |
"\ |
| 2188 |
split: invalid number of bytes: '7zq' |
| 2189 |
╭─[ split:1:11 ] |
| 2190 |
│ |
| 2191 |
1 │ split -b 7zq /dev/null |
| 2192 |
│ ─┬ |
| 2193 |
│ ╰── not a known unit |
| 2194 |
│ |
| 2195 |
│ Help: a size is a number and an optional unit: K, M, G and so on for 1024, KB, MB, GB for 1000 |
| 2196 |
───╯" |
| 2197 |
); |
| 2198 |
} |
| 2199 |
|
| 2200 |
#[cfg(unix)] |
| 2201 |
#[test] |
| 2202 |
fn test_snippet_points_inside_a_line_bytes_value() { |
| 2203 |
let result = new_ucmd!() |
| 2204 |
.terminal_sim_stderr() |
| 2205 |
.args(&["--line-bytes=3qq", "/dev/null"]) |
| 2206 |
.fails_with_code(1); |
| 2207 |
let stderr = result.stderr_as_displayed(); |
| 2208 |
|
| 2209 |
assert!(stderr.contains("split:1:21"), "{stderr}"); |
| 2210 |
assert!(stderr.contains("not a known unit"), "{stderr}"); |
| 2211 |
} |
| 2212 |
|
| 2213 |
#[cfg(unix)] |
| 2214 |
#[test] |
| 2215 |
fn test_snippet_underlines_a_count_with_no_number() { |
| 2216 |
let result = new_ucmd!() |
| 2217 |
.terminal_sim_stderr() |
| 2218 |
.args(&["-l", "qq", "/dev/null"]) |
| 2219 |
.fails_with_code(1); |
| 2220 |
let stderr = result.stderr_as_displayed(); |
| 2221 |
|
| 2222 |
// Nothing usable was read, so the whole value is underlined and the |
| 2223 |
// message says the rest. |
| 2224 |
assert!(stderr.contains("invalid number of lines"), "{stderr}"); |
| 2225 |
assert!(!stderr.contains("not a known unit"), "{stderr}"); |
| 2226 |
} |
| 2227 |
|
| 2228 |
#[test] |
| 2229 |
fn test_plain_message_when_stderr_is_a_pipe() { |
| 2230 |
new_ucmd!() |
| 2231 |
.args(&["-b", "7zq", "/dev/null"]) |
| 2232 |
.fails_with_code(1) |
| 2233 |
.stderr_is("split: invalid number of bytes: '7zq'\n"); |
| 2234 |
} |
| 2235 |
} |