Skip to content

Commit 736d87a

Browse files
committed
implement proof of concept for du -bkB
1 parent b2e8f73 commit 736d87a

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

tests/coreutils.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ mod date;
1919
#[path = "coreutils/dd.rs"]
2020
mod dd;
2121

22+
#[path = "coreutils/du.rs"]
23+
mod du;
24+
2225
#[path = "coreutils/echo.rs"]
2326
mod echo;
2427

tests/coreutils/du.rs

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
use std::ffi::OsString;
2+
use uutils_args::{Arguments, Options};
3+
4+
#[derive(Arguments)]
5+
enum Arg {
6+
#[arg("--apparent-size")]
7+
ApparentSize,
8+
9+
#[arg("-B[SIZE]")]
10+
#[arg("--block-size[=SIZE]")]
11+
BlockSize(OsString),
12+
13+
#[arg("-b")]
14+
#[arg("--bytes")]
15+
Bytes,
16+
17+
#[arg("-k")]
18+
KibiBytes,
19+
20+
#[arg("-m")]
21+
MibiBytes,
22+
23+
// Note that --si and -h only affect the *output formatting*,
24+
// and not the size determination itself.
25+
}
26+
27+
#[derive(Debug, Default, PartialEq, Eq)]
28+
struct Settings {
29+
apparent_size: bool,
30+
block_size_str: Option<OsString>,
31+
}
32+
33+
impl Options<Arg> for Settings {
34+
fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> {
35+
match arg {
36+
Arg::ApparentSize => self.apparent_size = true,
37+
Arg::BlockSize(os_str) => self.block_size_str = Some(os_str),
38+
Arg::Bytes => {
39+
self.apparent_size = true;
40+
self.block_size_str = Some("1".into());
41+
}
42+
Arg::KibiBytes => self.block_size_str = Some("K".into()),
43+
Arg::MibiBytes => self.block_size_str = Some("M".into()),
44+
}
45+
Ok(())
46+
}
47+
}
48+
49+
#[test]
50+
fn noarg() {
51+
let (settings, operands) = Settings::default().parse(["date"]).unwrap();
52+
assert_eq!(operands, Vec::<OsString>::new());
53+
assert_eq!(settings, Settings {
54+
apparent_size: false,
55+
block_size_str: None,
56+
});
57+
}
58+
59+
#[test]
60+
fn bytes() {
61+
let (settings, operands) = Settings::default().parse(["date", "-b"]).unwrap();
62+
assert_eq!(operands, Vec::<OsString>::new());
63+
assert_eq!(settings, Settings {
64+
apparent_size: true,
65+
block_size_str: Some("1".into()),
66+
});
67+
}
68+
69+
#[test]
70+
fn kibibytes() {
71+
let (settings, operands) = Settings::default().parse(["date", "-k"]).unwrap();
72+
assert_eq!(operands, Vec::<OsString>::new());
73+
assert_eq!(settings, Settings {
74+
apparent_size: false,
75+
block_size_str: Some("K".into()),
76+
});
77+
}
78+
79+
#[test]
80+
fn bytes_kibibytes() {
81+
let (settings, operands) = Settings::default().parse(["date", "-bk"]).unwrap();
82+
assert_eq!(operands, Vec::<OsString>::new());
83+
assert_eq!(settings, Settings {
84+
apparent_size: true,
85+
block_size_str: Some("K".into()),
86+
});
87+
}
88+
89+
#[test]
90+
fn kibibytes_bytes() {
91+
let (settings, operands) = Settings::default().parse(["date", "-kb"]).unwrap();
92+
assert_eq!(operands, Vec::<OsString>::new());
93+
assert_eq!(settings, Settings {
94+
apparent_size: true,
95+
block_size_str: Some("1".into()),
96+
});
97+
}
98+
99+
#[test]
100+
fn apparent_size() {
101+
let (settings, operands) = Settings::default().parse(["date", "--apparent-size"]).unwrap();
102+
assert_eq!(operands, Vec::<OsString>::new());
103+
assert_eq!(settings, Settings {
104+
apparent_size: true,
105+
block_size_str: None,
106+
});
107+
}
108+
109+
#[test]
110+
fn mibibytes() {
111+
let (settings, operands) = Settings::default().parse(["date", "-m"]).unwrap();
112+
assert_eq!(operands, Vec::<OsString>::new());
113+
assert_eq!(settings, Settings {
114+
apparent_size: false,
115+
block_size_str: Some("M".into()),
116+
});
117+
}
118+
119+
#[test]
120+
fn all() {
121+
let (settings, operands) = Settings::default().parse(["date", "--apparent-size", "-bkm", "-B123"]).unwrap();
122+
assert_eq!(operands, Vec::<OsString>::new());
123+
assert_eq!(settings, Settings {
124+
apparent_size: true,
125+
block_size_str: Some("123".into()),
126+
});
127+
}

0 commit comments

Comments
 (0)