Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove some unwraps #136

Merged
merged 2 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 35 additions & 22 deletions crates/shell/src/commands/uname.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,42 @@

impl ShellCommand for UnameCommand {
fn execute(&self, mut context: ShellCommandContext) -> LocalBoxFuture<'static, ExecuteResult> {
let matches = uu_uname::uu_app()
.no_binary_name(true)
.try_get_matches_from(context.args)
.unwrap();
Box::pin(async move {
match execute_uname(&mut context) {
Ok(_) => ExecuteResult::from_exit_code(0),
Err(e) => {
context.stderr.write_line(&e).ok();
ExecuteResult::from_exit_code(1)

Check warning on line 34 in crates/shell/src/commands/uname.rs

View check run for this annotation

Codecov / codecov/patch

crates/shell/src/commands/uname.rs#L32-L34

Added lines #L32 - L34 were not covered by tests
}
}
})
}
}

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

Check warning on line 46 in crates/shell/src/commands/uname.rs

View check run for this annotation

Codecov / codecov/patch

crates/shell/src/commands/uname.rs#L46

Added line #L46 was not covered by tests

let uname = UNameOutput::new(&options).unwrap();
context
.stdout
.write_line(display(&uname).trim_end())
.unwrap();
let options = uu_uname::Options {
all: matches.get_flag(options::ALL),
kernel_name: matches.get_flag(options::KERNEL_NAME),
nodename: matches.get_flag(options::NODENAME),
kernel_release: matches.get_flag(options::KERNEL_RELEASE),
kernel_version: matches.get_flag(options::KERNEL_VERSION),
machine: matches.get_flag(options::MACHINE),
processor: matches.get_flag(options::PROCESSOR),
hardware_platform: matches.get_flag(options::HARDWARE_PLATFORM),
os: matches.get_flag(options::OS),
};

Box::pin(futures::future::ready(ExecuteResult::from_exit_code(0)))
}
let uname = UNameOutput::new(&options).unwrap();
context
.stdout
.write_line(display(&uname).trim_end())
.map_err(|e| e.to_string())?;

Ok(())
}
31 changes: 18 additions & 13 deletions crates/shell/src/commands/which.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,33 @@
pub struct WhichCommand;

impl ShellCommand for WhichCommand {
fn execute(&self, context: ShellCommandContext) -> LocalBoxFuture<'static, ExecuteResult> {
Box::pin(futures::future::ready(execute_which(context)))
fn execute(&self, mut context: ShellCommandContext) -> LocalBoxFuture<'static, ExecuteResult> {
Box::pin(futures::future::ready(match execute_which(&mut context) {
Ok(_) => ExecuteResult::from_exit_code(0),
Err(exit_code) => ExecuteResult::from_exit_code(exit_code),
}))
}
}

fn execute_which(mut context: ShellCommandContext) -> ExecuteResult {
fn execute_which(context: &mut ShellCommandContext) -> Result<(), i32> {
if context.args.len() != 1 {
context.stderr.write_line("Expected one argument.").unwrap();
return ExecuteResult::from_exit_code(1);
context.stderr.write_line("Expected one argument").ok();
return Err(1);
}

let arg = &context.args[0];

if let Some(alias) = context.state.alias_map().get(arg) {
context
.stdout
.write_line(&format!("alias: \"{}\"", alias.join(" ")))
.unwrap();
return ExecuteResult::from_exit_code(0);
.ok();
return Ok(());
}

if context.state.resolve_custom_command(arg).is_some() {
context.stdout.write_line("<builtin function>").unwrap();
return ExecuteResult::from_exit_code(0);
context.stdout.write_line("<builtin function>").ok();
return Ok(());
}

if let Some(path) = context.state.env_vars().get("PATH") {
Expand All @@ -35,14 +39,15 @@
.and_then(|mut i| i.next().ok_or(which::Error::CannotFindBinaryPath));

if let Ok(p) = which_result {
context.stdout.write_line(&p.to_string_lossy()).unwrap();
return ExecuteResult::from_exit_code(0);
context.stdout.write_line(&p.to_string_lossy()).ok();
return Ok(());

Check warning on line 43 in crates/shell/src/commands/which.rs

View check run for this annotation

Codecov / codecov/patch

crates/shell/src/commands/which.rs#L42-L43

Added lines #L42 - L43 were not covered by tests
}
}

context
.stderr
.write_line(&format!("{} not found", arg))
.unwrap();
ExecuteResult::from_exit_code(1)
.ok();

Err(1)

Check warning on line 52 in crates/shell/src/commands/which.rs

View check run for this annotation

Codecov / codecov/patch

crates/shell/src/commands/which.rs#L52

Added line #L52 was not covered by tests
}
14 changes: 14 additions & 0 deletions crates/tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,20 @@ async fn which() {
.assert_stdout("<builtin function>\n")
.run()
.await;

TestBuilder::new()
.command("which bla foo")
.assert_exit_code(1)
.assert_stderr("Expected one argument\n")
.run()
.await;

TestBuilder::new()
.command("alias ll=\"ls -al\" && which ll")
.assert_exit_code(0)
.assert_stdout("alias: \"ls -al\"\n")
.run()
.await;
}

#[cfg(test)]
Expand Down
Loading