mcookie: add support for human-readable sizes with -m option (#268)

* Create size.rs

* parse max size from human-readable strings

* tests for human-readable strings

* fmt

* fix failed tests and edge cases

* fix fmt

* cpr & license

* use usimpleerror

* fmt
This commit is contained in:
Quang
2025-03-31 00:26:49 -07:00
committed by GitHub
parent c1a44627e4
commit 36b3af044c
3 changed files with 141 additions and 7 deletions

View File

@@ -32,7 +32,7 @@ fn test_verbose() {
}
#[test]
fn test_seed_files_and_max_size() {
fn test_seed_files_and_max_size_raw() {
let mut file1 = NamedTempFile::new().unwrap();
const CONTENT1: &str = "Some seed data";
file1.write_all(CONTENT1.as_bytes()).unwrap();
@@ -63,3 +63,38 @@ fn test_seed_files_and_max_size() {
file2.path().to_str().unwrap()
));
}
#[test]
fn test_seed_files_and_max_size_human_readable() {
let mut file = NamedTempFile::new().unwrap();
const CONTENT: [u8; 4096] = [1; 4096];
file.write_all(&CONTENT).unwrap();
let res = new_ucmd!()
.arg("--verbose")
.arg("-f")
.arg(file.path())
.arg("-m")
.arg("2KiB")
.succeeds();
// Ensure we only read up to 2KiB (2048 bytes)
res.stderr_contains(format!(
"Got 2048 bytes from {}",
file.path().to_str().unwrap()
));
}
#[test]
fn test_invalid_size_format() {
let file = NamedTempFile::new().unwrap();
let res = new_ucmd!()
.arg("-f")
.arg(file.path())
.arg("-m")
.arg("invalid")
.fails();
res.stderr_contains("Failed to parse max-size value");
}