Skip to content

Commit 144a8de

Browse files
authored
chore: remove some unwraps (#136)
* remove some unwraps * increase coverage
1 parent 16b3204 commit 144a8de

File tree

3 files changed

+67
-35
lines changed

3 files changed

+67
-35
lines changed

crates/shell/src/commands/uname.rs

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,42 @@ fn display(uname: &UNameOutput) -> String {
2626

2727
impl ShellCommand for UnameCommand {
2828
fn execute(&self, mut context: ShellCommandContext) -> LocalBoxFuture<'static, ExecuteResult> {
29-
let matches = uu_uname::uu_app()
30-
.no_binary_name(true)
31-
.try_get_matches_from(context.args)
32-
.unwrap();
29+
Box::pin(async move {
30+
match execute_uname(&mut context) {
31+
Ok(_) => ExecuteResult::from_exit_code(0),
32+
Err(e) => {
33+
context.stderr.write_line(&e).ok();
34+
ExecuteResult::from_exit_code(1)
35+
}
36+
}
37+
})
38+
}
39+
}
3340

34-
let options = uu_uname::Options {
35-
all: matches.get_flag(options::ALL),
36-
kernel_name: matches.get_flag(options::KERNEL_NAME),
37-
nodename: matches.get_flag(options::NODENAME),
38-
kernel_release: matches.get_flag(options::KERNEL_RELEASE),
39-
kernel_version: matches.get_flag(options::KERNEL_VERSION),
40-
machine: matches.get_flag(options::MACHINE),
41-
processor: matches.get_flag(options::PROCESSOR),
42-
hardware_platform: matches.get_flag(options::HARDWARE_PLATFORM),
43-
os: matches.get_flag(options::OS),
44-
};
41+
fn execute_uname(context: &mut ShellCommandContext) -> Result<(), String> {
42+
let matches = uu_uname::uu_app()
43+
.override_usage("uname [OPTION]...")
44+
.no_binary_name(true)
45+
.try_get_matches_from(&context.args)
46+
.map_err(|e| e.to_string())?;
4547

46-
let uname = UNameOutput::new(&options).unwrap();
47-
context
48-
.stdout
49-
.write_line(display(&uname).trim_end())
50-
.unwrap();
48+
let options = uu_uname::Options {
49+
all: matches.get_flag(options::ALL),
50+
kernel_name: matches.get_flag(options::KERNEL_NAME),
51+
nodename: matches.get_flag(options::NODENAME),
52+
kernel_release: matches.get_flag(options::KERNEL_RELEASE),
53+
kernel_version: matches.get_flag(options::KERNEL_VERSION),
54+
machine: matches.get_flag(options::MACHINE),
55+
processor: matches.get_flag(options::PROCESSOR),
56+
hardware_platform: matches.get_flag(options::HARDWARE_PLATFORM),
57+
os: matches.get_flag(options::OS),
58+
};
5159

52-
Box::pin(futures::future::ready(ExecuteResult::from_exit_code(0)))
53-
}
60+
let uname = UNameOutput::new(&options).unwrap();
61+
context
62+
.stdout
63+
.write_line(display(&uname).trim_end())
64+
.map_err(|e| e.to_string())?;
65+
66+
Ok(())
5467
}

crates/shell/src/commands/which.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,33 @@ use futures::future::LocalBoxFuture;
44
pub struct WhichCommand;
55

66
impl ShellCommand for WhichCommand {
7-
fn execute(&self, context: ShellCommandContext) -> LocalBoxFuture<'static, ExecuteResult> {
8-
Box::pin(futures::future::ready(execute_which(context)))
7+
fn execute(&self, mut context: ShellCommandContext) -> LocalBoxFuture<'static, ExecuteResult> {
8+
Box::pin(futures::future::ready(match execute_which(&mut context) {
9+
Ok(_) => ExecuteResult::from_exit_code(0),
10+
Err(exit_code) => ExecuteResult::from_exit_code(exit_code),
11+
}))
912
}
1013
}
1114

12-
fn execute_which(mut context: ShellCommandContext) -> ExecuteResult {
15+
fn execute_which(context: &mut ShellCommandContext) -> Result<(), i32> {
1316
if context.args.len() != 1 {
14-
context.stderr.write_line("Expected one argument.").unwrap();
15-
return ExecuteResult::from_exit_code(1);
17+
context.stderr.write_line("Expected one argument").ok();
18+
return Err(1);
1619
}
20+
1721
let arg = &context.args[0];
1822

1923
if let Some(alias) = context.state.alias_map().get(arg) {
2024
context
2125
.stdout
2226
.write_line(&format!("alias: \"{}\"", alias.join(" ")))
23-
.unwrap();
24-
return ExecuteResult::from_exit_code(0);
27+
.ok();
28+
return Ok(());
2529
}
2630

2731
if context.state.resolve_custom_command(arg).is_some() {
28-
context.stdout.write_line("<builtin function>").unwrap();
29-
return ExecuteResult::from_exit_code(0);
32+
context.stdout.write_line("<builtin function>").ok();
33+
return Ok(());
3034
}
3135

3236
if let Some(path) = context.state.env_vars().get("PATH") {
@@ -35,14 +39,15 @@ fn execute_which(mut context: ShellCommandContext) -> ExecuteResult {
3539
.and_then(|mut i| i.next().ok_or(which::Error::CannotFindBinaryPath));
3640

3741
if let Ok(p) = which_result {
38-
context.stdout.write_line(&p.to_string_lossy()).unwrap();
39-
return ExecuteResult::from_exit_code(0);
42+
context.stdout.write_line(&p.to_string_lossy()).ok();
43+
return Ok(());
4044
}
4145
}
4246

4347
context
4448
.stderr
4549
.write_line(&format!("{} not found", arg))
46-
.unwrap();
47-
ExecuteResult::from_exit_code(1)
50+
.ok();
51+
52+
Err(1)
4853
}

crates/tests/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,20 @@ async fn which() {
797797
.assert_stdout("<builtin function>\n")
798798
.run()
799799
.await;
800+
801+
TestBuilder::new()
802+
.command("which bla foo")
803+
.assert_exit_code(1)
804+
.assert_stderr("Expected one argument\n")
805+
.run()
806+
.await;
807+
808+
TestBuilder::new()
809+
.command("alias ll=\"ls -al\" && which ll")
810+
.assert_exit_code(0)
811+
.assert_stdout("alias: \"ls -al\"\n")
812+
.run()
813+
.await;
800814
}
801815

802816
#[cfg(test)]

0 commit comments

Comments
 (0)