Skip to content

Commit a0fe6d3

Browse files
committed
du: demonstrate importance of argument order
This is essentially the test from uutils/uutils-args#114 .
1 parent 86fd6c4 commit a0fe6d3

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

tests/by-util/test_du.rs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,3 +1264,92 @@ fn test_du_blocksize_zero_do_not_panic() {
12641264
));
12651265
}
12661266
}
1267+
1268+
#[test]
1269+
fn test_du_blocksize_bytes_order() {
1270+
for (args, expected_output) in [
1271+
// Division is only correct here because 123_456 is even. Otherwise, we would need div_ceil.
1272+
(["--apparent-size", "-B2"].as_slice(), 123_456 / 2),
1273+
(["-bB2"].as_slice(), 123_456 / 2),
1274+
(["-b", "-B2"].as_slice(), 123_456 / 2),
1275+
(["-B2", "-b"].as_slice(), 123_456),
1276+
(["-b", "-B2", "-b"].as_slice(), 123_456),
1277+
] {
1278+
let (at, mut ucmd) = at_and_ucmd!();
1279+
1280+
let fpath = at.plus("test.txt");
1281+
std::fs::File::create(&fpath)
1282+
.expect("cannot create test file")
1283+
.set_len(123_456)
1284+
.expect("cannot truncate test len to size");
1285+
ucmd.args(args)
1286+
.arg(&fpath)
1287+
.succeeds()
1288+
.stdout_only(format!("{expected_output}\t{}\n", fpath.to_string_lossy()));
1289+
}
1290+
}
1291+
1292+
#[test]
1293+
fn test_du_blocksize_multiplier() {
1294+
for (blocksize, expected_output) in [
1295+
("1", "123456789"),
1296+
("2", "61728395"),
1297+
("1000", "123457"),
1298+
("1024", "120564"),
1299+
("1kB", "123457"),
1300+
("1KB", "123457"),
1301+
("1k", "120564"),
1302+
("1K", "120564"),
1303+
("kB", "123457kB"),
1304+
("KB", "123457kB"),
1305+
("k", "120564k"),
1306+
("K", "120564k"),
1307+
("2kB", "61729"),
1308+
("2KB", "61729"),
1309+
("2k", "60282"),
1310+
("2K", "60282"),
1311+
] {
1312+
let (at, mut ucmd) = at_and_ucmd!();
1313+
let fpath = at.plus("test.txt");
1314+
std::fs::File::create(&fpath)
1315+
.expect("cannot create test file")
1316+
.set_len(123_456_789)
1317+
.expect("cannot truncate test len to size");
1318+
ucmd.arg("--apparent-size")
1319+
.arg("-B")
1320+
.arg(blocksize)
1321+
.arg(&fpath)
1322+
.succeeds()
1323+
.stdout_only(format!("{expected_output}\t{}\n", fpath.to_string_lossy()));
1324+
}
1325+
}
1326+
1327+
#[test]
1328+
fn test_du_blocksize_refuse_lowercase_b() {
1329+
for (has_suffix, blocksize) in [
1330+
(false, "kb"),
1331+
(false, "Kb"),
1332+
(true, "1kb"),
1333+
(true, "1Kb"),
1334+
(true, "2kb"),
1335+
(true, "2Kb"),
1336+
] {
1337+
let (at, mut ucmd) = at_and_ucmd!();
1338+
let fpath = at.plus("test.txt");
1339+
std::fs::File::create(&fpath)
1340+
.expect("cannot create test file")
1341+
.set_len(123_456)
1342+
.expect("cannot truncate test len to size");
1343+
ucmd.arg("--apparent-size")
1344+
.arg("-B")
1345+
.arg(blocksize)
1346+
.arg(&fpath)
1347+
.fails()
1348+
.stderr_only(format!(
1349+
"du: invalid {}--block-size argument '{blocksize}'\n",
1350+
if has_suffix { "suffix in " } else { "" }
1351+
));
1352+
}
1353+
}
1354+
1355+
// TODO: Also test ordering wrt. --si and -h

0 commit comments

Comments
 (0)