mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
Merge branch 'master' into fold-backspace-and-carriage-return
This commit is contained in:
commit
b2b45d8af2
8 changed files with 126 additions and 87 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -1861,6 +1861,7 @@ dependencies = [
|
|||
name = "uu_fold"
|
||||
version = "0.0.6"
|
||||
dependencies = [
|
||||
"clap",
|
||||
"uucore",
|
||||
"uucore_procs",
|
||||
]
|
||||
|
|
|
@ -15,6 +15,7 @@ edition = "2018"
|
|||
path = "src/fold.rs"
|
||||
|
||||
[dependencies]
|
||||
clap = "2.33"
|
||||
uucore = { version=">=0.0.8", package="uucore", path="../../uucore" }
|
||||
uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" }
|
||||
|
||||
|
|
|
@ -10,48 +10,71 @@
|
|||
#[macro_use]
|
||||
extern crate uucore;
|
||||
|
||||
use clap::{App, Arg};
|
||||
use std::fs::File;
|
||||
use std::io::{stdin, BufRead, BufReader, Read};
|
||||
use std::path::Path;
|
||||
|
||||
const TAB_WIDTH: usize = 8;
|
||||
|
||||
static NAME: &str = "fold";
|
||||
static VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
static SYNTAX: &str = "[OPTION]... [FILE]...";
|
||||
static SUMMARY: &str = "Writes each file (or standard input if no files are given)
|
||||
to standard output whilst breaking long lines";
|
||||
static LONG_HELP: &str = "";
|
||||
|
||||
mod options {
|
||||
pub const BYTES: &str = "bytes";
|
||||
pub const SPACES: &str = "spaces";
|
||||
pub const WIDTH: &str = "width";
|
||||
pub const FILE: &str = "file";
|
||||
}
|
||||
|
||||
pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||
let args = args.collect_str();
|
||||
|
||||
let (args, obs_width) = handle_obsolete(&args[..]);
|
||||
let matches = app!(SYNTAX, SUMMARY, LONG_HELP)
|
||||
.optflag(
|
||||
"b",
|
||||
"bytes",
|
||||
"count using bytes rather than columns (meaning control characters \
|
||||
let matches = App::new(executable!())
|
||||
.name(NAME)
|
||||
.version(VERSION)
|
||||
.usage(SYNTAX)
|
||||
.about(SUMMARY)
|
||||
.arg(
|
||||
Arg::with_name(options::BYTES)
|
||||
.long(options::BYTES)
|
||||
.short("b")
|
||||
.help(
|
||||
"count using bytes rather than columns (meaning control characters \
|
||||
such as newline are not treated specially)",
|
||||
)
|
||||
.takes_value(false),
|
||||
)
|
||||
.optflag(
|
||||
"s",
|
||||
"spaces",
|
||||
"break lines at word boundaries rather than a hard cut-off",
|
||||
.arg(
|
||||
Arg::with_name(options::SPACES)
|
||||
.long(options::SPACES)
|
||||
.short("s")
|
||||
.help("break lines at word boundaries rather than a hard cut-off")
|
||||
.takes_value(false),
|
||||
)
|
||||
.optopt(
|
||||
"w",
|
||||
"width",
|
||||
"set WIDTH as the maximum line width rather than 80",
|
||||
"WIDTH",
|
||||
.arg(
|
||||
Arg::with_name(options::WIDTH)
|
||||
.long(options::WIDTH)
|
||||
.short("w")
|
||||
.help("set WIDTH as the maximum line width rather than 80")
|
||||
.value_name("WIDTH")
|
||||
.allow_hyphen_values(true)
|
||||
.takes_value(true),
|
||||
)
|
||||
.parse(args);
|
||||
.arg(Arg::with_name(options::FILE).hidden(true).multiple(true))
|
||||
.get_matches_from(args.clone());
|
||||
|
||||
let bytes = matches.opt_present("b");
|
||||
let spaces = matches.opt_present("s");
|
||||
let poss_width = if matches.opt_present("w") {
|
||||
matches.opt_str("w")
|
||||
} else {
|
||||
obs_width
|
||||
let bytes = matches.is_present(options::BYTES);
|
||||
let spaces = matches.is_present(options::SPACES);
|
||||
let poss_width = match matches.value_of(options::WIDTH) {
|
||||
Some(v) => Some(v.to_owned()),
|
||||
None => obs_width,
|
||||
};
|
||||
|
||||
let width = match poss_width {
|
||||
Some(inp_width) => match inp_width.parse::<usize>() {
|
||||
Ok(width) => width,
|
||||
|
@ -59,11 +82,12 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
|||
},
|
||||
None => 80,
|
||||
};
|
||||
let files = if matches.free.is_empty() {
|
||||
vec!["-".to_owned()]
|
||||
} else {
|
||||
matches.free
|
||||
|
||||
let files = match matches.values_of(options::FILE) {
|
||||
Some(v) => v.map(|v| v.to_owned()).collect(),
|
||||
None => vec!["-".to_owned()],
|
||||
};
|
||||
|
||||
fold(files, bytes, spaces, width);
|
||||
|
||||
0
|
||||
|
|
|
@ -166,39 +166,30 @@ mod tests {
|
|||
let mut input = PeekReader::new(Cursor::new(&data));
|
||||
let mut sut = InputDecoder::new(&mut input, 8, 2, ByteOrder::Little);
|
||||
|
||||
match sut.peek_read() {
|
||||
Ok(mut mem) => {
|
||||
assert_eq!(8, mem.length());
|
||||
// Peek normal length
|
||||
let mut mem = sut.peek_read().unwrap();
|
||||
|
||||
assert_eq!(-2.0, mem.read_float(0, 8));
|
||||
assert_eq!(-2.0, mem.read_float(4, 4));
|
||||
assert_eq!(0xc000000000000000, mem.read_uint(0, 8));
|
||||
assert_eq!(0xc0000000, mem.read_uint(4, 4));
|
||||
assert_eq!(0xc000, mem.read_uint(6, 2));
|
||||
assert_eq!(0xc0, mem.read_uint(7, 1));
|
||||
assert_eq!(&[0, 0xc0], mem.get_buffer(6));
|
||||
assert_eq!(&[0, 0xc0, 0xff, 0xff], mem.get_full_buffer(6));
|
||||
assert_eq!(8, mem.length());
|
||||
|
||||
let mut copy: Vec<u8> = Vec::new();
|
||||
mem.clone_buffer(&mut copy);
|
||||
assert_eq!(vec![0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0], copy);
|
||||
assert_eq!(-2.0, mem.read_float(0, 8));
|
||||
assert_eq!(-2.0, mem.read_float(4, 4));
|
||||
assert_eq!(0xc000000000000000, mem.read_uint(0, 8));
|
||||
assert_eq!(0xc0000000, mem.read_uint(4, 4));
|
||||
assert_eq!(0xc000, mem.read_uint(6, 2));
|
||||
assert_eq!(0xc0, mem.read_uint(7, 1));
|
||||
assert_eq!(&[0, 0xc0], mem.get_buffer(6));
|
||||
assert_eq!(&[0, 0xc0, 0xff, 0xff], mem.get_full_buffer(6));
|
||||
|
||||
mem.zero_out_buffer(7, 8);
|
||||
assert_eq!(&[0, 0, 0xff, 0xff], mem.get_full_buffer(6));
|
||||
}
|
||||
Err(e) => {
|
||||
assert!(false, e);
|
||||
}
|
||||
}
|
||||
let mut copy: Vec<u8> = Vec::new();
|
||||
mem.clone_buffer(&mut copy);
|
||||
assert_eq!(vec![0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0], copy);
|
||||
|
||||
match sut.peek_read() {
|
||||
Ok(mem) => {
|
||||
assert_eq!(2, mem.length());
|
||||
assert_eq!(0xffff, mem.read_uint(0, 2));
|
||||
}
|
||||
Err(e) => {
|
||||
assert!(false, e);
|
||||
}
|
||||
}
|
||||
mem.zero_out_buffer(7, 8);
|
||||
assert_eq!(&[0, 0, 0xff, 0xff], mem.get_full_buffer(6));
|
||||
|
||||
// Peek tail
|
||||
let mem = sut.peek_read().unwrap();
|
||||
assert_eq!(2, mem.length());
|
||||
assert_eq!(0xffff, mem.read_uint(0, 2));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -108,7 +108,7 @@ macro_rules! safe_write(
|
|||
($fd:expr, $($args:tt)+) => (
|
||||
match write!($fd, $($args)+) {
|
||||
Ok(_) => {}
|
||||
Err(f) => panic!(f.to_string())
|
||||
Err(f) => panic!("{}", f)
|
||||
}
|
||||
)
|
||||
);
|
||||
|
@ -118,7 +118,7 @@ macro_rules! safe_writeln(
|
|||
($fd:expr, $($args:tt)+) => (
|
||||
match writeln!($fd, $($args)+) {
|
||||
Ok(_) => {}
|
||||
Err(f) => panic!(f.to_string())
|
||||
Err(f) => panic!("{}", f)
|
||||
}
|
||||
)
|
||||
);
|
||||
|
|
|
@ -534,3 +534,12 @@ fn test_bytewise_carriage_return_is_not_word_boundary() {
|
|||
.succeeds()
|
||||
.stdout_is("fizz\rb\nuzz\rfi\nzzbuzz");
|
||||
}
|
||||
#[test]
|
||||
fn test_obsolete_syntax() {
|
||||
new_ucmd!()
|
||||
.arg("-5")
|
||||
.arg("-s")
|
||||
.arg("space_separated_words.txt")
|
||||
.succeeds()
|
||||
.stdout_is("test1\n \ntest2\n \ntest3\n \ntest4\n \ntest5\n \ntest6\n ");
|
||||
}
|
|
@ -70,10 +70,10 @@ fn convert_path<'a>(path: &'a str) -> Cow<'a, str> {
|
|||
|
||||
#[test]
|
||||
fn test_relpath_with_from_no_d() {
|
||||
for test in TESTS.iter() {
|
||||
let scene = TestScenario::new(util_name!());
|
||||
let at = &scene.fixtures;
|
||||
let scene = TestScenario::new(util_name!());
|
||||
let at = &scene.fixtures;
|
||||
|
||||
for test in TESTS.iter() {
|
||||
let from: &str = &convert_path(test.from);
|
||||
let to: &str = &convert_path(test.to);
|
||||
let expected: &str = &convert_path(test.expected);
|
||||
|
@ -92,10 +92,10 @@ fn test_relpath_with_from_no_d() {
|
|||
|
||||
#[test]
|
||||
fn test_relpath_with_from_with_d() {
|
||||
for test in TESTS.iter() {
|
||||
let scene = TestScenario::new(util_name!());
|
||||
let at = &scene.fixtures;
|
||||
let scene = TestScenario::new(util_name!());
|
||||
let at = &scene.fixtures;
|
||||
|
||||
for test in TESTS.iter() {
|
||||
let from: &str = &convert_path(test.from);
|
||||
let to: &str = &convert_path(test.to);
|
||||
let pwd = at.as_string();
|
||||
|
@ -103,63 +103,75 @@ fn test_relpath_with_from_with_d() {
|
|||
at.mkdir_all(from);
|
||||
|
||||
// d is part of subpath -> expect relative path
|
||||
let mut result = scene
|
||||
let mut result_stdout = scene
|
||||
.ucmd()
|
||||
.arg(to)
|
||||
.arg(from)
|
||||
.arg(&format!("-d{}", pwd))
|
||||
.run();
|
||||
assert!(result.success);
|
||||
.succeeds()
|
||||
.stdout_move_str();
|
||||
// relax rules for windows test environment
|
||||
#[cfg(not(windows))]
|
||||
assert!(Path::new(&result.stdout).is_relative());
|
||||
assert!(Path::new(&result_stdout).is_relative());
|
||||
|
||||
// d is not part of subpath -> expect absolut path
|
||||
result = scene.ucmd().arg(to).arg(from).arg("-dnon_existing").run();
|
||||
assert!(result.success);
|
||||
assert!(Path::new(&result.stdout).is_absolute());
|
||||
result_stdout = scene
|
||||
.ucmd()
|
||||
.arg(to)
|
||||
.arg(from)
|
||||
.arg("-dnon_existing")
|
||||
.succeeds()
|
||||
.stdout_move_str();
|
||||
assert!(Path::new(&result_stdout).is_absolute());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_relpath_no_from_no_d() {
|
||||
for test in TESTS.iter() {
|
||||
let scene = TestScenario::new(util_name!());
|
||||
let at = &scene.fixtures;
|
||||
let scene = TestScenario::new(util_name!());
|
||||
let at = &scene.fixtures;
|
||||
|
||||
for test in TESTS.iter() {
|
||||
let to: &str = &convert_path(test.to);
|
||||
at.mkdir_all(to);
|
||||
|
||||
let result = scene.ucmd().arg(to).run();
|
||||
assert!(result.success);
|
||||
let result_stdout = scene.ucmd().arg(to).succeeds().stdout_move_str();
|
||||
#[cfg(not(windows))]
|
||||
assert_eq!(result.stdout, format!("{}\n", to));
|
||||
assert_eq!(result_stdout, format!("{}\n", to));
|
||||
// relax rules for windows test environment
|
||||
#[cfg(windows)]
|
||||
assert!(result.stdout.ends_with(&format!("{}\n", to)));
|
||||
assert!(result_stdout.ends_with(&format!("{}\n", to)));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_relpath_no_from_with_d() {
|
||||
for test in TESTS.iter() {
|
||||
let scene = TestScenario::new(util_name!());
|
||||
let at = &scene.fixtures;
|
||||
let scene = TestScenario::new(util_name!());
|
||||
let at = &scene.fixtures;
|
||||
|
||||
for test in TESTS.iter() {
|
||||
let to: &str = &convert_path(test.to);
|
||||
let pwd = at.as_string();
|
||||
at.mkdir_all(to);
|
||||
|
||||
// d is part of subpath -> expect relative path
|
||||
let mut result = scene.ucmd().arg(to).arg(&format!("-d{}", pwd)).run();
|
||||
assert!(result.success);
|
||||
let mut result_stdout = scene
|
||||
.ucmd()
|
||||
.arg(to)
|
||||
.arg(&format!("-d{}", pwd))
|
||||
.succeeds()
|
||||
.stdout_move_str();
|
||||
// relax rules for windows test environment
|
||||
#[cfg(not(windows))]
|
||||
assert!(Path::new(&result.stdout).is_relative());
|
||||
assert!(Path::new(&result_stdout).is_relative());
|
||||
|
||||
// d is not part of subpath -> expect absolut path
|
||||
result = scene.ucmd().arg(to).arg("-dnon_existing").run();
|
||||
assert!(result.success);
|
||||
assert!(Path::new(&result.stdout).is_absolute());
|
||||
result_stdout = scene
|
||||
.ucmd()
|
||||
.arg(to)
|
||||
.arg("-dnon_existing")
|
||||
.succeeds()
|
||||
.stdout_move_str();
|
||||
assert!(Path::new(&result_stdout).is_absolute());
|
||||
}
|
||||
}
|
||||
|
|
1
tests/fixtures/fold/space_separated_words.txt
vendored
Normal file
1
tests/fixtures/fold/space_separated_words.txt
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
test1 test2 test3 test4 test5 test6
|
Loading…
Add table
Add a link
Reference in a new issue