Remove all unwraps
This commit is contained in:
parent
0132d32507
commit
ce592985ce
2 changed files with 23 additions and 48 deletions
|
@ -10,7 +10,6 @@ use std::fmt;
|
|||
use std::fs::{File, OpenOptions};
|
||||
use std::io::{Read, Write};
|
||||
use std::path::PathBuf;
|
||||
use std::str::FromStr;
|
||||
|
||||
const CONFIG_FILENAME: &str = "bunbun.yaml";
|
||||
const DEFAULT_CONFIG: &[u8] = include_bytes!("../bunbun.default.yaml");
|
||||
|
@ -46,20 +45,6 @@ pub struct Route {
|
|||
pub max_args: Option<usize>,
|
||||
}
|
||||
|
||||
impl FromStr for Route {
|
||||
type Err = std::convert::Infallible;
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
Ok(Self {
|
||||
route_type: get_route_type(s),
|
||||
path: s.to_string(),
|
||||
hidden: false,
|
||||
description: None,
|
||||
min_args: None,
|
||||
max_args: None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for Route {
|
||||
fn from(s: String) -> Self {
|
||||
Self {
|
||||
|
@ -119,8 +104,7 @@ impl<'de> Deserialize<'de> for Route {
|
|||
where
|
||||
E: serde::de::Error,
|
||||
{
|
||||
// This is infallible
|
||||
Ok(Self::Value::from_str(path).unwrap())
|
||||
Ok(Self::Value::from(path.to_owned()))
|
||||
}
|
||||
|
||||
fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
|
||||
|
@ -354,19 +338,20 @@ mod route {
|
|||
use super::*;
|
||||
use anyhow::{Context, Result};
|
||||
use serde_yaml::{from_str, to_string};
|
||||
use std::path::Path;
|
||||
use tempfile::NamedTempFile;
|
||||
|
||||
#[test]
|
||||
fn deserialize_relative_path() -> Result<()> {
|
||||
let tmpfile = NamedTempFile::new_in(".")?;
|
||||
let path = format!("{}", tmpfile.path().display());
|
||||
let path = tmpfile.path().display().to_string();
|
||||
let path = path
|
||||
.get(path.rfind(".").context("While finding .")?..)
|
||||
.context("While getting the path")?;
|
||||
let path = std::path::Path::new(path);
|
||||
let path = Path::new(path);
|
||||
assert!(path.is_relative());
|
||||
let path = path.to_str().context("While stringifying path")?;
|
||||
assert_eq!(from_str::<Route>(path)?, Route::from_str(path)?);
|
||||
assert_eq!(from_str::<Route>(path)?, Route::from(path.to_owned()));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -375,7 +360,7 @@ mod route {
|
|||
let tmpfile = NamedTempFile::new()?;
|
||||
let path = format!("{}", tmpfile.path().display());
|
||||
assert!(tmpfile.path().is_absolute());
|
||||
assert_eq!(from_str::<Route>(&path)?, Route::from_str(&path)?);
|
||||
assert_eq!(from_str::<Route>(&path)?, Route::from(path));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -384,7 +369,7 @@ mod route {
|
|||
fn deserialize_http_path() -> Result<()> {
|
||||
assert_eq!(
|
||||
from_str::<Route>("http://google.com")?,
|
||||
Route::from_str("http://google.com")?
|
||||
Route::from("http://google.com")
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
@ -393,7 +378,7 @@ mod route {
|
|||
fn deserialize_https_path() -> Result<()> {
|
||||
assert_eq!(
|
||||
from_str::<Route>("https://google.com")?,
|
||||
Route::from_str("https://google.com")?
|
||||
Route::from("https://google.com")
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
@ -401,7 +386,7 @@ mod route {
|
|||
#[test]
|
||||
fn serialize() -> Result<()> {
|
||||
assert_eq!(
|
||||
&to_string(&Route::from_str("hello world")?)?,
|
||||
&to_string(&Route::from("hello world"))?,
|
||||
"---\nroute_type: External\npath: hello world\nhidden: false\ndescription: ~\nmin_args: ~\nmax_args: ~\n"
|
||||
);
|
||||
Ok(())
|
||||
|
|
|
@ -242,7 +242,6 @@ fn resolve_path(path: &Path, args: &str) -> Result<HopAction, BunBunError> {
|
|||
mod resolve_hop {
|
||||
use super::*;
|
||||
use anyhow::Result;
|
||||
use std::str::FromStr;
|
||||
|
||||
fn generate_route_result<'a>(
|
||||
keyword: &'a Route,
|
||||
|
@ -277,13 +276,10 @@ mod resolve_hop {
|
|||
#[test]
|
||||
fn only_default_routes_some_default_yields_default_hop() -> Result<()> {
|
||||
let mut map: HashMap<String, Route> = HashMap::new();
|
||||
map.insert("google".into(), Route::from_str("https://example.com")?);
|
||||
map.insert("google".into(), Route::from("https://example.com"));
|
||||
assert_eq!(
|
||||
resolve_hop("hello world", &map, &Some(String::from("google"))),
|
||||
generate_route_result(
|
||||
&Route::from_str("https://example.com")?,
|
||||
"hello world"
|
||||
),
|
||||
generate_route_result(&Route::from("https://example.com"), "hello world"),
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
@ -291,13 +287,10 @@ mod resolve_hop {
|
|||
#[test]
|
||||
fn non_default_routes_some_default_yields_non_default_hop() -> Result<()> {
|
||||
let mut map: HashMap<String, Route> = HashMap::new();
|
||||
map.insert("google".into(), Route::from_str("https://example.com")?);
|
||||
map.insert("google".into(), Route::from("https://example.com"));
|
||||
assert_eq!(
|
||||
resolve_hop("google hello world", &map, &Some(String::from("a"))),
|
||||
generate_route_result(
|
||||
&Route::from_str("https://example.com")?,
|
||||
"hello world"
|
||||
),
|
||||
generate_route_result(&Route::from("https://example.com"), "hello world"),
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
@ -305,13 +298,10 @@ mod resolve_hop {
|
|||
#[test]
|
||||
fn non_default_routes_no_default_yields_non_default_hop() -> Result<()> {
|
||||
let mut map: HashMap<String, Route> = HashMap::new();
|
||||
map.insert("google".into(), Route::from_str("https://example.com")?);
|
||||
map.insert("google".into(), Route::from("https://example.com"));
|
||||
assert_eq!(
|
||||
resolve_hop("google hello world", &map, &None),
|
||||
generate_route_result(
|
||||
&Route::from_str("https://example.com")?,
|
||||
"hello world"
|
||||
),
|
||||
generate_route_result(&Route::from("https://example.com"), "hello world"),
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
@ -370,9 +360,12 @@ mod check_route {
|
|||
|
||||
#[cfg(test)]
|
||||
mod resolve_path {
|
||||
use crate::error::BunBunError;
|
||||
|
||||
use super::{resolve_path, HopAction};
|
||||
use anyhow::Result;
|
||||
use std::env::current_dir;
|
||||
use std::io::ErrorKind;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
#[test]
|
||||
|
@ -397,14 +390,11 @@ mod resolve_path {
|
|||
|
||||
#[test]
|
||||
fn no_permissions_returns_err() {
|
||||
assert!(
|
||||
// Trying to run a command without permission
|
||||
format!(
|
||||
"{}",
|
||||
resolve_path(&Path::new("/root/some_exec"), "").unwrap_err()
|
||||
)
|
||||
.contains("Permission denied")
|
||||
);
|
||||
let result = match resolve_path(&Path::new("/root/some_exec"), "") {
|
||||
Err(BunBunError::Io(e)) => e.kind() == ErrorKind::PermissionDenied,
|
||||
_ => false,
|
||||
};
|
||||
assert!(result);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
Loading…
Reference in a new issue