mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
refactor(sync): Move to clap + add tests
This commit is contained in:
parent
f47005e999
commit
2febd13733
4 changed files with 26 additions and 53 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -2096,7 +2096,7 @@ dependencies = [
|
||||||
name = "uu_sync"
|
name = "uu_sync"
|
||||||
version = "0.0.1"
|
version = "0.0.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"getopts 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)",
|
"clap 2.33.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)",
|
"libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"uucore 0.0.4 (git+https://github.com/uutils/uucore.git?branch=canary)",
|
"uucore 0.0.4 (git+https://github.com/uutils/uucore.git?branch=canary)",
|
||||||
"uucore_procs 0.0.4 (git+https://github.com/uutils/uucore.git?branch=canary)",
|
"uucore_procs 0.0.4 (git+https://github.com/uutils/uucore.git?branch=canary)",
|
||||||
|
|
|
@ -15,7 +15,7 @@ edition = "2018"
|
||||||
path = "src/sync.rs"
|
path = "src/sync.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
getopts = "0.2.18"
|
clap = "2.33"
|
||||||
libc = "0.2.42"
|
libc = "0.2.42"
|
||||||
uucore = { version="0.0.4", package="uucore", git="https://github.com/uutils/uucore.git", branch="canary", features=["wide"] }
|
uucore = { version="0.0.4", package="uucore", git="https://github.com/uutils/uucore.git", branch="canary", features=["wide"] }
|
||||||
uucore_procs = { version="0.0.4", package="uucore_procs", git="https://github.com/uutils/uucore.git", branch="canary" }
|
uucore_procs = { version="0.0.4", package="uucore_procs", git="https://github.com/uutils/uucore.git", branch="canary" }
|
||||||
|
|
|
@ -7,17 +7,14 @@
|
||||||
|
|
||||||
/* Last synced with: sync (GNU coreutils) 8.13 */
|
/* Last synced with: sync (GNU coreutils) 8.13 */
|
||||||
|
|
||||||
extern crate getopts;
|
extern crate clap;
|
||||||
extern crate libc;
|
extern crate libc;
|
||||||
|
|
||||||
#[cfg(windows)]
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate uucore;
|
extern crate uucore;
|
||||||
|
|
||||||
#[cfg(not(windows))]
|
use clap::App;
|
||||||
extern crate uucore;
|
static ABOUT: &str = "Synchronize cached writes to persistent storage";
|
||||||
|
|
||||||
static NAME: &str = "sync";
|
|
||||||
static VERSION: &str = env!("CARGO_PKG_VERSION");
|
static VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
|
@ -118,57 +115,23 @@ mod platform {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_usage() -> String {
|
||||||
|
format!("{0} [OPTION]... FILE...", executable!())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn uumain(args: impl uucore::Args) -> i32 {
|
pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
let args = args.collect_str();
|
let usage = get_usage();
|
||||||
|
|
||||||
let mut opts = getopts::Options::new();
|
let _matches = App::new(executable!())
|
||||||
|
.version(VERSION)
|
||||||
opts.optflag("h", "help", "display this help and exit");
|
.about(ABOUT)
|
||||||
opts.optflag("V", "version", "output version information and exit");
|
.usage(&usage[..])
|
||||||
|
.get_matches_from(args);
|
||||||
let matches = match opts.parse(&args[1..]) {
|
|
||||||
Ok(m) => m,
|
|
||||||
_ => {
|
|
||||||
help(&opts);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if matches.opt_present("h") {
|
|
||||||
help(&opts);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if matches.opt_present("V") {
|
|
||||||
version();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
sync();
|
sync();
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn version() {
|
|
||||||
println!("{} (uutils) {}", NAME, VERSION);
|
|
||||||
println!("The MIT License");
|
|
||||||
println!();
|
|
||||||
println!("Author -- Alexander Fomin.");
|
|
||||||
}
|
|
||||||
|
|
||||||
fn help(opts: &getopts::Options) {
|
|
||||||
let msg = format!(
|
|
||||||
"{0} {1}
|
|
||||||
|
|
||||||
Usage:
|
|
||||||
{0} [OPTION]
|
|
||||||
|
|
||||||
Force changed blocks to disk, update the super block.",
|
|
||||||
NAME, VERSION
|
|
||||||
);
|
|
||||||
|
|
||||||
print!("{}", opts.usage(&msg));
|
|
||||||
}
|
|
||||||
|
|
||||||
fn sync() -> isize {
|
fn sync() -> isize {
|
||||||
unsafe { platform::do_sync() }
|
unsafe { platform::do_sync() }
|
||||||
}
|
}
|
||||||
|
|
|
@ -1 +1,11 @@
|
||||||
// ToDO: add tests
|
use crate::common::util::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_sync_default() {
|
||||||
|
new_ucmd!().run();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_sync_incorrect_arg() {
|
||||||
|
new_ucmd!().arg("--foo").fails();
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue