77 * included LICENSE file for details. *)
88
99let display_std_outputs = ref false
10- let dump_outputs = ref None
11- let dump_reports = ref None
12- let display_callback = ref false
1310let display_outcomes = ref false
1411let grade_student = ref None
1512let individual_timeout = ref None
@@ -47,29 +44,25 @@ let read_student_file exercise_dir path =
4744 else
4845 Lwt_io. with_file ~mode: Lwt_io. Input fn Lwt_io. read
4946
50- let grade ?(print_result =false ) ?dirname meta exercise output_json =
47+ let grade ?(print_result =false ) ?dirname
48+ ~dump_outputs ~dump_reports ~display_callback
49+ meta exercise output_json =
5150 Lwt. catch
5251 (fun () ->
5352 let code_to_grade = match ! grade_student with
5453 | Some path -> read_student_file (Sys. getcwd () ) path
55- | None ->
56- Lwt. return (Learnocaml_exercise. (decipher File. solution exercise)) in
54+ | None -> Lwt. return (Learnocaml_exercise. (decipher File. solution exercise)) in
5755 let callback =
58- if ! display_callback then Some (Printf. eprintf " [ %s ]%!\r\027 [K" ) else None in
56+ if display_callback then Some (Printf. eprintf " [ %s ]%!\r\027 [K" ) else None in
5957 let timeout = ! individual_timeout in
6058 code_to_grade >> = fun code ->
6159 Grading_cli. get_grade ?callback ?timeout ?dirname exercise code
6260 >> = fun (result , stdout_contents , stderr_contents , outcomes ) ->
6361 flush stderr;
6462 match result with
65- | Error exn ->
63+ | Error err ->
6664 let dump_error ppf =
67- begin match Grading. string_of_exn exn with
68- | Some msg ->
69- Format. fprintf ppf " %s@." msg
70- | None ->
71- Format. fprintf ppf " %a@." Location. report_exception exn
72- end ;
65+ Format. fprintf ppf " %s@." (Grading. string_of_err err);
7366 if stdout_contents <> " " then begin
7467 Format. fprintf ppf " grader stdout:@.%s@." stdout_contents
7568 end ;
@@ -79,7 +72,7 @@ let grade ?(print_result=false) ?dirname meta exercise output_json =
7972 if outcomes <> " " then begin
8073 Format. fprintf ppf " grader outcomes:@.%s@." outcomes
8174 end in
82- begin match ! dump_outputs with
75+ begin match dump_outputs with
8376 | None -> ()
8477 | Some prefix ->
8578 let oc = open_out (prefix ^ " .error" ) in
@@ -92,7 +85,7 @@ let grade ?(print_result=false) ?dirname meta exercise output_json =
9285 let (max, failure) = Learnocaml_report. result report in
9386 if ! display_reports then
9487 Learnocaml_report. print (Format. formatter_of_out_channel stderr) report;
95- begin match ! dump_reports with
88+ begin match dump_reports with
9689 | None -> ()
9790 | Some prefix ->
9891 let oc = open_out (prefix ^ " .report.txt" ) in
@@ -103,7 +96,7 @@ let grade ?(print_result=false) ?dirname meta exercise output_json =
10396 close_out oc
10497 end ;
10598 if stderr_contents <> " " then begin
106- begin match ! dump_outputs with
99+ begin match dump_outputs with
107100 | None -> ()
108101 | Some prefix ->
109102 let oc = open_out (prefix ^ " .stderr" ) in
@@ -114,7 +107,7 @@ let grade ?(print_result=false) ?dirname meta exercise output_json =
114107 Format. eprintf " %s" stderr_contents
115108 end ;
116109 if stdout_contents <> " " then begin
117- begin match ! dump_outputs with
110+ begin match dump_outputs with
118111 | None -> ()
119112 | Some prefix ->
120113 let oc = open_out (prefix ^ " .stdout" ) in
@@ -125,7 +118,7 @@ let grade ?(print_result=false) ?dirname meta exercise output_json =
125118 Format. printf " %s" stdout_contents
126119 end ;
127120 if outcomes <> " " then begin
128- begin match ! dump_outputs with
121+ begin match dump_outputs with
129122 | None -> ()
130123 | Some prefix ->
131124 let oc = open_out (prefix ^ " .outcomes" ) in
@@ -163,7 +156,8 @@ let grade ?(print_result=false) ?dirname meta exercise output_json =
163156 Lwt. return (Ok () )
164157 end)
165158 (fun exn ->
166- begin match ! dump_outputs with
159+ Lwt. wrap @@ fun () ->
160+ begin match dump_outputs with
167161 | None -> ()
168162 | Some prefix ->
169163 let oc = open_out (prefix ^ " .error" ) in
@@ -172,15 +166,20 @@ let grade ?(print_result=false) ?dirname meta exercise output_json =
172166 " %a@!" Location. report_exception exn ;
173167 close_out oc
174168 end ;
175- Format. eprintf " %a" Location. report_exception exn ;
176- Lwt. return ( Error (- 1 ) ))
169+ Format. eprintf " %a" Location. report_exception exn ;
170+ Error (- 1 ))
177171
178- let grade_from_dir ?(print_result =false ) exercise_dir output_json =
172+ let grade_from_dir
173+ ?(print_result =false )
174+ ~dump_outputs ~dump_reports ~display_callback
175+ exercise_dir output_json =
179176 let exercise_dir = remove_trailing_slash exercise_dir in
180177 read_exercise exercise_dir >> = fun exo ->
181178 Lwt_io. (with_file ~mode: Input (String. concat Filename. dir_sep [exercise_dir; " meta.json" ]) read) >> = fun content ->
182179 let meta = (match content with
183180 | "" -> `O []
184181 | s -> Ezjsonm. from_string s)
185182 |> Json_encoding. destruct Learnocaml_data.Exercise.Meta. enc in
186- grade ~print_result ~dirname: exercise_dir meta exo output_json
183+ grade
184+ ~dump_outputs ~dump_reports ~display_callback
185+ ~print_result ~dirname: exercise_dir meta exo output_json
0 commit comments