Skip to content

Commit ae7d03c

Browse files
authored
Merge pull request #6007 from BenWiederhake/dev-basexx-multi-arg
base32/base64/basenc: implement and test proper flag parsing
2 parents 56e26f7 + 445905a commit ae7d03c

File tree

5 files changed

+294
-11
lines changed

5 files changed

+294
-11
lines changed

src/uu/base32/src/base_common.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,23 +102,24 @@ pub fn base_app(about: &'static str, usage: &str) -> Command {
102102
.short('d')
103103
.long(options::DECODE)
104104
.help("decode data")
105-
.action(ArgAction::SetTrue),
105+
.action(ArgAction::SetTrue)
106+
.overrides_with(options::DECODE),
106107
)
107108
.arg(
108109
Arg::new(options::IGNORE_GARBAGE)
109110
.short('i')
110111
.long(options::IGNORE_GARBAGE)
111112
.help("when decoding, ignore non-alphabetic characters")
112-
.action(ArgAction::SetTrue),
113+
.action(ArgAction::SetTrue)
114+
.overrides_with(options::IGNORE_GARBAGE),
113115
)
114116
.arg(
115117
Arg::new(options::WRAP)
116118
.short('w')
117119
.long(options::WRAP)
118120
.value_name("COLS")
119-
.help(
120-
"wrap encoded lines after COLS character (default 76, 0 to disable wrapping)",
121-
),
121+
.help("wrap encoded lines after COLS character (default 76, 0 to disable wrapping)")
122+
.overrides_with(options::WRAP),
122123
)
123124
// "multiple" arguments are used to check whether there is more than one
124125
// file passed in.

src/uu/basenc/src/basenc.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,14 @@ const ENCODINGS: &[(&str, Format, &str)] = &[
5353
pub fn uu_app() -> Command {
5454
let mut command = base_common::base_app(ABOUT, USAGE);
5555
for encoding in ENCODINGS {
56-
command = command.arg(
57-
Arg::new(encoding.0)
58-
.long(encoding.0)
59-
.help(encoding.2)
60-
.action(ArgAction::SetTrue),
61-
);
56+
let raw_arg = Arg::new(encoding.0)
57+
.long(encoding.0)
58+
.help(encoding.2)
59+
.action(ArgAction::SetTrue);
60+
let overriding_arg = ENCODINGS
61+
.iter()
62+
.fold(raw_arg, |arg, enc| arg.overrides_with(enc.0));
63+
command = command.arg(overriding_arg);
6264
}
6365
command
6466
}

tests/by-util/test_base32.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,26 @@ fn test_encode() {
2222
.stdout_only("JBSWY3DPFQQFO33SNRSCC===\n"); // spell-checker:disable-line
2323
}
2424

25+
#[test]
26+
fn test_encode_repeat_flags_later_wrap_10() {
27+
let input = "Hello, World!\n";
28+
new_ucmd!()
29+
.args(&["-ii", "-w17", "-w10"])
30+
.pipe_in(input)
31+
.succeeds()
32+
.stdout_only("JBSWY3DPFQ\nQFO33SNRSC\nCCQ=\n"); // spell-checker:disable-line
33+
}
34+
35+
#[test]
36+
fn test_encode_repeat_flags_later_wrap_17() {
37+
let input = "Hello, World!\n";
38+
new_ucmd!()
39+
.args(&["-ii", "-w10", "-w17"])
40+
.pipe_in(input)
41+
.succeeds()
42+
.stdout_only("JBSWY3DPFQQFO33SN\nRSCCCQ=\n"); // spell-checker:disable-line
43+
}
44+
2545
#[test]
2646
fn test_base32_encode_file() {
2747
new_ucmd!()
@@ -42,6 +62,16 @@ fn test_decode() {
4262
}
4363
}
4464

65+
#[test]
66+
fn test_decode_repeat_flags() {
67+
let input = "JBSWY3DPFQQFO33SNRSCC===\n"; // spell-checker:disable-line
68+
new_ucmd!()
69+
.args(&["-didiw80", "--wrap=17", "--wrap", "8"]) // spell-checker:disable-line
70+
.pipe_in(input)
71+
.succeeds()
72+
.stdout_only("Hello, World!");
73+
}
74+
4575
#[test]
4676
fn test_garbage() {
4777
let input = "aGVsbG8sIHdvcmxkIQ==\0"; // spell-checker:disable-line

tests/by-util/test_base64.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,26 @@ fn test_encode() {
2020
.stdout_only("aGVsbG8sIHdvcmxkIQ==\n"); // spell-checker:disable-line
2121
}
2222

23+
#[test]
24+
fn test_encode_repeat_flags_later_wrap_10() {
25+
let input = "hello, world!";
26+
new_ucmd!()
27+
.args(&["-ii", "-w15", "-w10"])
28+
.pipe_in(input)
29+
.succeeds()
30+
.stdout_only("aGVsbG8sIH\ndvcmxkIQ==\n"); // spell-checker:disable-line
31+
}
32+
33+
#[test]
34+
fn test_encode_repeat_flags_later_wrap_15() {
35+
let input = "hello, world!";
36+
new_ucmd!()
37+
.args(&["-ii", "-w10", "-w15"])
38+
.pipe_in(input)
39+
.succeeds()
40+
.stdout_only("aGVsbG8sIHdvcmx\nkIQ==\n"); // spell-checker:disable-line
41+
}
42+
2343
#[test]
2444
fn test_base64_encode_file() {
2545
new_ucmd!()
@@ -40,6 +60,16 @@ fn test_decode() {
4060
}
4161
}
4262

63+
#[test]
64+
fn test_decode_repeat_flags() {
65+
let input = "aGVsbG8sIHdvcmxkIQ==\n"; // spell-checker:disable-line
66+
new_ucmd!()
67+
.args(&["-didiw80", "--wrap=17", "--wrap", "8"]) // spell-checker:disable-line
68+
.pipe_in(input)
69+
.succeeds()
70+
.stdout_only("hello, world!");
71+
}
72+
4373
#[test]
4474
fn test_garbage() {
4575
let input = "aGVsbG8sIHdvcmxkIQ==\0"; // spell-checker:disable-line

tests/by-util/test_basenc.rs

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
//
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
5+
6+
//spell-checker: ignore (encodings) lsbf msbf
57
use crate::common::util::TestScenario;
68

79
#[test]
@@ -31,3 +33,221 @@ fn test_invalid_input() {
3133
.fails()
3234
.stderr_only(error_message);
3335
}
36+
37+
#[test]
38+
fn test_base64() {
39+
new_ucmd!()
40+
.arg("--base64")
41+
.pipe_in("to>be?")
42+
.succeeds()
43+
.no_stderr()
44+
.stdout_only("dG8+YmU/\n"); // spell-checker:disable-line
45+
}
46+
47+
#[test]
48+
fn test_base64_decode() {
49+
new_ucmd!()
50+
.args(&["--base64", "-d"])
51+
.pipe_in("dG8+YmU/") // spell-checker:disable-line
52+
.succeeds()
53+
.no_stderr()
54+
.stdout_only("to>be?");
55+
}
56+
57+
#[test]
58+
fn test_base64url() {
59+
new_ucmd!()
60+
.arg("--base64url")
61+
.pipe_in("to>be?")
62+
.succeeds()
63+
.no_stderr()
64+
.stdout_only("dG8-YmU_\n"); // spell-checker:disable-line
65+
}
66+
67+
#[test]
68+
fn test_base64url_decode() {
69+
new_ucmd!()
70+
.args(&["--base64url", "-d"])
71+
.pipe_in("dG8-YmU_") // spell-checker:disable-line
72+
.succeeds()
73+
.no_stderr()
74+
.stdout_only("to>be?");
75+
}
76+
77+
#[test]
78+
fn test_base32() {
79+
new_ucmd!()
80+
.arg("--base32")
81+
.pipe_in("nice>base?")
82+
.succeeds()
83+
.no_stderr()
84+
.stdout_only("NZUWGZJ6MJQXGZJ7\n"); // spell-checker:disable-line
85+
}
86+
87+
#[test]
88+
fn test_base32_decode() {
89+
new_ucmd!()
90+
.args(&["--base32", "-d"])
91+
.pipe_in("NZUWGZJ6MJQXGZJ7") // spell-checker:disable-line
92+
.succeeds()
93+
.no_stderr()
94+
.stdout_only("nice>base?");
95+
}
96+
97+
#[test]
98+
fn test_base32hex() {
99+
new_ucmd!()
100+
.arg("--base32hex")
101+
.pipe_in("nice>base?")
102+
.succeeds()
103+
.no_stderr()
104+
.stdout_only("DPKM6P9UC9GN6P9V\n"); // spell-checker:disable-line
105+
}
106+
107+
#[test]
108+
fn test_base32hex_decode() {
109+
new_ucmd!()
110+
.args(&["--base32hex", "-d"])
111+
.pipe_in("DPKM6P9UC9GN6P9V") // spell-checker:disable-line
112+
.succeeds()
113+
.no_stderr()
114+
.stdout_only("nice>base?");
115+
}
116+
117+
#[test]
118+
fn test_base16() {
119+
new_ucmd!()
120+
.arg("--base16")
121+
.pipe_in("Hello, World!")
122+
.succeeds()
123+
.no_stderr()
124+
.stdout_only("48656C6C6F2C20576F726C6421\n");
125+
}
126+
127+
#[test]
128+
fn test_base16_decode() {
129+
new_ucmd!()
130+
.args(&["--base16", "-d"])
131+
.pipe_in("48656C6C6F2C20576F726C6421")
132+
.succeeds()
133+
.no_stderr()
134+
.stdout_only("Hello, World!");
135+
}
136+
137+
#[test]
138+
fn test_base2msbf() {
139+
new_ucmd!()
140+
.arg("--base2msbf")
141+
.pipe_in("msbf")
142+
.succeeds()
143+
.no_stderr()
144+
.stdout_only("01101101011100110110001001100110\n");
145+
}
146+
147+
#[test]
148+
fn test_base2msbf_decode() {
149+
new_ucmd!()
150+
.args(&["--base2msbf", "-d"])
151+
.pipe_in("01101101011100110110001001100110")
152+
.succeeds()
153+
.no_stderr()
154+
.stdout_only("msbf");
155+
}
156+
157+
#[test]
158+
fn test_base2lsbf() {
159+
new_ucmd!()
160+
.arg("--base2lsbf")
161+
.pipe_in("lsbf")
162+
.succeeds()
163+
.no_stderr()
164+
.stdout_only("00110110110011100100011001100110\n");
165+
}
166+
167+
#[test]
168+
fn test_base2lsbf_decode() {
169+
new_ucmd!()
170+
.args(&["--base2lsbf", "-d"])
171+
.pipe_in("00110110110011100100011001100110")
172+
.succeeds()
173+
.no_stderr()
174+
.stdout_only("lsbf");
175+
}
176+
177+
#[test]
178+
fn test_choose_last_encoding_z85() {
179+
new_ucmd!()
180+
.args(&[
181+
"--base2lsbf",
182+
"--base2msbf",
183+
"--base16",
184+
"--base32hex",
185+
"--base64url",
186+
"--base32",
187+
"--base64",
188+
"--z85",
189+
])
190+
.pipe_in("Hello, World")
191+
.succeeds()
192+
.no_stderr()
193+
.stdout_only("nm=QNz.92jz/PV8\n");
194+
}
195+
196+
#[test]
197+
fn test_choose_last_encoding_base64() {
198+
new_ucmd!()
199+
.args(&[
200+
"--base2msbf",
201+
"--base2lsbf",
202+
"--base64url",
203+
"--base32hex",
204+
"--base32",
205+
"--base16",
206+
"--z85",
207+
"--base64",
208+
])
209+
.pipe_in("Hello, World!")
210+
.succeeds()
211+
.no_stderr()
212+
.stdout_only("SGVsbG8sIFdvcmxkIQ==\n"); // spell-checker:disable-line
213+
}
214+
215+
#[test]
216+
fn test_choose_last_encoding_base2lsbf() {
217+
new_ucmd!()
218+
.args(&[
219+
"--base64url",
220+
"--base16",
221+
"--base2msbf",
222+
"--base32",
223+
"--base64",
224+
"--z85",
225+
"--base32hex",
226+
"--base2lsbf",
227+
])
228+
.pipe_in("lsbf")
229+
.succeeds()
230+
.no_stderr()
231+
.stdout_only("00110110110011100100011001100110\n");
232+
}
233+
234+
#[test]
235+
fn test_base32_decode_repeated() {
236+
new_ucmd!()
237+
.args(&[
238+
"--ignore",
239+
"--wrap=80",
240+
"--base32hex",
241+
"--z85",
242+
"--ignore",
243+
"--decode",
244+
"--z85",
245+
"--base32",
246+
"-w",
247+
"10",
248+
])
249+
.pipe_in("NZUWGZJ6MJQXGZJ7") // spell-checker:disable-line
250+
.succeeds()
251+
.no_stderr()
252+
.stdout_only("nice>base?");
253+
}

0 commit comments

Comments
 (0)