Skip to content

Commit

Permalink
fix: rm the 'Result::unwrap()' in lsp. (#571)
Browse files Browse the repository at this point in the history
* fix: rm the 'Result::unwrap()' in lsp.

* fix: return error if kpm metadata failed.

* fix: fix test cases

* fix: make fmt.
  • Loading branch information
zong-zhe committed Jun 8, 2023
1 parent d7c290c commit 2e40cd0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
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),
}
}

0 comments on commit 2e40cd0

Please sign in to comment.