Skip to content

Commit 22fb7b4

Browse files
committed
remove unncessary abstraction layers in su CLI testing
1 parent da46d92 commit 22fb7b4

File tree

2 files changed

+13
-21
lines changed

2 files changed

+13
-21
lines changed

src/su/cli.rs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,6 @@ impl SuAction {
1515
pub fn from_env() -> Result<Self, String> {
1616
SuOptions::parse_arguments(std::env::args())?.validate()
1717
}
18-
19-
#[cfg(test)]
20-
pub fn parse_arguments(args: impl IntoIterator<Item = String>) -> Result<Self, String> {
21-
SuOptions::parse_arguments(args)?.validate()
22-
}
23-
24-
#[cfg(test)]
25-
#[allow(clippy::result_large_err)]
26-
pub fn try_into_run(self) -> Result<SuRunOptions, Self> {
27-
if let Self::Run(v) = self {
28-
Ok(v)
29-
} else {
30-
Err(self)
31-
}
32-
}
3318
}
3419

3520
#[cfg_attr(test, derive(Debug, PartialEq))]
@@ -195,7 +180,7 @@ impl<T> IsAbsent for Vec<T> {
195180
}
196181

197182
#[derive(Debug, Default, PartialEq)]
198-
struct SuOptions {
183+
pub(super) struct SuOptions {
199184
// -c
200185
command: Option<String>,
201186
// -g
@@ -380,7 +365,9 @@ impl SuOptions {
380365
];
381366

382367
/// parse su arguments into SuOptions struct
383-
fn parse_arguments(arguments: impl IntoIterator<Item = String>) -> Result<SuOptions, String> {
368+
pub(super) fn parse_arguments(
369+
arguments: impl IntoIterator<Item = String>,
370+
) -> Result<SuOptions, String> {
384371
let mut options: SuOptions = SuOptions::default();
385372
let mut arg_iter = arguments.into_iter().skip(1);
386373

@@ -460,7 +447,7 @@ impl SuOptions {
460447
Ok(options)
461448
}
462449

463-
fn validate(self) -> Result<SuAction, String> {
450+
pub(super) fn validate(self) -> Result<SuAction, String> {
464451
let action = if self.help {
465452
SuAction::Help(self.try_into()?)
466453
} else if self.version {

src/su/context.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,18 +226,23 @@ mod tests {
226226

227227
use crate::{
228228
common::Error,
229-
su::cli::{SuAction, SuRunOptions},
229+
su::cli::{SuAction, SuOptions, SuRunOptions},
230230
};
231231

232232
use super::SuContext;
233233

234234
fn get_options(args: &[&str]) -> SuRunOptions {
235235
let mut args = args.iter().map(|s| s.to_string()).collect::<Vec<String>>();
236236
args.insert(0, "/bin/su".to_string());
237-
SuAction::parse_arguments(args)
237+
let SuAction::Run(options) = SuOptions::parse_arguments(args)
238238
.unwrap()
239-
.try_into_run()
239+
.validate()
240240
.unwrap()
241+
else {
242+
panic!();
243+
};
244+
245+
options
241246
}
242247

243248
#[test]

0 commit comments

Comments
 (0)