2015-11-16 05:25:01 +00:00
|
|
|
|
extern crate rand;
|
|
|
|
|
extern crate regex;
|
|
|
|
|
|
2020-04-13 18:36:03 +00:00
|
|
|
|
use self::rand::{thread_rng, Rng};
|
2016-08-06 03:18:34 +00:00
|
|
|
|
use self::regex::Regex;
|
2020-05-25 17:05:26 +00:00
|
|
|
|
use crate::common::util::*;
|
2021-05-04 11:01:01 +00:00
|
|
|
|
use rand::SeedableRng;
|
2021-01-18 13:42:44 +00:00
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
|
use std::env;
|
2020-04-13 18:36:03 +00:00
|
|
|
|
use std::io::Write;
|
|
|
|
|
use std::path::Path;
|
2021-05-04 11:01:01 +00:00
|
|
|
|
use std::{
|
|
|
|
|
fs::{read_dir, File},
|
|
|
|
|
io::BufWriter,
|
|
|
|
|
};
|
2015-11-16 05:25:01 +00:00
|
|
|
|
|
|
|
|
|
fn random_chars(n: usize) -> String {
|
2020-04-13 18:36:03 +00:00
|
|
|
|
thread_rng()
|
|
|
|
|
.sample_iter(&rand::distributions::Alphanumeric)
|
|
|
|
|
.take(n)
|
|
|
|
|
.collect::<String>()
|
2015-11-16 05:25:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct Glob {
|
|
|
|
|
directory: AtPath,
|
|
|
|
|
regex: Regex,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Glob {
|
|
|
|
|
fn new(at: &AtPath, directory: &str, regex: &str) -> Glob {
|
|
|
|
|
Glob {
|
|
|
|
|
directory: AtPath::new(Path::new(&at.plus_as_string(directory))),
|
|
|
|
|
regex: Regex::new(regex).unwrap(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn count(&self) -> usize {
|
|
|
|
|
self.collect().len()
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-18 13:42:44 +00:00
|
|
|
|
/// Get all files in `self.directory` that match `self.regex`
|
2015-11-16 05:25:01 +00:00
|
|
|
|
fn collect(&self) -> Vec<String> {
|
|
|
|
|
read_dir(Path::new(&self.directory.subdir))
|
|
|
|
|
.unwrap()
|
|
|
|
|
.filter_map(|entry| {
|
|
|
|
|
let path = entry.unwrap().path();
|
2020-04-13 18:36:03 +00:00
|
|
|
|
let name = self
|
|
|
|
|
.directory
|
|
|
|
|
.minus_as_string(path.as_path().to_str().unwrap_or(""));
|
2015-11-16 05:25:01 +00:00
|
|
|
|
if self.regex.is_match(&name) {
|
|
|
|
|
Some(name)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.collect()
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-18 13:42:44 +00:00
|
|
|
|
/// Accumulate bytes of all files in `self.collect()`
|
2015-11-16 05:25:01 +00:00
|
|
|
|
fn collate(&self) -> Vec<u8> {
|
|
|
|
|
let mut files = self.collect();
|
|
|
|
|
files.sort();
|
|
|
|
|
let mut data: Vec<u8> = vec![];
|
2016-11-25 19:14:46 +00:00
|
|
|
|
for name in &files {
|
2021-05-04 11:01:01 +00:00
|
|
|
|
data.extend(self.directory.read_bytes(name));
|
2015-11-16 05:25:01 +00:00
|
|
|
|
}
|
|
|
|
|
data
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-18 13:42:44 +00:00
|
|
|
|
/// File handle that user can add random bytes (line-formatted or not) to
|
2015-11-16 05:25:01 +00:00
|
|
|
|
struct RandomFile {
|
|
|
|
|
inner: File,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl RandomFile {
|
2021-01-18 13:42:44 +00:00
|
|
|
|
/// Size of each line that's being generated
|
|
|
|
|
const LINESIZE: usize = 32;
|
|
|
|
|
|
|
|
|
|
/// `create()` file handle located at `at` / `name`
|
2015-11-16 05:25:01 +00:00
|
|
|
|
fn new(at: &AtPath, name: &str) -> RandomFile {
|
2020-04-13 18:36:03 +00:00
|
|
|
|
RandomFile {
|
|
|
|
|
inner: File::create(&at.plus(name)).unwrap(),
|
|
|
|
|
}
|
2015-11-16 05:25:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn add_bytes(&mut self, bytes: usize) {
|
2021-05-04 11:01:01 +00:00
|
|
|
|
// Note that just writing random characters isn't enough to cover all
|
|
|
|
|
// cases. We need truly random bytes.
|
|
|
|
|
let mut writer = BufWriter::new(&self.inner);
|
|
|
|
|
|
|
|
|
|
// Seed the rng so as to avoid spurious test failures.
|
|
|
|
|
let mut rng = rand::rngs::StdRng::seed_from_u64(123);
|
|
|
|
|
let mut buffer = [0; 1024];
|
|
|
|
|
let mut remaining_size = bytes;
|
|
|
|
|
|
|
|
|
|
while remaining_size > 0 {
|
|
|
|
|
let to_write = std::cmp::min(remaining_size, buffer.len());
|
|
|
|
|
let buf = &mut buffer[..to_write];
|
|
|
|
|
rng.fill(buf);
|
2021-05-29 12:32:35 +00:00
|
|
|
|
writer.write_all(buf).unwrap();
|
2021-05-04 11:01:01 +00:00
|
|
|
|
|
|
|
|
|
remaining_size -= to_write;
|
2015-11-16 05:25:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-18 13:42:44 +00:00
|
|
|
|
/// Add n lines each of size `RandomFile::LINESIZE`
|
2015-11-16 05:25:01 +00:00
|
|
|
|
fn add_lines(&mut self, lines: usize) {
|
|
|
|
|
let mut n = lines;
|
|
|
|
|
while n > 0 {
|
2021-05-04 11:01:01 +00:00
|
|
|
|
writeln!(self.inner, "{}", random_chars(RandomFile::LINESIZE)).unwrap();
|
2015-11-16 05:25:01 +00:00
|
|
|
|
n -= 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_split_default() {
|
2016-08-23 11:52:43 +00:00
|
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
2015-11-16 05:25:01 +00:00
|
|
|
|
let name = "split_default";
|
|
|
|
|
RandomFile::new(&at, name).add_lines(2000);
|
2016-08-13 21:59:21 +00:00
|
|
|
|
ucmd.args(&[name]).succeeds();
|
2021-05-04 11:01:01 +00:00
|
|
|
|
|
|
|
|
|
let glob = Glob::new(&at, ".", r"x[[:alpha:]][[:alpha:]]$");
|
2015-11-16 05:25:01 +00:00
|
|
|
|
assert_eq!(glob.count(), 2);
|
2021-05-04 11:01:01 +00:00
|
|
|
|
assert_eq!(glob.collate(), at.read_bytes(name));
|
2015-11-16 05:25:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2021-02-11 19:45:23 +00:00
|
|
|
|
fn test_split_numeric_prefixed_chunks_by_bytes() {
|
2016-08-23 11:52:43 +00:00
|
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
2015-11-16 05:25:01 +00:00
|
|
|
|
let name = "split_num_prefixed_chunks_by_bytes";
|
|
|
|
|
RandomFile::new(&at, name).add_bytes(10000);
|
2021-02-11 19:45:23 +00:00
|
|
|
|
ucmd.args(&[
|
|
|
|
|
"-d", // --numeric-suffixes
|
|
|
|
|
"-b", // --bytes
|
|
|
|
|
"1000", name, "a",
|
|
|
|
|
])
|
|
|
|
|
.succeeds();
|
2021-05-04 11:01:01 +00:00
|
|
|
|
|
|
|
|
|
let glob = Glob::new(&at, ".", r"a\d\d$");
|
2015-11-16 05:25:01 +00:00
|
|
|
|
assert_eq!(glob.count(), 10);
|
2021-05-04 11:01:01 +00:00
|
|
|
|
for filename in glob.collect() {
|
|
|
|
|
assert_eq!(glob.directory.metadata(&filename).len(), 1000);
|
|
|
|
|
}
|
|
|
|
|
assert_eq!(glob.collate(), at.read_bytes(name));
|
2015-11-16 05:25:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_split_str_prefixed_chunks_by_bytes() {
|
2016-08-23 11:52:43 +00:00
|
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
2015-11-16 05:25:01 +00:00
|
|
|
|
let name = "split_str_prefixed_chunks_by_bytes";
|
|
|
|
|
RandomFile::new(&at, name).add_bytes(10000);
|
2021-05-04 11:01:01 +00:00
|
|
|
|
// Important that this is less than 1024 since that's our internal buffer
|
|
|
|
|
// size. Good to test that we don't overshoot.
|
2016-08-13 21:59:21 +00:00
|
|
|
|
ucmd.args(&["-b", "1000", name, "b"]).succeeds();
|
2021-05-04 11:01:01 +00:00
|
|
|
|
|
|
|
|
|
let glob = Glob::new(&at, ".", r"b[[:alpha:]][[:alpha:]]$");
|
2015-11-16 05:25:01 +00:00
|
|
|
|
assert_eq!(glob.count(), 10);
|
2021-05-04 11:01:01 +00:00
|
|
|
|
for filename in glob.collect() {
|
|
|
|
|
assert_eq!(glob.directory.metadata(&filename).len(), 1000);
|
|
|
|
|
}
|
|
|
|
|
assert_eq!(glob.collate(), at.read_bytes(name));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This is designed to test what happens when the desired part size is not a
|
|
|
|
|
// multiple of the buffer size and we hopefully don't overshoot the desired part
|
|
|
|
|
// size.
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_split_bytes_prime_part_size() {
|
|
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
|
|
|
|
let name = "test_split_bytes_prime_part_size";
|
|
|
|
|
RandomFile::new(&at, name).add_bytes(10000);
|
|
|
|
|
// 1753 is prime and greater than the buffer size, 1024.
|
|
|
|
|
ucmd.args(&["-b", "1753", name, "b"]).succeeds();
|
|
|
|
|
|
|
|
|
|
let glob = Glob::new(&at, ".", r"b[[:alpha:]][[:alpha:]]$");
|
|
|
|
|
assert_eq!(glob.count(), 6);
|
2021-05-04 22:19:35 +00:00
|
|
|
|
let mut fns = glob.collect();
|
|
|
|
|
// glob.collect() is not guaranteed to return in sorted order, so we sort.
|
|
|
|
|
fns.sort();
|
2021-05-29 12:32:35 +00:00
|
|
|
|
#[allow(clippy::needless_range_loop)]
|
2021-05-04 11:01:01 +00:00
|
|
|
|
for i in 0..5 {
|
2021-05-04 22:19:35 +00:00
|
|
|
|
assert_eq!(glob.directory.metadata(&fns[i]).len(), 1753);
|
2021-05-04 11:01:01 +00:00
|
|
|
|
}
|
2021-05-04 22:19:35 +00:00
|
|
|
|
assert_eq!(glob.directory.metadata(&fns[5]).len(), 1235);
|
2021-05-04 11:01:01 +00:00
|
|
|
|
assert_eq!(glob.collate(), at.read_bytes(name));
|
2015-11-16 05:25:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_split_num_prefixed_chunks_by_lines() {
|
2016-08-23 11:52:43 +00:00
|
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
2015-11-16 05:25:01 +00:00
|
|
|
|
let name = "split_num_prefixed_chunks_by_lines";
|
|
|
|
|
RandomFile::new(&at, name).add_lines(10000);
|
2016-08-13 21:59:21 +00:00
|
|
|
|
ucmd.args(&["-d", "-l", "1000", name, "c"]).succeeds();
|
2021-05-04 11:01:01 +00:00
|
|
|
|
|
|
|
|
|
let glob = Glob::new(&at, ".", r"c\d\d$");
|
2015-11-16 05:25:01 +00:00
|
|
|
|
assert_eq!(glob.count(), 10);
|
2021-05-04 11:01:01 +00:00
|
|
|
|
assert_eq!(glob.collate(), at.read_bytes(name));
|
2015-11-16 05:25:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_split_str_prefixed_chunks_by_lines() {
|
2016-08-23 11:52:43 +00:00
|
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
2015-11-16 05:25:01 +00:00
|
|
|
|
let name = "split_str_prefixed_chunks_by_lines";
|
|
|
|
|
RandomFile::new(&at, name).add_lines(10000);
|
2016-08-13 21:59:21 +00:00
|
|
|
|
ucmd.args(&["-l", "1000", name, "d"]).succeeds();
|
2021-05-04 11:01:01 +00:00
|
|
|
|
|
|
|
|
|
let glob = Glob::new(&at, ".", r"d[[:alpha:]][[:alpha:]]$");
|
2015-11-16 05:25:01 +00:00
|
|
|
|
assert_eq!(glob.count(), 10);
|
2021-05-04 11:01:01 +00:00
|
|
|
|
assert_eq!(glob.collate(), at.read_bytes(name));
|
2015-11-16 05:25:01 +00:00
|
|
|
|
}
|
2020-09-16 15:59:39 +00:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_split_additional_suffix() {
|
|
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
|
|
|
|
let name = "split_additional_suffix";
|
|
|
|
|
RandomFile::new(&at, name).add_lines(2000);
|
|
|
|
|
ucmd.args(&["--additional-suffix", ".txt", name]).succeeds();
|
2021-05-04 11:01:01 +00:00
|
|
|
|
|
|
|
|
|
let glob = Glob::new(&at, ".", r"x[[:alpha:]][[:alpha:]].txt$");
|
2020-09-16 15:59:39 +00:00
|
|
|
|
assert_eq!(glob.count(), 2);
|
2021-05-04 11:01:01 +00:00
|
|
|
|
assert_eq!(glob.collate(), at.read_bytes(name));
|
2020-09-16 15:59:39 +00:00
|
|
|
|
}
|
2021-01-18 13:42:44 +00:00
|
|
|
|
|
|
|
|
|
// note: the test_filter* tests below are unix-only
|
|
|
|
|
// windows support has been waived for now because of the difficulty of getting
|
|
|
|
|
// the `cmd` call right
|
|
|
|
|
// see https://github.com/rust-lang/rust/issues/29494
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
|
fn test_filter() {
|
|
|
|
|
// like `test_split_default()` but run a command before writing
|
|
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
|
|
|
|
let name = "filtered";
|
|
|
|
|
let n_lines = 3;
|
|
|
|
|
RandomFile::new(&at, name).add_lines(n_lines);
|
|
|
|
|
|
|
|
|
|
// change all characters to 'i'
|
|
|
|
|
ucmd.args(&["--filter=sed s/./i/g > $FILE", name])
|
|
|
|
|
.succeeds();
|
2021-05-04 11:01:01 +00:00
|
|
|
|
|
2021-01-18 13:42:44 +00:00
|
|
|
|
// assert all characters are 'i' / no character is not 'i'
|
2021-05-30 05:10:54 +00:00
|
|
|
|
// (assert that command succeeded)
|
2021-05-04 11:01:01 +00:00
|
|
|
|
let glob = Glob::new(&at, ".", r"x[[:alpha:]][[:alpha:]]$");
|
2021-01-18 13:42:44 +00:00
|
|
|
|
assert!(
|
|
|
|
|
glob.collate().iter().find(|&&c| {
|
|
|
|
|
// is not i
|
2021-05-29 12:32:35 +00:00
|
|
|
|
c != (b'i')
|
2021-01-18 13:42:44 +00:00
|
|
|
|
// is not newline
|
2021-05-29 12:32:35 +00:00
|
|
|
|
&& c != (b'\n')
|
2021-01-18 13:42:44 +00:00
|
|
|
|
}) == None
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
|
fn test_filter_with_env_var_set() {
|
|
|
|
|
// This test will ensure that if $FILE env var was set before running --filter, it'll stay that
|
|
|
|
|
// way
|
|
|
|
|
// implemented like `test_split_default()` but run a command before writing
|
|
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
|
|
|
|
let name = "filtered";
|
|
|
|
|
let n_lines = 3;
|
|
|
|
|
RandomFile::new(&at, name).add_lines(n_lines);
|
|
|
|
|
|
2021-05-30 05:10:54 +00:00
|
|
|
|
let env_var_value = "some-value";
|
2021-01-18 13:42:44 +00:00
|
|
|
|
env::set_var("FILE", &env_var_value);
|
|
|
|
|
ucmd.args(&[format!("--filter={}", "cat > $FILE").as_str(), name])
|
|
|
|
|
.succeeds();
|
2021-05-04 11:01:01 +00:00
|
|
|
|
|
|
|
|
|
let glob = Glob::new(&at, ".", r"x[[:alpha:]][[:alpha:]]$");
|
|
|
|
|
assert_eq!(glob.collate(), at.read_bytes(name));
|
2021-05-29 12:32:35 +00:00
|
|
|
|
assert!(env::var("FILE").unwrap_or_else(|_| "var was unset".to_owned()) == env_var_value);
|
2021-01-18 13:42:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
|
fn test_filter_command_fails() {
|
|
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
|
|
|
|
let name = "filter-will-fail";
|
|
|
|
|
RandomFile::new(&at, name).add_lines(4);
|
|
|
|
|
|
|
|
|
|
ucmd.args(&["--filter=/a/path/that/totally/does/not/exist", name])
|
|
|
|
|
.fails();
|
|
|
|
|
}
|
2021-06-02 16:37:21 +00:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_split_lines_number() {
|
|
|
|
|
// Test if stdout/stderr for '--lines' option is correct
|
2021-06-03 19:13:44 +00:00
|
|
|
|
let scene = TestScenario::new(util_name!());
|
|
|
|
|
let at = &scene.fixtures;
|
|
|
|
|
at.touch("file");
|
|
|
|
|
|
|
|
|
|
scene
|
|
|
|
|
.ucmd()
|
|
|
|
|
.args(&["--lines", "2", "file"])
|
2021-06-02 16:37:21 +00:00
|
|
|
|
.succeeds()
|
|
|
|
|
.no_stderr()
|
|
|
|
|
.no_stdout();
|
2021-06-03 19:13:44 +00:00
|
|
|
|
scene
|
|
|
|
|
.ucmd()
|
|
|
|
|
.args(&["--lines", "2fb", "file"])
|
2021-06-02 16:37:21 +00:00
|
|
|
|
.fails()
|
|
|
|
|
.code_is(1)
|
|
|
|
|
.stderr_only("split: invalid number of lines: ‘2fb’");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_split_invalid_bytes_size() {
|
|
|
|
|
new_ucmd!()
|
|
|
|
|
.args(&["-b", "1024R"])
|
|
|
|
|
.fails()
|
|
|
|
|
.code_is(1)
|
|
|
|
|
.stderr_only("split: invalid number of bytes: ‘1024R’");
|
|
|
|
|
#[cfg(not(target_pointer_width = "128"))]
|
|
|
|
|
new_ucmd!()
|
|
|
|
|
.args(&["-b", "1Y"])
|
|
|
|
|
.fails()
|
|
|
|
|
.code_is(1)
|
|
|
|
|
.stderr_only("split: invalid number of bytes: ‘1Y’: Value too large for defined data type");
|
|
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
|
{
|
|
|
|
|
let sizes = ["1000G", "10T"];
|
|
|
|
|
for size in &sizes {
|
|
|
|
|
new_ucmd!()
|
|
|
|
|
.args(&["-b", size])
|
|
|
|
|
.fails()
|
|
|
|
|
.code_is(1)
|
|
|
|
|
.stderr_only(format!(
|
|
|
|
|
"split: invalid number of bytes: ‘{}’: Value too large for defined data type",
|
|
|
|
|
size
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|