mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 03:27:44 +00:00
sort: Support check
This commit is contained in:
parent
3bc5a5f769
commit
3fd8136423
3 changed files with 57 additions and 13 deletions
|
@ -44,6 +44,7 @@ struct Settings {
|
||||||
reverse: bool,
|
reverse: bool,
|
||||||
outfile: Option<String>,
|
outfile: Option<String>,
|
||||||
unique: bool,
|
unique: bool,
|
||||||
|
check: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Settings {
|
impl Default for Settings {
|
||||||
|
@ -53,6 +54,7 @@ impl Default for Settings {
|
||||||
reverse: false,
|
reverse: false,
|
||||||
outfile: None,
|
outfile: None,
|
||||||
unique: false,
|
unique: false,
|
||||||
|
check: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -70,6 +72,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
opts.optopt("o", "output", "write output to FILENAME instead of stdout", "FILENAME");
|
opts.optopt("o", "output", "write output to FILENAME instead of stdout", "FILENAME");
|
||||||
opts.optflag("u", "unique", "output only the first of an equal run");
|
opts.optflag("u", "unique", "output only the first of an equal run");
|
||||||
opts.optflag("V", "version-sort", "Sort by SemVer version number, eg 1.12.2 > 1.1.2");
|
opts.optflag("V", "version-sort", "Sort by SemVer version number, eg 1.12.2 > 1.1.2");
|
||||||
|
opts.optflag("c", "check", "check for sorted input; do not sort");
|
||||||
|
|
||||||
let matches = match opts.parse(&args[1..]) {
|
let matches = match opts.parse(&args[1..]) {
|
||||||
Ok(m) => m,
|
Ok(m) => m,
|
||||||
|
@ -110,6 +113,7 @@ With no FILE, or when FILE is -, read standard input.", NAME, VERSION);
|
||||||
settings.reverse = matches.opt_present("reverse");
|
settings.reverse = matches.opt_present("reverse");
|
||||||
settings.outfile = matches.opt_str("output");
|
settings.outfile = matches.opt_str("output");
|
||||||
settings.unique = matches.opt_present("unique");
|
settings.unique = matches.opt_present("unique");
|
||||||
|
settings.check = matches.opt_present("check");
|
||||||
|
|
||||||
let mut files = matches.free;
|
let mut files = matches.free;
|
||||||
if files.is_empty() {
|
if files.is_empty() {
|
||||||
|
@ -117,12 +121,10 @@ With no FILE, or when FILE is -, read standard input.", NAME, VERSION);
|
||||||
files.push("-".to_owned());
|
files.push("-".to_owned());
|
||||||
}
|
}
|
||||||
|
|
||||||
exec(files, &settings);
|
exec(files, &settings)
|
||||||
|
|
||||||
0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn exec(files: Vec<String>, settings: &Settings) {
|
fn exec(files: Vec<String>, settings: &Settings) -> i32 {
|
||||||
let mut lines = Vec::new();
|
let mut lines = Vec::new();
|
||||||
for path in &files {
|
for path in &files {
|
||||||
let (reader, _) = match open(path) {
|
let (reader, _) = match open(path) {
|
||||||
|
@ -142,6 +144,8 @@ fn exec(files: Vec<String>, settings: &Settings) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let original_lines = lines.to_vec();
|
||||||
|
|
||||||
match settings.mode {
|
match settings.mode {
|
||||||
SortMode::Numeric => lines.sort_by(numeric_compare),
|
SortMode::Numeric => lines.sort_by(numeric_compare),
|
||||||
SortMode::HumanNumeric => lines.sort_by(human_numeric_size_compare),
|
SortMode::HumanNumeric => lines.sort_by(human_numeric_size_compare),
|
||||||
|
@ -154,12 +158,24 @@ fn exec(files: Vec<String>, settings: &Settings) {
|
||||||
lines.dedup()
|
lines.dedup()
|
||||||
}
|
}
|
||||||
|
|
||||||
let iter = lines.iter();
|
|
||||||
if settings.reverse {
|
if settings.reverse {
|
||||||
print_sorted(iter.rev(), &settings.outfile);
|
lines.reverse()
|
||||||
} else {
|
}
|
||||||
print_sorted(iter, &settings.outfile)
|
|
||||||
};
|
if settings.check {
|
||||||
|
for (i, line) in lines.iter().enumerate() {
|
||||||
|
if line != &original_lines[i] {
|
||||||
|
println!("sort: disorder in line {}", i);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
print_sorted(lines.iter(), &settings.outfile)
|
||||||
|
}
|
||||||
|
|
||||||
|
0
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse the beginning string into an f64, returning -inf instead of NaN on errors.
|
/// Parse the beginning string into an f64, returning -inf instead of NaN on errors.
|
||||||
|
|
8
tests/fixtures/sort/check_fail.txt
vendored
Normal file
8
tests/fixtures/sort/check_fail.txt
vendored
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
1
|
||||||
|
2
|
||||||
|
3
|
||||||
|
4
|
||||||
|
6
|
||||||
|
5
|
||||||
|
9
|
||||||
|
8
|
|
@ -58,15 +58,35 @@ fn test_multiple_files() {
|
||||||
ucmd.arg("-n");
|
ucmd.arg("-n");
|
||||||
ucmd.arg("multiple_files1.txt");
|
ucmd.arg("multiple_files1.txt");
|
||||||
ucmd.arg("multiple_files2.txt");
|
ucmd.arg("multiple_files2.txt");
|
||||||
let out = ucmd.run().stdout;
|
let res = ucmd.run();
|
||||||
assert_eq!(out, at.read("multiple_files.expected"));
|
assert_eq!(res.success, true);
|
||||||
|
assert_eq!(res.stdout, at.read("multiple_files.expected"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_check() {
|
||||||
|
let (_, mut ucmd) = testing(UTIL_NAME);
|
||||||
|
ucmd.arg("-c");
|
||||||
|
let res = ucmd.arg("check_fail.txt").run();
|
||||||
|
|
||||||
|
assert_eq!(res.success, false);
|
||||||
|
assert_eq!(res.stdout, "sort: disorder in line 4\n");
|
||||||
|
|
||||||
|
let (_, mut ucmd) = testing(UTIL_NAME);
|
||||||
|
ucmd.arg("-c");
|
||||||
|
let res = ucmd.arg("multiple_files.expected").run();
|
||||||
|
|
||||||
|
assert_eq!(res.success, true);
|
||||||
|
assert_eq!(res.stdout, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_helper(file_name: &str, args: &str) {
|
fn test_helper(file_name: &str, args: &str) {
|
||||||
let (at, mut ucmd) = testing(UTIL_NAME);
|
let (at, mut ucmd) = testing(UTIL_NAME);
|
||||||
ucmd.arg(args);
|
ucmd.arg(args);
|
||||||
let out = ucmd.arg(format!("{}{}", file_name, ".txt")).run().stdout;
|
let res = ucmd.arg(format!("{}{}", file_name, ".txt")).run();
|
||||||
|
|
||||||
|
assert_eq!(res.success, true);
|
||||||
|
|
||||||
let filename = format!("{}{}", file_name, ".expected");
|
let filename = format!("{}{}", file_name, ".expected");
|
||||||
assert_eq!(out, at.read(&filename));
|
assert_eq!(res.stdout, at.read(&filename));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue