Skip to content

Commit

Permalink
feat: support print functions for normal logger writer and not only f…
Browse files Browse the repository at this point in the history
…or stdout

Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Nov 20, 2023
1 parent 269632b commit b2a89dd
Show file tree
Hide file tree
Showing 20 changed files with 285 additions and 195 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.7.0-alpha.2
0.7.0-beta.1
20 changes: 15 additions & 5 deletions kclvm/api/src/capi_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn test_c_api_call_exec_program() {
"KclvmService.ExecProgram",
"exec-program.json",
"exec-program.response.json",
|res| res.escaped_time = "0".to_owned(),
|_| {},
);
}

Expand All @@ -27,7 +27,7 @@ fn test_c_api_call_exec_program_with_external_pkg() {
"KclvmService.ExecProgram",
"exec-program-with-external-pkg.json",
"exec-program-with-external-pkg.response.json",
|res| res.escaped_time = "0".to_owned(),
|_| {},
);
}

Expand All @@ -37,7 +37,7 @@ fn test_c_api_call_exec_program_with_include_schema_type_path() {
"KclvmService.ExecProgram",
"exec-program-with-include-schema-type-path.json",
"exec-program-with-include-schema-type-path.response.json",
|res| res.escaped_time = "0".to_owned(),
|_| {},
);
}

Expand All @@ -47,7 +47,17 @@ fn test_c_api_call_exec_program_with_path_selector() {
"KclvmService.ExecProgram",
"exec-program-with-path-selector.json",
"exec-program-with-path-selector.response.json",
|res| res.escaped_time = "0".to_owned(),
|_| {},
);
}

#[test]
fn test_c_api_call_exec_program_with_print() {
test_c_api::<ExecProgramArgs, ExecProgramResult, _>(
"KclvmService.ExecProgram",
"exec-program-with-print.json",
"exec-program-with-print.response.json",
|_| {},
);
}

Expand Down Expand Up @@ -111,7 +121,7 @@ fn test_c_api_call_exec_program_with_recursive() {
"KclvmService.ExecProgram",
"exec-program-with-recursive.json",
"exec-program-with-recursive.response.json",
|res| res.escaped_time = "0".to_owned(),
|_| {},
);
}

Expand Down
8 changes: 5 additions & 3 deletions kclvm/api/src/service/service_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,14 @@ impl KclvmServiceImpl {
let result = exec_program(
sess,
&kclvm_runner::ExecProgramArgs::from_str(args_json.as_str()),
)?;
)
.map_err(|err| err.to_string())?;

Ok(ExecProgramResult {
json_result: result.json_result,
yaml_result: result.yaml_result,
escaped_time: result.escaped_time,
log_message: result.log_message,
err_message: result.err_message,
})
}

Expand Down Expand Up @@ -415,7 +417,7 @@ impl KclvmServiceImpl {
transform_str_para(&args.code),
)) {
Ok(success) => (success, "".to_string()),
Err(err) => (false, err),
Err(err) => (false, err.to_string()),
};
Ok(ValidateCodeResult {
success,
Expand Down
6 changes: 6 additions & 0 deletions kclvm/api/src/testdata/exec-program-with-print.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"work_dir" : "./src/testdata",
"k_filename_list":[
"hello_with_print.k"
]
}
6 changes: 6 additions & 0 deletions kclvm/api/src/testdata/exec-program-with-print.response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"json_result": "[{\"a\": 1}]",
"yaml_result": "a: 1",
"log_message": "Hello world\n",
"err_message": ""
}
2 changes: 2 additions & 0 deletions kclvm/api/src/testdata/hello_with_print.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
print("Hello world")
a = 1
30 changes: 22 additions & 8 deletions kclvm/cmd/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,31 @@ pub fn run_command<W: Write>(matches: &ArgMatches, writer: &mut W) -> Result<()>
let output = settings.output();
let sess = Arc::new(ParseSession::default());
match exec_program(sess.clone(), &settings.try_into()?) {
Ok(result) => match output {
Some(o) => {
std::fs::write(o, result.yaml_result)?;
Ok(result) => {
// Output log message
if !result.log_message.is_empty() {
write!(writer, "{}", result.log_message)?;
}
// [`println!`] is not a good way to output content to stdout,
// using [`writeln`] can be better to redirect the output.
None => writeln!(writer, "{}", result.yaml_result)?,
},
// Output execute error message
if !result.err_message.is_empty() {
if !sess.0.diag_handler.has_errors()? {
sess.0.add_err(StringError(result.err_message))?;
}
sess.0.emit_stashed_diagnostics_and_abort()?;
}
if !result.yaml_result.is_empty() {
match output {
Some(o) => std::fs::write(o, result.yaml_result)?,
// [`println!`] is not a good way to output content to stdout,
// using [`writeln`] can be better to redirect the output.
None => writeln!(writer, "{}", result.yaml_result)?,
}
}
}
// Other error message
Err(msg) => {
if !sess.0.diag_handler.has_errors()? {
sess.0.add_err(StringError(msg))?;
sess.0.add_err(StringError(msg.to_string()))?;
}
sess.0.emit_stashed_diagnostics_and_abort()?;
}
Expand Down
24 changes: 18 additions & 6 deletions kclvm/cmd/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{

use kclvm_config::modfile::KCL_PKG_PATH;
use kclvm_parser::ParseSession;
use kclvm_runner::exec_program;
use kclvm_runner::{exec_program, MapErrorResult};

use crate::{
app,
Expand Down Expand Up @@ -522,7 +522,10 @@ fn test_main_pkg_not_found() {
]);
let settings = must_build_settings(matches.subcommand_matches("run").unwrap());
let sess = Arc::new(ParseSession::default());
match exec_program(sess.clone(), &settings.try_into().unwrap()) {
match exec_program(sess.clone(), &settings.try_into().unwrap())
.map_err_to_result()
.map_err(|e| e.to_string())
{
Ok(_) => panic!("unreachable code."),
Err(msg) => assert_eq!(
msg,
Expand Down Expand Up @@ -563,7 +566,7 @@ fn test_plugin_not_found() {
]);
let settings = must_build_settings(matches.subcommand_matches("run").unwrap());
let sess = Arc::new(ParseSession::default());
match exec_program(sess.clone(), &settings.try_into().unwrap()) {
match exec_program(sess.clone(), &settings.try_into().unwrap()).map_err_to_result().map_err(|e|e.to_string()) {
Ok(_) => panic!("unreachable code."),
Err(msg) => assert!(msg.contains("the plugin package `kcl_plugin.not_exist` is not found, please confirm if plugin mode is enabled")),
}
Expand All @@ -578,7 +581,10 @@ fn test_error_message_fuzz_matched() {
]);
let settings = must_build_settings(matches.subcommand_matches("run").unwrap());
let sess = Arc::new(ParseSession::default());
match exec_program(sess.clone(), &settings.try_into().unwrap()) {
match exec_program(sess.clone(), &settings.try_into().unwrap())
.map_err_to_result()
.map_err(|e| e.to_string())
{
Ok(_) => panic!("unreachable code."),
Err(msg) => {
assert!(msg
Expand All @@ -596,7 +602,10 @@ fn test_error_message_fuzz_unmatched() {
]);
let settings = must_build_settings(matches.subcommand_matches("run").unwrap());
let sess = Arc::new(ParseSession::default());
match exec_program(sess.clone(), &settings.try_into().unwrap()) {
match exec_program(sess.clone(), &settings.try_into().unwrap())
.map_err_to_result()
.map_err(|e| e.to_string())
{
Ok(_) => panic!("unreachable code."),
Err(msg) => {
assert!(msg.contains("attribute 'a' not found in schema 'Person'"))
Expand All @@ -613,7 +622,10 @@ fn test_keyword_argument_error_message() {
]);
let settings = must_build_settings(matches.subcommand_matches("run").unwrap());
let sess = Arc::new(ParseSession::default());
match exec_program(sess.clone(), &settings.try_into().unwrap()) {
match exec_program(sess.clone(), &settings.try_into().unwrap())
.map_err_to_result()
.map_err(|e| e.to_string())
{
Ok(_) => panic!("unreachable code."),
Err(msg) => {
assert!(msg.contains("keyword argument 'ID' not found"));
Expand Down
3 changes: 1 addition & 2 deletions kclvm/cmd/src/vet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ pub fn vet_command(matches: &ArgMatches) -> Result<()> {
Some(kcl_file.to_string()),
None,
))
.map_err(|err| anyhow::anyhow!(err))?;
Ok(())
.map(|_| ())
}
_ => Err(anyhow::anyhow!("No input data file or kcl file")),
}
Expand Down
Loading

0 comments on commit b2a89dd

Please sign in to comment.