From d83781ddec52a806d81c6563b0a5cc393b61803b Mon Sep 17 00:00:00 2001 From: Solomon Date: Sat, 12 Oct 2024 06:49:05 +0000 Subject: [PATCH] support filesize arguments in `random` `binary`/`chars` (#14068) Closes #13920 # User-Facing Changes `random binary` and `random chars` now support filesize arguments: ```nushell random binary 1kb random chars --length 1kb ``` --- crates/nu-command/src/random/binary.rs | 23 ++++++++++++++----- crates/nu-command/src/random/chars.rs | 7 +++++- .../tests/commands/random/binary.rs | 21 +++++++++++++++++ .../nu-command/tests/commands/random/chars.rs | 10 ++++++++ .../nu-command/tests/commands/random/mod.rs | 1 + 5 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 crates/nu-command/tests/commands/random/binary.rs diff --git a/crates/nu-command/src/random/binary.rs b/crates/nu-command/src/random/binary.rs index 136afc6e11..539796b8c5 100644 --- a/crates/nu-command/src/random/binary.rs +++ b/crates/nu-command/src/random/binary.rs @@ -14,7 +14,11 @@ impl Command for SubCommand { Signature::build("random binary") .input_output_types(vec![(Type::Nothing, Type::Binary)]) .allow_variants_without_examples(true) - .required("length", SyntaxShape::Int, "Length of the output binary.") + .required( + "length", + SyntaxShape::OneOf(vec![SyntaxShape::Int, SyntaxShape::Filesize]), + "Length of the output binary.", + ) .category(Category::Random) } @@ -43,11 +47,18 @@ impl Command for SubCommand { } fn examples(&self) -> Vec { - vec![Example { - description: "Generate 16 random bytes", - example: "random binary 16", - result: None, - }] + vec![ + Example { + description: "Generate 16 random bytes", + example: "random binary 16", + result: None, + }, + Example { + description: "Generate 1 random kilobyte", + example: "random binary 1kb", + result: None, + }, + ] } } diff --git a/crates/nu-command/src/random/chars.rs b/crates/nu-command/src/random/chars.rs index e2a1282f9e..d445a4aab8 100644 --- a/crates/nu-command/src/random/chars.rs +++ b/crates/nu-command/src/random/chars.rs @@ -21,7 +21,7 @@ impl Command for SubCommand { .allow_variants_without_examples(true) .named( "length", - SyntaxShape::Int, + SyntaxShape::OneOf(vec![SyntaxShape::Int, SyntaxShape::Filesize]), "Number of chars (default 25)", Some('l'), ) @@ -58,6 +58,11 @@ impl Command for SubCommand { example: "random chars --length 20", result: None, }, + Example { + description: "Generate one kilobyte of random chars", + example: "random chars --length 1kb", + result: None, + }, ] } } diff --git a/crates/nu-command/tests/commands/random/binary.rs b/crates/nu-command/tests/commands/random/binary.rs new file mode 100644 index 0000000000..9b8137eff2 --- /dev/null +++ b/crates/nu-command/tests/commands/random/binary.rs @@ -0,0 +1,21 @@ +use nu_test_support::nu; + +#[test] +fn generates_bytes_of_specified_length() { + let actual = nu!(r#" + random binary 16 | bytes length + "#); + + let result = actual.out; + assert_eq!(result, "16"); +} + +#[test] +fn generates_bytes_of_specified_filesize() { + let actual = nu!(r#" + random binary 1kb | bytes length + "#); + + let result = actual.out; + assert_eq!(result, "1000"); +} diff --git a/crates/nu-command/tests/commands/random/chars.rs b/crates/nu-command/tests/commands/random/chars.rs index 4b5b0a9ef4..9b4c433b95 100644 --- a/crates/nu-command/tests/commands/random/chars.rs +++ b/crates/nu-command/tests/commands/random/chars.rs @@ -9,3 +9,13 @@ fn generates_chars_of_specified_length() { let result = actual.out; assert_eq!(result, "15"); } + +#[test] +fn generates_chars_of_specified_filesize() { + let actual = nu!(r#" + random chars --length 1kb | str stats | get bytes + "#); + + let result = actual.out; + assert_eq!(result, "1000"); +} diff --git a/crates/nu-command/tests/commands/random/mod.rs b/crates/nu-command/tests/commands/random/mod.rs index a879b524b8..c16b2e18b8 100644 --- a/crates/nu-command/tests/commands/random/mod.rs +++ b/crates/nu-command/tests/commands/random/mod.rs @@ -1,3 +1,4 @@ +mod binary; mod bool; mod chars; mod dice;