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

fix: rm the 'Result::unwrap()' in lsp. #571

Merged
merged 4 commits into from
Jun 8, 2023
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
27 changes: 15 additions & 12 deletions kclvm/driver/src/kpm_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,25 @@ impl Metadata {

/// [`fetch_metadata`] will call `kpm metadata` to obtain the metadata.
pub fn fetch_metadata(manifest_path: PathBuf) -> Result<Metadata> {
let output = Command::new(kpm())
use std::result::Result::Ok;
match Command::new(kpm())
.arg("metadata")
.current_dir(manifest_path)
.output()
.unwrap();

if !output.status.success() {
bail!(
"fetch workspace failed with error: {}",
String::from_utf8_lossy(&output.stderr)
);
{
Ok(output) => {
if !output.status.success() {
bail!(
"fetch metadata failed with error: {}",
String::from_utf8_lossy(&output.stderr)
);
}
Ok(Metadata::parse(
String::from_utf8_lossy(&output.stdout).to_string(),
)?)
}
Err(err) => bail!("fetch metadata failed with error: {}", err),
}

Ok(Metadata::parse(
String::from_utf8_lossy(&output.stdout).to_string(),
)?)
}

/// [`lookup_the_nearest_file_dir`] will start from [`from`] and search for file [`the_nearest_file`] in the parent directories.
Expand Down
22 changes: 21 additions & 1 deletion kclvm/driver/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::env;
use std::path::{Path, PathBuf};
use std::{env, panic};

use kclvm_config::modfile::get_vendor_home;
use kclvm_config::settings::KeyValuePair;
Expand Down Expand Up @@ -196,3 +196,23 @@ fn test_fetch_metadata() {
.to_string()
);
}

#[test]
fn test_fetch_metadata_invalid() {
let result = panic::catch_unwind(|| {
let result = fetch_metadata("invalid_path".to_string().into());
match result {
Ok(_) => {
panic!("The method should not return Ok")
}
Err(_) => {
println!("return with an error.")
}
}
});

match result {
Ok(_) => println!("no panic"),
Err(e) => panic!("The method should not panic forever.: {:?}", e),
}
}