Skip to content

Commit a7e4e91

Browse files
authored
base32, base64, basenc: Simplifying the base encoding uu_app and adding basic buffer tests (uutils#9409)
* Simplifying the base encoding uu_app and adding basic buffer tests * Added an ignore for the spell checker on base encoding output
1 parent d3d16d9 commit a7e4e91

File tree

5 files changed

+19
-36
lines changed

5 files changed

+19
-36
lines changed

src/uu/base32/src/base32.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,11 @@ use uucore::{encoding::Format, error::UResult, translate};
1010

1111
#[uucore::main]
1212
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
13-
let format = Format::Base32;
14-
let (about, usage) = get_info();
15-
let config = base_common::parse_base_cmd_args(args, about, usage)?;
13+
let config = base_common::parse_base_cmd_args(args, uu_app())?;
1614
let mut input = base_common::get_input(&config)?;
17-
base_common::handle_input(&mut input, format, config)
15+
base_common::handle_input(&mut input, Format::Base32, config)
1816
}
1917

2018
pub fn uu_app() -> Command {
21-
let (about, usage) = get_info();
22-
base_common::base_app(about, usage)
23-
}
24-
25-
fn get_info() -> (&'static str, &'static str) {
26-
let about: &'static str = Box::leak(translate!("base32-about").into_boxed_str());
27-
let usage: &'static str = Box::leak(translate!("base32-usage").into_boxed_str());
28-
(about, usage)
19+
base_common::base_app(translate!("base32-about"), translate!("base32-usage"))
2920
}

src/uu/base32/src/base_common.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,21 +97,16 @@ impl Config {
9797
}
9898
}
9999

100-
pub fn parse_base_cmd_args(
101-
args: impl uucore::Args,
102-
about: &'static str,
103-
usage: &str,
104-
) -> UResult<Config> {
105-
let command = base_app(about, usage);
100+
pub fn parse_base_cmd_args(args: impl uucore::Args, command: Command) -> UResult<Config> {
106101
let matches = uucore::clap_localization::handle_clap_result(command, args)?;
107102
Config::from(&matches)
108103
}
109104

110-
pub fn base_app(about: &'static str, usage: &str) -> Command {
105+
pub fn base_app(about: String, usage: String) -> Command {
111106
let cmd = Command::new(uucore::util_name())
112107
.version(uucore::crate_version!())
113108
.about(about)
114-
.override_usage(format_usage(usage))
109+
.override_usage(format_usage(&usage))
115110
.infer_long_args(true);
116111
uucore::clap_localization::configure_localized_command(cmd)
117112
// Format arguments.

src/uu/base64/src/base64.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,11 @@ use uucore::{encoding::Format, error::UResult};
1010

1111
#[uucore::main]
1212
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
13-
let format = Format::Base64;
14-
let (about, usage) = get_info();
15-
let config = base_common::parse_base_cmd_args(args, about, usage)?;
13+
let config = base_common::parse_base_cmd_args(args, uu_app())?;
1614
let mut input = base_common::get_input(&config)?;
17-
base_common::handle_input(&mut input, format, config)
15+
base_common::handle_input(&mut input, Format::Base64, config)
1816
}
1917

2018
pub fn uu_app() -> Command {
21-
let (about, usage) = get_info();
22-
base_common::base_app(about, usage)
23-
}
24-
25-
fn get_info() -> (&'static str, &'static str) {
26-
let about: &'static str = Box::leak(translate!("base64-about").into_boxed_str());
27-
let usage: &'static str = Box::leak(translate!("base64-usage").into_boxed_str());
28-
(about, usage)
19+
base_common::base_app(translate!("base64-about"), translate!("base64-usage"))
2920
}

src/uu/basenc/src/basenc.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,8 @@ fn get_encodings() -> Vec<(&'static str, Format, String)> {
4444
}
4545

4646
pub fn uu_app() -> Command {
47-
let about: &'static str = Box::leak(translate!("basenc-about").into_boxed_str());
48-
let usage: &'static str = Box::leak(translate!("basenc-usage").into_boxed_str());
49-
5047
let encodings = get_encodings();
51-
let mut command = base_common::base_app(about, usage);
48+
let mut command = base_common::base_app(translate!("basenc-about"), translate!("basenc-usage"));
5249

5350
for encoding in &encodings {
5451
let raw_arg = Arg::new(encoding.0)

tests/by-util/test_base32.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,12 @@ fn test_base32_file_not_found() {
150150
.fails()
151151
.stderr_only("base32: a.txt: No such file or directory\n");
152152
}
153+
154+
#[test]
155+
fn test_encode_large_input_is_buffered() {
156+
let input = "A".repeat(6000);
157+
new_ucmd!()
158+
.pipe_in(input)
159+
.succeeds()
160+
.stdout_contains("BIFAUCQK"); // spell-checker:disable-line
161+
}

0 commit comments

Comments
 (0)