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(kclvm-parser): return loading file failed error message from meth… #237

Merged
merged 1 commit into from
Oct 12, 2022
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
9 changes: 8 additions & 1 deletion kclvm/parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,14 @@ pub fn parse_file(filename: &str, code: Option<String>) -> Result<ast::Module, S
let src = if let Some(s) = code {
s
} else {
std::fs::read_to_string(filename).unwrap()
match std::fs::read_to_string(filename) {
Ok(src) => src,
Err(_err) => {
let err_msg =
format!("Failed to load KCL file '{}'. Because '{}'", filename, _err);
return Err(err_msg);
}
}
};

let sm = kclvm_span::SourceMap::new(FilePathMapping::empty());
Expand Down
13 changes: 13 additions & 0 deletions kclvm/parser/src/parser/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::lexer::parse_token_streams;
use crate::parse_file;
use crate::parser::Parser;
use crate::session::ParseSession;
use expect_test::{expect, Expect};
Expand Down Expand Up @@ -1266,3 +1267,15 @@ fn smoke_test_parsing_stmt() {
assert_eq!(got, expect);
});
}

#[test]
fn test_parse_file_not_found() {
match parse_file("The file path is invalid", None) {
Ok(_) => {
panic!("unreachable")
}
Err(err_msg) => {
assert_eq!(err_msg, "Failed to load KCL file 'The file path is invalid'. Because 'No such file or directory (os error 2)'");
}
}
}